From b0f8b0dad6ec3b5c9b0ebff0bcf535ebf0901e30 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Thu, 29 May 2025 23:16:10 +0300 Subject: [PATCH] refactor: behaviour collector Count and indexer accessors added --- Engine.Core/Abstract/IBehaviourCollector.cs | 12 ++++++-- Engine.Core/ActiveBehaviourCollector.cs | 5 ++-- Engine.Core/BehaviourCollector.cs | 5 ++-- Engine.Core/Systems/DrawManager.cs | 12 ++++---- Engine.Core/Systems/UpdateManager.cs | 12 ++++---- Engine.Physics2D/PhysicsEngine2D.cs | 32 ++++++++++----------- 6 files changed, 44 insertions(+), 34 deletions(-) diff --git a/Engine.Core/Abstract/IBehaviourCollector.cs b/Engine.Core/Abstract/IBehaviourCollector.cs index 9c2ecc9..fa016d4 100644 --- a/Engine.Core/Abstract/IBehaviourCollector.cs +++ b/Engine.Core/Abstract/IBehaviourCollector.cs @@ -1,5 +1,3 @@ -using System.Collections.Generic; - namespace Syntriax.Engine.Core; /// @@ -19,6 +17,16 @@ public interface IBehaviourCollector : IHasUniverse where T : class /// event RemovedEventHandler? OnRemoved; + /// + /// Amount of collected. + /// + int Count { get; } + + /// + /// Get a collected by it's index. + /// + T this[System.Index index] { get; } + /// /// Delegate for handling the event. /// diff --git a/Engine.Core/ActiveBehaviourCollector.cs b/Engine.Core/ActiveBehaviourCollector.cs index 3b16125..3ab81dd 100644 --- a/Engine.Core/ActiveBehaviourCollector.cs +++ b/Engine.Core/ActiveBehaviourCollector.cs @@ -1,4 +1,3 @@ -using System; using System.Collections; using System.Collections.Generic; @@ -16,7 +15,6 @@ public class ActiveBehaviourCollector : IBehaviourCollector where T : clas protected readonly List activeBehaviours = new(32); protected readonly Dictionary monitoringActiveToBehaviour = new(32); - public IReadOnlyList Behaviours => activeBehaviours; public IUniverse Universe { get; private set; } = null!; public ActiveBehaviourCollector() { } @@ -117,4 +115,7 @@ public class ActiveBehaviourCollector : IBehaviourCollector where T : clas OnUnassigned?.Invoke(this); return true; } + + public int Count => activeBehaviours.Count; + public T this[System.Index index] => activeBehaviours[index]; } diff --git a/Engine.Core/BehaviourCollector.cs b/Engine.Core/BehaviourCollector.cs index 84da28a..6075ec5 100644 --- a/Engine.Core/BehaviourCollector.cs +++ b/Engine.Core/BehaviourCollector.cs @@ -1,4 +1,3 @@ -using System; using System.Collections; using System.Collections.Generic; @@ -14,7 +13,6 @@ public class BehaviourCollector : IBehaviourCollector where T : class protected readonly List behaviours = new(32); - public IReadOnlyList Behaviours => behaviours; public IUniverse Universe { get; private set; } = null!; public BehaviourCollector() { } @@ -96,4 +94,7 @@ public class BehaviourCollector : IBehaviourCollector where T : class OnUnassigned?.Invoke(this); return true; } + + public int Count => behaviours.Count; + public T this[System.Index index] => behaviours[index]; } diff --git a/Engine.Core/Systems/DrawManager.cs b/Engine.Core/Systems/DrawManager.cs index d924f37..2d05c91 100644 --- a/Engine.Core/Systems/DrawManager.cs +++ b/Engine.Core/Systems/DrawManager.cs @@ -11,20 +11,20 @@ public class DrawManager : UniverseObject private void OnPreDraw(IUniverse sender) { - for (int i = preDrawEntities.Behaviours.Count - 1; i >= 0; i--) - preDrawEntities.Behaviours[i].PreDraw(); + for (int i = preDrawEntities.Count - 1; i >= 0; i--) + preDrawEntities[i].PreDraw(); } private void OnDraw(IUniverse sender) { - for (int i = drawEntities.Behaviours.Count - 1; i >= 0; i--) - drawEntities.Behaviours[i].Draw(); + for (int i = drawEntities.Count - 1; i >= 0; i--) + drawEntities[i].Draw(); } private void OnPostDraw(IUniverse sender) { - for (int i = postDrawEntities.Behaviours.Count - 1; i >= 0; i--) - postDrawEntities.Behaviours[i].PostDraw(); + for (int i = postDrawEntities.Count - 1; i >= 0; i--) + postDrawEntities[i].PostDraw(); } protected override void OnEnteringUniverse(IUniverse universe) diff --git a/Engine.Core/Systems/UpdateManager.cs b/Engine.Core/Systems/UpdateManager.cs index 17bd49a..1380866 100644 --- a/Engine.Core/Systems/UpdateManager.cs +++ b/Engine.Core/Systems/UpdateManager.cs @@ -46,20 +46,20 @@ public class UpdateManager : UniverseObject toCallFirstFrameUpdates.RemoveAt(i); } - for (int i = preUpdateEntities.Behaviours.Count - 1; i >= 0; i--) - preUpdateEntities.Behaviours[i].PreUpdate(); + for (int i = preUpdateEntities.Count - 1; i >= 0; i--) + preUpdateEntities[i].PreUpdate(); } private void OnUpdate(IUniverse sender, UniverseTime engineTime) { - for (int i = updateEntities.Behaviours.Count - 1; i >= 0; i--) - updateEntities.Behaviours[i].Update(); + for (int i = updateEntities.Count - 1; i >= 0; i--) + updateEntities[i].Update(); } private void OnPostUpdate(IUniverse sender, UniverseTime engineTime) { - for (int i = postUpdateEntities.Behaviours.Count - 1; i >= 0; i--) - postUpdateEntities.Behaviours[i].PostUpdate(); + for (int i = postUpdateEntities.Count - 1; i >= 0; i--) + postUpdateEntities[i].PostUpdate(); } private void OnFirstFrameCollected(IBehaviourCollector sender, IFirstFrameUpdate behaviourCollected) diff --git a/Engine.Physics2D/PhysicsEngine2D.cs b/Engine.Physics2D/PhysicsEngine2D.cs index 511ecd0..ae9a5e9 100644 --- a/Engine.Physics2D/PhysicsEngine2D.cs +++ b/Engine.Physics2D/PhysicsEngine2D.cs @@ -28,32 +28,32 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D { float intervalDeltaTime = deltaTime / IterationPerStep; - for (int i = physicsPreUpdateCollector.Behaviours.Count - 1; i >= 0; i--) - physicsPreUpdateCollector.Behaviours[i].PrePhysicsUpdate(deltaTime); + for (int i = physicsPreUpdateCollector.Count - 1; i >= 0; i--) + physicsPreUpdateCollector[i].PrePhysicsUpdate(deltaTime); - for (int i = physicsUpdateCollector.Behaviours.Count - 1; i >= 0; i--) - physicsUpdateCollector.Behaviours[i].PhysicsUpdate(deltaTime); + for (int i = physicsUpdateCollector.Count - 1; i >= 0; i--) + physicsUpdateCollector[i].PhysicsUpdate(deltaTime); for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++) { // Can Parallel - for (int i = rigidBodyCollector.Behaviours.Count - 1; i >= 0; i--) - StepRigidBody(rigidBodyCollector.Behaviours[i], intervalDeltaTime); + for (int i = rigidBodyCollector.Count - 1; i >= 0; i--) + StepRigidBody(rigidBodyCollector[i], intervalDeltaTime); // Can Parallel - for (int i = colliderCollector.Behaviours.Count - 1; i >= 0; i--) - colliderCollector.Behaviours[i].Recalculate(); + for (int i = colliderCollector.Count - 1; i >= 0; i--) + colliderCollector[i].Recalculate(); // Can Parallel - for (int x = 0; x < colliderCollector.Behaviours.Count; x++) + for (int x = 0; x < colliderCollector.Count; x++) { - ICollider2D? colliderX = colliderCollector.Behaviours[x]; + ICollider2D? colliderX = colliderCollector[x]; if (!colliderX.IsActive) continue; - for (int y = x + 1; y < colliderCollector.Behaviours.Count; y++) + for (int y = x + 1; y < colliderCollector.Count; y++) { - ICollider2D? colliderY = colliderCollector.Behaviours[y]; + ICollider2D? colliderY = colliderCollector[y]; if (!colliderY.IsActive) continue; @@ -65,8 +65,8 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D OnPhysicsIteration?.Invoke(this, intervalDeltaTime); } - for (int i = physicsPostUpdateCollector.Behaviours.Count - 1; i >= 0; i--) - physicsPostUpdateCollector.Behaviours[i].PostPhysicsUpdate(deltaTime); + for (int i = physicsPostUpdateCollector.Count - 1; i >= 0; i--) + physicsPostUpdateCollector[i].PostPhysicsUpdate(deltaTime); OnPhysicsStep?.Invoke(this, deltaTime); } @@ -104,9 +104,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D if (!colliderX.IsActive) continue; - for (int y = 0; y < colliderCollector.Behaviours.Count; y++) + for (int y = 0; y < colliderCollector.Count; y++) { - ICollider2D? colliderY = colliderCollector.Behaviours[y]; + ICollider2D? colliderY = colliderCollector[y]; if (!colliderY.IsActive) continue;