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

37 lines
1.1 KiB
C#
Raw Normal View History

2024-01-30 12:36:02 +03:00
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
2024-01-30 12:36:02 +03:00
{
foreach (IGameObject gameObject in gameObjects)
if (gameObject.BehaviourController.TryGetBehaviour(out T? behaviour))
return behaviour;
return default;
}
2024-01-30 12:36:02 +03:00
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;
2024-01-30 12:36:02 +03:00
}
public static void FindBehaviours<T>(this IEnumerable<IGameObject> gameObjects, List<T> behaviours) where T : class
2024-01-30 12:36:02 +03:00
{
behaviours.Clear();
List<T> cache = [];
foreach (IGameObject gameObject in gameObjects)
{
gameObject.BehaviourController.GetBehaviours(cache);
behaviours.AddRange(cache);
}
}
}