feat!: GetRequiredBehaviour/HierarchyObject methods added for cleaner null handling
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
using Syntriax.Engine.Core.Exceptions;
|
||||
|
||||
namespace Syntriax.Engine.Core;
|
||||
|
||||
@@ -19,6 +20,15 @@ public static class BehaviourControllerExtensions
|
||||
return behaviour is not null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IBehaviour"/> of the specified type in the provided <see cref="IBehaviourController"/>. Throws an error if not found.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to start searching from.</param>
|
||||
/// <returns>The <see cref="IBehaviour"/> of the specified type if found; otherwise, throws <see cref="BehaviourNotFoundException"/>.</returns>
|
||||
public static T GetRequiredBehaviour<T>(this IBehaviourController behaviourController) where T : class
|
||||
=> behaviourController.GetBehaviour<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.HierarchyObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName}");
|
||||
|
||||
/// <summary>
|
||||
/// Gets an existing <see cref="IBehaviour"/> of the specified type, or adds and returns a new one if it doesn't exist.
|
||||
/// </summary>
|
||||
@@ -30,7 +40,7 @@ public static class BehaviourControllerExtensions
|
||||
=> behaviourController.GetBehaviour<T>() ?? behaviourController.AddBehaviour<T>(args);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a <see cref="IBehaviour"/> of the specified type in the parent hierarchy.
|
||||
/// Tries to get a <see cref="IBehaviour"/> of the specified type in it's <see cref="IHierarchyObject"/>'s parents recursively.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to start searching from.</param>
|
||||
@@ -43,7 +53,7 @@ public static class BehaviourControllerExtensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IBehaviour"/> of the specified type in the parent hierarchy.
|
||||
/// Gets a <see cref="IBehaviour"/> of the specified type in it's <see cref="IHierarchyObject"/>'s parents recursively.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to start searching from.</param>
|
||||
@@ -64,7 +74,16 @@ public static class BehaviourControllerExtensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a <see cref="IBehaviour"/> of the specified type in the child hierarchy.
|
||||
/// Gets a <see cref="IBehaviour"/> of the specified type in it's <see cref="IHierarchyObject"/>'s parents recursively. Throws an error if not found.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to start searching from.</param>
|
||||
/// <returns>The <see cref="IBehaviour"/> of the specified type if found; otherwise, throws <see cref="BehaviourNotFoundException"/>.</returns>
|
||||
public static T GetRequiredBehaviourInParent<T>(this IBehaviourController behaviourController) where T : class
|
||||
=> behaviourController.GetBehaviourInParent<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.HierarchyObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any parent");
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a <see cref="IBehaviour"/> of the specified type in it's <see cref="IHierarchyObject"/>'s children recursively.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to start searching from.</param>
|
||||
@@ -77,7 +96,7 @@ public static class BehaviourControllerExtensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IBehaviour"/> of the specified type in the child hierarchy.
|
||||
/// Gets a <see cref="IBehaviour"/> of the specified type in it's <see cref="IHierarchyObject"/>'s children recursively.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to start searching from.</param>
|
||||
@@ -93,4 +112,13 @@ public static class BehaviourControllerExtensions
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IBehaviour"/> of the specified type in the children recursively. Throws an error if not found.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to start searching from.</param>
|
||||
/// <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
|
||||
=> behaviourController.GetBehaviourInChildren<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.HierarchyObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any children ");
|
||||
}
|
||||
|
Reference in New Issue
Block a user