entity and component type index
parent
186ea66c7c
commit
ef7847ecf0
|
@ -14,6 +14,8 @@ namespace Encompass
|
|||
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, 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 HashSet<Guid> componentsMarkedForRemoval = new HashSet<Guid>();
|
||||
|
@ -38,6 +40,7 @@ namespace Encompass
|
|||
internal void RegisterEntity(Guid entityID)
|
||||
{
|
||||
entityIDToComponentIDs.Add(entityID, new HashSet<Guid>());
|
||||
entityIDToComponentTypeToComponentIDs.Add(entityID, new Dictionary<Type, HashSet<Guid>>());
|
||||
}
|
||||
|
||||
internal Guid NextID()
|
||||
|
@ -58,6 +61,11 @@ namespace Encompass
|
|||
typeToComponentIDs[typeof(TComponent)].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;
|
||||
|
||||
entitiesWithAddedComponents.Add(entity.ID);
|
||||
|
@ -100,16 +108,16 @@ namespace Encompass
|
|||
|
||||
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));
|
||||
var activeComponentsByType = GetComponentsByType<TComponent>();
|
||||
return activeComponentsByType.Select((triple) => (triple.Item2, triple.Item3)).Intersect(entityComponentsByType);
|
||||
return entityIDToComponentTypeToComponentIDs[entityID].ContainsKey(typeof(TComponent)) ?
|
||||
entityIDToComponentTypeToComponentIDs[entityID][typeof(TComponent)].Select(id => (id, (TComponent)GetComponentByID(id))) :
|
||||
Enumerable.Empty<(Guid, TComponent)>();
|
||||
}
|
||||
|
||||
internal IEnumerable<ValueTuple<Guid, IComponent>> GetComponentsByEntityAndType(Guid entityID, Type type)
|
||||
{
|
||||
var entityComponents = GetComponentsByEntity(entityID);
|
||||
var activeComponentsByType = GetComponentsByType(type);
|
||||
return entityComponents.Intersect(activeComponentsByType);
|
||||
return entityIDToComponentTypeToComponentIDs[entityID].ContainsKey(type) ?
|
||||
entityIDToComponentTypeToComponentIDs[entityID][type].Select(id => (id, GetComponentByID(id))) :
|
||||
Enumerable.Empty<(Guid, IComponent)>();
|
||||
}
|
||||
|
||||
internal IEnumerable<Type> GetAllComponentTypesOfEntity(Guid entityID)
|
||||
|
@ -209,6 +217,7 @@ namespace Encompass
|
|||
public void RegisterDestroyedEntity(Guid entityID)
|
||||
{
|
||||
entityIDToComponentIDs.Remove(entityID);
|
||||
entityIDToComponentTypeToComponentIDs.Remove(entityID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue