encompass-cs/encompass-cs/ComponentManager.cs

224 lines
8.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
using Collections.Pooled;
namespace Encompass
{
internal class ComponentManager
{
private readonly DrawLayerManager drawLayerManager;
private readonly Dictionary<Guid, Type> componentIDToType = new Dictionary<Guid, Type>();
private readonly Dictionary<Guid, IComponent> IDToComponent = new Dictionary<Guid, IComponent>();
private readonly Dictionary<Guid, PooledSet<Guid>> entityIDToComponentIDs = new Dictionary<Guid, PooledSet<Guid>>();
private readonly Dictionary<Guid, Guid> componentIDToEntityID = new Dictionary<Guid, Guid>();
private readonly Dictionary<Guid, PooledDictionary<Type, Guid>> entityIDToComponentTypeToComponentID = new Dictionary<Guid, PooledDictionary<Type, Guid>>();
private readonly Dictionary<Type, HashSet<Guid>> typeToComponentIDs = new Dictionary<Type, HashSet<Guid>>();
private readonly List<(Entity, Type, Guid, IComponent)> componentAddData = new List<(Entity, Type, Guid, IComponent)>();
private readonly HashSet<Guid> componentsMarkedForRemoval = new HashSet<Guid>();
private readonly Dictionary<Guid, IComponent> pendingUpdates = new Dictionary<Guid, IComponent>();
public ComponentManager(DrawLayerManager drawLayerManager)
{
this.drawLayerManager = drawLayerManager;
}
internal void RegisterEntity(Guid entityID)
{
entityIDToComponentIDs.Add(entityID, new PooledSet<Guid>());
entityIDToComponentTypeToComponentID.Add(entityID, new PooledDictionary<Type, Guid>());
}
private Guid NextID()
{
return Guid.NewGuid();
}
internal Guid MarkComponentForAdd<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
var id = NextID();
componentAddData.Add((entity, typeof(TComponent), id, component));
IDToComponent[id] = component; // add these here so entity lookup doesnt break on pending components
componentIDToEntityID[id] = entity.ID;
return id;
}
internal Guid MarkDrawComponentForAdd<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent
{
var id = MarkComponentForAdd(entity, component);
drawLayerManager.RegisterComponentWithLayer(id, layer);
return id;
}
internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component)
{
componentIDToType[componentID] = type;
if (!typeToComponentIDs.ContainsKey(type))
{
typeToComponentIDs.Add(type, new HashSet<Guid>());
}
typeToComponentIDs[type].Add(componentID);
entityIDToComponentIDs[entity.ID].Add(componentID);
if (!entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type))
{
entityIDToComponentTypeToComponentID[entity.ID][type] = componentID;
}
else
{
throw new MultipleComponentOfSameTypeException("Entity {0} cannot have multiple components of type {1}", entity.ID, type.Name);
}
}
internal void AddMarkedComponents()
{
foreach (var (entity, type, componentID, component) in componentAddData)
{
AddComponent(entity, type, componentID, component);
}
componentAddData.Clear();
}
internal IEnumerable<Guid> GetComponentIDsByEntityID(Guid entityID)
{
if (entityIDToComponentIDs.TryGetValue(entityID, out PooledSet<Guid> idSet))
{
return idSet;
}
return Enumerable.Empty<Guid>();
}
internal IEnumerable<(Guid, TComponent)> GetComponentsByType<TComponent>() where TComponent : struct, IComponent
{
if (typeToComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
{
return idSet.Select(id => (id, (TComponent)IDToComponent[id]));
}
return Enumerable.Empty<(Guid, TComponent)>();
}
internal (Guid, TComponent) GetComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
{
if (entityIDToComponentTypeToComponentID.ContainsKey(entity.ID) && entityIDToComponentTypeToComponentID[entity.ID].TryGetValue(typeof(TComponent), out Guid id))
{
return (id, (TComponent)IDToComponent[id]);
}
throw new NoComponentOfTypeOnEntityException("No Component of type {0} exists on Entity {1}", typeof(TComponent).Name, entity.ID);
}
internal bool EntityHasComponentOfType<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return (entityIDToComponentTypeToComponentID.ContainsKey(entity.ID) && entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(typeof(TComponent)));
}
internal bool ComponentOfTypeExists<TComponent>() where TComponent : struct, IComponent
{
if (typeToComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
{
return idSet.Count > 0;
}
return false;
}
internal IComponent GetComponentByID(Guid componentID)
{
return IDToComponent[componentID];
}
internal Type GetComponentTypeByID(Guid componentID)
{
return componentIDToType[componentID];
}
internal Guid GetEntityIDByComponentID(Guid componentID)
{
return componentIDToEntityID[componentID];
}
internal void AddUpdateComponentOperation<TComponent>(Guid componentID, TComponent newComponentValue) where TComponent : struct, IComponent
{
if (pendingUpdates.ContainsKey(componentID))
{
throw new RepeatUpdateComponentException("Component {0} with ID {1} was updated multiple times this frame", typeof(TComponent).Name, componentID);
}
pendingUpdates.Add(componentID, newComponentValue);
}
internal void PerformComponentUpdates()
{
foreach (var idPair in pendingUpdates)
{
IDToComponent[idPair.Key] = idPair.Value;
}
pendingUpdates.Clear();
}
internal void MarkAllComponentsOnEntityForRemoval(Guid entityID)
{
foreach (var componentID in GetComponentIDsByEntityID(entityID))
{
MarkForRemoval(componentID);
}
}
internal void MarkForRemoval(Guid componentID)
{
componentsMarkedForRemoval.Add(componentID);
}
internal void RemoveMarkedComponents()
{
foreach (var componentID in componentsMarkedForRemoval)
{
Remove(componentID);
}
componentsMarkedForRemoval.Clear();
}
private void Remove(Guid componentID)
{
var type = componentIDToType[componentID];
var entityID = componentIDToEntityID[componentID];
if (entityIDToComponentIDs.ContainsKey(entityID))
{
entityIDToComponentIDs[entityID].Remove(componentID);
}
if (entityIDToComponentTypeToComponentID.ContainsKey(entityID))
{
entityIDToComponentTypeToComponentID[entityID].Remove(type);
}
IDToComponent.Remove(componentID);
componentIDToType.Remove(componentID);
componentIDToEntityID.Remove(componentID);
typeToComponentIDs[type].Remove(componentID);
drawLayerManager.UnRegisterComponentWithLayer(componentID);
}
public void RegisterDestroyedEntity(Guid entityID)
{
entityIDToComponentIDs[entityID].Dispose();
entityIDToComponentIDs.Remove(entityID);
entityIDToComponentTypeToComponentID[entityID].Dispose();
entityIDToComponentTypeToComponentID.Remove(entityID);
}
}
}