This commit is contained in:
SoulliesOfficial
2026-05-27 15:15:28 -04:00
parent 76f498ae2b
commit 72756712f7
669 changed files with 5361 additions and 12268 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using UniRx;
using UnityEngine;
namespace SLSUtilities.General
@@ -9,47 +10,63 @@ namespace SLSUtilities.General
public float duration;
public float currentTime;
public bool isInfinite;
public bool keepActions;
public float Percentage => duration > 0 ? Mathf.Clamp01(currentTime / duration) : 1f;
public virtual bool IsCompleted => currentTime >= duration;
public bool isCompleted;
public List<PrioritizedAction> onComplete;
private IDisposable _autoObserver;
public Timer(float duration, bool isInfinite = false)
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)
{
if (!IsCompleted)
currentTime += deltaTime;
if (!isInfinite && !isCompleted && currentTime >= duration)
{
currentTime += deltaTime;
isCompleted = true;
onComplete.Invoke();
if (!keepActions)
{
onComplete.Clear();
}
}
else
else if (isInfinite)
{
if (!isInfinite)
{
onComplete.Invoke();
onComplete.Clear(); // 确保只调用一次
}
else
{
onComplete.Invoke();
Reset();
}
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();
}
}
}
}
}