feat: extension methods for parent & children behaviour list search
This commit is contained in:
parent
8f8558a262
commit
d3fb612904
@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
using Syntriax.Engine.Core.Exceptions;
|
using Syntriax.Engine.Core.Exceptions;
|
||||||
@ -81,6 +82,27 @@ public static class BehaviourControllerExtensions
|
|||||||
public static T GetRequiredBehaviourInParent<T>(this IBehaviourController behaviourController) where T : class
|
public static T GetRequiredBehaviourInParent<T>(this IBehaviourController behaviourController) where T : class
|
||||||
=> behaviourController.GetBehaviourInParent<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.UniverseObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any parent");
|
=> behaviourController.GetBehaviourInParent<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.UniverseObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any parent");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all <see cref="IBehaviour"/>s of the specified type in it's <see cref="IUniverseObject"/>'s parents recursively and stores them in the provided list.
|
||||||
|
/// </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
|
||||||
|
{
|
||||||
|
IBehaviourController? controller = behaviourController;
|
||||||
|
List<T> cache = [];
|
||||||
|
behavioursInParent.Clear();
|
||||||
|
|
||||||
|
while (controller is not null)
|
||||||
|
{
|
||||||
|
controller.GetBehaviours(cache);
|
||||||
|
foreach (T behaviour in cache)
|
||||||
|
behavioursInParent.Add(behaviour);
|
||||||
|
|
||||||
|
controller = controller.UniverseObject.Parent?.BehaviourController;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to get a <see cref="IBehaviour"/> of the specified type in it's <see cref="IUniverseObject"/>'s children recursively.
|
/// Tries to get a <see cref="IBehaviour"/> of the specified type in it's <see cref="IUniverseObject"/>'s children recursively.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -120,4 +142,28 @@ public static class BehaviourControllerExtensions
|
|||||||
/// <returns>The <see cref="IBehaviour"/> of the specified type if found; otherwise, throws <see cref="BehaviourNotFoundException"/>.</returns>
|
/// <returns>The <see cref="IBehaviour"/> of the specified type if found; otherwise, throws <see cref="BehaviourNotFoundException"/>.</returns>
|
||||||
public static T GetRequiredBehaviourInChildren<T>(this IBehaviourController behaviourController) where T : class
|
public static T GetRequiredBehaviourInChildren<T>(this IBehaviourController behaviourController) where T : class
|
||||||
=> behaviourController.GetBehaviourInChildren<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.UniverseObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any children ");
|
=> behaviourController.GetBehaviourInChildren<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.UniverseObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any children ");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all <see cref="IBehaviour"/>s of the specified type in it's <see cref="IUniverseObject"/>'s children recursively and stores them in the provided list.
|
||||||
|
/// </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
|
||||||
|
{
|
||||||
|
List<T> cache = [];
|
||||||
|
behavioursInChildren.Clear();
|
||||||
|
|
||||||
|
TraverseChildrenForBehaviour(behaviourController.UniverseObject, behavioursInChildren, cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void TraverseChildrenForBehaviour<T>(IUniverseObject universeObject, IList<T> behaviours, IList<T> cache) where T : class
|
||||||
|
{
|
||||||
|
universeObject.BehaviourController.GetBehaviours(cache);
|
||||||
|
|
||||||
|
foreach (T behaviour in cache)
|
||||||
|
behaviours.Add(behaviour);
|
||||||
|
|
||||||
|
foreach (IUniverseObject child in universeObject)
|
||||||
|
TraverseChildrenForBehaviour(child, behaviours, cache);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user