Syntriax.Engine/Engine.Systems/Time/TickerBehaviour.cs
Syntriax 61e2761580 perf!: events refactored throughout all the project to use Event<> class
All delegate events are refactored to use the Event<TSender> and Event<TSender, TArgument> for performance issues regarding delegate events creating garbage, also this gives us better control on event invocation since C# Delegates did also create unnecessary garbage during Delegate.DynamicInvoke
2025-05-31 00:32:58 +03:00

41 lines
799 B
C#

using Syntriax.Engine.Core;
namespace Syntriax.Engine.Systems.Time;
public class TickerBehaviour : StopwatchBehaviour, ITicker
{
public Event<ITicker> OnTick { get; } = new();
public double Period { get; set; } = 1f;
public int TickCounter { get; private set; } = 0;
private double nextTick = 0f;
public override void Start()
{
TickCounter = 0;
base.Start();
nextTick = Time + Period;
}
protected override void OnUpdate()
{
base.OnUpdate();
while (Time > nextTick)
{
nextTick += Period;
TickCounter++;
OnTick?.Invoke(this);
}
}
protected override void OnFinalize()
{
base.OnFinalize();
TickCounter = 0;
nextTick = 0f;
}
}