encompass-cs/encompass-cs/ComponentManager.cs

146 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace Encompass
{
internal class ComponentManager
{
private Dictionary<Guid, IComponent> IDToComponent = new Dictionary<Guid, IComponent>();
private Dictionary<Guid, List<Guid>> entityIDToComponentIDs = new Dictionary<Guid, List<Guid>>();
private Dictionary<Guid, Guid> componentIDToEntityID = new Dictionary<Guid, Guid>();
private Dictionary<Type, List<Guid>> activeComponents = new Dictionary<Type, List<Guid>>();
private Dictionary<Type, List<Guid>> inactiveComponents = new Dictionary<Type, List<Guid>>();
private List<Guid> componentsToActivate = new List<Guid>();
private List<Guid> componentsToDeactivate = new List<Guid>();
private List<Guid> componentsToRemove = new List<Guid>();
internal Guid AddComponent<TComponent>(Guid entityID, TComponent component) where TComponent : struct, IComponent
{
var componentID = Guid.NewGuid();
IDToComponent[componentID] = component;
if (!entityIDToComponentIDs.ContainsKey(entityID))
{
entityIDToComponentIDs.Add(entityID, new List<Guid>());
}
entityIDToComponentIDs[entityID].Add(componentID);
componentIDToEntityID[componentID] = entityID;
if (!activeComponents.ContainsKey(typeof(TComponent)))
{
activeComponents.Add(typeof(TComponent), new List<Guid>());
inactiveComponents.Add(typeof(TComponent), new List<Guid>());
}
MarkForActivation(componentID);
return componentID;
}
internal IEnumerable<KeyValuePair<Guid, IComponent>> GetComponentsByEntity(Guid entityID)
{
return entityIDToComponentIDs[entityID].Select((id) => new KeyValuePair<Guid, IComponent>(id, IDToComponent[id]));
}
internal IEnumerable<KeyValuePair<Guid, TComponent>> GetActiveComponentsByType<TComponent>() where TComponent : struct, IComponent
{
return activeComponents.ContainsKey(typeof(TComponent)) ?
activeComponents[typeof(TComponent)].Select((id) => new KeyValuePair<Guid, TComponent>(id, (TComponent)IDToComponent[id])) :
Enumerable.Empty<KeyValuePair<Guid, TComponent>>();
}
internal KeyValuePair<Guid, TComponent> GetActiveComponentByType<TComponent>() where TComponent : struct, IComponent
{
return GetActiveComponentsByType<TComponent>().Single();
}
internal IEnumerable<KeyValuePair<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent
{
var entity_components = GetComponentsByEntity(entityID).Select((kv) => new KeyValuePair<Guid, TComponent>(kv.Key, (TComponent)kv.Value));
var active_components_by_type = GetActiveComponentsByType<TComponent>();
return entity_components.Intersect(active_components_by_type);
}
internal bool EntityHasComponentOfType<TComponent>(Guid entityID) where TComponent : struct, IComponent
{
return GetComponentsByEntityAndType<TComponent>(entityID).Any();
}
internal void UpdateComponent<TComponent>(Guid componentID, TComponent newComponentValue) where TComponent : struct, IComponent
{
var entityID = componentIDToEntityID[componentID];
IDToComponent[componentID] = newComponentValue;
}
internal void RemoveAllComponentsFromEntity(Guid entityID)
{
var componentIDs = entityIDToComponentIDs[entityID];
foreach (var componentID in componentIDs)
{
var component = IDToComponent[componentID];
activeComponents[component.GetType()].Remove(componentID);
inactiveComponents[component.GetType()].Remove(componentID);
}
entityIDToComponentIDs.Remove(entityID);
}
internal void MarkForActivation(Guid componentID)
{
componentsToActivate.Add(componentID);
}
internal void MarkForDeactivation(Guid componentID)
{
componentsToDeactivate.Add(componentID);
}
internal void MarkForRemoval(Guid componentID)
{
componentsToRemove.Add(componentID);
}
internal void ActivateComponents()
{
foreach (var componentID in componentsToActivate)
{
var component = IDToComponent[componentID];
activeComponents[component.GetType()].Add(componentID);
inactiveComponents[component.GetType()].Remove(componentID);
}
componentsToActivate.Clear();
}
internal void DeactivateComponents()
{
foreach (var componentID in componentsToDeactivate)
{
var component = IDToComponent[componentID];
activeComponents[component.GetType()].Remove(componentID);
inactiveComponents[component.GetType()].Add(componentID);
}
componentsToDeactivate.Clear();
}
internal void RemoveComponents()
{
foreach (var componentID in componentsToRemove)
{
var component = IDToComponent[componentID];
activeComponents[component.GetType()].Remove(componentID);
inactiveComponents[component.GetType()].Remove(componentID);
}
componentsToRemove.Clear();
}
}
}