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(); } }
/// <summary> /// Serializes all entities matching the specified filter /// </summary> /// <param name="filter">Filter</param> public byte[] Serialize(EcsFilter filter) { IEcsGroup group = Filter(filter); byte[] data; using (BinaryDataWriter writer = new BinaryDataWriter()) { foreach (IEcsArchetype archetype in group) { byte[] indices = archetype.Indices; for (int i = 0; i < archetype.EntitiesCount; i++) { IEcsEntity entity = archetype[i]; BinaryDataWriter entityWriter = writer.TryWriteNode(sizeof(uint)); foreach (byte index in indices) { CompositeBinarySerializer ser = Serializer.GetSerializer(EcsTypeManager.Types[index]); BinaryDataWriter componentWriter = entityWriter.TryWriteNode(sizeof(byte)); ser.Serialize(archetype.GetComponentPool(index).Get(i), componentWriter); entityWriter.WriteByte(index); componentWriter.PushNode(); } writer.WriteUInt(entity.Id); entityWriter.PushNode(); } } data = writer.GetData(); } return(data); }
public void AllFilterTest() { List <IEcsEntity> entities = new List <IEcsEntity> { _entityABD, _entityBD0, _entityBD1, _entityBC, _entityAB }; IEcsGroup group = _world.Filter(new EcsFilter().AllOf <ComponentB>()); Assert.AreEqual(entities.Count, group.CalculateCount()); group.ForEach((IEcsEntity entity, ComponentB compB) => { Assert.IsTrue(entities.Contains(entity)); }); entities = new List <IEcsEntity> { _entityABD, _entityBD0, _entityBD1 }; group = _world.Filter(new EcsFilter().AllOf <ComponentB, ComponentD>()); Assert.AreEqual(3, group.CalculateCount()); group.ForEach((IEcsEntity entity, ComponentB compB, ComponentD compD) => { Assert.IsTrue(entities.Contains(entity)); }); }
public void AllAnyFilterTest() { List <IEcsEntity> entities = new List <IEcsEntity> { _entityABD }; IEcsGroup group = _world.Filter(new EcsFilter().AllOf <ComponentB, ComponentB, ComponentD>().AnyOf <ComponentA>()); Assert.AreEqual(entities.Count, group.CalculateCount()); group.ForEach(entity => { Assert.IsTrue(entities.Contains(entity)); }); entities = new List <IEcsEntity> { _entityABD, _entityBD0, _entityBD1 }; group = _world.Filter(new EcsFilter().AllOf <ComponentD, ComponentD>().AnyOf <ComponentB, ComponentC, ComponentC>()); Assert.AreEqual(entities.Count, group.CalculateCount()); group.ForEach(entity => { Assert.IsTrue(entities.Contains(entity)); }); }
public void NoneFilterTest() { List <IEcsEntity> entities = new List <IEcsEntity> { _entityAC }; IEcsGroup group = _world.Filter(new EcsFilter().NoneOf <ComponentB, ComponentD>()); Assert.AreEqual(entities.Count, group.CalculateCount()); group.ForEach(entity => { Assert.IsTrue(entities.Contains(entity)); }); group = _world.Filter(new EcsFilter().NoneOf <ComponentB, ComponentD, ComponentB>()); Assert.AreEqual(entities.Count, group.CalculateCount()); group.ForEach(entity => { Assert.IsTrue(entities.Contains(entity)); }); }
/// <summary> /// Serializes all objects matching the specified filter. Regarding baseline /// </summary> /// <param name="filter">Filter</param> /// <param name="baseline">Baseline</param> public byte[] Serialize(EcsFilter filter, Baseline <uint> baseline) { IEcsGroup group = Filter(filter); byte[] data; using (BinaryDataWriter writer = new BinaryDataWriter()) { List <uint> entitiesBaseKeys = new List <uint>(baseline.BaselineKeys); foreach (IEcsArchetype archetype in group) { byte[] indices = archetype.Indices; for (int i = 0; i < archetype.EntitiesCount; i++) { IEcsEntity entity = archetype[i]; uint entityId = entity.Id; BinaryDataWriter entityWriter = writer.TryWriteNode(sizeof(uint)); Baseline <byte> entityBaseline = baseline.GetOrCreateBaseline <Baseline <byte> >(entityId, 0, out bool entIsNew); List <byte> entityBaseKeys = new List <byte>(entityBaseline.BaselineKeys); foreach (byte index in indices) { CompositeBinarySerializer ser = Serializer.GetSerializer(EcsTypeManager.Types[index]); BinaryDataWriter compWriter = entityWriter.TryWriteNode(sizeof(byte)); Baseline <byte> compBaseline = entityBaseline.GetOrCreateBaseline <Baseline <byte> >(index, ser.Count, out bool compIsNew); ser.Serialize(archetype.GetComponentPool(index).Get(i), compWriter, compBaseline); if (compWriter.Length > 0 || compIsNew) { entityWriter.WriteByte(index); compWriter.PushNode(); } entityBaseKeys.Remove(index); } if (entityBaseKeys.Count > 0) { entityWriter.WriteByte(byte.MaxValue); foreach (byte key in entityBaseKeys) { entityWriter.WriteByte(key); entityBaseline.DestroyBaseline(key); } } if (entityWriter.Length > 0 || entIsNew) { writer.WriteUInt(entityId); entityWriter.PushNode(); } entitiesBaseKeys.Remove(entityId); } } if (entitiesBaseKeys.Count > 0) { writer.WriteUInt(uint.MaxValue); foreach (uint key in entitiesBaseKeys) { writer.WriteUInt(key); baseline.DestroyBaseline(key); } } data = writer.GetData(); } return(data); }