forked from Misaki/GhostEngine
Add managed entity and script component.
Added ManagedEntity and related methods in EntityManager; Added ScriptComponent to write game play logic in oop;
This commit is contained in:
@@ -29,8 +29,12 @@ public partial class EntityTest : ITest
|
||||
|
||||
public void Run()
|
||||
{
|
||||
var entity1 = _world.EntityManager.CreateEntity(ComponentTypeID<Transform>.value);
|
||||
var entity1 = _world.EntityManager.CreateEntity(ComponentTypeID<Transform>.value, ComponentTypeID<ManagedEntityRef>.value);
|
||||
_world.EntityManager.AddComponent(entity1, new Mesh { index = 1 });
|
||||
_world.EntityManager.SetComponent(entity1, new ManagedEntityRef
|
||||
{
|
||||
entity = _world.EntityManager.CreateManagedEntity()
|
||||
});
|
||||
|
||||
var entity2 = _world.EntityManager.CreateEntity(ComponentTypeID<Transform>.value);
|
||||
_world.EntityManager.SetComponent(entity2, new Transform { position = new float3(1, 2, 3) });
|
||||
@@ -38,32 +42,38 @@ public partial class EntityTest : ITest
|
||||
var queryID = new QueryBuilder().WithAll<Transform>().Build(_world);
|
||||
ref var query = ref _world.GetEntityQueryReference(queryID);
|
||||
|
||||
var testJob = new TestEntityQueryJob();
|
||||
var handle = query.ScheduleChunkParallel(testJob, 64, JobHandle.Invalid);
|
||||
_jobScheduler.WaitComplete(handle);
|
||||
// var testJob = new TestEntityQueryJob();
|
||||
// var handle = query.ScheduleChunkParallel(testJob, 64, JobHandle.Invalid);
|
||||
// _jobScheduler.WaitComplete(handle);
|
||||
|
||||
_world.EntityManager.AddScriptComponent<TestScriptComponent>(entity1);
|
||||
_world.EntityManager.RemoveComponent<ManagedEntityRef>(entity1); // This should destory the managed entity and call OnDestroy
|
||||
|
||||
query.ForEach<Transform>((e, ref t) =>
|
||||
{
|
||||
Console.WriteLine($"Entity {e} Has Position: {t.position}");
|
||||
});
|
||||
|
||||
foreach (var (entity, transform) in query.GetEntityComponentIterator<Transform>())
|
||||
{
|
||||
Console.WriteLine($"Entity {entity} Updated Position: {transform.Get().position}");
|
||||
}
|
||||
// foreach (var (entity, transform) in query.GetEntityComponentIterator<Transform>())
|
||||
// {
|
||||
// Console.WriteLine($"Entity {entity} Updated Position: {transform.Get().position}");
|
||||
// }
|
||||
//
|
||||
// foreach (var chunk in query.GetChunkIterator())
|
||||
// {
|
||||
// var transforms = chunk.GetComponentData<Transform>();
|
||||
// var entities = chunk.GetEntities();
|
||||
// var bits = chunk.GetEnableBits<Transform>();
|
||||
//
|
||||
// var it = bits.GetIterator();
|
||||
// while (it.Next(out var index) && index < chunk.Count)
|
||||
// {
|
||||
// Console.WriteLine($"Entity {entities[index]} Updated Position: {transforms[index].position}");
|
||||
// }
|
||||
// }
|
||||
|
||||
foreach (var chunk in query.GetChunkIterator())
|
||||
{
|
||||
var transforms = chunk.GetComponentData<Transform>();
|
||||
var entities = chunk.GetEntities();
|
||||
var bits = chunk.GetEnableBits<Transform>();
|
||||
|
||||
var it = bits.GetIterator();
|
||||
while (it.Next(out var index) && index < chunk.Count)
|
||||
{
|
||||
Console.WriteLine($"Entity {entities[index]} Updated Position: {transforms[index].position}");
|
||||
}
|
||||
}
|
||||
_world.EntityManager.DestroyEntity(entity1);
|
||||
_world.EntityManager.DestroyEntity(entity2);
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
@@ -83,3 +93,18 @@ public struct Mesh : IComponent
|
||||
{
|
||||
public int index;
|
||||
}
|
||||
|
||||
public class TestScriptComponent : ScriptComponent
|
||||
{
|
||||
public override void OnCreate()
|
||||
{
|
||||
Console.WriteLine($"TestScriptComponent OnCreate called for Entity {Entity}");
|
||||
ref var transform = ref GetComponent<Transform>();
|
||||
transform.position += new float3(0, 1, 0);
|
||||
}
|
||||
|
||||
public override void OnDestroy()
|
||||
{
|
||||
Console.WriteLine($"TestScriptComponent OnDestroy called for Entity {Entity}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user