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;
/// <summary>
@ -19,6 +17,16 @@ public interface IBehaviourCollector<T> : IHasUniverse where T : class
/// </summary>
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>
/// Delegate for handling the <see cref="OnCollected"/> event.
/// </summary>

View File

@ -1,4 +1,3 @@
using System;
using System.Collections;
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 Dictionary<IActive, T> monitoringActiveToBehaviour = new(32);
public IReadOnlyList<T> Behaviours => activeBehaviours;
public IUniverse Universe { get; private set; } = null!;
public ActiveBehaviourCollector() { }
@ -117,4 +115,7 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
OnUnassigned?.Invoke(this);
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.Generic;
@ -14,7 +13,6 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
protected readonly List<T> behaviours = new(32);
public IReadOnlyList<T> Behaviours => behaviours;
public IUniverse Universe { get; private set; } = null!;
public BehaviourCollector() { }
@ -96,4 +94,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
OnUnassigned?.Invoke(this);
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)
{
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)

View File

@ -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<IFirstFrameUpdate> sender, IFirstFrameUpdate behaviourCollected)

View File

@ -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;