3 Commits

12 changed files with 93 additions and 10 deletions

View File

@@ -1,6 +1,12 @@
namespace Syntriax.Engine.Core; namespace Syntriax.Engine.Core;
/// <summary>
/// Represents a <see cref="IBehaviour"/> to be notified when the draw phase of the <see cref="IUniverse"/> occurs.
/// </summary>
public interface IDraw : IBehaviour public interface IDraw : IBehaviour
{ {
/// <summary>
/// Calls draw logic for the <see cref="IBehaviour"/> to be displayed visually.
/// </summary>
void Draw(); void Draw();
} }

View File

@@ -1,6 +1,12 @@
namespace Syntriax.Engine.Core; namespace Syntriax.Engine.Core;
/// <summary>
/// Represents a <see cref="IBehaviour"/> to be notified after the draw phase of the <see cref="IUniverse"/> occurs.
/// </summary>
public interface IPostDraw : IBehaviour public interface IPostDraw : IBehaviour
{ {
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/> after the main draw phase happens.
/// </summary>
void PostDraw(); void PostDraw();
} }

View File

@@ -1,6 +1,12 @@
namespace Syntriax.Engine.Core; namespace Syntriax.Engine.Core;
/// <summary>
/// Represents a <see cref="IBehaviour"/> to be notified after the update phase of the <see cref="IUniverse"/> occurs.
/// </summary>
public interface IPostUpdate : IBehaviour public interface IPostUpdate : IBehaviour
{ {
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/> after the main update phase happens.
/// </summary>
void PostUpdate(); void PostUpdate();
} }

View File

@@ -1,6 +1,12 @@
namespace Syntriax.Engine.Core; namespace Syntriax.Engine.Core;
/// <summary>
/// Represents a <see cref="IBehaviour"/> to be notified before the draw phase of the <see cref="IUniverse"/> occurs.
/// </summary>
public interface IPreDraw : IBehaviour public interface IPreDraw : IBehaviour
{ {
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/> before the main draw phase happens.
/// </summary>
void PreDraw(); void PreDraw();
} }

View File

@@ -1,6 +1,12 @@
namespace Syntriax.Engine.Core; namespace Syntriax.Engine.Core;
/// <summary>
/// Represents a <see cref="IBehaviour"/> to be notified before the update phase of the <see cref="IUniverse"/> occurs.
/// </summary>
public interface IPreUpdate : IBehaviour public interface IPreUpdate : IBehaviour
{ {
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/> before the main update phase happens.
/// </summary>
void PreUpdate(); void PreUpdate();
} }

View File

@@ -1,6 +1,12 @@
namespace Syntriax.Engine.Core; namespace Syntriax.Engine.Core;
/// <summary>
/// Represents a <see cref="IBehaviour"/> to be notified when the update phase of the <see cref="IUniverse"/> occurs.
/// </summary>
public interface IUpdate : IBehaviour public interface IUpdate : IBehaviour
{ {
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/>.
/// </summary>
void Update(); void Update();
} }

View File

@@ -3,9 +3,11 @@ namespace Syntriax.Engine.Core;
public class DrawManager : UniverseObject public class DrawManager : UniverseObject
{ {
private readonly BehaviourCollector<IPreDraw> preDrawEntities = new(); private static System.Comparison<IBehaviour> SortByPriority() => (x, y) => y.Priority.CompareTo(x.Priority);
private readonly BehaviourCollector<IDraw> drawEntities = new();
private readonly BehaviourCollector<IPostDraw> postDrawEntities = new(); private readonly ActiveBehaviourCollectorSorted<IPreDraw> preDrawEntities = new() { SortBy = SortByPriority() };
private readonly ActiveBehaviourCollectorSorted<IDraw> drawEntities = new() { SortBy = SortByPriority() };
private readonly ActiveBehaviourCollectorSorted<IPostDraw> postDrawEntities = new() { SortBy = SortByPriority() };
private void OnPreDraw(IUniverse sender) private void OnPreDraw(IUniverse sender)
{ {

View File

@@ -5,10 +5,12 @@ namespace Syntriax.Engine.Core;
public class UpdateManager : UniverseObject public class UpdateManager : UniverseObject
{ {
private readonly BehaviourCollector<IFirstFrameUpdate> firstFrameUpdates = new(); private static System.Comparison<IBehaviour> SortByPriority() => (x, y) => y.Priority.CompareTo(x.Priority);
private readonly BehaviourCollector<IPreUpdate> preUpdateEntities = new();
private readonly BehaviourCollector<IUpdate> updateEntities = new(); private readonly ActiveBehaviourCollectorSorted<IFirstFrameUpdate> firstFrameUpdates = new() { SortBy = SortByPriority() };
private readonly BehaviourCollector<IPostUpdate> postUpdateEntities = new(); private readonly ActiveBehaviourCollectorSorted<IPreUpdate> preUpdateEntities = new() { SortBy = SortByPriority() };
private readonly ActiveBehaviourCollectorSorted<IUpdate> updateEntities = new() { SortBy = SortByPriority() };
private readonly ActiveBehaviourCollectorSorted<IPostUpdate> postUpdateEntities = new() { SortBy = SortByPriority() };
private readonly List<IFirstFrameUpdate> toCallFirstFrameUpdates = []; private readonly List<IFirstFrameUpdate> toCallFirstFrameUpdates = [];

View File

@@ -0,0 +1,15 @@
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D;
/// <summary>
/// Represents a <see cref="IBehaviour"/> that listens to the phase after the physics simulation phase.
/// </summary>
public interface IPostPhysicsUpdate : IBehaviour
{
/// <summary>
/// Execute logic that should occur after the physics simulation has been updated.
/// </summary>
/// <param name="delta">The time elapsed since the last physics update, typically in seconds.</param>
void PostPhysicsUpdate(float delta);
}

View File

@@ -0,0 +1,15 @@
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D;
/// <summary>
/// Represents a <see cref="IBehaviour"/> that listens to the phase before the physics simulation phase.
/// </summary>
public interface IPrePhysicsUpdate : IBehaviour
{
/// <summary>
/// Execute logic that should occur before the physics simulation is updated.
/// </summary>
/// <param name="delta">The time elapsed since the last physics update, typically in seconds.</param>
void PrePhysicsUpdate(float delta);
}

View File

@@ -14,9 +14,12 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
protected readonly ICollisionDetector2D collisionDetector = null!; protected readonly ICollisionDetector2D collisionDetector = null!;
protected readonly ICollisionResolver2D collisionResolver = null!; protected readonly ICollisionResolver2D collisionResolver = null!;
private static System.Comparison<IBehaviour> SortByPriority() => (x, y) => y.Priority.CompareTo(x.Priority);
protected ActiveBehaviourCollectorSorted<IPrePhysicsUpdate> physicsPreUpdateCollector = new() { SortBy = SortByPriority() };
protected ActiveBehaviourCollectorSorted<IPhysicsUpdate> physicsUpdateCollector = new() { SortBy = SortByPriority() };
protected ActiveBehaviourCollectorSorted<IPostPhysicsUpdate> physicsPostUpdateCollector = new() { SortBy = SortByPriority() };
protected BehaviourCollector<IRigidBody2D> rigidBodyCollector = new(); protected BehaviourCollector<IRigidBody2D> rigidBodyCollector = new();
protected BehaviourCollector<ICollider2D> colliderCollector = new(); protected BehaviourCollector<ICollider2D> colliderCollector = new();
protected BehaviourCollector<IPhysicsUpdate> physicsUpdateCollector = new();
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; } public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
public float IterationPeriod { get => _iterationPeriod; set => _iterationPeriod = value.Max(0.0001f); } public float IterationPeriod { get => _iterationPeriod; set => _iterationPeriod = value.Max(0.0001f); }
@@ -25,6 +28,12 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
{ {
float intervalDeltaTime = deltaTime / IterationPerStep; float intervalDeltaTime = deltaTime / IterationPerStep;
foreach (IPrePhysicsUpdate physicsPreUpdate in physicsPreUpdateCollector)
physicsPreUpdate.PrePhysicsUpdate(deltaTime);
foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector)
physicsUpdate.PhysicsUpdate(deltaTime);
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++) for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
{ {
// Can Parallel // Can Parallel
@@ -92,8 +101,8 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
OnPhysicsIteration?.InvokeSafe(this, intervalDeltaTime); OnPhysicsIteration?.InvokeSafe(this, intervalDeltaTime);
} }
foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector) foreach (IPostPhysicsUpdate physicsPostUpdate in physicsPostUpdateCollector)
physicsUpdate.PhysicsUpdate(deltaTime); physicsPostUpdate.PostPhysicsUpdate(deltaTime);
OnPhysicsStep?.InvokeSafe(this, deltaTime); OnPhysicsStep?.InvokeSafe(this, deltaTime);
} }
@@ -109,7 +118,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
protected override void OnEnteringUniverse(IUniverse universe) protected override void OnEnteringUniverse(IUniverse universe)
{ {
physicsPreUpdateCollector.Assign(universe);
physicsUpdateCollector.Assign(universe); physicsUpdateCollector.Assign(universe);
physicsPostUpdateCollector.Assign(universe);
colliderCollector.Assign(universe); colliderCollector.Assign(universe);
rigidBodyCollector.Assign(universe); rigidBodyCollector.Assign(universe);
@@ -118,7 +129,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
protected override void OnExitingUniverse(IUniverse universe) protected override void OnExitingUniverse(IUniverse universe)
{ {
physicsPreUpdateCollector.Unassign();
physicsUpdateCollector.Unassign(); physicsUpdateCollector.Unassign();
physicsPostUpdateCollector.Unassign();
colliderCollector.Unassign(); colliderCollector.Unassign();
rigidBodyCollector.Unassign(); rigidBodyCollector.Unassign();