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

@@ -0,0 +1,19 @@
namespace Ghost.Entities.Utilities;
internal class Box<T>
where T : struct
{
public T Value
{
get;
set;
}
public Box(T value)
{
Value = value;
}
public static implicit operator T(Box<T> box) => box.Value;
public static implicit operator Box<T>(T value) => new(value);
}

View File

@@ -0,0 +1,64 @@
using System.Numerics;
namespace Ghost.Entities.Utilities;
internal readonly struct ComponentMask
{
private readonly ulong[] _words;
public ComponentMask(int entityCapacity)
{
_words = new ulong[(entityCapacity + 63) / 64];
}
public void Set(int entityIndex)
=> _words[entityIndex >> 6] |= 1UL << (entityIndex & 63);
public void Clear(int entityIndex)
=> _words[entityIndex >> 6] &= ~(1UL << (entityIndex & 63));
public bool Get(int entityIndex)
=> ((_words[entityIndex >> 6] >> (entityIndex & 63)) & 1) != 0;
// Bitwise AND
public ComponentMask And(in ComponentMask other)
{
var result = new ComponentMask(_words.Length * 64);
for (var i = 0; i < _words.Length; i++)
result._words[i] = _words[i] & other._words[i];
return result;
}
// Bitwise OR
public ComponentMask Or(in ComponentMask other)
{
var result = new ComponentMask(_words.Length * 64);
for (var i = 0; i < _words.Length; i++)
result._words[i] = _words[i] | other._words[i];
return result;
}
// Bitwise NOT
public ComponentMask Not()
{
var result = new ComponentMask(_words.Length * 64);
for (var i = 0; i < _words.Length; i++)
result._words[i] = ~_words[i];
return result;
}
// Iterate set bits (fast scan)
public IEnumerable<int> GetEntityIndices()
{
for (var word = 0; word < _words.Length; word++)
{
var bits = _words[word];
while (bits != 0)
{
var lowBit = BitOperations.TrailingZeroCount(bits);
yield return (word << 6) + lowBit;
bits &= bits - 1; // clear lowest set bit
}
}
}
}

View File

@@ -0,0 +1,6 @@
namespace Ghost.Entities.Utilities;
internal static class TypeHandle<T>
{
public static nint Value => typeof(T).TypeHandle.Value;
}