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
|
|
|
|
{
|
2024-02-08 17:58:15 +03:00
|
|
|
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)
|
2024-11-24 11:34:36 +03:00
|
|
|
if (gameObject.BehaviourController.GetBehaviour<T>() is T behaviour)
|
2024-02-08 17:58:15 +03:00
|
|
|
return behaviour;
|
|
|
|
|
|
|
|
return default;
|
|
|
|
}
|
2024-01-30 12:36:02 +03:00
|
|
|
|
2024-02-08 17:58:15 +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
|
|
|
}
|
|
|
|
|
2024-02-08 17:58:15 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|