feat: ticker is decoupled from stopwatch and added timer and stopwatch tickers
This commit is contained in:
40
Engine.Systems/Time/TickerStopwatch.cs
Normal file
40
Engine.Systems/Time/TickerStopwatch.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Systems.Time;
|
||||
|
||||
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 = double.MaxValue;
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
TickCounter = 0;
|
||||
base.Start();
|
||||
nextTick = Time + Period;
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
base.Update();
|
||||
|
||||
while (Time >= nextTick)
|
||||
{
|
||||
nextTick += Period;
|
||||
TickCounter++;
|
||||
OnTick?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnFinalize()
|
||||
{
|
||||
base.OnFinalize();
|
||||
|
||||
TickCounter = 0;
|
||||
nextTick = 0f;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user