42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
public static class UniverseObjectExtensions
|
|
{
|
|
public static T SetUniverseObject<T>(this T universeObject, string? name = "", IUniverseObject? parent = null) where T : IUniverseObject
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(name))
|
|
universeObject.Name = name;
|
|
if (parent is not null)
|
|
universeObject.SetParent(parent);
|
|
return universeObject;
|
|
}
|
|
|
|
public static T? GetUniverseObject<T>(this IEnumerable<IUniverseObject> universeObjects) where T : class
|
|
{
|
|
foreach (IUniverseObject universeObject in universeObjects)
|
|
if (universeObject is T @object)
|
|
return @object;
|
|
|
|
return default;
|
|
}
|
|
|
|
public static bool TryGetUniverseObject<T>(this IEnumerable<IUniverseObject> universeObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
|
{
|
|
behaviour = GetUniverseObject<T>(universeObjects);
|
|
return behaviour is not null;
|
|
}
|
|
|
|
public static void GetUniverseObjects<T>(this IEnumerable<IUniverseObject> universeObjects, List<T> behaviours) where T : class
|
|
{
|
|
behaviours.Clear();
|
|
foreach (IUniverseObject universeObject in universeObjects)
|
|
if (universeObject is T @object)
|
|
behaviours.Add(@object);
|
|
}
|
|
}
|