72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UniRx;
|
|
using UnityEngine;
|
|
|
|
namespace SLSUtilities.General
|
|
{
|
|
public class Timer
|
|
{
|
|
public float duration;
|
|
public float currentTime;
|
|
public bool isInfinite;
|
|
public bool keepActions;
|
|
public float Percentage => duration > 0 ? Mathf.Clamp01(currentTime / duration) : 1f;
|
|
public bool isCompleted;
|
|
|
|
public List<PrioritizedAction> onComplete;
|
|
private IDisposable _autoObserver;
|
|
|
|
public Timer(float duration, bool keepActions = true, bool isInfinite = false)
|
|
{
|
|
this.duration = duration;
|
|
this.isInfinite = isInfinite;
|
|
this.keepActions = keepActions;
|
|
currentTime = 0f;
|
|
onComplete = new List<PrioritizedAction>();
|
|
}
|
|
|
|
public virtual void Update(float deltaTime)
|
|
{
|
|
currentTime += deltaTime;
|
|
|
|
if (!isInfinite && !isCompleted && currentTime >= duration)
|
|
{
|
|
isCompleted = true;
|
|
onComplete.Invoke();
|
|
if (!keepActions)
|
|
{
|
|
onComplete.Clear();
|
|
}
|
|
}
|
|
else if (isInfinite)
|
|
{
|
|
onComplete.Invoke();
|
|
}
|
|
}
|
|
|
|
public virtual void Reset(float newDuration = -1f)
|
|
{
|
|
isCompleted = false;
|
|
currentTime = 0f;
|
|
if(newDuration >= 0f)
|
|
{
|
|
duration = newDuration;
|
|
}
|
|
}
|
|
|
|
public virtual void Complete(bool invokeAction = true)
|
|
{
|
|
currentTime = duration;
|
|
isCompleted = true;
|
|
if (invokeAction)
|
|
{
|
|
onComplete.Invoke();
|
|
if (!keepActions)
|
|
{
|
|
onComplete.Clear();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |