48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
public static class BehaviourControllerExtensions
|
|
{
|
|
public static bool TryGetBehaviourInParent<T>(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
|
{
|
|
behaviour = GetBehaviourInParent<T>(behaviourController);
|
|
return behaviour is not null;
|
|
}
|
|
|
|
public static T? GetBehaviourInParent<T>(this IBehaviourController behaviourController) where T : class
|
|
{
|
|
IBehaviourController? controller = behaviourController;
|
|
|
|
while (controller is not null)
|
|
{
|
|
if (behaviourController.GetBehaviour<T>() is T behaviour)
|
|
return behaviour;
|
|
|
|
controller = controller.GameObject.Transform.Parent?.GameObject.BehaviourController;
|
|
}
|
|
|
|
return default;
|
|
}
|
|
|
|
public static bool TryGetBehaviourInChildren<T>(this IBehaviourController behaviourController, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
|
{
|
|
behaviour = GetBehaviourInChildren<T>(behaviourController);
|
|
return behaviour is not null;
|
|
}
|
|
|
|
public static T? GetBehaviourInChildren<T>(this IBehaviourController behaviourController) where T : class
|
|
{
|
|
if (behaviourController.GetBehaviour<T>() is T localBehaviour)
|
|
return localBehaviour;
|
|
|
|
foreach (ITransform transform in behaviourController.GameObject.Transform)
|
|
if (GetBehaviourInChildren<T>(transform.GameObject.BehaviourController) is T behaviour)
|
|
return behaviour;
|
|
|
|
return default;
|
|
}
|
|
}
|