feat: IPhysicsEngine2D Events

This commit is contained in:
Syntriax 2024-10-25 23:10:32 +03:00
parent 9c2b098821
commit 62e50aefc1
3 changed files with 24 additions and 1 deletions

View File

@ -5,6 +5,16 @@ namespace Syntriax.Engine.Physics2D.Abstract;
/// </summary> /// </summary>
public interface IPhysicsEngine2D public interface IPhysicsEngine2D
{ {
/// <summary>
/// Event triggered when the <see cref="IPhysicsEngine2D"/> has done a single physics iteration.
/// </summary>
event OnPhysicsIterationDelegate? OnPhysicsIteration;
/// <summary>
/// Event triggered when the <see cref="IPhysicsEngine2D"/> has done a full physics step/>.
/// </summary>
event OnPhysicsStepDelegate? OnPhysicsStep;
/// <summary> /// <summary>
/// The number of iterations the <see cref="IPhysicsEngine2D"/> performs per step. /// The number of iterations the <see cref="IPhysicsEngine2D"/> performs per step.
/// </summary> /// </summary>
@ -15,4 +25,7 @@ public interface IPhysicsEngine2D
/// </summary> /// </summary>
/// <param name="deltaTime">The time step.</param> /// <param name="deltaTime">The time step.</param>
void Step(float deltaTime); void Step(float deltaTime);
delegate void OnPhysicsIterationDelegate(IPhysicsEngine2D sender, float iterationDeltaTime);
delegate void OnPhysicsStepDelegate(IPhysicsEngine2D sender, float stepDeltaTime);
} }

View File

@ -1,6 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Abstract; using Syntriax.Engine.Physics2D.Abstract;
@ -8,6 +7,9 @@ namespace Syntriax.Engine.Physics2D;
public class PhysicsEngine2D : IPhysicsEngine2D public class PhysicsEngine2D : IPhysicsEngine2D
{ {
public event IPhysicsEngine2D.OnPhysicsIterationDelegate? OnPhysicsIteration = null;
public event IPhysicsEngine2D.OnPhysicsStepDelegate? OnPhysicsStep = null;
private readonly List<IRigidBody2D> rigidBodies = new(32); private readonly List<IRigidBody2D> rigidBodies = new(32);
private readonly List<ICollider2D> colliders = new(64); private readonly List<ICollider2D> colliders = new(64);
@ -96,7 +98,9 @@ public class PhysicsEngine2D : IPhysicsEngine2D
} }
} }
} }
OnPhysicsIteration?.Invoke(this, intervalDeltaTime);
} }
OnPhysicsStep?.Invoke(this, deltaTime);
} }
private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime) private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)

View File

@ -9,6 +9,8 @@ public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager
public event IAssignable.OnUnassignedDelegate? OnUnassigned = null; public event IAssignable.OnUnassignedDelegate? OnUnassigned = null;
public event IAssignableGameManager.OnGameManagerAssignedDelegate? OnGameManagerAssigned = null; public event IAssignableGameManager.OnGameManagerAssignedDelegate? OnGameManagerAssigned = null;
public event IPhysicsEngine2D.OnPhysicsIterationDelegate? OnPhysicsIteration = null;
public event IPhysicsEngine2D.OnPhysicsStepDelegate? OnPhysicsStep = null;
private int _iterationPerStep = 1; private int _iterationPerStep = 1;
@ -82,10 +84,14 @@ public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager
} }
} }
} }
OnPhysicsIteration?.Invoke(this, intervalDeltaTime);
} }
foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector) foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector)
physicsUpdate.PhysicsUpdate(deltaTime); physicsUpdate.PhysicsUpdate(deltaTime);
OnPhysicsStep?.Invoke(this, deltaTime);
} }
private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime) private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)