You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB
C#

using System;
namespace Script.FrameWork.Time
{
public class Timer
{
private float _duration;
private Action<float> _updateAction;
private Action _completeAction;
public bool Finished;
public Timer(float duration, Action<float> updateAction = null, Action completeAction = null)
{
_duration = duration;
_updateAction = updateAction;
_completeAction = completeAction;
}
public void Cancel(bool destroyImmediately = false)
{
Finished = true;
if (destroyImmediately)
Destroy();
}
public void Update(float dt)
{
_duration -= dt;
if (_duration <= 0)
{
_completeAction?.Invoke();
Finished = true;
return;
}
_updateAction?.Invoke(_duration);
}
public void Destroy()
{
_completeAction = null;
_updateAction = null;
}
}
}