refactor: Renamed BehaviourCacher to BehaviourCollector
This commit is contained in:
parent
be06575f91
commit
2cf6135063
|
@ -6,13 +6,13 @@ using Syntriax.Engine.Core.Abstract;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core;
|
namespace Syntriax.Engine.Core;
|
||||||
|
|
||||||
public class BehaviourCacher<T> : IAssignableGameManager, IEnumerable<T>
|
public class BehaviourCollector<T> : IAssignableGameManager, IEnumerable<T>
|
||||||
{
|
{
|
||||||
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
||||||
public Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; } = null;
|
public Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; } = null;
|
||||||
|
|
||||||
public Action<BehaviourCacher<T>, T>? OnCached { get; set; } = null;
|
public Action<BehaviourCollector<T>, T>? OnCollected { get; set; } = null;
|
||||||
public Action<BehaviourCacher<T>, T>? OnUncached { get; set; } = null;
|
public Action<BehaviourCollector<T>, T>? OnRemoved { get; set; } = null;
|
||||||
|
|
||||||
private readonly List<T> _behaviours = new(32);
|
private readonly List<T> _behaviours = new(32);
|
||||||
|
|
||||||
|
@ -21,8 +21,8 @@ public class BehaviourCacher<T> : IAssignableGameManager, IEnumerable<T>
|
||||||
|
|
||||||
public T this[Index index] => _behaviours[index];
|
public T this[Index index] => _behaviours[index];
|
||||||
|
|
||||||
public BehaviourCacher() { }
|
public BehaviourCollector() { }
|
||||||
public BehaviourCacher(IGameManager gameManager) => Assign(gameManager);
|
public BehaviourCollector(IGameManager gameManager) => Assign(gameManager);
|
||||||
|
|
||||||
private void OnGameObjectRegistered(IGameManager manager, IGameObject gameObject)
|
private void OnGameObjectRegistered(IGameManager manager, IGameObject gameObject)
|
||||||
{
|
{
|
||||||
|
@ -42,7 +42,7 @@ public class BehaviourCacher<T> : IAssignableGameManager, IEnumerable<T>
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_behaviours.Add(tBehaviour);
|
_behaviours.Add(tBehaviour);
|
||||||
OnCached?.Invoke(this, tBehaviour);
|
OnCollected?.Invoke(this, tBehaviour);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnBehaviourRemoved(IBehaviourController controller, IBehaviour behaviour)
|
private void OnBehaviourRemoved(IBehaviourController controller, IBehaviour behaviour)
|
||||||
|
@ -53,7 +53,7 @@ public class BehaviourCacher<T> : IAssignableGameManager, IEnumerable<T>
|
||||||
if (!_behaviours.Remove(tBehaviour))
|
if (!_behaviours.Remove(tBehaviour))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
OnUncached?.Invoke(this, tBehaviour);
|
OnRemoved?.Invoke(this, tBehaviour);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Assign(IGameManager gameManager)
|
public bool Assign(IGameManager gameManager)
|
|
@ -6,7 +6,7 @@ using Syntriax.Engine.Physics2D.Abstract;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Physics2D;
|
namespace Syntriax.Engine.Physics2D;
|
||||||
|
|
||||||
public class PhysicsEngine2DCacher : IPhysicsEngine2D, IAssignableGameManager
|
public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager
|
||||||
{
|
{
|
||||||
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
||||||
public Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; } = null;
|
public Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; } = null;
|
||||||
|
@ -17,8 +17,8 @@ public class PhysicsEngine2DCacher : IPhysicsEngine2D, IAssignableGameManager
|
||||||
protected readonly ICollisionDetector2D collisionDetector = null!;
|
protected readonly ICollisionDetector2D collisionDetector = null!;
|
||||||
protected readonly ICollisionResolver2D collisionResolver = null!;
|
protected readonly ICollisionResolver2D collisionResolver = null!;
|
||||||
|
|
||||||
protected BehaviourCacher<IRigidBody2D> rigidBodyCacher = new();
|
protected BehaviourCollector<IRigidBody2D> rigidBodyCollector = new();
|
||||||
protected BehaviourCacher<ICollider2D> colliderCacher = new();
|
protected BehaviourCollector<ICollider2D> colliderCollector = new();
|
||||||
|
|
||||||
|
|
||||||
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
|
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
|
||||||
|
@ -31,23 +31,23 @@ public class PhysicsEngine2DCacher : IPhysicsEngine2D, IAssignableGameManager
|
||||||
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
|
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
|
||||||
{
|
{
|
||||||
// Can Parallel
|
// Can Parallel
|
||||||
foreach (var rigidBody in rigidBodyCacher)
|
foreach (var rigidBody in rigidBodyCollector)
|
||||||
StepRigidBody(rigidBody, intervalDeltaTime);
|
StepRigidBody(rigidBody, intervalDeltaTime);
|
||||||
|
|
||||||
// Can Parallel
|
// Can Parallel
|
||||||
foreach (var collider in colliderCacher)
|
foreach (var collider in colliderCollector)
|
||||||
collider.Recalculate();
|
collider.Recalculate();
|
||||||
|
|
||||||
// Can Parallel
|
// Can Parallel
|
||||||
for (int x = 0; x < colliderCacher.Behaviours.Count; x++)
|
for (int x = 0; x < colliderCollector.Behaviours.Count; x++)
|
||||||
{
|
{
|
||||||
ICollider2D? colliderX = colliderCacher.Behaviours[x];
|
ICollider2D? colliderX = colliderCollector.Behaviours[x];
|
||||||
if (!colliderX.IsActive)
|
if (!colliderX.IsActive)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (int y = x + 1; y < colliderCacher.Behaviours.Count; y++)
|
for (int y = x + 1; y < colliderCollector.Behaviours.Count; y++)
|
||||||
{
|
{
|
||||||
ICollider2D? colliderY = colliderCacher.Behaviours[y];
|
ICollider2D? colliderY = colliderCollector.Behaviours[y];
|
||||||
|
|
||||||
if (!colliderY.IsActive)
|
if (!colliderY.IsActive)
|
||||||
return;
|
return;
|
||||||
|
@ -100,8 +100,8 @@ public class PhysicsEngine2DCacher : IPhysicsEngine2D, IAssignableGameManager
|
||||||
if (GameManager is not null)
|
if (GameManager is not null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
colliderCacher.Assign(gameManager);
|
colliderCollector.Assign(gameManager);
|
||||||
rigidBodyCacher.Assign(gameManager);
|
rigidBodyCollector.Assign(gameManager);
|
||||||
|
|
||||||
GameManager = gameManager;
|
GameManager = gameManager;
|
||||||
OnGameManagerAssigned?.Invoke(this);
|
OnGameManagerAssigned?.Invoke(this);
|
||||||
|
@ -114,35 +114,35 @@ public class PhysicsEngine2DCacher : IPhysicsEngine2D, IAssignableGameManager
|
||||||
if (GameManager is null)
|
if (GameManager is null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
colliderCacher.Unassign();
|
colliderCollector.Unassign();
|
||||||
rigidBodyCacher.Unassign();
|
rigidBodyCollector.Unassign();
|
||||||
|
|
||||||
GameManager = null!;
|
GameManager = null!;
|
||||||
OnUnassigned?.Invoke(this);
|
OnUnassigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PhysicsEngine2DCacher()
|
public PhysicsEngine2DCollector()
|
||||||
{
|
{
|
||||||
collisionDetector = new CollisionDetector2D();
|
collisionDetector = new CollisionDetector2D();
|
||||||
collisionResolver = new CollisionResolver2D();
|
collisionResolver = new CollisionResolver2D();
|
||||||
}
|
}
|
||||||
|
|
||||||
public PhysicsEngine2DCacher(IGameManager gameManager)
|
public PhysicsEngine2DCollector(IGameManager gameManager)
|
||||||
{
|
{
|
||||||
Assign(gameManager);
|
Assign(gameManager);
|
||||||
collisionDetector = new CollisionDetector2D();
|
collisionDetector = new CollisionDetector2D();
|
||||||
collisionResolver = new CollisionResolver2D();
|
collisionResolver = new CollisionResolver2D();
|
||||||
}
|
}
|
||||||
|
|
||||||
public PhysicsEngine2DCacher(IGameManager gameManager, ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
|
public PhysicsEngine2DCollector(IGameManager gameManager, ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
|
||||||
{
|
{
|
||||||
Assign(gameManager);
|
Assign(gameManager);
|
||||||
this.collisionDetector = collisionDetector;
|
this.collisionDetector = collisionDetector;
|
||||||
this.collisionResolver = collisionResolver;
|
this.collisionResolver = collisionResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PhysicsEngine2DCacher(ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
|
public PhysicsEngine2DCollector(ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
|
||||||
{
|
{
|
||||||
this.collisionDetector = collisionDetector;
|
this.collisionDetector = collisionDetector;
|
||||||
this.collisionResolver = collisionResolver;
|
this.collisionResolver = collisionResolver;
|
Loading…
Reference in New Issue