| | | 1 | | using UnityEngine; |
| | | 2 | | using System.Collections; |
| | | 3 | | using UnityEngine.SceneManagement; |
| | | 4 | | |
| | | 5 | | public class Crystal : MonoBehaviour { |
| | | 6 | | |
| | 2 | 7 | | private Vector3 _rotationVector = Vector3.forward; |
| | 2 | 8 | | private float _rotationSpeed = 50f; |
| | 2 | 9 | | private float _blinkSpeed = 0.2f; |
| | | 10 | | private Color _emissionColor; |
| | | 11 | | private AudioSource _audio; |
| | 2 | 12 | | private bool _touched = false; |
| | 2 | 13 | | private bool _hide_dialog = true; |
| | 2 | 14 | | private bool _teleporting = false; |
| | | 15 | | |
| | | 16 | | public GameObject speechBubble; |
| | | 17 | | |
| | | 18 | | [Header("Sounds")] |
| | | 19 | | public AudioClip teleport_sound; |
| | | 20 | | |
| | | 21 | | [Header("Dialogs")] |
| | | 22 | | public GameObject dialog_hi; |
| | | 23 | | public GameObject dialog_teleport; |
| | | 24 | | public GameObject dialog_end; |
| | | 25 | | public GameObject dialog_fail; |
| | | 26 | | |
| | | 27 | | // Use this for initialization |
| | 1 | 28 | | void Start () { |
| | | 29 | | // save material emission color |
| | 1 | 30 | | _emissionColor = gameObject.GetComponentInChildren<MeshRenderer>().material.GetColor("_EmissionColor"); |
| | | 31 | | |
| | | 32 | | // initially, do not allow to click on crystal |
| | | 33 | | // player needs to enter the temple |
| | 1 | 34 | | gameObject.GetComponent<MeshCollider>().enabled = false; |
| | 1 | 35 | | } |
| | | 36 | | |
| | | 37 | | // Update is called once per frame |
| | 85071 | 38 | | void Update () { |
| | | 39 | | // rotate |
| | 85071 | 40 | | gameObject.transform.Rotate(_rotationVector * _rotationSpeed * Time.deltaTime); |
| | | 41 | | |
| | | 42 | | // change emission color |
| | 85071 | 43 | | float emission = Mathf.PingPong(Time.time * _blinkSpeed, 1.0f); |
| | 85071 | 44 | | gameObject.GetComponentInChildren<MeshRenderer>().material.SetColor("_EmissionColor", _emissionColor * Mathf.Lin |
| | | 45 | | |
| | 85071 | 46 | | if (_touched) { |
| | | 47 | | // size pulsation |
| | 0 | 48 | | gameObject.transform.localScale = Vector3.one + 0.2f * Vector3.one * Mathf.Abs(Mathf.Sin(Time.time * 3f)); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | // show dialogs after greetings audio is played |
| | 85071 | 52 | | if (!_hide_dialog && !_audio.isPlaying) { |
| | 0 | 53 | | speechBubble.GetComponent<Canvas>().enabled = true; |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | // show oops dialog after teleport audio is played |
| | 85071 | 57 | | if (_teleporting && !_audio.isPlaying) { |
| | 0 | 58 | | dialog_end.SetActive(true); |
| | 0 | 59 | | _teleporting = false; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | // vibrate while teleporting |
| | 85071 | 63 | | if (_teleporting) |
| | 0 | 64 | | { |
| | 0 | 65 | | Handheld.Vibrate(); |
| | 0 | 66 | | } |
| | 85071 | 67 | | } |
| | | 68 | | |
| | | 69 | | public void Awake() |
| | 1 | 70 | | { |
| | 1 | 71 | | speechBubble.GetComponent<Canvas>().enabled = false; |
| | 1 | 72 | | _audio = gameObject.GetComponent<AudioSource>(); |
| | | 73 | | |
| | | 74 | | // init state values |
| | 1 | 75 | | _hide_dialog = true; |
| | 1 | 76 | | _touched = false; |
| | | 77 | | |
| | | 78 | | // init dialogs |
| | 1 | 79 | | dialog_hi.SetActive(true); |
| | 1 | 80 | | dialog_teleport.SetActive(false); |
| | 1 | 81 | | dialog_end.SetActive(false); |
| | 1 | 82 | | dialog_fail.SetActive(false); |
| | 1 | 83 | | } |
| | | 84 | | |
| | | 85 | | public void Touch() |
| | 0 | 86 | | { |
| | | 87 | | // play sound and then show dialog |
| | 0 | 88 | | _audio.Play(); |
| | 0 | 89 | | _hide_dialog = false; |
| | 0 | 90 | | _touched = true; |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | public void TakeCoins() |
| | 0 | 94 | | { |
| | 0 | 95 | | dialog_hi.SetActive(false); |
| | | 96 | | // check if coins collected |
| | 0 | 97 | | if (GameObject.FindGameObjectsWithTag("Coin").Length == 0) { |
| | | 98 | | // all coins collected |
| | 0 | 99 | | dialog_teleport.SetActive(true); |
| | 0 | 100 | | } else { |
| | 0 | 101 | | dialog_fail.SetActive(true); |
| | 0 | 102 | | } |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | public void Teleport() |
| | 0 | 106 | | { |
| | 0 | 107 | | dialog_teleport.SetActive(false); |
| | | 108 | | // play sound and then show end dialog |
| | 0 | 109 | | _audio.clip = teleport_sound; |
| | 0 | 110 | | _audio.Play(); |
| | 0 | 111 | | _teleporting = true; |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | public void Restart() |
| | 0 | 115 | | { |
| | 0 | 116 | | SceneManager.LoadScene(0); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | // called when player enters temple |
| | | 120 | | public void MakeClickable() |
| | 0 | 121 | | { |
| | 0 | 122 | | gameObject.GetComponent<MeshCollider>().enabled = true; |
| | 0 | 123 | | } |
| | | 124 | | } |