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 unsafe void Update(float deltaTime, EcsWorld world) { BroadphaseSAPComponent bpChunks = world.GetOrCreateSingleton <BroadphaseSAPComponent>(); IEcsEntity[] entities = world.Filter(_entitiesFilter).ToEntityArray(); for (int i = 0; i < entities.Length; i++) { IEcsEntity entity = entities[i]; uint entityId = entity.Id; TransformComponent tr = entity.GetComponent <TransformComponent>(); ColliderComponent col = entity.GetComponent <ColliderComponent>(); RigBodyComponent rig = entity.GetComponent <RigBodyComponent>(); AABB aabb = new AABB(col.Size, tr.Position, col.ColliderType == ColliderType.Rect ? tr.Rotation : 0f); bool isStatic = MathHelper.Equal(rig.InvMass, 0); int layer = col.Layer; List <SAPChunk> chunks = new List <SAPChunk>(4); foreach (int chunkId in BroadphaseHelper.GetChunks(aabb)) { chunks.Add(BroadphaseHelper.GetOrCreateChunk(chunkId, bpChunks)); } BroadphaseRefComponent bpRef = new BroadphaseRefComponent { Chunks = chunks, ChunksHash = BroadphaseHelper.CalculateChunksHash(aabb), AABB = aabb }; entity.AddComponent(bpRef); foreach (SAPChunk chunk in chunks) { if (chunk.Length >= chunk.Items.Length) { Array.Resize(ref chunk.Items, 2 * chunk.Length); fixed(AABB *pAABB = &bpRef.AABB) { chunk.Items[chunk.Length++] = new BroadphaseAABB { AABB = pAABB, Id = entityId, IsStatic = isStatic, Layer = layer, Entity = entity }; } if (!isStatic) chunk.DynamicCounter++; } } } }