feat: ticker is decoupled from stopwatch and added timer and stopwatch tickers
This commit is contained in:
parent
ad365dc722
commit
df06e8d134
@ -2,7 +2,7 @@ using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Systems.Time;
|
||||
|
||||
public interface ITicker : IStopwatch
|
||||
public interface ITicker
|
||||
{
|
||||
Event<ITicker> OnTick { get; }
|
||||
|
||||
|
@ -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++;
|
40
Engine.Systems/Time/TickerTimer.cs
Normal file
40
Engine.Systems/Time/TickerTimer.cs
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user