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

@@ -100,18 +100,16 @@ public static class BehaviourControllerExtensions
/// </summary>
/// <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>
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;
List<T> cache = [];
behavioursInParent.Clear();
if (collectionMethod == IBehaviourController.CollectionMethod.Clear)
behavioursInParent.Clear();
IBehaviourController? controller = behaviourController;
while (controller is not null)
{
controller.GetBehaviours(cache);
foreach (T behaviour in cache)
behavioursInParent.Add(behaviour);
controller.GetBehaviours(behavioursInParent, IBehaviourController.CollectionMethod.Append);
controller = controller.UniverseObject.Parent?.BehaviourController;
}
}
@@ -161,22 +159,20 @@ public static class BehaviourControllerExtensions
/// </summary>
/// <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>
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 = [];
behavioursInChildren.Clear();
if (collectionMethod == IBehaviourController.CollectionMethod.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);
foreach (T behaviour in cache)
behaviours.Add(behaviour);
universeObject.BehaviourController.GetBehaviours(behaviours, IBehaviourController.CollectionMethod.Append);
foreach (IUniverseObject child in universeObject.Children)
TraverseChildrenForBehaviour(child, behaviours, cache);
TraverseChildrenForBehaviour(child, behaviours);
}
}