diff --git a/Engine.Core/Abstract/IBehaviourController.cs b/Engine.Core/Abstract/IBehaviourController.cs index 914b80a..5bfb27a 100644 --- a/Engine.Core/Abstract/IBehaviourController.cs +++ b/Engine.Core/Abstract/IBehaviourController.cs @@ -56,14 +56,6 @@ public interface IBehaviourController : IInitialize, IAssignableGameObject, IEnu /// The of the specified type if found; otherwise, . T? GetBehaviour(); - /// - /// Tries to get a of the specified type. - /// - /// The type of to get. - /// When this method returns, contains the of the specified type, if found; otherwise, see. - /// if a of the specified type was found; otherwise, . - bool TryGetBehaviour([NotNullWhen(returnValue: true)] out T? behaviour); - /// /// Gets all s of the specified type. /// diff --git a/Engine.Core/BehaviourController.cs b/Engine.Core/BehaviourController.cs index de5dfb6..3d51fec 100644 --- a/Engine.Core/BehaviourController.cs +++ b/Engine.Core/BehaviourController.cs @@ -74,12 +74,6 @@ public class BehaviourController : IBehaviourController return default; } - public bool TryGetBehaviour([NotNullWhen(returnValue: true)] out T? behaviour) - { - behaviour = GetBehaviour(); - return behaviour is not null; - } - public IList GetBehaviours() { List? behaviours = null; diff --git a/Engine.Core/Extensions/BehaviourControllerExtensions.cs b/Engine.Core/Extensions/BehaviourControllerExtensions.cs index 5665307..fc338f3 100644 --- a/Engine.Core/Extensions/BehaviourControllerExtensions.cs +++ b/Engine.Core/Extensions/BehaviourControllerExtensions.cs @@ -6,15 +6,48 @@ namespace Syntriax.Engine.Core; public static class BehaviourControllerExtensions { + /// + /// Tries to get a of the specified type. + /// + /// The type of to get. + /// The to search in. + /// When this method returns, contains the of the specified type, if found; otherwise, null. + /// if a of the specified type was found; otherwise, . + public static bool TryGetBehaviour(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour) + { + behaviour = behaviourController.GetBehaviour(); + return behaviour is not null; + } + + /// + /// Gets an existing of the specified type, or adds and returns a new one if it doesn't exist. + /// + /// The type of to get or add. + /// The to search in. + /// Optional arguments to pass to the constructor of the if a new one is added. + /// The existing or newly added of the specified type. public static T GetOrAddBehaviour(this IBehaviourController behaviourController, params object?[]? args) where T : class, IBehaviour => behaviourController.GetBehaviour() ?? behaviourController.AddBehaviour(args); + /// + /// Tries to get a of the specified type in the parent hierarchy. + /// + /// The type of to get. + /// The to start searching from. + /// When this method returns, contains the of the specified type, if found; otherwise, null. + /// if a of the specified type was found in the parent hierarchy; otherwise, . public static bool TryGetBehaviourInParent(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class { behaviour = GetBehaviourInParent(behaviourController); return behaviour is not null; } + /// + /// Gets a of the specified type in the parent hierarchy. + /// + /// The type of to get. + /// The to start searching from. + /// The of the specified type if found; otherwise, null. public static T? GetBehaviourInParent(this IBehaviourController behaviourController) where T : class { IBehaviourController? controller = behaviourController; @@ -30,12 +63,25 @@ public static class BehaviourControllerExtensions return default; } + /// + /// Tries to get a of the specified type in the child hierarchy. + /// + /// The type of to get. + /// The to start searching from. + /// When this method returns, contains the of the specified type, if found; otherwise, null. + /// if a of the specified type was found in the child hierarchy; otherwise, . public static bool TryGetBehaviourInChildren(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class { behaviour = GetBehaviourInChildren(behaviourController); return behaviour is not null; } + /// + /// Gets a of the specified type in the child hierarchy. + /// + /// The type of to get. + /// The to start searching from. + /// The of the specified type if found; otherwise, null. public static T? GetBehaviourInChildren(this IBehaviourController behaviourController) where T : class { if (behaviourController.GetBehaviour() is T localBehaviour) diff --git a/Engine.Core/Extensions/BehaviourExtensions.cs b/Engine.Core/Extensions/BehaviourExtensions.cs index 28c71f8..d78d7a6 100644 --- a/Engine.Core/Extensions/BehaviourExtensions.cs +++ b/Engine.Core/Extensions/BehaviourExtensions.cs @@ -10,7 +10,7 @@ public static class BehaviourExtensions public static T? FindBehaviour(this IEnumerable gameObjects) where T : class { foreach (IGameObject gameObject in gameObjects) - if (gameObject.BehaviourController.TryGetBehaviour(out T? behaviour)) + if (gameObject.BehaviourController.GetBehaviour() is T behaviour) return behaviour; return default;