encompass-cs/encompass-cs/EntityManager.cs

150 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
namespace Encompass
{
internal class EntityManager
{
private Dictionary<Guid, Entity> IDToEntity = new Dictionary<Guid, Entity>();
private List<Guid> entitiesMarkedForDestroy = new List<Guid>();
private Dictionary<Type, HashSet<IEntityTracker>> componentTypeToEntityTrackers = new Dictionary<Type, HashSet<IEntityTracker>>();
private Dictionary<Guid, HashSet<IEntityTracker>> entityToEntityTrackers = new Dictionary<Guid, HashSet<IEntityTracker>>();
private List<Guid> entitiesWithAddedComponents;
private List<Guid> entitiesWithRemovedComponents;
private ComponentManager componentManager;
public EntityManager(
ComponentManager componentManager,
List<Guid> entitiesWithAddedComponents,
List<Guid> entitiesWithRemovedComponents
)
{
this.componentManager = componentManager;
this.entitiesWithAddedComponents = entitiesWithAddedComponents;
this.entitiesWithRemovedComponents = entitiesWithRemovedComponents;
}
public Entity CreateEntity()
{
var id = NextID();
var entity = new Entity(id, componentManager);
IDToEntity[id] = entity;
return entity;
}
public Entity GetEntity(Guid id)
{
return IDToEntity[id];
}
public void MarkForDestroy(Guid entityID)
{
entitiesMarkedForDestroy.Add(entityID);
}
public void DestroyMarkedEntities()
{
foreach (var entityID in entitiesMarkedForDestroy)
{
var entity = IDToEntity[entityID];
entity.RemoveAllComponents();
IDToEntity.Remove(entityID);
entityToEntityTrackers.Remove(entityID);
}
entitiesMarkedForDestroy.Clear();
}
private Guid NextID()
{
return Guid.NewGuid();
}
public void RegisterEntityTracker(IEntityTracker entityTracker)
{
foreach (var componentType in entityTracker.ComponentTypes)
{
if (!componentTypeToEntityTrackers.ContainsKey(componentType))
{
componentTypeToEntityTrackers.Add(componentType, new HashSet<IEntityTracker>());
}
componentTypeToEntityTrackers[componentType].Add(entityTracker);
}
if (entityTracker is EntityRenderer)
{
var entityRenderer = entityTracker as EntityRenderer;
if (!componentTypeToEntityTrackers.ContainsKey(entityRenderer.DrawComponentType))
{
componentTypeToEntityTrackers.Add(entityRenderer.DrawComponentType, new HashSet<IEntityTracker>());
}
componentTypeToEntityTrackers[entityRenderer.DrawComponentType].Add(entityRenderer);
}
}
public void RegisterDirtyEntityWithAddedComponents(Guid entityID)
{
entitiesWithAddedComponents.Add(entityID);
}
public void RegisterDirtyEntityWithRemovedComponents(Guid entityID)
{
entitiesWithRemovedComponents.Add(entityID);
}
public void CheckEntitiesWithAddedComponents()
{
foreach (var entityID in entitiesWithAddedComponents)
{
CheckAndRegisterEntity(entityID);
}
entitiesWithAddedComponents.Clear();
}
public void CheckEntitiesWithRemovedComponents()
{
foreach (var entityID in entitiesWithRemovedComponents)
{
if (entityToEntityTrackers.ContainsKey(entityID))
{
foreach (var engine in entityToEntityTrackers[entityID])
{
engine.CheckAndUntrackEntity(entityID);
}
}
}
entitiesWithRemovedComponents.Clear();
}
private void CheckAndRegisterEntity(Guid entityID)
{
var componentTypes = componentManager.GetAllComponentTypesOfEntity(entityID);
foreach (var componentType in componentTypes)
{
if (componentTypeToEntityTrackers.ContainsKey(componentType))
{
foreach (var entityTracker in componentTypeToEntityTrackers[componentType])
{
if (entityTracker.CheckAndTrackEntity(entityID))
{
if (!entityToEntityTrackers.ContainsKey(entityID))
{
entityToEntityTrackers.Add(entityID, new HashSet<IEntityTracker>());
}
entityToEntityTrackers[entityID].Add(entityTracker);
}
}
}
}
}
}
}