entity and component type index

pull/5/head
Evan Hemsley 2019-07-31 12:07:13 -07:00
parent 186ea66c7c
commit ef7847ecf0
1 changed files with 15 additions and 6 deletions

View File

@ -14,6 +14,8 @@ namespace Encompass
private readonly Dictionary<Guid, HashSet<Guid>> entityIDToComponentIDs = new Dictionary<Guid, HashSet<Guid>>(); private readonly Dictionary<Guid, HashSet<Guid>> entityIDToComponentIDs = new Dictionary<Guid, HashSet<Guid>>();
private readonly Dictionary<Guid, Guid> componentIDToEntityID = new Dictionary<Guid, Guid>(); private readonly Dictionary<Guid, Guid> componentIDToEntityID = new Dictionary<Guid, Guid>();
private readonly Dictionary<Guid, Dictionary<Type, HashSet<Guid>>> entityIDToComponentTypeToComponentIDs = new Dictionary<Guid, Dictionary<Type, HashSet<Guid>>>();
private readonly Dictionary<Type, HashSet<Guid>> typeToComponentIDs = new Dictionary<Type, HashSet<Guid>>(); private readonly Dictionary<Type, HashSet<Guid>> typeToComponentIDs = new Dictionary<Type, HashSet<Guid>>();
private readonly HashSet<Guid> componentsMarkedForRemoval = new HashSet<Guid>(); private readonly HashSet<Guid> componentsMarkedForRemoval = new HashSet<Guid>();
@ -38,6 +40,7 @@ namespace Encompass
internal void RegisterEntity(Guid entityID) internal void RegisterEntity(Guid entityID)
{ {
entityIDToComponentIDs.Add(entityID, new HashSet<Guid>()); entityIDToComponentIDs.Add(entityID, new HashSet<Guid>());
entityIDToComponentTypeToComponentIDs.Add(entityID, new Dictionary<Type, HashSet<Guid>>());
} }
internal Guid NextID() internal Guid NextID()
@ -58,6 +61,11 @@ namespace Encompass
typeToComponentIDs[typeof(TComponent)].Add(componentID); typeToComponentIDs[typeof(TComponent)].Add(componentID);
entityIDToComponentIDs[entity.ID].Add(componentID); entityIDToComponentIDs[entity.ID].Add(componentID);
if (!entityIDToComponentTypeToComponentIDs[entity.ID].ContainsKey(typeof(TComponent))) {
entityIDToComponentTypeToComponentIDs[entity.ID].Add(typeof(TComponent), new HashSet<Guid>());
}
entityIDToComponentTypeToComponentIDs[entity.ID][typeof(TComponent)].Add(componentID);
componentIDToEntityID[componentID] = entity.ID; componentIDToEntityID[componentID] = entity.ID;
entitiesWithAddedComponents.Add(entity.ID); entitiesWithAddedComponents.Add(entity.ID);
@ -100,16 +108,16 @@ namespace Encompass
internal IEnumerable<ValueTuple<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent internal IEnumerable<ValueTuple<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent
{ {
var entityComponentsByType = GetComponentsByEntity(entityID).Where((pair) => componentIDToType[pair.Item1] == typeof(TComponent)).Select((pair) => new ValueTuple<Guid, TComponent>(pair.Item1, (TComponent)pair.Item2)); return entityIDToComponentTypeToComponentIDs[entityID].ContainsKey(typeof(TComponent)) ?
var activeComponentsByType = GetComponentsByType<TComponent>(); entityIDToComponentTypeToComponentIDs[entityID][typeof(TComponent)].Select(id => (id, (TComponent)GetComponentByID(id))) :
return activeComponentsByType.Select((triple) => (triple.Item2, triple.Item3)).Intersect(entityComponentsByType); Enumerable.Empty<(Guid, TComponent)>();
} }
internal IEnumerable<ValueTuple<Guid, IComponent>> GetComponentsByEntityAndType(Guid entityID, Type type) internal IEnumerable<ValueTuple<Guid, IComponent>> GetComponentsByEntityAndType(Guid entityID, Type type)
{ {
var entityComponents = GetComponentsByEntity(entityID); return entityIDToComponentTypeToComponentIDs[entityID].ContainsKey(type) ?
var activeComponentsByType = GetComponentsByType(type); entityIDToComponentTypeToComponentIDs[entityID][type].Select(id => (id, GetComponentByID(id))) :
return entityComponents.Intersect(activeComponentsByType); Enumerable.Empty<(Guid, IComponent)>();
} }
internal IEnumerable<Type> GetAllComponentTypesOfEntity(Guid entityID) internal IEnumerable<Type> GetAllComponentTypesOfEntity(Guid entityID)
@ -209,6 +217,7 @@ namespace Encompass
public void RegisterDestroyedEntity(Guid entityID) public void RegisterDestroyedEntity(Guid entityID)
{ {
entityIDToComponentIDs.Remove(entityID); entityIDToComponentIDs.Remove(entityID);
entityIDToComponentTypeToComponentIDs.Remove(entityID);
} }
} }
} }