feat: IBehaviourController.CollectionMethod added

This commit is contained in:
2025-11-01 23:27:40 +03:00
parent 0bd2b0618d
commit 5f019892f1
3 changed files with 22 additions and 21 deletions

View File

@@ -62,7 +62,8 @@ public interface IBehaviourController : IEntity, IHasUniverseObject
/// </summary> /// </summary>
/// <typeparam name="T">The type of <see cref="IBehaviour"/>s to get.</typeparam> /// <typeparam name="T">The type of <see cref="IBehaviour"/>s to get.</typeparam>
/// <param name="results">The list to store the <see cref="IBehaviour"/>s.</param> /// <param name="results">The list to store the <see cref="IBehaviour"/>s.</param>
void GetBehaviours<T>(IList<T> results); /// <param name="collectionMethod">Whether to clear the <paramref name="results"/> before collection or append the results to the list.</param>
void GetBehaviours<T>(IList<T> results, CollectionMethod collectionMethod = CollectionMethod.Clear);
/// <summary> /// <summary>
/// Removes <see cref="IBehaviour"/>s of the specified type from the <see cref="IBehaviourController"/>. /// Removes <see cref="IBehaviour"/>s of the specified type from the <see cref="IBehaviourController"/>.
@@ -78,6 +79,8 @@ public interface IBehaviourController : IEntity, IHasUniverseObject
/// <param name="behaviour">The <see cref="IBehaviour"/> to remove.</param> /// <param name="behaviour">The <see cref="IBehaviour"/> to remove.</param>
void RemoveBehaviour<T>(T behaviour) where T : class, IBehaviour; void RemoveBehaviour<T>(T behaviour) where T : class, IBehaviour;
enum CollectionMethod { Clear, Append };
readonly record struct BehaviourAddedArguments(IBehaviour BehaviourAdded); readonly record struct BehaviourAddedArguments(IBehaviour BehaviourAdded);
readonly record struct BehaviourRemovedArguments(IBehaviour BehaviourRemoved); readonly record struct BehaviourRemovedArguments(IBehaviour BehaviourRemoved);
} }

View File

@@ -58,9 +58,11 @@ public class BehaviourController : BaseEntity, IBehaviourController
return behaviours; return behaviours;
} }
public void GetBehaviours<T>(IList<T> results) public void GetBehaviours<T>(IList<T> results, IBehaviourController.CollectionMethod collectionMethod = IBehaviourController.CollectionMethod.Clear)
{ {
results.Clear(); if (collectionMethod == IBehaviourController.CollectionMethod.Clear)
results.Clear();
foreach (IBehaviour behaviourItem in behaviours) foreach (IBehaviour behaviourItem in behaviours)
{ {
if (behaviourItem is not T behaviour) if (behaviourItem is not T behaviour)

View File

@@ -100,18 +100,16 @@ public static class BehaviourControllerExtensions
/// </summary> /// </summary>
/// <typeparam name="T">The type of <see cref="IBehaviour"/>s to get.</typeparam> /// <typeparam name="T">The type of <see cref="IBehaviour"/>s to get.</typeparam>
/// <param name="behavioursInParent">The list to store the <see cref="IBehaviour"/>s.</param> /// <param name="behavioursInParent">The list to store the <see cref="IBehaviour"/>s.</param>
public static void GetBehavioursInParent<T>(this IBehaviourController behaviourController, IList<T> behavioursInParent) where T : class /// <param name="collectionMethod">Whether to clear the <paramref name="behavioursInParent"/> before collection or append the results to the list.</param>
public static void GetBehavioursInParent<T>(this IBehaviourController behaviourController, IList<T> behavioursInParent, IBehaviourController.CollectionMethod collectionMethod = IBehaviourController.CollectionMethod.Clear) where T : class
{ {
IBehaviourController? controller = behaviourController; if (collectionMethod == IBehaviourController.CollectionMethod.Clear)
List<T> cache = []; behavioursInParent.Clear();
behavioursInParent.Clear();
IBehaviourController? controller = behaviourController;
while (controller is not null) while (controller is not null)
{ {
controller.GetBehaviours(cache); controller.GetBehaviours(behavioursInParent, IBehaviourController.CollectionMethod.Append);
foreach (T behaviour in cache)
behavioursInParent.Add(behaviour);
controller = controller.UniverseObject.Parent?.BehaviourController; controller = controller.UniverseObject.Parent?.BehaviourController;
} }
} }
@@ -161,22 +159,20 @@ public static class BehaviourControllerExtensions
/// </summary> /// </summary>
/// <typeparam name="T">The type of <see cref="IBehaviour"/>s to get.</typeparam> /// <typeparam name="T">The type of <see cref="IBehaviour"/>s to get.</typeparam>
/// <param name="behavioursInChildren">The list to store the <see cref="IBehaviour"/>s.</param> /// <param name="behavioursInChildren">The list to store the <see cref="IBehaviour"/>s.</param>
public static void GetBehavioursInChildren<T>(this IBehaviourController behaviourController, IList<T> behavioursInChildren) where T : class /// <param name="collectionMethod">Whether to clear the <paramref name="behavioursInChildren"/> before collection or append the results to the list.</param>
public static void GetBehavioursInChildren<T>(this IBehaviourController behaviourController, IList<T> behavioursInChildren, IBehaviourController.CollectionMethod collectionMethod = IBehaviourController.CollectionMethod.Clear) where T : class
{ {
List<T> cache = []; if (collectionMethod == IBehaviourController.CollectionMethod.Clear)
behavioursInChildren.Clear(); behavioursInChildren.Clear();
TraverseChildrenForBehaviour(behaviourController.UniverseObject, behavioursInChildren, cache); TraverseChildrenForBehaviour(behaviourController.UniverseObject, behavioursInChildren);
} }
private static void TraverseChildrenForBehaviour<T>(IUniverseObject universeObject, IList<T> behaviours, IList<T> cache) where T : class private static void TraverseChildrenForBehaviour<T>(IUniverseObject universeObject, IList<T> behaviours) where T : class
{ {
universeObject.BehaviourController.GetBehaviours(cache); universeObject.BehaviourController.GetBehaviours(behaviours, IBehaviourController.CollectionMethod.Append);
foreach (T behaviour in cache)
behaviours.Add(behaviour);
foreach (IUniverseObject child in universeObject.Children) foreach (IUniverseObject child in universeObject.Children)
TraverseChildrenForBehaviour(child, behaviours, cache); TraverseChildrenForBehaviour(child, behaviours);
} }
} }