Syntriax.Engine/Engine.Core/Extensions/BehaviourExtensions.cs

37 lines
1.1 KiB
C#

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public static class BehaviourExtensions
{
public static T? FindBehaviour<T>(this IEnumerable<IGameObject> gameObjects) where T : class
{
foreach (IGameObject gameObject in gameObjects)
if (gameObject.BehaviourController.GetBehaviour<T>() is T behaviour)
return behaviour;
return default;
}
public static bool TryFindBehaviour<T>(this IEnumerable<IGameObject> gameObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
{
behaviour = FindBehaviour<T>(gameObjects);
return behaviour is not null;
}
public static void FindBehaviours<T>(this IEnumerable<IGameObject> gameObjects, List<T> behaviours) where T : class
{
behaviours.Clear();
List<T> cache = [];
foreach (IGameObject gameObject in gameObjects)
{
gameObject.BehaviourController.GetBehaviours(cache);
behaviours.AddRange(cache);
}
}
}