refactor: IBehaviourController & Sorted Collector

This commit is contained in:
2024-11-24 22:14:42 +03:00
parent eb5345dc77
commit 58a9ada345
3 changed files with 62 additions and 8 deletions

View File

@@ -6,15 +6,15 @@ using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public class BehaviourCollector<T> : IAssignableGameManager, IEnumerable<T>
public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
{
public event IAssignable.OnUnassignedEventHandler? OnUnassigned = null;
public event IAssignableGameManager.OnGameManagerAssignedEventHandler? OnGameManagerAssigned = null;
public event OnCollectedEventHandler? OnCollected = null;
public event OnRemovedEventHandler? OnRemoved = null;
public event IBehaviourCollector<T>.OnCollectedEventHandler? OnCollected = null;
public event IBehaviourCollector<T>.OnRemovedEventHandler? OnRemoved = null;
private readonly List<T> _behaviours = new(32);
protected readonly List<T> _behaviours = new(32);
public IReadOnlyList<T> Behaviours => _behaviours;
public IGameManager GameManager { get; private set; } = null!;
@@ -42,15 +42,18 @@ public class BehaviourCollector<T> : IAssignableGameManager, IEnumerable<T>
OnBehaviourRemoved(gameObject.BehaviourController, item);
}
protected virtual void OnBehaviourAdd(IBehaviour behaviour) { }
private void OnBehaviourAdded(IBehaviourController controller, IBehaviour behaviour)
{
if (behaviour is not T tBehaviour)
return;
_behaviours.Add(tBehaviour);
OnBehaviourAdd(behaviour);
OnCollected?.Invoke(this, tBehaviour);
}
protected virtual void OnBehaviourRemove(IBehaviour behaviour) { }
private void OnBehaviourRemoved(IBehaviourController controller, IBehaviour behaviour)
{
if (behaviour is not T tBehaviour)
@@ -59,6 +62,7 @@ public class BehaviourCollector<T> : IAssignableGameManager, IEnumerable<T>
if (!_behaviours.Remove(tBehaviour))
return;
OnBehaviourRemove(behaviour);
OnRemoved?.Invoke(this, tBehaviour);
}
@@ -97,8 +101,4 @@ public class BehaviourCollector<T> : IAssignableGameManager, IEnumerable<T>
public IEnumerator<T> GetEnumerator() => _behaviours.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _behaviours.GetEnumerator();
public delegate void OnCollectedEventHandler(BehaviourCollector<T> sender, T behaviourCollected);
public delegate void OnRemovedEventHandler(BehaviourCollector<T> sender, T behaviourRemoved);
}