feat: IBehaviourController.CollectionMethod added
This commit is contained in:
		@@ -62,7 +62,8 @@ public interface IBehaviourController : IEntity, IHasUniverseObject
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <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>
 | 
			
		||||
    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>
 | 
			
		||||
    /// 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>
 | 
			
		||||
    void RemoveBehaviour<T>(T behaviour) where T : class, IBehaviour;
 | 
			
		||||
 | 
			
		||||
    enum CollectionMethod { Clear, Append };
 | 
			
		||||
 | 
			
		||||
    readonly record struct BehaviourAddedArguments(IBehaviour BehaviourAdded);
 | 
			
		||||
    readonly record struct BehaviourRemovedArguments(IBehaviour BehaviourRemoved);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -58,9 +58,11 @@ public class BehaviourController : BaseEntity, IBehaviourController
 | 
			
		||||
        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)
 | 
			
		||||
        {
 | 
			
		||||
            if (behaviourItem is not T behaviour)
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user