feat: Physics Coroutine Manager

This commit is contained in:
Syntriax 2024-10-29 23:14:08 +03:00
parent 0f3f1594d0
commit 90370a2b43
2 changed files with 65 additions and 37 deletions

View File

@ -0,0 +1,62 @@
using System.Collections;
using System.Collections.Generic;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Abstract;
namespace Engine.Physics2D;
public class PhysicsCoroutineManager : HierarchyObjectBase
{
private readonly List<IEnumerator> enumerators = [];
private IPhysicsEngine2D? physicsEngine = null;
public IEnumerator CreateCoroutine(IEnumerator enumerator)
{
enumerators.Add(enumerator);
return enumerator;
}
public void StopCoroutine(IEnumerator enumerator)
{
enumerators.Remove(enumerator);
}
protected override void OnEnteringHierarchy(IGameManager gameManager)
{
physicsEngine = gameManager.HierarchyObjects.FindObject<IPhysicsEngine2D>();
if (physicsEngine is IPhysicsEngine2D foundPhysicsEngine)
foundPhysicsEngine.OnPhysicsStep += OnPhysicsStep;
else
gameManager.OnUpdate += OnUpdate;
}
private void OnPhysicsStep(IPhysicsEngine2D sender, float stepDeltaTime)
{
for (int i = enumerators.Count - 1; i >= 0; i--)
if (!enumerators[i].MoveNext())
enumerators.RemoveAt(i);
}
protected override void OnExitingHierarchy(IGameManager gameManager)
{
if (physicsEngine is IPhysicsEngine2D existingPhysicsEngine)
existingPhysicsEngine.OnPhysicsStep -= OnPhysicsStep;
gameManager.OnUpdate -= OnUpdate;
}
private void OnUpdate(IGameManager sender, EngineTime time)
{
if (GameManager is not IGameManager gameManager)
return;
physicsEngine = gameManager.HierarchyObjects.FindObject<IPhysicsEngine2D>();
if (physicsEngine is IPhysicsEngine2D foundPhysicsEngine)
{
foundPhysicsEngine.OnPhysicsStep += OnPhysicsStep;
gameManager.OnUpdate -= OnUpdate;
}
}
}

View File

@ -4,11 +4,8 @@ using Syntriax.Engine.Physics2D.Abstract;
namespace Syntriax.Engine.Physics2D;
public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager
public class PhysicsEngine2DCollector : HierarchyObjectBase, IPhysicsEngine2D
{
public event IAssignable.OnUnassignedDelegate? OnUnassigned = null;
public event IAssignableGameManager.OnGameManagerAssignedDelegate? OnGameManagerAssigned = null;
public event IPhysicsEngine2D.OnPhysicsIterationDelegate? OnPhysicsIteration = null;
public event IPhysicsEngine2D.OnPhysicsStepDelegate? OnPhysicsStep = null;
@ -23,7 +20,6 @@ public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
public IGameManager GameManager { get; private set; } = null!;
public void Step(float deltaTime)
{
@ -102,34 +98,18 @@ public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager
rigidBody.Transform.Position += rigidBody.Velocity * intervalDeltaTime;
rigidBody.Transform.Rotation += rigidBody.AngularVelocity * intervalDeltaTime;
}
public bool Assign(IGameManager gameManager)
protected override void OnEnteringHierarchy(IGameManager gameManager)
{
if (GameManager is not null)
return false;
physicsUpdateCollector.Assign(gameManager);
colliderCollector.Assign(gameManager);
rigidBodyCollector.Assign(gameManager);
GameManager = gameManager;
OnGameManagerAssigned?.Invoke(this);
return true;
}
public bool Unassign()
protected override void OnExitingHierarchy(IGameManager gameManager)
{
if (GameManager is null)
return false;
physicsUpdateCollector.Unassign();
colliderCollector.Unassign();
rigidBodyCollector.Unassign();
GameManager = null!;
OnUnassigned?.Invoke(this);
return true;
}
public PhysicsEngine2DCollector()
@ -138,20 +118,6 @@ public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager
collisionResolver = new CollisionResolver2D();
}
public PhysicsEngine2DCollector(IGameManager gameManager)
{
Assign(gameManager);
collisionDetector = new CollisionDetector2D();
collisionResolver = new CollisionResolver2D();
}
public PhysicsEngine2DCollector(IGameManager gameManager, ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
{
Assign(gameManager);
this.collisionDetector = collisionDetector;
this.collisionResolver = collisionResolver;
}
public PhysicsEngine2DCollector(ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
{
this.collisionDetector = collisionDetector;