private static void CreateOrDestroyEntities(EcsWorld world, EcsFilter filter, int count, Action <EcsWorld> createEntity) { IEcsGroup group = world.Filter(filter); if (group.CalculateCount() == count) { return; } IEcsEntity[] entities = group.ToEntityArray(); for (int i = entities.Length; i < count; i++) { createEntity(world); } for (int i = count; i < entities.Length; i++) { IEcsEntity entity = entities[i]; BroadphaseRefComponent brRef = entity.GetComponent <BroadphaseRefComponent>(); CharacterComponent character = entity.GetComponent <CharacterComponent>(); Object.Destroy(character.Ref.gameObject); foreach (SAPChunk chunk in brRef.Chunks) { BroadphaseHelper.RemoveFormChunk(chunk, entity.Id); } entity.Destroy(); } }
public void CreateEntityFromProcessingTest() { EcsWorld world = new EcsWorld(); Assert.AreEqual(0, world.EntitiesInProcessing); IEcsEntity entity = world.CreateEntity(new ComponentA(), new ComponentB(), new ComponentC(), new ComponentD()); uint entityId = entity.Id; entity.Destroy(); Assert.AreEqual(1, world.EntitiesInProcessing); IEcsEntity newEntity = world.CreateEntity(new ComponentB()); uint newEntityId = newEntity.Id; Assert.AreEqual(0, world.EntitiesInProcessing); Assert.IsFalse(newEntity.HasComponent <ComponentA>()); Assert.IsTrue(newEntity.HasComponent <ComponentB>()); Assert.IsTrue(newEntityId > entityId); }
public void SerializeUpdateBaselineTest() { Baseline <uint> baseline = new Baseline <uint>(); byte[] data = _world.Serialize(new EcsFilter(), baseline); EcsWorld source = new EcsWorld(); source.Update(data); IEcsEntity entityAB = source[_entityAB.Id]; Assert.AreEqual(_entityAB.GetComponent <ComponentA>(), entityAB.GetComponent <ComponentA>()); Assert.AreEqual(_entityAB.GetComponent <ComponentB>(), entityAB.GetComponent <ComponentB>()); IEcsEntity entityABD = source[_entityABD.Id]; Assert.AreEqual(_entityABD.GetComponent <ComponentA>(), entityABD.GetComponent <ComponentA>()); Assert.AreEqual(_entityABD.GetComponent <ComponentB>(), entityABD.GetComponent <ComponentB>()); Assert.AreEqual(_entityABD.GetComponent <ComponentD>(), entityABD.GetComponent <ComponentD>()); IEcsEntity entityAC = source[_entityAC.Id]; Assert.AreEqual(_entityAC.GetComponent <ComponentA>(), entityAC.GetComponent <ComponentA>()); Assert.AreEqual(_entityAC.GetComponent <ComponentC>(), entityAC.GetComponent <ComponentC>()); IEcsEntity entityAD = source[_entityAD.Id]; Assert.AreEqual(_entityAD.GetComponent <ComponentA>(), entityAD.GetComponent <ComponentA>()); Assert.AreEqual(_entityAD.GetComponent <ComponentD>(), entityAD.GetComponent <ComponentD>()); IEcsEntity entityBC = source[_entityBC.Id]; Assert.AreEqual(_entityBC.GetComponent <ComponentB>(), entityBC.GetComponent <ComponentB>()); Assert.AreEqual(_entityBC.GetComponent <ComponentC>(), entityBC.GetComponent <ComponentC>()); IEcsEntity entityBD0 = source[_entityBD0.Id]; Assert.AreEqual(_entityBD0.GetComponent <ComponentB>(), entityBD0.GetComponent <ComponentB>()); Assert.AreEqual(_entityBD0.GetComponent <ComponentD>(), entityBD0.GetComponent <ComponentD>()); IEcsEntity entityBD1 = source[_entityBD1.Id]; Assert.AreEqual(_entityBD1.GetComponent <ComponentB>(), entityBD1.GetComponent <ComponentB>()); Assert.AreEqual(_entityBD1.GetComponent <ComponentD>(), entityBD1.GetComponent <ComponentD>()); data = source.Serialize(new EcsFilter(), baseline); Assert.AreEqual(0, data.Length); EcsWorld target = new EcsWorld(); // change Component entityAB.GetComponent <ComponentA>().Value = int.MaxValue; data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(1, target.EntitiesCount); Assert.AreEqual(entityAB.GetComponent <ComponentA>(), target[entityAB.Id].GetComponent <ComponentA>()); //removeComponent entityAB.RemoveComponent <ComponentA>(); data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(1, target.EntitiesCount); Assert.IsFalse(target[entityAB.Id].HasComponent <ComponentA>()); //AddComponent entityAB.AddComponent(new ComponentA()); data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(1, target.EntitiesCount); Assert.IsTrue(target[entityAB.Id].HasComponent <ComponentA>()); //remove Entity entityAB.Destroy(); data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(0, target.EntitiesCount); //create Entity entityAB = source.CreateEntity(new ComponentA { Value = 100 }, new ComponentB { Value = 200 }); data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(1, target.EntitiesCount); IEcsEntity targetEntityAB = target[entityAB.Id]; Assert.AreEqual(2, targetEntityAB.ComponentsCount); Assert.AreEqual(entityAB.GetComponent <ComponentA>(), targetEntityAB.GetComponent <ComponentA>()); Assert.AreEqual(entityAB.GetComponent <ComponentB>(), targetEntityAB.GetComponent <ComponentB>()); }