Unity_Utils/JohnsonUtils/Common/TimerWrapper.cs
2023-03-17 23:41:38 -04:00

45 lines
1.3 KiB
C#

using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Events;
namespace JohnsonUtils.Common
{
public class TimerWrapper : MonoBehaviour
{
[PublicAPI] public UnityAction OnTimerOver;
[SerializeField, Min(0)] private float startTime;
[SerializeField] private bool countDown = true;
[SerializeField] private bool startOnAwake;
[SerializeField] private bool startOnStart;
private Timer _timer;
[PublicAPI] public bool IsRunning => _timer.IsRunning;
[PublicAPI] public float CurrentTime => _timer.CurrentTime;
private void Awake()
{
_timer = new Timer(startTime, countDown);
_timer.OnTimeOver += OnTimeOver;
if (startOnAwake) _timer.Start();
}
private void Start()
{
if (startOnStart) _timer.Start();
}
private void Update()
{
_timer.Tick(Time.deltaTime);
}
[PublicAPI] public void StartTimer() => _timer.Start();
[PublicAPI] public void PauseTimer() => _timer.Pause();
[PublicAPI] public void StopTimer() => _timer.Stop();
[PublicAPI] public void ResetTimer() => _timer.Reset();
private void OnTimeOver() => OnTimerOver?.Invoke();
}
}