refactor: updated systems to use the update interfaces
This commit is contained in:
parent
fbdea47dc7
commit
45524e474e
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
|||||||
|
|
||||||
namespace Syntriax.Engine.Core;
|
namespace Syntriax.Engine.Core;
|
||||||
|
|
||||||
public class CoroutineManager : Behaviour
|
public class CoroutineManager : Behaviour, IUpdate
|
||||||
{
|
{
|
||||||
private readonly List<IEnumerator> enumerators = [];
|
private readonly List<IEnumerator> enumerators = [];
|
||||||
|
|
||||||
@ -18,17 +18,7 @@ public class CoroutineManager : Behaviour
|
|||||||
enumerators.Remove(enumerator);
|
enumerators.Remove(enumerator);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnEnteredUniverse(IUniverse universe)
|
void IUpdate.Update()
|
||||||
{
|
|
||||||
universe.OnUpdate.AddListener(OnUpdate);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnExitedUniverse(IUniverse universe)
|
|
||||||
{
|
|
||||||
universe.OnUpdate.RemoveListener(OnUpdate);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnUpdate(IUniverse sender, IUniverse.UpdateArguments args)
|
|
||||||
{
|
{
|
||||||
for (int i = enumerators.Count - 1; i >= 0; i--)
|
for (int i = enumerators.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
@ -39,4 +29,6 @@ public class CoroutineManager : Behaviour
|
|||||||
enumerators.RemoveAt(i);
|
enumerators.RemoveAt(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CoroutineManager() => Priority = int.MinValue;
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ using Syntriax.Engine.Core;
|
|||||||
|
|
||||||
namespace Syntriax.Engine.Physics2D;
|
namespace Syntriax.Engine.Physics2D;
|
||||||
|
|
||||||
public class PhysicsCoroutineManager : Behaviour
|
public class PhysicsCoroutineManager : Behaviour, IPhysicsUpdate
|
||||||
{
|
{
|
||||||
private readonly Event<IUniverse, IUniverse.UpdateArguments>.EventHandler delegateOnUpdate = null!;
|
private readonly Event<IUniverse, IUniverse.UpdateArguments>.EventHandler delegateOnUpdate = null!;
|
||||||
|
|
||||||
@ -23,16 +23,7 @@ public class PhysicsCoroutineManager : Behaviour
|
|||||||
enumerators.Remove(enumerator);
|
enumerators.Remove(enumerator);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnEnteredUniverse(IUniverse universe)
|
public void PhysicsUpdate(float delta)
|
||||||
{
|
|
||||||
physicsEngine = universe.FindRequiredBehaviour<IPhysicsEngine2D>();
|
|
||||||
if (physicsEngine is IPhysicsEngine2D foundPhysicsEngine)
|
|
||||||
foundPhysicsEngine.OnPhysicsStep.RemoveListener(OnPhysicsStep);
|
|
||||||
else
|
|
||||||
universe.OnUpdate.AddListener(OnUpdate);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnPhysicsStep(IPhysicsEngine2D sender, float stepDeltaTime)
|
|
||||||
{
|
{
|
||||||
for (int i = enumerators.Count - 1; i >= 0; i--)
|
for (int i = enumerators.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
@ -44,28 +35,5 @@ public class PhysicsCoroutineManager : Behaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnExitedUniverse(IUniverse universe)
|
public PhysicsCoroutineManager() => Priority = int.MinValue;
|
||||||
{
|
|
||||||
if (physicsEngine is IPhysicsEngine2D existingPhysicsEngine)
|
|
||||||
existingPhysicsEngine.OnPhysicsStep.RemoveListener(OnPhysicsStep);
|
|
||||||
universe.OnUpdate.RemoveListener(delegateOnUpdate);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnUpdate(IUniverse sender, IUniverse.UpdateArguments args)
|
|
||||||
{
|
|
||||||
if (Universe is not IUniverse universe)
|
|
||||||
return;
|
|
||||||
|
|
||||||
physicsEngine = universe.FindRequiredBehaviour<IPhysicsEngine2D>();
|
|
||||||
if (physicsEngine is IPhysicsEngine2D foundPhysicsEngine)
|
|
||||||
{
|
|
||||||
foundPhysicsEngine.OnPhysicsStep.AddListener(OnPhysicsStep);
|
|
||||||
universe.OnUpdate.RemoveListener(delegateOnUpdate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public PhysicsCoroutineManager()
|
|
||||||
{
|
|
||||||
delegateOnUpdate = OnUpdate;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,11 @@ using Syntriax.Engine.Core;
|
|||||||
|
|
||||||
namespace Syntriax.Engine.Physics2D;
|
namespace Syntriax.Engine.Physics2D;
|
||||||
|
|
||||||
public class PhysicsEngine2D : Behaviour, IPhysicsEngine2D
|
public class PhysicsEngine2D : Behaviour, IPreUpdate, IPhysicsEngine2D
|
||||||
{
|
{
|
||||||
public Event<IPhysicsEngine2D, float> OnPhysicsIteration { get; } = new();
|
public Event<IPhysicsEngine2D, float> OnPhysicsIteration { get; } = new();
|
||||||
public Event<IPhysicsEngine2D, float> OnPhysicsStep { get; } = new();
|
public Event<IPhysicsEngine2D, float> OnPhysicsStep { get; } = new();
|
||||||
|
|
||||||
private readonly Event<IUniverse, IUniverse.UpdateArguments>.EventHandler delegateOnPreUpdate = null!;
|
|
||||||
|
|
||||||
private float physicsTicker = 0f;
|
private float physicsTicker = 0f;
|
||||||
private int _iterationPerStep = 1;
|
private int _iterationPerStep = 1;
|
||||||
private float _iterationPeriod = 1f / 60f;
|
private float _iterationPeriod = 1f / 60f;
|
||||||
@ -191,8 +189,6 @@ public class PhysicsEngine2D : Behaviour, IPhysicsEngine2D
|
|||||||
physicsPostUpdateCollector.Assign(universe);
|
physicsPostUpdateCollector.Assign(universe);
|
||||||
colliderCollector.Assign(universe);
|
colliderCollector.Assign(universe);
|
||||||
rigidBodyCollector.Assign(universe);
|
rigidBodyCollector.Assign(universe);
|
||||||
|
|
||||||
universe.OnPreUpdate.AddListener(OnEnginePreUpdate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnExitedUniverse(IUniverse universe)
|
protected override void OnExitedUniverse(IUniverse universe)
|
||||||
@ -203,13 +199,11 @@ public class PhysicsEngine2D : Behaviour, IPhysicsEngine2D
|
|||||||
physicsPostUpdateCollector.Unassign();
|
physicsPostUpdateCollector.Unassign();
|
||||||
colliderCollector.Unassign();
|
colliderCollector.Unassign();
|
||||||
rigidBodyCollector.Unassign();
|
rigidBodyCollector.Unassign();
|
||||||
|
|
||||||
universe.OnPreUpdate.RemoveListener(OnEnginePreUpdate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnEnginePreUpdate(IUniverse sender, IUniverse.UpdateArguments args)
|
void IPreUpdate.PreUpdate()
|
||||||
{
|
{
|
||||||
physicsTicker += args.EngineTime.DeltaTime;
|
physicsTicker += Universe.Time.DeltaTime;
|
||||||
|
|
||||||
while (physicsTicker >= IterationPeriod)
|
while (physicsTicker >= IterationPeriod)
|
||||||
{
|
{
|
||||||
@ -222,15 +216,13 @@ public class PhysicsEngine2D : Behaviour, IPhysicsEngine2D
|
|||||||
{
|
{
|
||||||
collisionDetector = new CollisionDetector2D();
|
collisionDetector = new CollisionDetector2D();
|
||||||
collisionResolver = new CollisionResolver2D();
|
collisionResolver = new CollisionResolver2D();
|
||||||
|
Priority = int.MaxValue;
|
||||||
delegateOnPreUpdate = OnEnginePreUpdate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public PhysicsEngine2D(ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
|
public PhysicsEngine2D(ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
|
||||||
{
|
{
|
||||||
this.collisionDetector = collisionDetector;
|
this.collisionDetector = collisionDetector;
|
||||||
this.collisionResolver = collisionResolver;
|
this.collisionResolver = collisionResolver;
|
||||||
|
Priority = int.MaxValue;
|
||||||
delegateOnPreUpdate = OnEnginePreUpdate;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user