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
This commit is contained in:
@@ -7,6 +7,8 @@ namespace Syntriax.Engine.Physics2D;
|
||||
|
||||
public class PhysicsCoroutineManager : UniverseObject
|
||||
{
|
||||
private readonly Event<IUniverse, IUniverse.UpdateArguments>.EventHandler delegateOnUpdate = null!;
|
||||
|
||||
private readonly List<IEnumerator> enumerators = [];
|
||||
private IPhysicsEngine2D? physicsEngine = null;
|
||||
|
||||
@@ -25,9 +27,9 @@ public class PhysicsCoroutineManager : UniverseObject
|
||||
{
|
||||
physicsEngine = universe.GetUniverseObject<IPhysicsEngine2D>();
|
||||
if (physicsEngine is IPhysicsEngine2D foundPhysicsEngine)
|
||||
foundPhysicsEngine.OnPhysicsStep += OnPhysicsStep;
|
||||
foundPhysicsEngine.OnPhysicsStep.RemoveListener(OnPhysicsStep);
|
||||
else
|
||||
universe.OnUpdate += OnUpdate;
|
||||
universe.OnUpdate.AddListener(OnUpdate);
|
||||
}
|
||||
|
||||
private void OnPhysicsStep(IPhysicsEngine2D sender, float stepDeltaTime)
|
||||
@@ -45,11 +47,11 @@ public class PhysicsCoroutineManager : UniverseObject
|
||||
protected override void OnExitingUniverse(IUniverse universe)
|
||||
{
|
||||
if (physicsEngine is IPhysicsEngine2D existingPhysicsEngine)
|
||||
existingPhysicsEngine.OnPhysicsStep -= OnPhysicsStep;
|
||||
universe.OnUpdate -= OnUpdate;
|
||||
existingPhysicsEngine.OnPhysicsStep.RemoveListener(OnPhysicsStep);
|
||||
universe.OnUpdate.RemoveListener(OnUpdate);
|
||||
}
|
||||
|
||||
private void OnUpdate(IUniverse sender, UniverseTime engineTime)
|
||||
private void OnUpdate(IUniverse sender, IUniverse.UpdateArguments args)
|
||||
{
|
||||
if (Universe is not IUniverse universe)
|
||||
return;
|
||||
@@ -57,8 +59,13 @@ public class PhysicsCoroutineManager : UniverseObject
|
||||
physicsEngine = universe.GetUniverseObject<IPhysicsEngine2D>();
|
||||
if (physicsEngine is IPhysicsEngine2D foundPhysicsEngine)
|
||||
{
|
||||
foundPhysicsEngine.OnPhysicsStep += OnPhysicsStep;
|
||||
universe.OnUpdate -= OnUpdate;
|
||||
foundPhysicsEngine.OnPhysicsStep.AddListener(OnPhysicsStep);
|
||||
universe.OnUpdate.RemoveListener(OnUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
public PhysicsCoroutineManager()
|
||||
{
|
||||
delegateOnUpdate = OnUpdate;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user