diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/TimerModule/GameTimerTick.cs b/UnityProject/Assets/TEngine/Runtime/Modules/TimerModule/GameTimerTick.cs new file mode 100644 index 00000000..87d3e5fa --- /dev/null +++ b/UnityProject/Assets/TEngine/Runtime/Modules/TimerModule/GameTimerTick.cs @@ -0,0 +1,60 @@ +using UnityEngine; + +namespace TEngine +{ + public class GameTimerTick + { + protected OnTick Handle; + protected float LastTime; + protected float Interval; + protected bool ResetInterval = true; + + public GameTimerTick(float interval, OnTick tickHandle) => Init(interval, true, true, tickHandle); + + public GameTimerTick(float interval, bool immediately, OnTick tickHandle) => Init(interval, immediately, true, tickHandle); + + public GameTimerTick(float interval, bool immediately, bool resetInterval, OnTick tickHandle) + { + Init(interval, immediately, resetInterval, tickHandle); + } + + private void Init(float interval, bool immediately, bool resetInterval, OnTick tickHandle) + { + Interval = interval; + Handle = tickHandle; + ResetInterval = resetInterval; + if (immediately) + { + return; + } + + LastTime = Time.time; + } + + public void OnUpdate() + { + float time = Time.time; + if (LastTime + Interval >= time) + { + return; + } + + if (ResetInterval) + { + LastTime = time; + } + else if (LastTime == 0.0) + { + LastTime = time; + } + else + { + LastTime += Interval; + } + + Handle(); + } + + public delegate void OnTick(); + } +} \ No newline at end of file diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/TimerModule/GameTimerTick.cs.meta b/UnityProject/Assets/TEngine/Runtime/Modules/TimerModule/GameTimerTick.cs.meta new file mode 100644 index 00000000..f41e6d2b --- /dev/null +++ b/UnityProject/Assets/TEngine/Runtime/Modules/TimerModule/GameTimerTick.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 76843c14c1344aa68863289bf7388238 +timeCreated: 1701088103 \ No newline at end of file