using System.Diagnostics.CodeAnalysis;
using Syntriax.Engine.Core.Abstract;
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;
while (controller is not null)
{
if (behaviourController.GetBehaviour() is T behaviour)
return behaviour;
controller = controller.GameObject.Transform.Parent?.GameObject.BehaviourController;
}
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)
return localBehaviour;
foreach (ITransform transform in behaviourController.GameObject.Transform)
if (GetBehaviourInChildren(transform.GameObject.BehaviourController) is T behaviour)
return behaviour;
return default;
}
}