| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using UnityEngine; |
| | | 4 | | using UnityEngine.UI; |
| | | 5 | | using TMPro; |
| | | 6 | | using System; |
| | | 7 | | |
| | | 8 | | public class Timer : MonoBehaviour |
| | | 9 | | { |
| | | 10 | | public ExitDoor exitDoor; |
| | 2 | 11 | | public float timeCountdown = 180; |
| | | 12 | | public TextMeshProUGUI timerText; |
| | 2 | 13 | | public bool StartTimer = false; |
| | | 14 | | [SerializeField] private AudioSource myAudio; |
| | | 15 | | private bool gameDone; |
| | 2 | 16 | | public int oneTimeEvent = 1; |
| | | 17 | | |
| | | 18 | | public Clock clock; |
| | | 19 | | |
| | | 20 | | public GameObject tempOutOfTime; |
| | | 21 | | |
| | 2 | 22 | | public bool outOfTime = false; |
| | | 23 | | |
| | | 24 | | public void Start() |
| | 1 | 25 | | { |
| | 1 | 26 | | tempOutOfTime.SetActive(false); |
| | 1 | 27 | | } |
| | | 28 | | |
| | | 29 | | // Update is called once per frame |
| | | 30 | | void Update() |
| | 1297 | 31 | | { |
| | 1297 | 32 | | if (StartTimer == true && !gameDone) |
| | 0 | 33 | | { |
| | 0 | 34 | | if (timeCountdown > 0) |
| | 0 | 35 | | { |
| | 0 | 36 | | timeCountdown -= Time.deltaTime; |
| | 0 | 37 | | } |
| | | 38 | | else |
| | 0 | 39 | | { |
| | 0 | 40 | | if(!outOfTime) |
| | 0 | 41 | | { |
| | 0 | 42 | | timeCountdown = 0; |
| | 0 | 43 | | onOutOfTime(); |
| | 0 | 44 | | } |
| | 0 | 45 | | } |
| | 0 | 46 | | } |
| | 1297 | 47 | | TimeDisplay(timeCountdown); |
| | 1297 | 48 | | } |
| | | 49 | | |
| | | 50 | | internal void removeOutOfTime() |
| | 0 | 51 | | { |
| | 0 | 52 | | tempOutOfTime.SetActive(false); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | private void onOutOfTime() |
| | 0 | 56 | | { |
| | 0 | 57 | | tempOutOfTime.SetActive(true); |
| | 0 | 58 | | outOfTime = true; |
| | 0 | 59 | | clock.onTimeOut(); |
| | 0 | 60 | | exitDoor.onFinish(); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | void TimeDisplay(float timeToDisplay) |
| | 1297 | 64 | | { |
| | 1297 | 65 | | if(timeToDisplay < 0) |
| | 0 | 66 | | { |
| | 0 | 67 | | timeToDisplay = 0; |
| | 0 | 68 | | } |
| | | 69 | | |
| | 1297 | 70 | | float minutes = Mathf.FloorToInt(timeToDisplay / 60); |
| | 1297 | 71 | | float seconds = Mathf.FloorToInt(timeToDisplay % 60); |
| | 1297 | 72 | | timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds); |
| | 1297 | 73 | | } |
| | | 74 | | |
| | | 75 | | public void OnTriggerEnter(Collider other) |
| | 0 | 76 | | { |
| | 0 | 77 | | StartTimer = true; |
| | | 78 | | |
| | 0 | 79 | | if (oneTimeEvent == 1) |
| | 0 | 80 | | { |
| | 0 | 81 | | myAudio.Play(); |
| | 0 | 82 | | oneTimeEvent += 1; |
| | 0 | 83 | | } |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | public void onWin() |
| | 3 | 87 | | { |
| | 3 | 88 | | gameDone = true; |
| | 3 | 89 | | } |
| | | 90 | | } |