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

@@ -13,4 +13,10 @@
<ProjectReference Include="..\Ghost.Entities\Ghost.Entities.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Misaki.HighPerformance.Unsafe">
<HintPath>..\..\Class\Misaki.HighPerformance\Misaki.HighPerformance.Unsafe\bin\Release\net9.0\Misaki.HighPerformance.Unsafe.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@@ -1,48 +1,167 @@
using Ghost.Entities;
using Ghost.Entities.Helpers;
using System.Numerics;
var world = new World();
var t = new Test();
t.Run();
var entity1 = world.CreateEntity();
var entity2 = world.CreateEntity();
var entity3 = world.CreateEntity();
world.AddComponent(entity1, new Transform { position = new Vector3(1, 2, 3) });
world.AddComponent(entity1, new Mesh { index = 42 });
world.AddComponent(entity2, new Transform { position = new Vector3(4, 5, 6) });
world.AddComponent(entity2, new Mesh { index = 43 });
world.AddComponent(entity3, new Transform { position = new Vector3(7, 8, 9) });
world.Query<Transform>((Entity entity, ref Transform transform) =>
public partial class Test
{
transform.position += new Vector3(1, 1, 1);
});
public void Run()
{
var world = World.Create();
world.Query<Mesh>((Entity entity, ref Mesh mesh) =>
var entity1 = world.EntityManager.CreateEntity();
var entity2 = world.EntityManager.CreateEntity();
var entity3 = world.EntityManager.CreateEntity();
entity1.AddComponent(new Transform { position = new Vector3(1, 2, 3) });
entity1.AddComponent(new Mesh { index = 42 });
entity1.AddScript<UIManager>();
entity1.AddScript<EventManager>();
entity2.AddComponent(new Transform { position = new Vector3(4, 5, 6) });
entity2.AddComponent(new Mesh { index = 43 });
entity2.AddScript<UserScript>();
entity3.AddComponent(new Transform { position = new Vector3(7, 8, 9) });
entity3.AddScript<EventManager>();
foreach (var (_, transform) in world.Query<Transform>())
{
transform.ValueRW.position += new Vector3(1, 1, 1);
}
foreach (var (_, mesh) in world.Query<Mesh>())
{
mesh.ValueRW.index += 1;
}
world.EntityManager.RemoveEntity(ref entity2);
var entity4 = world.EntityManager.CreateEntity();
entity4.AddComponent(new Transform { position = new Vector3(10, 11, 12) });
entity4.AddComponent(new Mesh { index = 44 });
entity4.AddScript<UserScript>();
world.AddSystem<TestSystem>();
world._systemStorage.UpdateSystems();
//world.SystemStorage.RebuildExecutionList();
//world.ComponentStorage.RebuildExecutionList();
//Console.WriteLine();
//Console.WriteLine("Starting scripts...");
//foreach (var component in world.QueryScript())
//{
// if (!component.Enable)
// {
// continue;
// }
// component.Start();
//}
//Console.WriteLine();
//Console.WriteLine("Updating scripts...");
//foreach (var component in world.QueryScript())
//{
// if (!component.Enable)
// {
// continue;
// }
// component.Update();
//}
//Console.WriteLine();
//Console.WriteLine("LateUpdating scripts...");
//foreach (var component in world.QueryScript())
//{
// if (!component.Enable)
// {
// continue;
// }
// component.LateUpdate();
//}
world.Dispose();
}
}
public class TestSystem : SystemBase
{
mesh.index += 1;
});
public override void OnUpdate()
{
foreach (var (entity, transform) in World.Query<Transform>().WithAny<Mesh>())
{
Console.WriteLine($"Entity {entity.ID}: Transform Position = {transform.ValueRO.position}");
}
}
}
world.RemoveEntity(entity2);
var entity4 = world.CreateEntity();
world.AddComponent(entity4, new Transform { position = new Vector3(10, 11, 12) });
world.AddComponent(entity4, new Mesh { index = 44 });
world.Query<Transform, Mesh>((Entity entity, ref Transform transform, ref Mesh mesh) =>
{
Console.WriteLine($"Entity {entity.ID}: Transform Position = {transform.position}, Mesh Index = {mesh.index}");
});
world.Dispose();
public struct Transform : IComponent
public struct Transform : IComponentData
{
public Vector3 position;
public Quaternion rotation;
public Vector3 scale;
}
public struct Mesh : IComponent
public struct Mesh : IComponentData
{
public uint index;
}
public class UserScript : ScriptComponent
{
public override int ExecutionOrder => -1;
public override void Start()
{
Console.WriteLine("UserScript started for entity: " + Owner.ID);
}
public override void Update()
{
Console.WriteLine("UserScript updating for entity: " + Owner.ID);
}
public override void OnDestroy()
{
Console.WriteLine("UserScript destroyed for entity: " + Owner.ID);
}
}
public class UIManager : ScriptComponent
{
public override void Start()
{
Console.WriteLine("UIManager started for entity: " + Owner.ID);
}
public override void Update()
{
Console.WriteLine("UIManager updating for entity: " + Owner.ID);
}
public override void OnDestroy()
{
Console.WriteLine("UIManager destroyed for entity: " + Owner.ID);
}
}
public class EventManager : ScriptComponent
{
public override void Start()
{
Console.WriteLine("EventManager started for entity: " + Owner.ID);
}
public override void Update()
{
Console.WriteLine("EventManager updating for entity: " + Owner.ID);
}
public override void OnDestroy()
{
Console.WriteLine("EventManager destroyed for entity: " + Owner.ID);
}
}