using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.Serialization; public class Timer : MonoBehaviour { public UnityEvent OnTimerEnded; [FormerlySerializedAs("TimeLabel")] public TextMeshProUGUI timeLabel; [SerializeField, FormerlySerializedAs("restTime")] private float _restTime; [SerializeField] private AudioClip _warningSound; private bool _processing; private bool _recentlyPlayed; private float _lastSignalTime; private void Update() { if (_processing == false) return; UiDraw(); _restTime -= Time.deltaTime; if (_restTime <= 0f) { _restTime = 0f; StopTimer(); OnTimerEnded?.Invoke(); } int rest = (int)_restTime; if (_recentlyPlayed == false && (rest % 60 == 0 || rest == 30 || rest < 11)) { SoundManager.instance.PlaySfx(_warningSound); _recentlyPlayed = true; _lastSignalTime = Time.time; } if (_recentlyPlayed && Time.time - _lastSignalTime >= 1f) _recentlyPlayed = false; } public void SetupTimer(float seconds) { _restTime = seconds; UiDraw(); } public void StartTimer() => _processing = true; public void StopTimer() => _processing = false; private void UiDraw() { string minutes = ((int)(_restTime / 60)).ToString(); string seconds = ((int)(_restTime % 60)).ToString(); if (minutes.Length < 2) minutes = "0" + minutes; if (seconds.Length < 2) seconds = "0" + seconds; timeLabel.text = minutes + ":" + seconds; } }