From 62e50aefc1b11f2af5ad29ac5af9eac658969686 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 25 Oct 2024 23:10:32 +0300 Subject: [PATCH] feat: IPhysicsEngine2D Events --- Engine.Physics2D/Abstract/IPhysicsEngine2D.cs | 13 +++++++++++++ Engine.Physics2D/PhysicsEngine2D.cs | 6 +++++- Engine.Physics2D/PhysicsEngine2DCollector.cs | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Engine.Physics2D/Abstract/IPhysicsEngine2D.cs b/Engine.Physics2D/Abstract/IPhysicsEngine2D.cs index 71d2003..cd54c09 100644 --- a/Engine.Physics2D/Abstract/IPhysicsEngine2D.cs +++ b/Engine.Physics2D/Abstract/IPhysicsEngine2D.cs @@ -5,6 +5,16 @@ namespace Syntriax.Engine.Physics2D.Abstract; /// public interface IPhysicsEngine2D { + /// + /// Event triggered when the has done a single physics iteration. + /// + event OnPhysicsIterationDelegate? OnPhysicsIteration; + + /// + /// Event triggered when the has done a full physics step/>. + /// + event OnPhysicsStepDelegate? OnPhysicsStep; + /// /// The number of iterations the performs per step. /// @@ -15,4 +25,7 @@ public interface IPhysicsEngine2D /// /// The time step. void Step(float deltaTime); + + delegate void OnPhysicsIterationDelegate(IPhysicsEngine2D sender, float iterationDeltaTime); + delegate void OnPhysicsStepDelegate(IPhysicsEngine2D sender, float stepDeltaTime); } diff --git a/Engine.Physics2D/PhysicsEngine2D.cs b/Engine.Physics2D/PhysicsEngine2D.cs index e333cbe..217396e 100644 --- a/Engine.Physics2D/PhysicsEngine2D.cs +++ b/Engine.Physics2D/PhysicsEngine2D.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; -using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D.Abstract; @@ -8,6 +7,9 @@ namespace Syntriax.Engine.Physics2D; public class PhysicsEngine2D : IPhysicsEngine2D { + public event IPhysicsEngine2D.OnPhysicsIterationDelegate? OnPhysicsIteration = null; + public event IPhysicsEngine2D.OnPhysicsStepDelegate? OnPhysicsStep = null; + private readonly List rigidBodies = new(32); private readonly List 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) diff --git a/Engine.Physics2D/PhysicsEngine2DCollector.cs b/Engine.Physics2D/PhysicsEngine2DCollector.cs index c7eb131..d89c1b8 100644 --- a/Engine.Physics2D/PhysicsEngine2DCollector.cs +++ b/Engine.Physics2D/PhysicsEngine2DCollector.cs @@ -9,6 +9,8 @@ public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager public event IAssignable.OnUnassignedDelegate? OnUnassigned = null; public event IAssignableGameManager.OnGameManagerAssignedDelegate? OnGameManagerAssigned = null; + public event IPhysicsEngine2D.OnPhysicsIterationDelegate? OnPhysicsIteration = null; + public event IPhysicsEngine2D.OnPhysicsStepDelegate? OnPhysicsStep = null; private int _iterationPerStep = 1; @@ -82,10 +84,14 @@ public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager } } } + + OnPhysicsIteration?.Invoke(this, intervalDeltaTime); } foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector) physicsUpdate.PhysicsUpdate(deltaTime); + + OnPhysicsStep?.Invoke(this, deltaTime); } private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)