Files
Crash-Course/Assets/Scripts/Game/GameTimer.cs

113 lines
3.1 KiB
C#
Raw Normal View History

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