2019-06-17 18:33:38 +00:00
|
|
|
using System;
|
2019-06-14 03:28:26 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
namespace Encompass
|
|
|
|
{
|
|
|
|
internal class EntityManager
|
|
|
|
{
|
2019-06-20 17:46:15 +00:00
|
|
|
private readonly Dictionary<Guid, Entity> IDToEntity = new Dictionary<Guid, Entity>();
|
2019-06-14 03:28:26 +00:00
|
|
|
|
2019-06-20 17:46:15 +00:00
|
|
|
private readonly List<Guid> entitiesMarkedForDestroy = new List<Guid>();
|
2019-06-19 23:13:02 +00:00
|
|
|
|
2019-06-20 17:46:15 +00:00
|
|
|
private readonly Dictionary<Type, HashSet<IEntityTracker>> componentTypeToEntityTrackers = new Dictionary<Type, HashSet<IEntityTracker>>();
|
|
|
|
private readonly Dictionary<Guid, HashSet<IEntityTracker>> entityToEntityTrackers = new Dictionary<Guid, HashSet<IEntityTracker>>();
|
2019-06-19 23:13:02 +00:00
|
|
|
|
2019-07-11 01:22:08 +00:00
|
|
|
private readonly HashSet<Guid> entitiesWithAddedComponents;
|
|
|
|
private readonly HashSet<Guid> entitiesWithRemovedComponents;
|
2019-06-14 03:28:26 +00:00
|
|
|
|
2019-06-20 17:46:15 +00:00
|
|
|
private readonly ComponentManager componentManager;
|
2019-06-14 03:28:26 +00:00
|
|
|
|
|
|
|
public EntityManager(
|
2019-06-19 23:13:02 +00:00
|
|
|
ComponentManager componentManager,
|
2019-07-11 01:22:08 +00:00
|
|
|
HashSet<Guid> entitiesWithAddedComponents,
|
|
|
|
HashSet<Guid> entitiesWithRemovedComponents
|
2019-06-16 01:05:56 +00:00
|
|
|
)
|
|
|
|
{
|
2019-06-14 03:28:26 +00:00
|
|
|
this.componentManager = componentManager;
|
2019-06-19 23:13:02 +00:00
|
|
|
this.entitiesWithAddedComponents = entitiesWithAddedComponents;
|
|
|
|
this.entitiesWithRemovedComponents = entitiesWithRemovedComponents;
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
public Entity CreateEntity()
|
|
|
|
{
|
2019-06-19 23:13:02 +00:00
|
|
|
var id = NextID();
|
|
|
|
var entity = new Entity(id, componentManager);
|
|
|
|
IDToEntity[id] = entity;
|
|
|
|
return entity;
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 18:33:38 +00:00
|
|
|
public Entity GetEntity(Guid id)
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-06-19 21:14:44 +00:00
|
|
|
return IDToEntity[id];
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:13:02 +00:00
|
|
|
public void MarkForDestroy(Guid entityID)
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-06-19 23:13:02 +00:00
|
|
|
entitiesMarkedForDestroy.Add(entityID);
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:13:02 +00:00
|
|
|
public void DestroyMarkedEntities()
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-06-19 23:13:02 +00:00
|
|
|
foreach (var entityID in entitiesMarkedForDestroy)
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-06-19 23:13:02 +00:00
|
|
|
var entity = IDToEntity[entityID];
|
2019-06-14 03:28:26 +00:00
|
|
|
entity.RemoveAllComponents();
|
2019-06-19 23:13:02 +00:00
|
|
|
IDToEntity.Remove(entityID);
|
|
|
|
entityToEntityTrackers.Remove(entityID);
|
2019-06-20 03:37:46 +00:00
|
|
|
componentManager.RegisterDestroyedEntity(entityID);
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
2019-06-19 23:13:02 +00:00
|
|
|
|
|
|
|
entitiesMarkedForDestroy.Clear();
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 18:33:38 +00:00
|
|
|
private Guid NextID()
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-06-17 18:33:38 +00:00
|
|
|
return Guid.NewGuid();
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
2019-06-19 23:13:02 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
}
|