refactor: IBehaviourController.TryGetBehaviour to Extension Method
This commit is contained in:
parent
1b3f40be5f
commit
4416f64287
|
@ -56,14 +56,6 @@ public interface IBehaviourController : IInitialize, IAssignableGameObject, IEnu
|
||||||
/// <returns>The <see cref="IBehaviour"/> of the specified type if found; otherwise, <see cref="null"/>.</returns>
|
/// <returns>The <see cref="IBehaviour"/> of the specified type if found; otherwise, <see cref="null"/>.</returns>
|
||||||
T? GetBehaviour<T>();
|
T? GetBehaviour<T>();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tries to get a <see cref="IBehaviour"/> of the specified type.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
|
||||||
/// <param name="behaviour">When this method returns, contains the <see cref="IBehaviour"/> of the specified type, if found; otherwise, see.</param>
|
|
||||||
/// <returns><see cref="true"/> if a <see cref="IBehaviour"/> of the specified type was found; otherwise, <see cref="false"/>.</returns>
|
|
||||||
bool TryGetBehaviour<T>([NotNullWhen(returnValue: true)] out T? behaviour);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets all <see cref="IBehaviour"/>s of the specified type.
|
/// Gets all <see cref="IBehaviour"/>s of the specified type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -74,12 +74,6 @@ public class BehaviourController : IBehaviourController
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryGetBehaviour<T>([NotNullWhen(returnValue: true)] out T? behaviour)
|
|
||||||
{
|
|
||||||
behaviour = GetBehaviour<T>();
|
|
||||||
return behaviour is not null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IList<T> GetBehaviours<T>()
|
public IList<T> GetBehaviours<T>()
|
||||||
{
|
{
|
||||||
List<T>? behaviours = null;
|
List<T>? behaviours = null;
|
||||||
|
|
|
@ -6,15 +6,48 @@ namespace Syntriax.Engine.Core;
|
||||||
|
|
||||||
public static class BehaviourControllerExtensions
|
public static class BehaviourControllerExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to get a <see cref="IBehaviour"/> of the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||||
|
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to search in.</param>
|
||||||
|
/// <param name="behaviour">When this method returns, contains the <see cref="IBehaviour"/> of the specified type, if found; otherwise, null.</param>
|
||||||
|
/// <returns><see cref="true"/> if a <see cref="IBehaviour"/> of the specified type was found; otherwise, <see cref="false"/>.</returns>
|
||||||
|
public static bool TryGetBehaviour<T>(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour)
|
||||||
|
{
|
||||||
|
behaviour = behaviourController.GetBehaviour<T>();
|
||||||
|
return behaviour is not null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets an existing <see cref="IBehaviour"/> of the specified type, or adds and returns a new one if it doesn't exist.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get or add.</typeparam>
|
||||||
|
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to search in.</param>
|
||||||
|
/// <param name="args">Optional arguments to pass to the constructor of the <see cref="IBehaviour"/> if a new one is added.</param>
|
||||||
|
/// <returns>The existing or newly added <see cref="IBehaviour"/> of the specified type.</returns>
|
||||||
public static T GetOrAddBehaviour<T>(this IBehaviourController behaviourController, params object?[]? args) where T : class, IBehaviour
|
public static T GetOrAddBehaviour<T>(this IBehaviourController behaviourController, params object?[]? args) where T : class, IBehaviour
|
||||||
=> behaviourController.GetBehaviour<T>() ?? behaviourController.AddBehaviour<T>(args);
|
=> behaviourController.GetBehaviour<T>() ?? behaviourController.AddBehaviour<T>(args);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to get a <see cref="IBehaviour"/> of the specified type in the parent hierarchy.
|
||||||
|
/// </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>
|
||||||
|
/// <param name="behaviour">When this method returns, contains the <see cref="IBehaviour"/> of the specified type, if found; otherwise, null.</param>
|
||||||
|
/// <returns><see cref="true"/> if a <see cref="IBehaviour"/> of the specified type was found in the parent hierarchy; otherwise, <see cref="false"/>.</returns>
|
||||||
public static bool TryGetBehaviourInParent<T>(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
public static bool TryGetBehaviourInParent<T>(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
||||||
{
|
{
|
||||||
behaviour = GetBehaviourInParent<T>(behaviourController);
|
behaviour = GetBehaviourInParent<T>(behaviourController);
|
||||||
return behaviour is not null;
|
return behaviour is not null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a <see cref="IBehaviour"/> of the specified type in the parent hierarchy.
|
||||||
|
/// </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, null.</returns>
|
||||||
public static T? GetBehaviourInParent<T>(this IBehaviourController behaviourController) where T : class
|
public static T? GetBehaviourInParent<T>(this IBehaviourController behaviourController) where T : class
|
||||||
{
|
{
|
||||||
IBehaviourController? controller = behaviourController;
|
IBehaviourController? controller = behaviourController;
|
||||||
|
@ -30,12 +63,25 @@ public static class BehaviourControllerExtensions
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to get a <see cref="IBehaviour"/> of the specified type in the child hierarchy.
|
||||||
|
/// </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>
|
||||||
|
/// <param name="behaviour">When this method returns, contains the <see cref="IBehaviour"/> of the specified type, if found; otherwise, null.</param>
|
||||||
|
/// <returns><see cref="true"/> if a <see cref="IBehaviour"/> of the specified type was found in the child hierarchy; otherwise, <see cref="false"/>.</returns>
|
||||||
public static bool TryGetBehaviourInChildren<T>(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
public static bool TryGetBehaviourInChildren<T>(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
||||||
{
|
{
|
||||||
behaviour = GetBehaviourInChildren<T>(behaviourController);
|
behaviour = GetBehaviourInChildren<T>(behaviourController);
|
||||||
return behaviour is not null;
|
return behaviour is not null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a <see cref="IBehaviour"/> of the specified type in the child hierarchy.
|
||||||
|
/// </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, null.</returns>
|
||||||
public static T? GetBehaviourInChildren<T>(this IBehaviourController behaviourController) where T : class
|
public static T? GetBehaviourInChildren<T>(this IBehaviourController behaviourController) where T : class
|
||||||
{
|
{
|
||||||
if (behaviourController.GetBehaviour<T>() is T localBehaviour)
|
if (behaviourController.GetBehaviour<T>() is T localBehaviour)
|
||||||
|
|
|
@ -10,7 +10,7 @@ public static class BehaviourExtensions
|
||||||
public static T? FindBehaviour<T>(this IEnumerable<IGameObject> gameObjects) where T : class
|
public static T? FindBehaviour<T>(this IEnumerable<IGameObject> gameObjects) where T : class
|
||||||
{
|
{
|
||||||
foreach (IGameObject gameObject in gameObjects)
|
foreach (IGameObject gameObject in gameObjects)
|
||||||
if (gameObject.BehaviourController.TryGetBehaviour(out T? behaviour))
|
if (gameObject.BehaviourController.GetBehaviour<T>() is T behaviour)
|
||||||
return behaviour;
|
return behaviour;
|
||||||
|
|
||||||
return default;
|
return default;
|
||||||
|
|
Loading…
Reference in New Issue