Files
GhostEngine/Ghost.Entities.Test/ArcEntityTest.cs
Misaki 224b2b2dd5 Refactor ECS framework and improve performance
Refactored `ArcEntityTest` to use updated `Transform` and `Mesh` components, improving query logic with `GetChunkIterator` and introducing `ForEach` methods for better modularity.

Enhanced `Chunk` and `Archetype` structs with `readonly` properties and memory optimizations. Fixed bugs in memory copy logic and entity relocation.

Improved `EntityManager` with proper disposal handling, added a destructor, and fixed pointer usage in `AddComponent` and `SetComponentData`.

Refactored `EntityQuery` to use `ChunkIterator` and `ChunkView` for better abstraction. Simplified `EntityQueryMask` logic for performance.

Introduced templated `ForEach` methods and `ForEachWithEntity` methods, dynamically generated using T4 templates for scalability.

Added disposal logic for archetypes and queries in `World`. Updated `Program.cs` to include memory debugging setup.

Integrated T4 templates for dynamic code generation and added helper functions for template generation. Updated project file to include templates and generated outputs.

General improvements include enforcing immutability, optimizing memory management, and adding debugging/logging for better traceability.
2025-12-05 00:29:12 +09:00

57 lines
1.4 KiB
C#

using Ghost.Test.Core;
using Misaki.HighPerformance.Mathematics;
namespace Ghost.Entities.Test;
public partial class ArcEntityTest : ITest
{
private World _world = null!;
public void Setup()
{
_world = World.Create();
}
public void Run()
{
var entity1 = _world.EntityManager.CreateEntity(ComponentTypeID<Transform>.value);
Console.WriteLine(entity1);
_world.EntityManager.AddComponent<Mesh>(entity1, new Mesh { index = 1 });
var queryID = new QueryBuilder().WithAll<Transform>().Build(_world);
ref var query = ref _world.GetEntityQueryReference(queryID);
foreach (var chunk in query.GetChunkIterator())
{
var transforms = chunk.GetComponentData<Transform>();
var entities = chunk.GetEntities();
for (var i = 0; i < chunk.Count; i++)
{
Console.WriteLine($"Entity {entities[i]} Position: {transforms[i].position}");
transforms[i].position = new float3(1, 2, 3);
}
}
query.ForEach<Transform>((e, ref t) =>
{
Console.WriteLine($"Entity {e} Updated Position: {t.position}");
});
}
public void Cleanup()
{
_world.Dispose();
}
}
public struct Transform : IComponent
{
public float3 position;
}
public struct Mesh : IComponent
{
public int index;
}