From 90370a2b43d7465324d2530e3931a11e3a9731c7 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 29 Oct 2024 23:14:08 +0300 Subject: [PATCH] feat: Physics Coroutine Manager --- Engine.Physics2D/PhysicsCoroutineManager.cs | 62 ++++++++++++++++++++ Engine.Physics2D/PhysicsEngine2DCollector.cs | 40 +------------ 2 files changed, 65 insertions(+), 37 deletions(-) create mode 100644 Engine.Physics2D/PhysicsCoroutineManager.cs diff --git a/Engine.Physics2D/PhysicsCoroutineManager.cs b/Engine.Physics2D/PhysicsCoroutineManager.cs new file mode 100644 index 0000000..8bda9db --- /dev/null +++ b/Engine.Physics2D/PhysicsCoroutineManager.cs @@ -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 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(); + 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(); + if (physicsEngine is IPhysicsEngine2D foundPhysicsEngine) + { + foundPhysicsEngine.OnPhysicsStep += OnPhysicsStep; + gameManager.OnUpdate -= OnUpdate; + } + } +} diff --git a/Engine.Physics2D/PhysicsEngine2DCollector.cs b/Engine.Physics2D/PhysicsEngine2DCollector.cs index d89c1b8..0f6abc2 100644 --- a/Engine.Physics2D/PhysicsEngine2DCollector.cs +++ b/Engine.Physics2D/PhysicsEngine2DCollector.cs @@ -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;