3 Commits

12 changed files with 93 additions and 10 deletions

View File

@@ -1,6 +1,12 @@
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
{
/// <summary>
/// Calls draw logic for the <see cref="IBehaviour"/> to be displayed visually.
/// </summary>
void Draw();
}

View File

@@ -1,6 +1,12 @@
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
{
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/> after the main draw phase happens.
/// </summary>
void PostDraw();
}

View File

@@ -1,6 +1,12 @@
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
{
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/> after the main update phase happens.
/// </summary>
void PostUpdate();
}

View File

@@ -1,6 +1,12 @@
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
{
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/> before the main draw phase happens.
/// </summary>
void PreDraw();
}

View File

@@ -1,6 +1,12 @@
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
{
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/> before the main update phase happens.
/// </summary>
void PreUpdate();
}

View File

@@ -1,6 +1,12 @@
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
{
/// <summary>
/// Updates the state of the <see cref="IBehaviour"/>.
/// </summary>
void Update();
}

View File

@@ -3,9 +3,11 @@ namespace Syntriax.Engine.Core;
public class DrawManager : UniverseObject
{
private readonly BehaviourCollector<IPreDraw> preDrawEntities = new();
private readonly BehaviourCollector<IDraw> drawEntities = new();
private readonly BehaviourCollector<IPostDraw> postDrawEntities = new();
private static System.Comparison<IBehaviour> SortByPriority() => (x, y) => y.Priority.CompareTo(x.Priority);
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)
{

View File

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

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 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<ICollider2D> colliderCollector = new();
protected BehaviourCollector<IPhysicsUpdate> physicsUpdateCollector = new();
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
public float IterationPeriod { get => _iterationPeriod; set => _iterationPeriod = value.Max(0.0001f); }
@@ -25,6 +28,12 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
{
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++)
{
// Can Parallel
@@ -92,8 +101,8 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
OnPhysicsIteration?.InvokeSafe(this, intervalDeltaTime);
}
foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector)
physicsUpdate.PhysicsUpdate(deltaTime);
foreach (IPostPhysicsUpdate physicsPostUpdate in physicsPostUpdateCollector)
physicsPostUpdate.PostPhysicsUpdate(deltaTime);
OnPhysicsStep?.InvokeSafe(this, deltaTime);
}
@@ -109,7 +118,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
protected override void OnEnteringUniverse(IUniverse universe)
{
physicsPreUpdateCollector.Assign(universe);
physicsUpdateCollector.Assign(universe);
physicsPostUpdateCollector.Assign(universe);
colliderCollector.Assign(universe);
rigidBodyCollector.Assign(universe);
@@ -118,7 +129,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
protected override void OnExitingUniverse(IUniverse universe)
{
physicsPreUpdateCollector.Unassign();
physicsUpdateCollector.Unassign();
physicsPostUpdateCollector.Unassign();
colliderCollector.Unassign();
rigidBodyCollector.Unassign();