Files
Cielonos/Assets/Scripts/SLSUtilities/General/Timer.cs

72 lines
1.9 KiB
C#
Raw Normal View History

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