Refactor project structure and improve performance

Changed the `ProjectRepository` class to be static for easier usage.
Changed `ProjectService` constants to public properties for accessibility.
Changed `App.xaml` to consolidate theme resources into `Override.xaml`.
Changed `App.xaml.cs` to implement an `AppStateMachine` for better state management.
Changed `ConsolePage` and `HierarchyPage` to utilize the new ViewModel structure.
Changed `ProjectPage` to use the `ExplorerItem` model for asset display.
Changed `Entity` and `EntityManager` to enhance component management with a new `IComponentData` interface.
Changed the `Logger` class to introduce structured logging functionality.
Changed the system architecture to support dependency management for better organization.
Changed the `QueryEnumerable` class to allow for more flexible entity queries.
Changed the `TypeHandle` class to improve efficiency in retrieving type handles.
Changed the `World` class to support robust world management and multiple worlds.
Updated the `Test` class to demonstrate the new entity and component management system.
This commit is contained in:
2025-06-05 21:45:50 +09:00
parent 61bbb1bc68
commit bab3be2508
69 changed files with 2184 additions and 1582 deletions

View File

@@ -1,5 +1,5 @@
global using EntityID = System.Int32;
global using GenerationID = System.UInt16;
global using GenerationID = System.Byte;
global using WorldID = System.UInt16;
using System.Runtime.CompilerServices;

View File

@@ -3,12 +3,7 @@ using Misaki.HighPerformance.Unsafe.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Ghost.Entities;
public interface IComponentData
{
}
namespace Ghost.Entities.Components;
internal static class SingletonContainer<T>
where T : struct, IComponentData
@@ -50,7 +45,7 @@ internal class ComponentPool<T> : IComponentPool<T>
public EntityID Count => _nextId;
public ComponentPool(int initialSize = 16)
public ComponentPool(int initialSize)
{
_nextId = 0;
_capacity = initialSize;
@@ -144,7 +139,7 @@ internal class ComponentPool<T> : IComponentPool<T>
return index != Entity.INVALID_ID && _components[index].owner.Generation == entity.Generation;
}
public void Set(Entity entity, T component)
public void Set(Entity entity, in T component)
{
if (!entity.IsValid || entity.ID >= _lookup.Length || GetComponentIndex(entity) == Entity.INVALID_ID)
{
@@ -334,7 +329,8 @@ internal class ScriptComponentPool : IComponentPool<ScriptComponent>
}
}
internal class ComponentStorage : IDisposable
[SkipLocalsInit]
internal readonly struct ComponentStorage : IDisposable
{
private readonly Dictionary<nint, IComponentPool> _componentPools = new();
private readonly Dictionary<nint, BitSet> _componentEntityMasks = new();
@@ -360,25 +356,25 @@ internal class ComponentStorage : IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetPool(Type type, [MaybeNullWhen(false)] out IComponentPool pool)
{
return TryGetPool(type.TypeHandle.Value, out pool);
return TryGetPool(TypeHandle.Get(type), out pool);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetPool<T>([MaybeNullWhen(false)] out ComponentPool<T> pool)
where T : struct, IComponentData
{
var result = TryGetPool(TypeHandle<T>.Value, out var obj);
pool = (ComponentPool<T>?)obj;
var result = TryGetPool(TypeHandle.Get<T>(), out var obj);
pool = (ComponentPool<T>?)obj ?? default;
return result;
}
public ComponentPool<T> GetOrCreateComponentPool<T>()
where T : struct, IComponentData
{
var key = TypeHandle<T>.Value;
var key = TypeHandle.Get<T>();
if (!_componentPools.TryGetValue(key, out var obj))
{
var pool = new ComponentPool<T>();
var pool = new ComponentPool<T>(16);
_componentPools[key] = pool;
return pool;
}
@@ -402,7 +398,7 @@ internal class ComponentStorage : IDisposable
public bool TryGetMask<T>([MaybeNullWhen(false)] out BitSet bitSet)
where T : struct, IComponentData
{
return TryGetMask(TypeHandle<T>.Value, out bitSet);
return TryGetMask(TypeHandle.Get<T>(), out bitSet);
}
public BitSet GetOrCreateMask(nint typeHandle)

View File

@@ -0,0 +1,4 @@
namespace Ghost.Entities.Components;
public interface IComponentData
{
}

View File

@@ -1,4 +1,4 @@
namespace Ghost.Entities;
namespace Ghost.Entities.Components;
public abstract class ScriptComponent : IComponentData
{

View File

@@ -1,18 +1,14 @@
using Ghost.Entities.Query;
using Ghost.Entities.Utilities;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace Ghost.Entities;
[SkipLocalsInit]
public struct Entity : IEquatable<Entity>, IComparable<Entity>
{
public const int WORLD_INDEX_BITS = 4;
public const int GENERATION_BITS = 8;
public const int INDEX_BITS = sizeof(EntityID) * 8 - WORLD_INDEX_BITS - GENERATION_BITS;
// Is 256 generations enough? If not, increase the size of GenerationID or make generation as a separate int field.
public const int GENERATION_BITS = sizeof(GenerationID) * 8;
public const int INDEX_BITS = sizeof(EntityID) * 8 - GENERATION_BITS;
private const int _WORLD_INDEX_MASK = (1 << WORLD_INDEX_BITS) - 1;
private const int _GENERATION_MASK = (1 << GENERATION_BITS) - 1;
private const int _INDEX_MASK = (1 << INDEX_BITS) - 1;
@@ -38,24 +34,18 @@ public struct Entity : IEquatable<Entity>, IComparable<Entity>
get => (GenerationID)(_id >> INDEX_BITS & _GENERATION_MASK);
}
public readonly WorldID WorldID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (WorldID)(_id >> (INDEX_BITS + GENERATION_BITS) & _WORLD_INDEX_MASK);
}
public static Entity Invalid
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(INVALID_ID, 0, 0);
get => new(INVALID_ID, 0);
}
internal Entity(EntityID id, GenerationID generation, WorldID worldID)
internal Entity(EntityID id, GenerationID generation)
{
_id = worldID << (INDEX_BITS + GENERATION_BITS) | generation << INDEX_BITS | id;
_id = generation << INDEX_BITS | id;
}
public void IncrementGeneration()
internal void IncrementGeneration()
{
var generation = Generation + 1;
if (generation >= _GENERATION_MASK)
@@ -98,263 +88,6 @@ public struct Entity : IEquatable<Entity>, IComparable<Entity>
public override readonly string ToString()
{
return $"Entity {{ Index: {ID}, Generation: {Generation}, WorldIndex: {WorldID} }}";
}
}
public class EntityManager : IDisposable
{
private readonly List<Entity> _entities;
private readonly Queue<EntityID> _freeEntitySlots;
private readonly World _world;
public int EntityCount => _entities.Count;
public ReadOnlySpan<Entity> Entities => CollectionsMarshal.AsSpan(_entities);
public event Action<Entity, Type>? OnComponentAdded;
public event Action<Entity, Type>? OnComponentRemoved;
public event Action<Entity>? OnEntityCreated;
public event Action<EntityID>? OnEntityRemoved;
internal EntityManager(World world, int initialCapacity)
{
_entities = new(initialCapacity);
_freeEntitySlots = new(initialCapacity);
_world = world;
}
/// <summary>
/// Adds a new <see cref="Entity"/> to the world.
/// </summary>
/// <returns>The created <see cref="Entity"/>.</returns>
public Entity CreateEntity()
{
Entity entity;
if (_freeEntitySlots.TryDequeue(out var id))
{
entity = _entities[id];
}
else
{
id = _entities.Count;
entity = new Entity(id, 0, _world.ID);
_entities.Add(entity);
}
OnEntityCreated?.Invoke(entity);
return entity;
}
/// <summary>
/// Removes the specified <see cref="Entity"/> from the world.
/// </summary>
/// <param name="entity"></param>
public void RemoveEntity(ref Entity entity)
{
if (entity.ID >= _entities.Count || _entities[entity.ID].Generation != entity.Generation)
{
return;
}
_world.ComponentStorage.Remove(entity);
var slot = _entities[entity.ID];
slot.IncrementGeneration();
_entities[entity.ID] = slot;
_freeEntitySlots.Enqueue(entity.ID);
OnEntityRemoved?.Invoke(entity.ID);
entity = Entity.Invalid;
}
/// <summary>
/// Checks if the given <see cref="Entity"/> is valid and belongs to this <see cref="World"/>.
/// </summary>
/// <param name="entity">The entity to check.</param>
/// <returns>True if the entity is valid and belongs to this world; otherwise, false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasEntity(Entity entity)
{
if (!entity.IsValid
|| entity.WorldID != _world.ID
|| entity.ID >= _entities.Count)
{
return false;
}
return _entities[entity.ID].Generation == entity.Generation;
}
/// <summary>
/// Adds a component of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the component to set.</typeparam>
/// <param name="entity">The entity for which the component is to be add.</param>
/// <param name="component">The component value to add.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddComponent<T>(Entity entity, T component)
where T : struct, IComponentData
{
_world.ComponentStorage.GetOrCreateComponentPool<T>().Add(entity, component);
_world.ComponentStorage.GetOrCreateMask(TypeHandle<T>.Value).SetBit(entity.ID);
OnComponentAdded?.Invoke(entity, typeof(T));
}
/// <summary>
/// Removes a component of type <typeparamref name="T"/> from the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the component to remove.</typeparam>
/// <param name="entity">The entity for which the component is to be remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool RemoveComponent<T>(Entity entity)
where T : struct, IComponentData
{
if (!_world.ComponentStorage.TryGetPool<T>(out var pool) || !pool.Has(entity))
{
return false;
}
if (!pool.Remove(entity))
{
return false;
}
_world.ComponentStorage.GetOrCreateMask(TypeHandle<T>.Value).ClearBit(entity.ID);
OnComponentRemoved?.Invoke(entity, typeof(T));
return true;
}
/// <summary>
/// Sets a component of type <typeparamref name="T"/> for the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the component to set.</typeparam>
/// <param name="entity">The entity for which the component is to be set.</param>
/// <param name="component">The component value to set.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetComponent<T>(Entity entity, T component)
where T : struct, IComponentData
{
_world.ComponentStorage.GetOrCreateComponentPool<T>().Set(entity, component);
}
/// <summary>
/// Checks if the given <see cref="Entity"/> has a component of the specified type.
/// </summary>
/// <param name="entity">The entity to check.</param>
/// <param name="typeHandle">The handle of the component type.</param>
/// <returns>True if the entity has the component; otherwise, false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasComponent(Entity entity, nint typeHandle)
{
return _world.ComponentStorage.TryGetMask(typeHandle, out var bitSet) && bitSet.IsSet(entity.ID);
}
/// <summary>
/// Retrieves a reference to a component of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the component to retrieve.</typeparam>
/// <param name="entity">The entity whose component is to be retrieved.</param>
/// <returns>A <see cref="Ref{T}"/> to the component, or a null reference if the component does not exist.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Ref<T> GetComponent<T>(Entity entity)
where T : struct, IComponentData
{
if (_world.ComponentStorage.TryGetPool<T>(out var pool) && pool.Has(entity))
{
return new Ref<T>(ref pool.GetRef(entity));
}
else
{
return new Ref<T>(ref Unsafe.NullRef<T>(), false);
}
}
/// <summary>
/// Adds a script of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the script to add.</typeparam>
/// <param name="entity">The entity to which the script is to be added.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddScript<T>(Entity entity)
where T : ScriptComponent, new()
{
_world.ComponentStorage.ScriptComponentPool.Add(entity, new T());
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
}
/// <summary>
/// Adds a script of the specified type to the given <see cref="Entity"/>.
/// </summary>
/// <param name="entity">The entity to which the script is to be added.</param>
/// <param name="type">The type of the script to add.</param>
/// <exception cref="ArgumentException">Thrown if the specified type does not inherit from <see cref="ScriptComponent"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the script instance could not be created.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddScript(Entity entity, Type type)
{
if (!typeof(ScriptComponent).IsAssignableFrom(type))
{
throw new ArgumentException($"Type {type} must inherit from ScriptComponent.", nameof(type));
}
var instance = (ScriptComponent?)Activator.CreateInstance(type) ?? throw new InvalidOperationException($"Failed to create instance of {type}.");
_world.ComponentStorage.ScriptComponentPool.Add(entity, instance);
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
}
public bool RemoveScript<T>(Entity entity)
where T : ScriptComponent
{
if (!_world.ComponentStorage.ScriptComponentPool.Remove<T>(entity))
{
return false;
}
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
return true;
}
public bool RemoveScriptAt(Entity entity, int index)
{
if (!_world.ComponentStorage.ScriptComponentPool.RemoveAt(entity, index))
{
return false;
}
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
return true;
}
/// <summary>
/// Retrieves the first script of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the script to retrieve.</typeparam>
/// <param name="entity">The entity whose script is to be retrieved.</param>
/// <returns>The script of type <typeparamref name="T"/>, or null if no such script exists.</returns>
public T? GetScript<T>(Entity entity)
where T : ScriptComponent
{
return (T?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?
.FirstOrDefault(script => script is T tScript);
}
/// <summary>
/// Retrieves all scripts of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the scripts to retrieve.</typeparam>
/// <param name="entity">The entity whose scripts are to be retrieved.</param>
/// <returns>An enumerable of scripts of type <typeparamref name="T"/>.</returns>
public IEnumerable<T> GetScripts<T>(Entity entity)
where T : ScriptComponent
{
return (IEnumerable<T>?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?.Where(script => script is T tScript) ?? Enumerable.Empty<T>();
}
public void Dispose()
{
_entities.Clear();
_freeEntitySlots.Clear();
return $"Entity {{ Index: {ID}, Generation: {Generation} }}";
}
}

View File

@@ -0,0 +1,262 @@
using Ghost.Entities.Components;
using Ghost.Entities.Query;
using Ghost.Entities.Utilities;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ghost.Entities;
public struct EntityManager : IDisposable
{
private readonly List<Entity> _entities;
private readonly Queue<EntityID> _freeEntitySlots;
private readonly World _world;
public readonly int EntityCount => _entities.Count;
public readonly ReadOnlySpan<Entity> Entities => CollectionsMarshal.AsSpan(_entities);
public event Action<Entity>? OnEntityCreated;
public event Action<EntityID>? OnEntityRemoved;
public event Action<Entity, Type>? OnComponentAdded;
public event Action<Entity, Type>? OnComponentRemoved;
internal EntityManager(World world, int initialCapacity)
{
_entities = new(initialCapacity);
_freeEntitySlots = new(initialCapacity);
_world = world;
}
/// <summary>
/// Adds a new <see cref="Entity"/> to the world.
/// </summary>
/// <returns>The created <see cref="Entity"/>.</returns>
public readonly Entity CreateEntity()
{
Entity entity;
if (_freeEntitySlots.TryDequeue(out var id))
{
entity = _entities[id];
}
else
{
id = _entities.Count;
entity = new Entity(id, 0);
_entities.Add(entity);
}
OnEntityCreated?.Invoke(entity);
return entity;
}
/// <summary>
/// Removes the specified <see cref="Entity"/> from the world.
/// </summary>
/// <param name="entity"></param>
public readonly void RemoveEntity(ref Entity entity)
{
if (entity.ID >= _entities.Count || _entities[entity.ID].Generation != entity.Generation)
{
return;
}
_world.ComponentStorage.Remove(entity);
var slot = _entities[entity.ID];
slot.IncrementGeneration();
_entities[entity.ID] = slot;
_freeEntitySlots.Enqueue(entity.ID);
OnEntityRemoved?.Invoke(entity.ID);
entity = Entity.Invalid;
}
/// <summary>
/// Checks if the given <see cref="Entity"/> is valid and belongs to this <see cref="World"/>.
/// </summary>
/// <param name="entity">The entity to check.</param>
/// <returns>True if the entity is valid and belongs to this world; otherwise, false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool HasEntity(Entity entity)
{
if (!entity.IsValid
|| entity.ID >= _entities.Count)
{
return false;
}
return _entities[entity.ID].Generation == entity.Generation;
}
/// <summary>
/// Adds a component of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the component to set.</typeparam>
/// <param name="entity">The entity for which the component is to be add.</param>
/// <param name="component">The component value to add.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void AddComponent<T>(Entity entity, T component)
where T : struct, IComponentData
{
_world.ComponentStorage.GetOrCreateComponentPool<T>().Add(entity, component);
_world.ComponentStorage.GetOrCreateMask(TypeHandle.Get<T>()).SetBit(entity.ID);
OnComponentAdded?.Invoke(entity, typeof(T));
}
/// <summary>
/// Removes a component of type <typeparamref name="T"/> from the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the component to remove.</typeparam>
/// <param name="entity">The entity for which the component is to be remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool RemoveComponent<T>(Entity entity)
where T : struct, IComponentData
{
if (!_world.ComponentStorage.TryGetPool<T>(out var pool) || !pool.Has(entity))
{
return false;
}
if (!pool.Remove(entity))
{
return false;
}
_world.ComponentStorage.GetOrCreateMask(TypeHandle.Get<T>()).ClearBit(entity.ID);
OnComponentRemoved?.Invoke(entity, typeof(T));
return true;
}
/// <summary>
/// Sets a component of type <typeparamref name="T"/> for the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the component to set.</typeparam>
/// <param name="entity">The entity for which the component is to be set.</param>
/// <param name="component">The component value to set.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void SetComponent<T>(Entity entity, in T component)
where T : struct, IComponentData
{
_world.ComponentStorage.GetOrCreateComponentPool<T>().Set(entity, in component);
}
/// <summary>
/// Checks if the given <see cref="Entity"/> has a component of the specified type.
/// </summary>
/// <param name="entity">The entity to check.</param>
/// <param name="typeHandle">The handle of the component type.</param>
/// <returns>True if the entity has the component; otherwise, false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool HasComponent(Entity entity, nint typeHandle)
{
return _world.ComponentStorage.TryGetMask(typeHandle, out var bitSet) && bitSet.IsSet(entity.ID);
}
/// <summary>
/// Retrieves a reference to a component of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the component to retrieve.</typeparam>
/// <param name="entity">The entity whose component is to be retrieved.</param>
/// <returns>A <see cref="Ref{T}"/> to the component, or a null reference if the component does not exist.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Ref<T> GetComponent<T>(Entity entity)
where T : struct, IComponentData
{
if (_world.ComponentStorage.TryGetPool<T>(out var pool) && pool.Has(entity))
{
return new Ref<T>(ref pool.GetRef(entity));
}
else
{
return new Ref<T>(ref Unsafe.NullRef<T>(), false);
}
}
/// <summary>
/// Adds a script of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the script to add.</typeparam>
/// <param name="entity">The entity to which the script is to be added.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void AddScript<T>(Entity entity)
where T : ScriptComponent, new()
{
_world.ComponentStorage.ScriptComponentPool.Add(entity, new T());
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
}
/// <summary>
/// Adds a script of the specified type to the given <see cref="Entity"/>.
/// </summary>
/// <param name="entity">The entity to which the script is to be added.</param>
/// <param name="type">The type of the script to add.</param>
/// <exception cref="ArgumentException">Thrown if the specified type does not inherit from <see cref="ScriptComponent"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the script instance could not be created.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void AddScript(Entity entity, Type type)
{
if (!typeof(ScriptComponent).IsAssignableFrom(type))
{
throw new ArgumentException($"Type {type} must inherit from ScriptComponent.", nameof(type));
}
var instance = (ScriptComponent?)Activator.CreateInstance(type) ?? throw new InvalidOperationException($"Failed to create instance of {type}.");
_world.ComponentStorage.ScriptComponentPool.Add(entity, instance);
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
}
public readonly bool RemoveScript<T>(Entity entity)
where T : ScriptComponent
{
if (!_world.ComponentStorage.ScriptComponentPool.Remove<T>(entity))
{
return false;
}
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
return true;
}
public readonly bool RemoveScriptAt(Entity entity, int index)
{
if (!_world.ComponentStorage.ScriptComponentPool.RemoveAt(entity, index))
{
return false;
}
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
return true;
}
/// <summary>
/// Retrieves the first script of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the script to retrieve.</typeparam>
/// <param name="entity">The entity whose script is to be retrieved.</param>
/// <returns>The script of type <typeparamref name="T"/>, or null if no such script exists.</returns>
public readonly T? GetScript<T>(Entity entity)
where T : ScriptComponent
{
return (T?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?
.FirstOrDefault(script => script is T tScript);
}
/// <summary>
/// Retrieves all scripts of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
/// </summary>
/// <typeparam name="T">The type of the scripts to retrieve.</typeparam>
/// <param name="entity">The entity whose scripts are to be retrieved.</param>
/// <returns>An enumerable of scripts of type <typeparamref name="T"/>.</returns>
public readonly IEnumerable<T> GetScripts<T>(Entity entity)
where T : ScriptComponent
{
return (IEnumerable<T>?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?.Where(script => script is T tScript) ?? Enumerable.Empty<T>();
}
public readonly void Dispose()
{
_entities.Clear();
_freeEntitySlots.Clear();
}
}

View File

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

View File

@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using Ghost.Entities.Components;
using System.Runtime.CompilerServices;
namespace Ghost.Entities.Query;

View File

@@ -1,135 +0,0 @@
using System.Runtime.CompilerServices;
namespace Ghost.Entities;
public abstract class SystemBase
{
/// <summary>
/// Gets the execution order of the current operation or component.
/// </summary>
public virtual int ExecutionOrder => 0;
/// <summary>
/// Gets or sets a value indicating whether the feature is enabled.
/// </summary>
public virtual bool Enable
{
get;
set;
} = true;
/// <summary>
/// The world that this system belongs to.
/// </summary>
public World World
{
get;
internal set;
} = null!;
public virtual void OnCreate()
{
}
public virtual void OnUpdate()
{
}
public virtual void OnDestroy()
{
}
}
public class SystemStorage : IDisposable
{
private readonly List<SystemBase> _systems = new();
private readonly List<SystemBase> _executionList = new();
private readonly World _world;
public event Action<SystemBase>? SystemAdded;
public event Action<SystemBase>? SystemRemoved;
internal SystemStorage(World world)
{
_world = world;
}
public void AddSystem<T>(T system)
where T : SystemBase
{
_systems.Add(system);
system.World = _world;
if (system.Enable)
{
system.OnCreate();
}
SystemAdded?.Invoke(system);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddSystem<T>()
where T : SystemBase, new()
{
AddSystem(new T());
}
public void RemoveSystem<T>(T system)
where T : SystemBase
{
system.World = null!;
_systems.Remove(system);
if (system.Enable)
{
system.OnDestroy();
}
SystemRemoved?.Invoke(system);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveSystem<T>()
where T : SystemBase, new()
{
var system = _systems.FirstOrDefault(s => s is T);
if (system != null)
{
RemoveSystem(system);
}
}
internal void RebuildExecutionList()
{
_executionList.Clear();
_executionList.AddRange(_systems.OrderBy(s => s.ExecutionOrder));
}
internal void UpdateSystems()
{
foreach (var system in _systems)
{
if (!system.Enable)
{
continue;
}
system.OnUpdate();
}
}
public void Dispose()
{
foreach (var system in _systems)
{
if (!system.Enable)
{
continue;
}
system.OnDestroy();
}
_systems.Clear();
_executionList.Clear();
}
}

View File

@@ -0,0 +1,27 @@
namespace Ghost.Entities.Systems;
/// <summary>
/// Attribute to declare that a system depends on one or more other systems.
/// </summary>
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class, AllowMultiple = false)]
public class DependsOnAttribute : Attribute
{
public Type[] Prerequisites
{
get;
}
public DependsOnAttribute(params Type[] prerequisites)
{
Prerequisites = prerequisites;
}
}
public interface ISystem
{
public void OnCreate(in SystemState state);
public void OnUpdate(in SystemState state);
public void OnDestroy(in SystemState state);
}

View File

@@ -0,0 +1,92 @@
using System.Reflection;
namespace Ghost.Entities.Systems;
internal class SystemDependencyBuilder(List<Type> allSystemTypes)
{
private Dictionary<Type, List<Type>> _dependencies = new();
public void BuildDependencyGraph()
{
foreach (var systemType in allSystemTypes)
{
if (!typeof(ISystem).IsAssignableFrom(systemType) || systemType.IsAbstract || systemType.IsInterface)
{
throw new ArgumentException($"{systemType.Name} is not a concrete ISystem type.");
}
var directDependencies = new List<Type>();
var dependsOnAttributes = systemType.GetCustomAttributes<DependsOnAttribute>(false);
foreach (var attr in dependsOnAttributes)
{
directDependencies.AddRange(attr.Prerequisites);
}
_dependencies[systemType] = directDependencies;
}
}
private void Visit(Type systemType, HashSet<Type> visited, HashSet<Type> permanentMark, List<Type> executionOrder)
{
if (permanentMark.Contains(systemType))
{
return;
}
if (visited.Contains(systemType))
{
throw new InvalidOperationException($"Circular dependency detected involving system: {systemType.Name}");
}
visited.Add(systemType); // Mark as currently visiting
if (_dependencies.TryGetValue(systemType, out var directDependencies))
{
foreach (var dependencyType in directDependencies)
{
// Ensure the dependency is a registered system type
if (!allSystemTypes.Contains(dependencyType))
{
throw new InvalidOperationException($"System {systemType.Name} depends on unregistered system {dependencyType.Name}.");
}
Visit(dependencyType, visited, permanentMark, executionOrder);
}
}
visited.Remove(systemType); // Done visiting this node in the current path
permanentMark.Add(systemType); // Mark as permanently processed
executionOrder.Add(systemType); // Add to the sorted list (this will be reversed later for correct order)
}
/// <summary>
/// Builds the topological order of systems.
/// </summary>
/// <returns>A list of system types in the order they should be executed.</returns>
/// <exception cref="InvalidOperationException">Thrown if a circular dependency is detected.</exception>"
public List<Type> BuildExecutionOrder()
{
var executionOrder = new List<Type>(allSystemTypes.Count);
var visited = new HashSet<Type>(); // Tracks visited nodes in the current DFS path (for cycle detection)
var permanentMark = new HashSet<Type>(); // Tracks nodes whose dependencies have been fully resolved
// Initialize dependencies for all registered systems, even those without explicit attributes
foreach (var sysType in allSystemTypes)
{
if (!_dependencies.ContainsKey(sysType))
{
_dependencies[sysType] = new();
}
}
foreach (var systemType in allSystemTypes)
{
if (!permanentMark.Contains(systemType))
{
Visit(systemType, visited, permanentMark, executionOrder);
}
}
return executionOrder;
}
}

View File

@@ -0,0 +1,10 @@
namespace Ghost.Entities.Systems;
public struct SystemState
{
public World World
{
get;
init;
}
}

View File

@@ -0,0 +1,95 @@
using System.Runtime.CompilerServices;
namespace Ghost.Entities.Systems;
[SkipLocalsInit]
public struct SystemStorage
{
private readonly List<Type> _systems = new();
private readonly List<ISystem> _executionList = new();
private readonly World _world;
public event Action<Type>? SystemAdded;
public event Action<Type>? SystemRemoved;
internal SystemStorage(World world)
{
_world = world;
}
public readonly void AddSystem(Type systemType)
{
_systems.Add(systemType);
SystemAdded?.Invoke(systemType);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void AddSystem<T>()
where T : ISystem, new()
{
AddSystem(typeof(T));
}
public readonly void RemoveSystem(Type systemType)
{
_systems.Remove(systemType);
SystemRemoved?.Invoke(systemType);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void RemoveSystem<T>()
where T : ISystem, new()
{
RemoveSystem(typeof(T));
}
internal void CreateSystems()
{
var builder = new SystemDependencyBuilder(_systems);
builder.BuildDependencyGraph();
var executionOrder = builder.BuildExecutionOrder();
var state = new SystemState()
{
World = _world,
};
foreach (var systemType in executionOrder)
{
var system = (ISystem?)Activator.CreateInstance(systemType) ?? throw new InvalidOperationException($"Failed to create instance of system type {systemType.Name}.");
_executionList.Add(system);
system.OnCreate(in state);
}
}
internal void UpdateSystems()
{
var state = new SystemState()
{
World = _world,
};
foreach (var system in _executionList)
{
system.OnUpdate(in state);
}
}
internal void Dispose()
{
var state = new SystemState()
{
World = _world,
};
foreach (var system in _executionList)
{
system.OnDestroy(in state);
}
_systems.Clear();
_executionList.Clear();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -5,6 +5,7 @@
<#@ import namespace="System.Linq" #>
<#@ include file="Helpers.ttinclude" #>
using Ghost.Entities.Components;
using Ghost.Entities.Query;
using Ghost.Entities.Utilities;
using Misaki.HighPerformance.Unsafe.Collections;
@@ -47,11 +48,11 @@ public struct QueryEnumerable<<#= generics #>>
_filters = new();
<# for (int i = 0; i < arity; i++) {#>
_filters._all.Add(TypeHandle<T<#= i #>>.Value);
_filters._all.Add(TypeHandle.Get<T<#= i #>>());
<# } #>
}
public Enumerator GetEnumerator() => new Enumerator(_world, <#= constructorParams #>, _count, _filters);
public readonly Enumerator GetEnumerator() => new (_world, <#= constructorParams #>, _count, _filters);
public ref struct Enumerator
{
@@ -110,7 +111,7 @@ var compRestrictions = AppendGenericRestrictions(i, "TComponent", "struct, IComp
<#= compRestrictions #>
{
<# for (int j = 0; j < i; j++) {#>
_filters._all.Add(TypeHandle<TComponent<#= j #>>.Value);
_filters._all.Add(TypeHandle.Get<TComponent<#= j #>>());
<# } #>
return this;
}
@@ -119,7 +120,7 @@ var compRestrictions = AppendGenericRestrictions(i, "TComponent", "struct, IComp
<#= compRestrictions #>
{
<# for (int j = 0; j < i; j++) {#>
_filters._any.Add(TypeHandle<TComponent<#= j #>>.Value);
_filters._any.Add(TypeHandle.Get<TComponent<#= j #>>());
<# } #>
return this;
}
@@ -128,7 +129,7 @@ var compRestrictions = AppendGenericRestrictions(i, "TComponent", "struct, IComp
<#= compRestrictions #>
{
<# for (int j = 0; j < i; j++) {#>
_filters._absent.Add(TypeHandle<TComponent<#= j #>>.Value);
_filters._absent.Add(TypeHandle.Get<TComponent<#= j #>>());
<# } #>
return this;
}
@@ -137,7 +138,7 @@ var compRestrictions = AppendGenericRestrictions(i, "TComponent", "struct, IComp
<#= compRestrictions #>
{
<# for (int j = 0; j < i; j++) {#>
_filters._disabled.Add(TypeHandle<TComponent<#= j #>>.Value);
_filters._disabled.Add(TypeHandle.Get<TComponent<#= j #>>());
<# } #>
return this;
}

View File

@@ -1,5 +1,6 @@
using Ghost.Entities.Components;
using Ghost.Entities.Query;
namespace Ghost.Entities;

View File

@@ -1,5 +1,7 @@
using Ghost.Entities.Components;
namespace Ghost.Entities;
public delegate void QueryRefComponent<T0>(Entity entity, ref T0 t0Component)

View File

@@ -1,5 +1,7 @@
using Ghost.Entities.Components;
namespace Ghost.Entities;
public partial class World

View File

@@ -1,6 +1,28 @@
namespace Ghost.Entities.Utilities;
using System.Runtime.CompilerServices;
internal static class TypeHandle<T>
namespace Ghost.Entities.Utilities;
internal static class TypeHandle
{
public static nint Value => typeof(T).TypeHandle.Value;
/// <summary>
/// Gets the type handle for the specified type.
/// </summary>
/// <typeparam name="T">The type to get the handle for.</typeparam>
/// <returns>The type handle as a nint.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint Get<T>()
{
return typeof(T).TypeHandle.Value;
}
/// <summary>
/// Gets the type handle for the specified type.
/// </summary>
/// <param name="type">The type to get the handle for.</param>
/// <returns>The type handle as a nint.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint Get(Type type)
{
return type.TypeHandle.Value;
}
}

View File

@@ -1,4 +1,6 @@
using Ghost.Entities.Query;
using Ghost.Entities.Components;
using Ghost.Entities.Query;
using Ghost.Entities.Systems;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -7,11 +9,9 @@ namespace Ghost.Entities;
// TODO: Archetype system for better performance
public partial class World
{
private static List<World> s_worlds = new(2);
private static List<World> s_worlds = new(4);
private static Queue<WorldID> s_freeWorldSlots = new();
private static int s_maxWorldCount = (int)MathF.Pow(2, Entity.WORLD_INDEX_BITS);
public static int WorldCount => s_worlds.Count - s_freeWorldSlots.Count;
public static World Create(int entityCapacity = 16)
@@ -24,7 +24,7 @@ public partial class World
}
else
{
if (s_worlds.Count >= s_maxWorldCount)
if (s_worlds.Count >= WorldID.MaxValue)
{
throw new InvalidOperationException("Maximum number of worlds reached");
}