37 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using Engine.Core.Exceptions;
 | 
						|
 | 
						|
namespace Engine.Core;
 | 
						|
 | 
						|
public static class UniverseExtensions
 | 
						|
{
 | 
						|
    public static IUniverseObject InstantiateUniverseObject(this IUniverse universe, params object?[]? args)
 | 
						|
        => universe.InstantiateUniverseObject<UniverseObject>(args);
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Searches through all <see cref="IUniverseObject"/>s to find the specified instance of the type.
 | 
						|
    /// </summary>
 | 
						|
    /// <typeparam name="T">Type to be searched through the <see cref="IUniverse"/>.</typeparam>
 | 
						|
    /// <returns>The specified type if found; otherwise, throws <see cref="UniverseObjectNotFoundException"/>.</returns>
 | 
						|
    public static T GetRequiredUniverseObject<T>(this IUniverse universe) where T : class
 | 
						|
        => universe.GetUniverseObject<T>() ?? throw new UniverseObjectNotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} object of type {typeof(T).FullName}");
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Searches through all <see cref="IBehaviours"/>s to find the specified instance of the type.
 | 
						|
    /// </summary>
 | 
						|
    /// <typeparam name="T">Type to be searched through the <see cref="IUniverse"/>.</typeparam>
 | 
						|
    /// <returns>The specified type if found; otherwise, throws <see cref="BehaviourNotFoundException"/>.</returns>
 | 
						|
    public static T FindRequiredBehaviour<T>(this IUniverse universe) where T : class
 | 
						|
        => universe.FindBehaviour<T>() ?? throw new BehaviourNotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} with {nameof(IBehaviour)} of type {typeof(T).FullName}");
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Searches through all <see cref="IUniverseObject"/>s and <see cref="IBehaviours"/>s to find the specified instance of the type.
 | 
						|
    /// </summary>
 | 
						|
    /// <remarks>
 | 
						|
    /// WARNING: This is more expensive compared to <see cref="GetRequiredUniverseObject{T}(IUniverse)"/> or <see cref="FindRequiredBehaviour{T}(IUniverse)"/> as it combines the two. If you know whether the type is either a type that gets implemented on an <see cref="IBehaviour"/> or <see cref="IUniverseObject"/> use the method appropriate for it for performance.
 | 
						|
    /// </remarks>
 | 
						|
    /// <typeparam name="T">Type to be searched through the <see cref="IUniverse"/>.</typeparam>
 | 
						|
    /// <returns>The specified type if found; otherwise, throws <see cref="NotFoundException"/>.</returns>
 | 
						|
    public static T FindRequired<T>(this IUniverse universe) where T : class
 | 
						|
        => universe.Root.BehaviourController.GetBehaviourInChildren<T>() ?? throw new NotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} or {nameof(IBehaviour)} of type {typeof(T).FullName}");
 | 
						|
}
 |