using Ghost.Entities.Query;
using System.Runtime.CompilerServices;
namespace Ghost.Entities.Helpers;
///
/// Provides extension methods for working with entities in the Ghost framework.
///
public static class EntityHelpers
{
public static World GetWorld(this Entity entity)
{
return World.GetWorld(entity.WorldID);
}
///
/// Adds a component of type to the given .
///
/// The type of the component to set.
/// The entity for which the component is to be add.
/// The component value to add.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddComponent(this Entity entity, T component)
where T : struct, IComponentData
{
var world = entity.GetWorld();
world.EntityManager.AddComponent(entity, component);
}
///
/// Removes a component of type from the given .
///
/// The type of the component to remove.
/// The entity for which the component is to be remove.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RemoveComponent(this Entity entity)
where T : struct, IComponentData
{
var world = entity.GetWorld();
world.EntityManager.RemoveComponent(entity);
}
///
/// Sets a component of type for the given .
///
/// The type of the component to set.
/// The entity for which the component is to be set.
/// The component value to set.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetComponent(this Entity entity, T component)
where T : struct, IComponentData
{
var world = entity.GetWorld();
world.EntityManager.SetComponent(entity, component);
}
///
/// Checks if the given has a component of the specified type.
///
/// The entity to check.
/// The handle of the component type.
/// True if the entity has the component; otherwise, false.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasComponent(this Entity entity, nint typeHandle)
{
var world = entity.GetWorld();
return world.EntityManager.HasComponent(entity, typeHandle);
}
///
/// Retrieves a reference to a component of type associated with the given .
///
/// The type of the component to retrieve.
/// The entity whose component is to be retrieved.
/// A to the component, or a null reference if the component does not exist.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Ref GetComponent(this Entity entity)
where T : struct, IComponentData
{
var world = entity.GetWorld();
return world.EntityManager.GetComponent(entity);
}
///
/// Adds a script of type to the given .
///
/// The type of the script to add.
/// The entity to which the script is to be added.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddScript(this Entity entity)
where T : ScriptComponent, new()
{
var world = entity.GetWorld();
world.EntityManager.AddScript(entity);
}
///
/// Adds a script of the specified type to the given .
///
/// The entity to which the script is to be added.
/// The type of the script to add.
/// Thrown if the specified type does not inherit from .
/// Thrown if the script instance could not be created.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddScript(this Entity entity, Type type)
{
var world = entity.GetWorld();
world.EntityManager.AddScript(entity, type);
}
///
/// Retrieves the first script of type associated with the given .
///
/// The type of the script to retrieve.
/// The entity whose script is to be retrieved.
/// The script of type , or null if no such script exists.
public static T? GetScript(this Entity entity)
where T : ScriptComponent
{
var world = entity.GetWorld();
return world.EntityManager.GetScript(entity);
}
///
/// Retrieves all scripts of type associated with the given .
///
/// The type of the scripts to retrieve.
/// The entity whose scripts are to be retrieved.
/// An enumerable of scripts of type .
public static IEnumerable GetScripts(this Entity entity)
where T : ScriptComponent
{
var world = entity.GetWorld();
return world.EntityManager.GetScripts(entity);
}
///
/// Destroys the given by removing it from its associated .
///
/// The entity to destroy.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Destroy(this Entity entity)
{
var world = entity.GetWorld();
world.EntityManager.RemoveEntity(ref entity);
}
}