2025-08-30 22:59:11 +05:00
|
|
|
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;
|
|
|
|
|
2025-08-30 22:59:11 +05:00
|
|
|
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)
|
2025-08-30 22:59:11 +05:00
|
|
|
OnTimerEnded?.Invoke();
|
2025-09-06 00:08:22 +05:00
|
|
|
|
|
|
|
// Show Game Over UI right here
|
|
|
|
if (gameOverPanel != null)
|
|
|
|
gameOverPanel.ShowGameOver(ClawScoreManager.Instance);
|
2025-08-30 22:59:11 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2025-08-30 22:59:11 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void StopTimer()
|
|
|
|
{
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddTime(float seconds)
|
|
|
|
{
|
2025-09-06 00:08:22 +05:00
|
|
|
Debug.Log("Time added: " + seconds);
|
2025-08-30 22:59:11 +05:00
|
|
|
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}";
|
|
|
|
}
|
|
|
|
}
|