MusicBeat
This commit is contained in:
55
Assets/Scripts/SLSUtilities/General/Timer.cs
Normal file
55
Assets/Scripts/SLSUtilities/General/Timer.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SLSUtilities.General
|
||||
{
|
||||
public class Timer
|
||||
{
|
||||
public float duration;
|
||||
public float currentTime;
|
||||
public bool isInfinite;
|
||||
public float Percentage => duration > 0 ? Mathf.Clamp01(currentTime / duration) : 1f;
|
||||
public virtual bool IsCompleted => currentTime >= duration;
|
||||
|
||||
public List<PrioritizedAction> onComplete;
|
||||
|
||||
public Timer(float duration, bool isInfinite = false)
|
||||
{
|
||||
this.duration = duration;
|
||||
this.isInfinite = isInfinite;
|
||||
currentTime = 0f;
|
||||
onComplete = new List<PrioritizedAction>();
|
||||
}
|
||||
|
||||
public virtual void Update(float deltaTime)
|
||||
{
|
||||
if (!IsCompleted)
|
||||
{
|
||||
currentTime += deltaTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!isInfinite)
|
||||
{
|
||||
onComplete.Invoke();
|
||||
onComplete.Clear(); // 确保只调用一次
|
||||
}
|
||||
else
|
||||
{
|
||||
onComplete.Invoke();
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Reset(float newDuration = -1f)
|
||||
{
|
||||
currentTime = 0f;
|
||||
if(newDuration >= 0f)
|
||||
{
|
||||
duration = newDuration;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user