using TMPro; using UnityEngine; using Game; using Music; using Player; using UnityEngine.UI; namespace Game { /// /// This class manages the game's countdown timer. /// It starts, updates, and stops the timer, and ends the game when time runs out. /// public class GameTimer : MonoBehaviour { /// /// The starting time for the timer, in seconds. /// public float startTime = 180f; /// /// The time remaining on the timer, in seconds. /// private float timeRemaining; /// /// Indicates whether the timer is currently running. /// private bool timerRunning = false; /// /// The UI text element that displays the timer. /// public Text timerText; /// /// The TextMeshPro element that displays the timer. /// [SerializeField] private TextMeshProUGUI timer; /// /// Sets up the timer when the game starts. /// private void Start() { // Set the timer to the starting time and display the initial value timeRemaining = startTime; timer.text = "3:00.00"; UpdateTimerDisplay(); } /// /// Updates the timer every frame to show the time remaining. /// private void Update() { if (timerRunning) { // Decrease the time remaining timeRemaining -= Time.deltaTime; // Stop the timer if time runs out if (timeRemaining <= 0) { timeRemaining = 0; timerRunning = false; OnTimerEnd(); } // Update the timer display UpdateTimerDisplay(); } } /// /// Starts the timer if it is not already running. /// public void StartTimer() { if (!timerRunning) { // Reset the timer and start it timeRemaining = startTime; timerRunning = true; } } /// /// Updates the timer display to show the current time remaining. /// private void UpdateTimerDisplay() { // Calculate minutes and seconds from the remaining time int minutes = Mathf.FloorToInt(timeRemaining / 60); int seconds = Mathf.FloorToInt(timeRemaining % 60); // Format the time as "MM:SS" and update the UI timer.text = string.Format("{0}:{1:D2}", minutes, seconds); } /// /// Ends the game when the timer reaches zero. /// private void OnTimerEnd() { // Notify the GameManager that the game is over GameManager.Instance.GameOver(); } } }