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.
This commit is contained in:
@@ -14,24 +14,29 @@ public partial class ArcEntityTest : ITest
|
||||
|
||||
public void Run()
|
||||
{
|
||||
var entity1 = _world.EntityManager.CreateEntity(ComponentTypeID<TransformData>.value);
|
||||
var mesh = new MeshData { index = 1 };
|
||||
_world.EntityManager.AddComponent<MeshData>(entity1, ref mesh);
|
||||
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<TransformData>().Build(_world);
|
||||
var queryID = new QueryBuilder().WithAll<Transform>().Build(_world);
|
||||
ref var query = ref _world.GetEntityQueryReference(queryID);
|
||||
|
||||
foreach (var item in query)
|
||||
foreach (var chunk in query.GetChunkIterator())
|
||||
{
|
||||
var transforms = item.GetComponentData<TransformData>();
|
||||
Console.WriteLine($"Item Count: {item.Count}");
|
||||
for (var i = 0; i < item.Count; i++)
|
||||
var transforms = chunk.GetComponentData<Transform>();
|
||||
var entities = chunk.GetEntities();
|
||||
|
||||
for (var i = 0; i < chunk.Count; i++)
|
||||
{
|
||||
Console.WriteLine($"Entity Position: {transforms[i].position}");
|
||||
Console.WriteLine($"Entity {entities[i]} Position: {transforms[i].position}");
|
||||
transforms[i].position = new float3(1, 2, 3);
|
||||
Console.WriteLine($"Updated Entity Position: {transforms[i].position}");
|
||||
}
|
||||
}
|
||||
|
||||
query.ForEach<Transform>((e, ref t) =>
|
||||
{
|
||||
Console.WriteLine($"Entity {e} Updated Position: {t.position}");
|
||||
});
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
@@ -40,12 +45,12 @@ public partial class ArcEntityTest : ITest
|
||||
}
|
||||
}
|
||||
|
||||
public struct TransformData : IComponent
|
||||
public struct Transform : IComponent
|
||||
{
|
||||
public float3 position;
|
||||
}
|
||||
|
||||
public struct MeshData : IComponent
|
||||
public struct Mesh : IComponent
|
||||
{
|
||||
public int index;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user