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.
167 lines
4.3 KiB
C#
167 lines
4.3 KiB
C#
using Ghost.Entities;
|
|
using Ghost.Entities.Helpers;
|
|
using System.Numerics;
|
|
|
|
var t = new Test();
|
|
t.Run();
|
|
|
|
public partial class Test
|
|
{
|
|
public void Run()
|
|
{
|
|
var world = World.Create();
|
|
|
|
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
|
|
{
|
|
public override void OnUpdate()
|
|
{
|
|
foreach (var (entity, transform) in World.Query<Transform>().WithAny<Mesh>())
|
|
{
|
|
Console.WriteLine($"Entity {entity.ID}: Transform Position = {transform.ValueRO.position}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct Transform : IComponentData
|
|
{
|
|
public Vector3 position;
|
|
public Quaternion rotation;
|
|
public Vector3 scale;
|
|
}
|
|
|
|
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);
|
|
}
|
|
} |