Refactor entity-component system and related classes

Changed the `Component` class to an interface `IComponentData` to support a data-oriented design.
Changed the `Transform` class from a class to a struct, implementing `IComponentData` and updating properties.
Changed the `GameObject` class to use a dictionary for components and added properties for state management.
Changed the `PlayerLoopService` class to `GameLoopService` and updated methods to integrate with the new `SceneManager`.
Changed the `World` class to manage multiple worlds and enhance entity management with new querying methods.

Added the `Scene` class to manage root game objects and their lifecycle.
Added new utility classes like `ComponentMask`, `Box<T>`, and `TypeHandle<T>` for better component management.
Added the `ScriptComponent` class to allow for modular scriptable components attached to entities.
Added the `QueryEnumerable` class to facilitate flexible querying of entities with specific components.

Updated the `Test` class in `Program.cs` to demonstrate the new entity and component management system.
Updated project files to include new references and settings supporting the changes made in the codebase.
This commit is contained in:
2025-05-28 15:21:43 +09:00
parent 0cf3104a6a
commit 67b6040b5e
31 changed files with 3670 additions and 811 deletions

View File

@@ -1,12 +1,39 @@
namespace Ghost.Entities;
using Ghost.Entities.Utilities;
using Misaki.HighPerformance.Unsafe.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
public interface IComponent
namespace Ghost.Entities;
public interface IComponentData
{
}
internal class ComponentPool<T> : IDisposable
where T : struct, IComponent
internal static class SingletonContainer<T>
where T : struct, IComponentData
{
public static readonly Dictionary<int, T> container = new();
}
internal interface IComponentPool : IDisposable
{
public EntityID Count
{
get;
}
public void Remove(Entity entity);
public bool Has(Entity entity);
}
internal interface IComponentPool<T> : IComponentPool
{
public void Add(Entity entity, T Component);
}
internal class ComponentPool<T> : IComponentPool<T>
where T : struct, IComponentData
{
private struct ComponentData
{
@@ -20,6 +47,8 @@ internal class ComponentPool<T> : IDisposable
private ComponentData[] _components;
private EntityID[] _lookup;
public EntityID Count => _nextId;
public ComponentPool(int initialSize = 16)
{
_nextId = 0;
@@ -31,26 +60,34 @@ internal class ComponentPool<T> : IDisposable
_lookup.AsSpan().Fill(Entity.INVALID_ID);
}
public EntityID Count => _nextId;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static EntityID GetLookupIndex(Entity entity)
{
return entity.ID;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private EntityID GetComponentIndex(Entity entity)
{
return _lookup[entity.ID];
return _lookup[GetLookupIndex(entity)];
}
public void Add(Entity entity, T component)
{
if (entity.ID >= _lookup.Length)
if (!entity.IsValid)
{
_lookup.AsSpan(_nextId, entity.ID - _lookup.Length + 1).Fill(Entity.INVALID_ID);
return;
}
if (_lookup[entity.ID] != Entity.INVALID_ID)
var lookupIndex = GetLookupIndex(entity);
var componentIndex = GetComponentIndex(entity);
if (componentIndex != Entity.INVALID_ID)
{
// Overwrite the old data if generation is larger
if (entity.Generation > _components[_lookup[entity.ID]].owner.Generation)
if (entity.Generation > _components[componentIndex].owner.Generation)
{
var index = _lookup[entity.ID];
var index = _lookup[lookupIndex];
_components[index].data = component;
_components[index].owner = entity;
}
@@ -68,19 +105,29 @@ internal class ComponentPool<T> : IDisposable
_capacity = newCapacity;
}
_lookup[lookupIndex] = _nextId;
_components[_nextId] = new ComponentData
{
data = component,
owner = entity
};
_lookup[entity.ID] = _nextId;
_nextId++;
}
public void Remove(Entity entity)
{
}
public ref T GetRef(Entity entity)
{
return ref _components[_lookup[entity.ID]].data;
if (!entity.IsValid)
{
return ref Unsafe.NullRef<T>();
}
var index = GetComponentIndex(entity);
return ref _components[index].data;
}
public bool Has(Entity entity)
@@ -96,17 +143,235 @@ internal class ComponentPool<T> : IDisposable
public void Set(Entity entity, T component)
{
if (entity.ID >= _lookup.Length || _lookup[entity.ID] == Entity.INVALID_ID)
if (!entity.IsValid || entity.ID >= _lookup.Length || GetComponentIndex(entity) == Entity.INVALID_ID)
{
return;
}
var index = _lookup[entity.ID];
var index = GetComponentIndex(entity);
_components[index].data = component;
_components[index].owner = entity;
}
public void Dispose()
{
_components = Array.Empty<ComponentData>();
_lookup = Array.Empty<EntityID>();
_nextId = 0;
_capacity = 0;
}
}
internal class ScriptComponentPool : IComponentPool<ScriptComponent>
{
private Dictionary<Entity, List<ScriptComponent>>? _scriptComponents;
private List<ScriptComponent>? _executionList;
internal Dictionary<Entity, List<ScriptComponent>>? ScriptComponents => _scriptComponents;
internal List<ScriptComponent>? ExecutionList => _executionList;
public bool IsInitialized => _scriptComponents != null;
public int Count => _scriptComponents?.Keys.Count ?? 0;
internal void Initialize(int capacity = 16)
{
_scriptComponents ??= new(capacity);
}
internal void RebuildExecutionList()
{
if (_scriptComponents == null)
{
return;
}
_executionList ??= new List<ScriptComponent>(_scriptComponents.Count);
_executionList.Clear();
foreach (var kvp in _scriptComponents)
{
_executionList.AddRange(kvp.Value);
}
_executionList.Sort((a, b) => a.ExecutionOrder.CompareTo(b.ExecutionOrder));
}
public void Add(Entity entity, ScriptComponent component)
{
if (!IsInitialized)
{
Initialize();
}
if (!_scriptComponents!.TryGetValue(entity, out var scriptList))
{
scriptList = new();
_scriptComponents[entity] = scriptList;
}
scriptList.Add(component);
component.Owner = entity;
}
public void Remove(Entity entity)
{
if (!Has(entity)
|| !_scriptComponents!.TryGetValue(entity, out var scriptList)
|| scriptList == null)
{
return;
}
foreach (var script in scriptList)
{
script.OnDestroy();
}
_scriptComponents.Remove(entity);
}
public bool Has(Entity entity)
{
return _scriptComponents?.ContainsKey(entity) ?? false;
}
public List<ScriptComponent>? Get(Entity entity)
{
if (_scriptComponents == null
|| !_scriptComponents.TryGetValue(entity, out var scriptList))
{
return null;
}
return scriptList;
}
public void Dispose()
{
if (_scriptComponents != null)
{
if (_executionList != null)
{
foreach (var script in _executionList)
{
script.OnDestroy();
}
_executionList.Clear();
}
else
{
foreach (var scriptList in _scriptComponents.Values)
{
if (scriptList == null)
{
continue;
}
foreach (var script in scriptList)
{
script.OnDestroy();
}
}
}
_scriptComponents.Clear();
}
}
}
internal class ComponentStorage : IDisposable
{
private readonly Dictionary<nint, IComponentPool> _componentPools = new();
private readonly Dictionary<nint, BitSet> _componentEntityMasks = new();
private readonly ScriptComponentPool _scriptComponentPool = new();
internal Dictionary<nint, IComponentPool> ComponentPools => _componentPools;
internal ScriptComponentPool ScriptComponentPool => _scriptComponentPool;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetPool(nint typeHandle, [MaybeNullWhen(false)] out IComponentPool pool)
{
return _componentPools.TryGetValue(typeHandle, out pool);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetPool(Type type, [MaybeNullWhen(false)] out IComponentPool pool)
{
return TryGetPool(type.TypeHandle.Value, 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;
return result;
}
public ComponentPool<T> GetOrCreateComponentPool<T>()
where T : struct, IComponentData
{
var key = TypeHandle<T>.Value;
if (!_componentPools.TryGetValue(key, out var obj))
{
var pool = new ComponentPool<T>();
_componentPools[key] = pool;
return pool;
}
return (ComponentPool<T>)obj;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetMask(nint typeHandle, [MaybeNullWhen(false)] out BitSet bitSet)
{
return _componentEntityMasks.TryGetValue(typeHandle, out bitSet);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetMask(Type type, [MaybeNullWhen(false)] out BitSet bitSet)
{
return TryGetMask(type.TypeHandle.Value, out bitSet);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetMask<T>([MaybeNullWhen(false)] out BitSet bitSet)
where T : struct, IComponentData
{
return TryGetMask(TypeHandle<T>.Value, out bitSet);
}
public BitSet GetOrCreateMask(nint typeHandle)
{
if (!_componentEntityMasks.TryGetValue(typeHandle, out var mask))
{
mask = new BitSet();
_componentEntityMasks[typeHandle] = mask;
}
return mask;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RebuildExecutionList()
{
_scriptComponentPool.RebuildExecutionList();
}
public void Remove(Entity entity)
{
_scriptComponentPool.Remove(entity);
}
public void Dispose()
{
foreach (var pool in _componentPools.Values)
{
pool.Dispose();
}
_componentPools.Clear();
_scriptComponentPool.Dispose();
}
}