diff --git a/src/Snapshot.cs b/src/Snapshot.cs index 4537e14..25f9bad 100644 --- a/src/Snapshot.cs +++ b/src/Snapshot.cs @@ -15,11 +15,11 @@ public class Snapshot : IDisposable private List RelationSnapshots = new List(); - private Dictionary> EntityRelationIndex = - new Dictionary>(); + private List> EntityRelationIndex = + new List>(); - private Dictionary> EntityComponentIndex = - new Dictionary>(); + private List> EntityComponentIndex = + new List>(); private List EntityTags = new List(); @@ -76,32 +76,36 @@ public class Snapshot : IDisposable // restore entity relation index state // FIXME: arghhhh this is so slow - foreach (var (id, relationTypeSet) in world.EntityRelationIndex) + foreach (var relationTypeSet in world.EntityRelationIndex) { relationTypeSet.Clear(); } - foreach (var (id, relationTypeSet) in EntityRelationIndex) + for (var i = 0; i < EntityRelationIndex.Count; i += 1) { + var relationTypeSet = EntityRelationIndex[i]; + foreach (var typeId in relationTypeSet) { - world.EntityRelationIndex[id].Add(typeId); + world.EntityRelationIndex[i].Add(typeId); } } // restore entity component index state // FIXME: arrghghhh this is so slow - foreach (var (id, componentTypeSet) in world.EntityComponentIndex) + foreach (var componentTypeSet in world.EntityComponentIndex) { componentTypeSet.Clear(); } - foreach (var (id, componentTypeSet) in EntityComponentIndex) + for (var i = 0; i < EntityComponentIndex.Count; i += 1) { + var componentTypeSet = EntityComponentIndex[i]; + foreach (var typeId in componentTypeSet) { - world.EntityComponentIndex[id].Add(typeId); + world.EntityComponentIndex[i].Add(typeId); } } @@ -145,37 +149,39 @@ public class Snapshot : IDisposable RelationSnapshots[i].Take(world.RelationIndex[i]); } + // fill in missing index structures + + for (var i = EntityComponentIndex.Count; i < world.EntityComponentIndex.Count; i += 1) + { + EntityComponentIndex.Add(new IndexableSet()); + } + + for (var i = EntityRelationIndex.Count; i < world.EntityRelationIndex.Count; i += 1) + { + EntityRelationIndex.Add(new IndexableSet()); + } + // copy entity relation index // FIXME: arghhhh this is so slow - foreach (var (id, relationTypeSet) in world.EntityRelationIndex) + for (var i = 0; i < world.EntityRelationIndex.Count; i += 1) { - if (!EntityRelationIndex.ContainsKey(id)) - { - EntityRelationIndex.Add(id, new IndexableSet()); - } + EntityRelationIndex[i].Clear(); - EntityRelationIndex[id].Clear(); - - foreach (var typeId in relationTypeSet) + foreach (var typeId in world.EntityRelationIndex[i]) { - EntityRelationIndex[id].Add(typeId); + EntityRelationIndex[i].Add(typeId); } } // copy entity component index // FIXME: arghhhh this is so slow - foreach (var (id, componentTypeSet) in world.EntityComponentIndex) + for (var i = 0; i < world.EntityComponentIndex.Count; i += 1) { - if (!EntityComponentIndex.ContainsKey(id)) - { - EntityComponentIndex.Add(id, new IndexableSet()); - } + EntityComponentIndex[i].Clear(); - EntityComponentIndex[id].Clear(); - - foreach (var typeId in componentTypeSet) + foreach (var typeId in world.EntityComponentIndex[i]) { - EntityComponentIndex[id].Add(typeId); + EntityComponentIndex[i].Add(typeId); } } @@ -346,12 +352,12 @@ public class Snapshot : IDisposable relationSnapshot.Dispose(); } - foreach (var componentSet in EntityComponentIndex.Values) + foreach (var componentSet in EntityComponentIndex) { componentSet.Dispose(); } - foreach (var relationSet in EntityRelationIndex.Values) + foreach (var relationSet in EntityRelationIndex) { relationSet.Dispose(); } diff --git a/src/World.cs b/src/World.cs index 9ef9ff7..753d0b7 100644 --- a/src/World.cs +++ b/src/World.cs @@ -26,7 +26,7 @@ public class World : IDisposable // Relation Storages internal List RelationIndex = new List(); - internal Dictionary> EntityRelationIndex = new Dictionary>(); + internal List> EntityRelationIndex = new List>(); // Message Storages private List MessageIndex = new List(); @@ -34,7 +34,7 @@ public class World : IDisposable public FilterBuilder FilterBuilder => new FilterBuilder(this); internal readonly List ComponentIndex = new List(); - internal Dictionary> EntityComponentIndex = new Dictionary>(); + internal List> EntityComponentIndex = new List>(); internal IdAssigner EntityIdAssigner = new IdAssigner(); @@ -97,10 +97,10 @@ public class World : IDisposable { var entity = new Entity(EntityIdAssigner.Assign()); - if (!EntityComponentIndex.ContainsKey(entity)) + if (entity.ID == EntityComponentIndex.Count) { - EntityRelationIndex.Add(entity, new IndexableSet()); - EntityComponentIndex.Add(entity, new IndexableSet()); + EntityRelationIndex.Add(new IndexableSet()); + EntityComponentIndex.Add(new IndexableSet()); EntityTags.Add(tag); } @@ -119,8 +119,11 @@ public class World : IDisposable public void Destroy(in Entity entity) { + var componentSet = EntityComponentIndex[(int) entity.ID]; + var relationSet = EntityRelationIndex[(int) entity.ID]; + // remove all components from storages - foreach (var componentTypeIndex in EntityComponentIndex[entity]) + foreach (var componentTypeIndex in componentSet) { var componentStorage = ComponentIndex[componentTypeIndex]; componentStorage.Remove(entity); @@ -132,14 +135,14 @@ public class World : IDisposable } // remove all relations from storage - foreach (var relationTypeIndex in EntityRelationIndex[entity]) + foreach (var relationTypeIndex in relationSet) { var relationStorage = RelationIndex[relationTypeIndex]; relationStorage.RemoveEntity(entity); } - EntityComponentIndex[entity].Clear(); - EntityRelationIndex[entity].Clear(); + componentSet.Clear(); + relationSet.Clear(); // recycle ID EntityIdAssigner.Unassign(entity.ID); @@ -155,7 +158,7 @@ public class World : IDisposable internal bool Has(in Entity entity, in TypeId typeId) { - return EntityComponentIndex[entity].Contains(typeId); + return EntityComponentIndex[(int) entity.ID].Contains(typeId); } public bool Some() where T : unmanaged @@ -188,7 +191,7 @@ public class World : IDisposable if (!componentStorage.Set(entity, component)) { - EntityComponentIndex[entity].Add(componentStorage.TypeId); + EntityComponentIndex[(int) entity.ID].Add(componentStorage.TypeId); foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId]) { @@ -203,7 +206,7 @@ public class World : IDisposable if (componentStorage.Remove(entity)) { - EntityComponentIndex[entity].Remove(componentStorage.TypeId); + EntityComponentIndex[(int) entity.ID].Remove(componentStorage.TypeId); foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId]) { @@ -235,8 +238,8 @@ public class World : IDisposable { var relationStorage = GetRelationStorage(); relationStorage.Set(entityA, entityB, relation); - EntityRelationIndex[entityA].Add(new TypeId(RelationTypeIdAssigner.Id)); - EntityRelationIndex[entityB].Add(new TypeId(RelationTypeIdAssigner.Id)); + EntityRelationIndex[(int) entityA.ID].Add(new TypeId(RelationTypeIdAssigner.Id)); + EntityRelationIndex[(int) entityB.ID].Add(new TypeId(RelationTypeIdAssigner.Id)); } public void Unrelate(in Entity entityA, in Entity entityB) where T : unmanaged @@ -388,7 +391,7 @@ public class World : IDisposable #if DEBUG public ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity) { - return new ComponentTypeEnumerator(this, EntityComponentIndex[entity]); + return new ComponentTypeEnumerator(this, EntityComponentIndex[(int) entity.ID]); } public IEnumerable Debug_GetEntities(Type componentType) @@ -457,14 +460,14 @@ public class World : IDisposable messageStorage.Dispose(); } - foreach (var typeSet in EntityComponentIndex.Values) + foreach (var componentSet in EntityComponentIndex) { - typeSet.Dispose(); + componentSet.Dispose(); } - foreach (var typeSet in EntityRelationIndex.Values) + foreach (var relationSet in EntityRelationIndex) { - typeSet.Dispose(); + relationSet.Dispose(); } foreach (var filter in FilterIndex.Values)