refactor: behaviour collector Count and indexer accessors added

This commit is contained in:
Syntriax 2025-05-29 23:16:10 +03:00
parent 67d7f401b8
commit b0f8b0dad6
6 changed files with 44 additions and 34 deletions

View File

@ -1,5 +1,3 @@
using System.Collections.Generic;
namespace Syntriax.Engine.Core; namespace Syntriax.Engine.Core;
/// <summary> /// <summary>
@ -19,6 +17,16 @@ public interface IBehaviourCollector<T> : IHasUniverse where T : class
/// </summary> /// </summary>
event RemovedEventHandler? OnRemoved; event RemovedEventHandler? OnRemoved;
/// <summary>
/// Amount of <typeparamref name="T"/> collected.
/// </summary>
int Count { get; }
/// <summary>
/// Get a <typeparamref name="T"/> collected by it's index.
/// </summary>
T this[System.Index index] { get; }
/// <summary> /// <summary>
/// Delegate for handling the <see cref="OnCollected"/> event. /// Delegate for handling the <see cref="OnCollected"/> event.
/// </summary> /// </summary>

View File

@ -1,4 +1,3 @@
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -16,7 +15,6 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
protected readonly List<T> activeBehaviours = new(32); protected readonly List<T> activeBehaviours = new(32);
protected readonly Dictionary<IActive, T> monitoringActiveToBehaviour = new(32); protected readonly Dictionary<IActive, T> monitoringActiveToBehaviour = new(32);
public IReadOnlyList<T> Behaviours => activeBehaviours;
public IUniverse Universe { get; private set; } = null!; public IUniverse Universe { get; private set; } = null!;
public ActiveBehaviourCollector() { } public ActiveBehaviourCollector() { }
@ -117,4 +115,7 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
OnUnassigned?.Invoke(this); OnUnassigned?.Invoke(this);
return true; return true;
} }
public int Count => activeBehaviours.Count;
public T this[System.Index index] => activeBehaviours[index];
} }

View File

@ -1,4 +1,3 @@
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -14,7 +13,6 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
protected readonly List<T> behaviours = new(32); protected readonly List<T> behaviours = new(32);
public IReadOnlyList<T> Behaviours => behaviours;
public IUniverse Universe { get; private set; } = null!; public IUniverse Universe { get; private set; } = null!;
public BehaviourCollector() { } public BehaviourCollector() { }
@ -96,4 +94,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
OnUnassigned?.Invoke(this); OnUnassigned?.Invoke(this);
return true; return true;
} }
public int Count => behaviours.Count;
public T this[System.Index index] => behaviours[index];
} }

View File

@ -11,20 +11,20 @@ public class DrawManager : UniverseObject
private void OnPreDraw(IUniverse sender) private void OnPreDraw(IUniverse sender)
{ {
for (int i = preDrawEntities.Behaviours.Count - 1; i >= 0; i--) for (int i = preDrawEntities.Count - 1; i >= 0; i--)
preDrawEntities.Behaviours[i].PreDraw(); preDrawEntities[i].PreDraw();
} }
private void OnDraw(IUniverse sender) private void OnDraw(IUniverse sender)
{ {
for (int i = drawEntities.Behaviours.Count - 1; i >= 0; i--) for (int i = drawEntities.Count - 1; i >= 0; i--)
drawEntities.Behaviours[i].Draw(); drawEntities[i].Draw();
} }
private void OnPostDraw(IUniverse sender) private void OnPostDraw(IUniverse sender)
{ {
for (int i = postDrawEntities.Behaviours.Count - 1; i >= 0; i--) for (int i = postDrawEntities.Count - 1; i >= 0; i--)
postDrawEntities.Behaviours[i].PostDraw(); postDrawEntities[i].PostDraw();
} }
protected override void OnEnteringUniverse(IUniverse universe) protected override void OnEnteringUniverse(IUniverse universe)

View File

@ -46,20 +46,20 @@ public class UpdateManager : UniverseObject
toCallFirstFrameUpdates.RemoveAt(i); toCallFirstFrameUpdates.RemoveAt(i);
} }
for (int i = preUpdateEntities.Behaviours.Count - 1; i >= 0; i--) for (int i = preUpdateEntities.Count - 1; i >= 0; i--)
preUpdateEntities.Behaviours[i].PreUpdate(); preUpdateEntities[i].PreUpdate();
} }
private void OnUpdate(IUniverse sender, UniverseTime engineTime) private void OnUpdate(IUniverse sender, UniverseTime engineTime)
{ {
for (int i = updateEntities.Behaviours.Count - 1; i >= 0; i--) for (int i = updateEntities.Count - 1; i >= 0; i--)
updateEntities.Behaviours[i].Update(); updateEntities[i].Update();
} }
private void OnPostUpdate(IUniverse sender, UniverseTime engineTime) private void OnPostUpdate(IUniverse sender, UniverseTime engineTime)
{ {
for (int i = postUpdateEntities.Behaviours.Count - 1; i >= 0; i--) for (int i = postUpdateEntities.Count - 1; i >= 0; i--)
postUpdateEntities.Behaviours[i].PostUpdate(); postUpdateEntities[i].PostUpdate();
} }
private void OnFirstFrameCollected(IBehaviourCollector<IFirstFrameUpdate> sender, IFirstFrameUpdate behaviourCollected) private void OnFirstFrameCollected(IBehaviourCollector<IFirstFrameUpdate> sender, IFirstFrameUpdate behaviourCollected)

View File

@ -28,32 +28,32 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
{ {
float intervalDeltaTime = deltaTime / IterationPerStep; float intervalDeltaTime = deltaTime / IterationPerStep;
for (int i = physicsPreUpdateCollector.Behaviours.Count - 1; i >= 0; i--) for (int i = physicsPreUpdateCollector.Count - 1; i >= 0; i--)
physicsPreUpdateCollector.Behaviours[i].PrePhysicsUpdate(deltaTime); physicsPreUpdateCollector[i].PrePhysicsUpdate(deltaTime);
for (int i = physicsUpdateCollector.Behaviours.Count - 1; i >= 0; i--) for (int i = physicsUpdateCollector.Count - 1; i >= 0; i--)
physicsUpdateCollector.Behaviours[i].PhysicsUpdate(deltaTime); physicsUpdateCollector[i].PhysicsUpdate(deltaTime);
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++) for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
{ {
// Can Parallel // Can Parallel
for (int i = rigidBodyCollector.Behaviours.Count - 1; i >= 0; i--) for (int i = rigidBodyCollector.Count - 1; i >= 0; i--)
StepRigidBody(rigidBodyCollector.Behaviours[i], intervalDeltaTime); StepRigidBody(rigidBodyCollector[i], intervalDeltaTime);
// Can Parallel // Can Parallel
for (int i = colliderCollector.Behaviours.Count - 1; i >= 0; i--) for (int i = colliderCollector.Count - 1; i >= 0; i--)
colliderCollector.Behaviours[i].Recalculate(); colliderCollector[i].Recalculate();
// Can Parallel // 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) if (!colliderX.IsActive)
continue; 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) if (!colliderY.IsActive)
continue; continue;
@ -65,8 +65,8 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
OnPhysicsIteration?.Invoke(this, intervalDeltaTime); OnPhysicsIteration?.Invoke(this, intervalDeltaTime);
} }
for (int i = physicsPostUpdateCollector.Behaviours.Count - 1; i >= 0; i--) for (int i = physicsPostUpdateCollector.Count - 1; i >= 0; i--)
physicsPostUpdateCollector.Behaviours[i].PostPhysicsUpdate(deltaTime); physicsPostUpdateCollector[i].PostPhysicsUpdate(deltaTime);
OnPhysicsStep?.Invoke(this, deltaTime); OnPhysicsStep?.Invoke(this, deltaTime);
} }
@ -104,9 +104,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
if (!colliderX.IsActive) if (!colliderX.IsActive)
continue; 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) if (!colliderY.IsActive)
continue; continue;