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;
|
||||
|
||||
public class CoroutineManager : Behaviour
|
||||
public class CoroutineManager : Behaviour, IUpdate
|
||||
{
|
||||
private readonly List<IEnumerator> enumerators = [];
|
||||
|
||||
@ -18,17 +18,7 @@ public class CoroutineManager : Behaviour
|
||||
enumerators.Remove(enumerator);
|
||||
}
|
||||
|
||||
protected override void OnEnteredUniverse(IUniverse universe)
|
||||
{
|
||||
universe.OnUpdate.AddListener(OnUpdate);
|
||||
}
|
||||
|
||||
protected override void OnExitedUniverse(IUniverse universe)
|
||||
{
|
||||
universe.OnUpdate.RemoveListener(OnUpdate);
|
||||
}
|
||||
|
||||
private void OnUpdate(IUniverse sender, IUniverse.UpdateArguments args)
|
||||
void IUpdate.Update()
|
||||
{
|
||||
for (int i = enumerators.Count - 1; i >= 0; i--)
|
||||
{
|
||||
@ -39,4 +29,6 @@ public class CoroutineManager : Behaviour
|
||||
enumerators.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
public CoroutineManager() => Priority = int.MinValue;
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Physics2D;
|
||||
|
||||
public class PhysicsCoroutineManager : Behaviour
|
||||
public class PhysicsCoroutineManager : Behaviour, IPhysicsUpdate
|
||||
{
|
||||
private readonly Event<IUniverse, IUniverse.UpdateArguments>.EventHandler delegateOnUpdate = null!;
|
||||
|
||||
@ -23,16 +23,7 @@ public class PhysicsCoroutineManager : Behaviour
|
||||
enumerators.Remove(enumerator);
|
||||
}
|
||||
|
||||
protected override void OnEnteredUniverse(IUniverse universe)
|
||||
{
|
||||
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)
|
||||
public void PhysicsUpdate(float delta)
|
||||
{
|
||||
for (int i = enumerators.Count - 1; i >= 0; i--)
|
||||
{
|
||||
@ -44,28 +35,5 @@ public class PhysicsCoroutineManager : Behaviour
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnExitedUniverse(IUniverse universe)
|
||||
{
|
||||
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;
|
||||
}
|
||||
public PhysicsCoroutineManager() => Priority = int.MinValue;
|
||||
}
|
||||
|
@ -4,13 +4,11 @@ using Syntriax.Engine.Core;
|
||||
|
||||
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> OnPhysicsStep { get; } = new();
|
||||
|
||||
private readonly Event<IUniverse, IUniverse.UpdateArguments>.EventHandler delegateOnPreUpdate = null!;
|
||||
|
||||
private float physicsTicker = 0f;
|
||||
private int _iterationPerStep = 1;
|
||||
private float _iterationPeriod = 1f / 60f;
|
||||
@ -191,8 +189,6 @@ public class PhysicsEngine2D : Behaviour, IPhysicsEngine2D
|
||||
physicsPostUpdateCollector.Assign(universe);
|
||||
colliderCollector.Assign(universe);
|
||||
rigidBodyCollector.Assign(universe);
|
||||
|
||||
universe.OnPreUpdate.AddListener(OnEnginePreUpdate);
|
||||
}
|
||||
|
||||
protected override void OnExitedUniverse(IUniverse universe)
|
||||
@ -203,13 +199,11 @@ public class PhysicsEngine2D : Behaviour, IPhysicsEngine2D
|
||||
physicsPostUpdateCollector.Unassign();
|
||||
colliderCollector.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)
|
||||
{
|
||||
@ -222,15 +216,13 @@ public class PhysicsEngine2D : Behaviour, IPhysicsEngine2D
|
||||
{
|
||||
collisionDetector = new CollisionDetector2D();
|
||||
collisionResolver = new CollisionResolver2D();
|
||||
|
||||
delegateOnPreUpdate = OnEnginePreUpdate;
|
||||
Priority = int.MaxValue;
|
||||
}
|
||||
|
||||
public PhysicsEngine2D(ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
|
||||
{
|
||||
this.collisionDetector = collisionDetector;
|
||||
this.collisionResolver = collisionResolver;
|
||||
|
||||
delegateOnPreUpdate = OnEnginePreUpdate;
|
||||
Priority = int.MaxValue;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user