feat: ticker is decoupled from stopwatch and added timer and stopwatch tickers

This commit is contained in:
Syntriax 2025-07-25 23:24:08 +03:00
parent ad365dc722
commit df06e8d134
4 changed files with 45 additions and 5 deletions

View File

@ -2,7 +2,7 @@ using Syntriax.Engine.Core;
namespace Syntriax.Engine.Systems.Time;
public interface ITicker : IStopwatch
public interface ITicker
{
Event<ITicker> OnTick { get; }

View File

@ -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<ITicker> 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++;

View File

@ -0,0 +1,40 @@
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Systems.Time;
public class TickerTimer : Timer, ITicker
{
public Event<ITicker> 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;
}
}

View File

@ -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;