feat: BehaviourExtensions.FindBehaviour

This commit is contained in:
Syntriax 2024-02-08 17:58:15 +03:00
parent ed6975bf24
commit be06575f91
1 changed files with 11 additions and 7 deletions

View File

@ -7,18 +7,22 @@ namespace Syntriax.Engine.Core;
public static class BehaviourExtensions public static class BehaviourExtensions
{ {
public static bool TryFindBehaviour<T>(this IEnumerable<IGameObject> gameObjects, [NotNullWhen(returnValue: true)] out T? behaviour) public static T? FindBehaviour<T>(this IEnumerable<IGameObject> gameObjects) where T : class
{ {
behaviour = default;
foreach (IGameObject gameObject in gameObjects) foreach (IGameObject gameObject in gameObjects)
if (gameObject.BehaviourController.TryGetBehaviour(out behaviour)) if (gameObject.BehaviourController.TryGetBehaviour(out T? behaviour))
return true; return behaviour;
return false; return default;
} }
public static void FindBehaviours<T>(this IEnumerable<IGameObject> gameObjects, List<T> behaviours) 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(); behaviours.Clear();
List<T> cache = []; List<T> cache = [];