58 lines
1.1 KiB
C#
58 lines
1.1 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class ShipPlacementTimer : MonoBehaviour
|
|
{
|
|
[SerializeField] private float timeLimit = 120f;
|
|
[SerializeField] private TMP_Text timerText;
|
|
|
|
public event Action TimerExpired;
|
|
|
|
private float timeLeft;
|
|
private bool isRunning;
|
|
|
|
public void StartTimer()
|
|
{
|
|
timeLeft = timeLimit;
|
|
isRunning = true;
|
|
UpdateTimerText();
|
|
}
|
|
|
|
public void StopTimer()
|
|
{
|
|
isRunning = false;
|
|
|
|
if (timerText != null)
|
|
timerText.text = string.Empty;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isRunning)
|
|
return;
|
|
|
|
timeLeft -= Time.deltaTime;
|
|
|
|
if (timeLeft <= 0f)
|
|
{
|
|
timeLeft = 0f;
|
|
isRunning = false;
|
|
UpdateTimerText();
|
|
TimerExpired?.Invoke();
|
|
return;
|
|
}
|
|
|
|
UpdateTimerText();
|
|
}
|
|
|
|
private void UpdateTimerText()
|
|
{
|
|
if (timerText == null)
|
|
return;
|
|
|
|
TimeSpan time = TimeSpan.FromSeconds(Mathf.CeilToInt(timeLeft));
|
|
timerText.text = time.ToString(@"mm\:ss");
|
|
}
|
|
}
|