From 58a9ada345022d2724a8f2ede52a08a9a784b25c Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 24 Nov 2024 22:14:42 +0300 Subject: [PATCH] refactor: IBehaviourController & Sorted Collector --- Engine.Core/Abstract/IBehaviourCollector.cs | 35 +++++++++++++++++++++ Engine.Core/BehaviourCollector.cs | 16 +++++----- Engine.Core/BehaviourCollectorSorted.cs | 19 +++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 Engine.Core/Abstract/IBehaviourCollector.cs create mode 100644 Engine.Core/BehaviourCollectorSorted.cs diff --git a/Engine.Core/Abstract/IBehaviourCollector.cs b/Engine.Core/Abstract/IBehaviourCollector.cs new file mode 100644 index 0000000..967abe1 --- /dev/null +++ b/Engine.Core/Abstract/IBehaviourCollector.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; + +namespace Syntriax.Engine.Core.Abstract; + +/// +/// Represents a collector for the class type of . +/// Provides mechanisms for tracking additions and removals, and notifies subscribers when such events occur on the assigned . +/// +/// The type of objects tracked by the collector. +public interface IBehaviourCollector : IAssignableGameManager, IEnumerable where T : class +{ + /// + /// Event triggered when an object of type is added to the collector. + /// + event OnCollectedEventHandler? OnCollected; + + /// + /// Event triggered when an object of type is removed from the collector. + /// + event OnRemovedEventHandler? OnRemoved; + + /// + /// Delegate for handling the event. + /// + /// The instance of the that triggered the event. + /// The object of type that was added to the collector. + public delegate void OnCollectedEventHandler(BehaviourCollector sender, T behaviourCollected); + + /// + /// Delegate for handling the event. + /// + /// The instance of the that triggered the event. + /// The object of type that was removed from the collector. + public delegate void OnRemovedEventHandler(BehaviourCollector sender, T behaviourRemoved); +} diff --git a/Engine.Core/BehaviourCollector.cs b/Engine.Core/BehaviourCollector.cs index df6c7b6..769825c 100644 --- a/Engine.Core/BehaviourCollector.cs +++ b/Engine.Core/BehaviourCollector.cs @@ -6,15 +6,15 @@ using Syntriax.Engine.Core.Abstract; namespace Syntriax.Engine.Core; -public class BehaviourCollector : IAssignableGameManager, IEnumerable +public class BehaviourCollector : IBehaviourCollector 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.OnCollectedEventHandler? OnCollected = null; + public event IBehaviourCollector.OnRemovedEventHandler? OnRemoved = null; - private readonly List _behaviours = new(32); + protected readonly List _behaviours = new(32); public IReadOnlyList Behaviours => _behaviours; public IGameManager GameManager { get; private set; } = null!; @@ -42,15 +42,18 @@ public class BehaviourCollector : IAssignableGameManager, IEnumerable 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 : IAssignableGameManager, IEnumerable if (!_behaviours.Remove(tBehaviour)) return; + OnBehaviourRemove(behaviour); OnRemoved?.Invoke(this, tBehaviour); } @@ -97,8 +101,4 @@ public class BehaviourCollector : IAssignableGameManager, IEnumerable public IEnumerator GetEnumerator() => _behaviours.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => _behaviours.GetEnumerator(); - - - public delegate void OnCollectedEventHandler(BehaviourCollector sender, T behaviourCollected); - public delegate void OnRemovedEventHandler(BehaviourCollector sender, T behaviourRemoved); } diff --git a/Engine.Core/BehaviourCollectorSorted.cs b/Engine.Core/BehaviourCollectorSorted.cs new file mode 100644 index 0000000..3a9fc2d --- /dev/null +++ b/Engine.Core/BehaviourCollectorSorted.cs @@ -0,0 +1,19 @@ +using System; + +using Syntriax.Engine.Core.Abstract; + +namespace Syntriax.Engine.Core; + +public class BehaviourCollectorSorted : BehaviourCollector where T : class +{ + public Comparison? 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) { } +}