65 lines
1.7 KiB
C#
65 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
using Encompass.Exceptions;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal class EntityManager
|
|
{
|
|
private readonly int entityCapacity;
|
|
private readonly IDManager idManager = new IDManager();
|
|
private readonly HashSet<int> IDs = new HashSet<int>();
|
|
|
|
private readonly HashSet<Entity> entitiesMarkedForDestroy = new HashSet<Entity>();
|
|
|
|
private readonly ComponentManager componentManager;
|
|
|
|
public EntityManager(ComponentManager componentManager, int entityCapacity)
|
|
{
|
|
this.componentManager = componentManager;
|
|
this.entityCapacity = entityCapacity;
|
|
}
|
|
|
|
private int NextID()
|
|
{
|
|
return idManager.NextID();
|
|
}
|
|
|
|
public Entity CreateEntity()
|
|
{
|
|
if (IDs.Count < entityCapacity)
|
|
{
|
|
var id = NextID();
|
|
var entity = new Entity(id);
|
|
IDs.Add(id);
|
|
return entity;
|
|
}
|
|
else
|
|
{
|
|
throw new EntityOverflowException("The number of entities has exceeded the entity capacity of {0}", entityCapacity);
|
|
}
|
|
}
|
|
|
|
public bool EntityExists(int id)
|
|
{
|
|
return IDs.Contains(id);
|
|
}
|
|
|
|
public void MarkForDestroy(Entity entity)
|
|
{
|
|
entitiesMarkedForDestroy.Add(entity);
|
|
}
|
|
|
|
public void DestroyMarkedEntities()
|
|
{
|
|
foreach (var entity in entitiesMarkedForDestroy)
|
|
{
|
|
componentManager.MarkAllComponentsOnEntityForRemoval(entity);
|
|
IDs.Remove(entity.ID);
|
|
idManager.Free(entity.ID);
|
|
}
|
|
|
|
entitiesMarkedForDestroy.Clear();
|
|
}
|
|
}
|
|
}
|