refactor: IBehaviourController & Sorted Collector
This commit is contained in:
parent
eb5345dc77
commit
58a9ada345
|
@ -0,0 +1,35 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Syntriax.Engine.Core.Abstract;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a collector for the class type of <typeparamref name="T"/>.
|
||||
/// Provides mechanisms for tracking additions and removals, and notifies subscribers when such events occur on the assigned <see cref="IGameManager"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of objects tracked by the collector.</typeparam>
|
||||
public interface IBehaviourCollector<T> : IAssignableGameManager, IEnumerable<T> where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Event triggered when an object of type <typeparamref name="T"/> is added to the collector.
|
||||
/// </summary>
|
||||
event OnCollectedEventHandler? OnCollected;
|
||||
|
||||
/// <summary>
|
||||
/// Event triggered when an object of type <typeparamref name="T"/> is removed from the collector.
|
||||
/// </summary>
|
||||
event OnRemovedEventHandler? OnRemoved;
|
||||
|
||||
/// <summary>
|
||||
/// Delegate for handling the <see cref="OnCollected"/> event.
|
||||
/// </summary>
|
||||
/// <param name="sender">The instance of the <see cref="BehaviourCollector{T}"/> that triggered the event.</param>
|
||||
/// <param name="behaviourCollected">The object of type <typeparamref name="T"/> that was added to the collector.</param>
|
||||
public delegate void OnCollectedEventHandler(BehaviourCollector<T> sender, T behaviourCollected);
|
||||
|
||||
/// <summary>
|
||||
/// Delegate for handling the <see cref="OnRemoved"/> event.
|
||||
/// </summary>
|
||||
/// <param name="sender">The instance of the <see cref="BehaviourCollector{T}"/> that triggered the event.</param>
|
||||
/// <param name="behaviourRemoved">The object of type <typeparamref name="T"/> that was removed from the collector.</param>
|
||||
public delegate void OnRemovedEventHandler(BehaviourCollector<T> sender, T behaviourRemoved);
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Core;
|
||||
|
||||
public class BehaviourCollectorSorted<T> : BehaviourCollector<T> where T : class
|
||||
{
|
||||
public Comparison<T>? SortBy { get; set; } = null;
|
||||
|
||||
protected override void OnBehaviourAdd(IBehaviour behaviour)
|
||||
{
|
||||
if (SortBy is not null)
|
||||
_behaviours.Sort(SortBy);
|
||||
}
|
||||
|
||||
public BehaviourCollectorSorted() { }
|
||||
public BehaviourCollectorSorted(IGameManager gameManager) : base(gameManager) { }
|
||||
}
|
Loading…
Reference in New Issue