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; // 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; // Fire event (if someone else wants to listen) OnTimerEnded?.Invoke(); // Show Game Over UI right here if (gameOverPanel != null) gameOverPanel.ShowGameOver(ClawScoreManager.Instance); } UpdateUI(); } public void StartTimer() { timeRemaining = startTimeSeconds; running = true; UpdateUI(); // 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) { 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}"; } }