feat: BehaviourControllerExtensions
- TryGetBehaviourInParent - GetBehaviourInParent - TryGetBehaviourInChildren - GetBehaviourInChildren
This commit is contained in:
parent
f729cdc0a8
commit
14e3337daa
|
@ -0,0 +1,47 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue