diff --git a/Engine.Systems/Time/ITicker.cs b/Engine.Systems/Time/ITicker.cs index 1d88e4a..17ec73d 100644 --- a/Engine.Systems/Time/ITicker.cs +++ b/Engine.Systems/Time/ITicker.cs @@ -2,7 +2,7 @@ using Syntriax.Engine.Core; namespace Syntriax.Engine.Systems.Time; -public interface ITicker : IStopwatch +public interface ITicker { Event OnTick { get; } diff --git a/Engine.Systems/Time/Ticker.cs b/Engine.Systems/Time/TickerStopwatch.cs similarity index 83% rename from Engine.Systems/Time/Ticker.cs rename to Engine.Systems/Time/TickerStopwatch.cs index ec09f88..d0ad899 100644 --- a/Engine.Systems/Time/Ticker.cs +++ b/Engine.Systems/Time/TickerStopwatch.cs @@ -2,14 +2,14 @@ using Syntriax.Engine.Core; namespace Syntriax.Engine.Systems.Time; -public class Ticker : Stopwatch, ITicker +public class TickerStopwatch : Stopwatch, ITicker { public Event OnTick { get; } = new(); public double Period { get; set; } = 1f; public int TickCounter { get; private set; } = 0; - private double nextTick = 0f; + private double nextTick = double.MaxValue; public override void Start() { @@ -22,7 +22,7 @@ public class Ticker : Stopwatch, ITicker { base.Update(); - while (Time > nextTick) + while (Time >= nextTick) { nextTick += Period; TickCounter++; diff --git a/Engine.Systems/Time/TickerTimer.cs b/Engine.Systems/Time/TickerTimer.cs new file mode 100644 index 0000000..ec011c4 --- /dev/null +++ b/Engine.Systems/Time/TickerTimer.cs @@ -0,0 +1,40 @@ +using Syntriax.Engine.Core; + +namespace Syntriax.Engine.Systems.Time; + +public class TickerTimer : Timer, ITicker +{ + public Event OnTick { get; } = new(); + + public double Period { get; set; } = 1f; + public int TickCounter { get; private set; } = 0; + + private double nextTick = double.MinValue; + + public override void Start(double time) + { + TickCounter = 0; + base.Start(time); + nextTick = Remaining - Period; + } + + public override void Update() + { + base.Update(); + + while (Remaining <= nextTick) + { + nextTick -= Period; + TickCounter++; + OnTick?.Invoke(this); + } + } + + protected override void OnFinalize() + { + base.OnFinalize(); + + TickCounter = 0; + nextTick = 0f; + } +} diff --git a/Engine.Systems/Time/Timer.cs b/Engine.Systems/Time/Timer.cs index 45c01fb..2c8faa1 100644 --- a/Engine.Systems/Time/Timer.cs +++ b/Engine.Systems/Time/Timer.cs @@ -53,7 +53,7 @@ public class Timer : Behaviour, IUpdate, ITimer OnStopped?.Invoke(this); } - public void Update() + public virtual void Update() { if (State is not TimerState.Ticking) return;