2026-04-20 카트 잡기
This commit is contained in:
44
Assets/02_Scripts/GameTimer.cs
Normal file
44
Assets/02_Scripts/GameTimer.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameTimer : MonoBehaviour
|
||||
{
|
||||
[SerializeField, Min(0f)] private float _timeLimit = 180f;
|
||||
[SerializeField] private bool _autoStart = true;
|
||||
|
||||
public event Action<float> OnTick;
|
||||
public event Action OnTimeUp;
|
||||
|
||||
private float _remaining;
|
||||
private bool _running;
|
||||
|
||||
public float Remaining => _remaining;
|
||||
public float TimeLimit => _timeLimit;
|
||||
public bool IsRunning => _running;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_remaining = _timeLimit;
|
||||
OnTick?.Invoke(_remaining);
|
||||
if (_autoStart) StartTimer();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_running) return;
|
||||
|
||||
_remaining -= Time.deltaTime;
|
||||
if (_remaining <= 0f)
|
||||
{
|
||||
_remaining = 0f;
|
||||
_running = false;
|
||||
OnTick?.Invoke(_remaining);
|
||||
OnTimeUp?.Invoke();
|
||||
return;
|
||||
}
|
||||
OnTick?.Invoke(_remaining);
|
||||
}
|
||||
|
||||
public void StartTimer() => _running = true;
|
||||
public void StopTimer() => _running = false;
|
||||
}
|
||||
Reference in New Issue
Block a user