encompass-cs/encompass-cs/EntityManager.cs

64 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
namespace Encompass
{
internal class EntityManager
{
private readonly Dictionary<Guid, Entity> IDToEntity = new Dictionary<Guid, Entity>();
private readonly HashSet<Guid> entitiesMarkedForDestroy = new HashSet<Guid>();
private readonly ComponentManager componentManager;
private readonly ComponentMessageManager componentMessageManager;
public EntityManager(ComponentManager componentManager, ComponentMessageManager componentMessageManager)
{
this.componentManager = componentManager;
this.componentMessageManager = componentMessageManager;
}
public Entity CreateEntity()
{
var id = NextID();
var entity = new Entity(id);
IDToEntity[id] = entity;
componentManager.RegisterEntity(id);
return entity;
}
public bool EntityExists(Guid id)
{
return IDToEntity.ContainsKey(id);
}
public Entity GetEntity(Guid id)
{
return IDToEntity[id];
}
public void MarkForDestroy(Guid entityID)
{
entitiesMarkedForDestroy.Add(entityID);
}
public void DestroyMarkedEntities()
{
foreach (var entityID in entitiesMarkedForDestroy)
{
componentMessageManager.RegisterDestroyedEntity(GetEntity(entityID));
componentManager.MarkAllComponentsOnEntityForRemoval(entityID);
IDToEntity.Remove(entityID);
componentManager.RegisterDestroyedEntity(entityID);
}
entitiesMarkedForDestroy.Clear();
}
private Guid NextID()
{
return Guid.NewGuid();
}
}
}