68 lines
1.7 KiB
C#
68 lines
1.7 KiB
C#
using System.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Encompass.Exceptions;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal class EntityManager
|
|
{
|
|
private readonly Dictionary<Guid, Entity> IDToEntity = new Dictionary<Guid, Entity>(1024);
|
|
|
|
private readonly HashSet<Entity> entitiesMarkedForDestroy = new HashSet<Entity>();
|
|
|
|
private readonly ComponentManager componentManager;
|
|
|
|
public EntityManager(ComponentManager componentManager)
|
|
{
|
|
this.componentManager = componentManager;
|
|
}
|
|
|
|
public Entity CreateEntity()
|
|
{
|
|
var id = NextID();
|
|
var entity = new Entity(id);
|
|
IDToEntity[id] = entity;
|
|
return entity;
|
|
}
|
|
|
|
public bool EntityExists(Guid id)
|
|
{
|
|
return IDToEntity.ContainsKey(id);
|
|
}
|
|
|
|
public Entity GetEntity(Guid id)
|
|
{
|
|
if (IDToEntity.ContainsKey(id))
|
|
{
|
|
return IDToEntity[id];
|
|
}
|
|
else
|
|
{
|
|
throw new EntityNotFoundException("Entity with ID {0} does not exist.", id);
|
|
}
|
|
}
|
|
|
|
public void MarkForDestroy(Entity entity)
|
|
{
|
|
entitiesMarkedForDestroy.Add(entity);
|
|
}
|
|
|
|
public void DestroyMarkedEntities()
|
|
{
|
|
foreach (var entity in entitiesMarkedForDestroy)
|
|
{
|
|
componentManager.MarkAllComponentsOnEntityForRemoval(entity);
|
|
IDToEntity.Remove(entity.ID);
|
|
}
|
|
|
|
entitiesMarkedForDestroy.Clear();
|
|
}
|
|
|
|
private Guid NextID()
|
|
{
|
|
return Guid.NewGuid();
|
|
}
|
|
}
|
|
}
|