93 lines
2.0 KiB
C#
Raw Normal View History

using UnityEngine;
using TMPro;
using System;
public class RoundTimer : MonoBehaviour
{
[Header("UI Reference")]
public TextMeshProUGUI timerText;
[Header("Settings")]
public float startTimeSeconds = 45f;
public bool autoStart = true;
2025-09-06 00:08:22 +05:00
// NEW: Game Over Panel reference
[Header("Game Over")]
public GameOverPanelController gameOverPanel;
public event Action OnTimerEnded;
private float timeRemaining;
private bool running;
void OnEnable()
{
if (ClawScoreManager.Instance != null)
ClawScoreManager.Instance.OnTimeAdded += AddTime;
}
void OnDisable()
{
if (ClawScoreManager.Instance != null)
ClawScoreManager.Instance.OnTimeAdded -= AddTime;
}
void Start()
{
if (autoStart)
StartTimer();
}
void Update()
{
if (!running) return;
timeRemaining -= Time.deltaTime;
if (timeRemaining <= 0f)
{
timeRemaining = 0f;
running = false;
2025-09-06 00:08:22 +05:00
// Fire event (if someone else wants to listen)
OnTimerEnded?.Invoke();
2025-09-06 00:08:22 +05:00
// Show Game Over UI right here
if (gameOverPanel != null)
gameOverPanel.ShowGameOver(ClawScoreManager.Instance);
}
UpdateUI();
}
public void StartTimer()
{
timeRemaining = startTimeSeconds;
running = true;
UpdateUI();
2025-09-06 00:08:22 +05:00
// Ensure panel is hidden at the start of a round
if (gameOverPanel != null)
gameOverPanel.Hide();
}
public void StopTimer()
{
running = false;
}
public void AddTime(float seconds)
{
2025-09-06 00:08:22 +05:00
Debug.Log("Time added: " + seconds);
timeRemaining += seconds;
}
private void UpdateUI()
{
if (!timerText) return;
int minutes = Mathf.FloorToInt(timeRemaining / 60f);
int seconds = Mathf.FloorToInt(timeRemaining % 60f);
timerText.text = $"{minutes:00}:{seconds:00}";
}
}