2019-12-20 20:52:38 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Encompass.Exceptions;
|
2019-06-14 03:28:26 +00:00
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
|
namespace Encompass
|
|
|
|
|
{
|
|
|
|
|
internal class EntityManager
|
|
|
|
|
{
|
2019-12-20 20:52:38 +00:00
|
|
|
|
private readonly int entityCapacity;
|
|
|
|
|
private readonly IDManager idManager = new IDManager();
|
2019-12-22 09:15:58 +00:00
|
|
|
|
private readonly Dictionary<int, Entity> IDs = new Dictionary<int, Entity>(32768);
|
2019-06-14 03:28:26 +00:00
|
|
|
|
|
2019-12-06 01:07:54 +00:00
|
|
|
|
private readonly HashSet<Entity> entitiesMarkedForDestroy = new HashSet<Entity>();
|
2019-06-19 23:13:02 +00:00
|
|
|
|
|
2019-06-20 17:46:15 +00:00
|
|
|
|
private readonly ComponentManager componentManager;
|
2019-06-14 03:28:26 +00:00
|
|
|
|
|
2019-12-22 09:15:58 +00:00
|
|
|
|
public IEnumerable<Entity> Entities
|
|
|
|
|
{
|
|
|
|
|
get { return IDs.Values; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 20:52:38 +00:00
|
|
|
|
public EntityManager(ComponentManager componentManager, int entityCapacity)
|
2019-06-16 01:05:56 +00:00
|
|
|
|
{
|
2019-06-14 03:28:26 +00:00
|
|
|
|
this.componentManager = componentManager;
|
2019-12-20 20:52:38 +00:00
|
|
|
|
this.entityCapacity = entityCapacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int NextID()
|
|
|
|
|
{
|
|
|
|
|
return idManager.NextID();
|
2019-06-14 03:28:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
|
public Entity CreateEntity()
|
|
|
|
|
{
|
2019-12-20 20:52:38 +00:00
|
|
|
|
if (IDs.Count < entityCapacity)
|
|
|
|
|
{
|
|
|
|
|
var id = NextID();
|
|
|
|
|
var entity = new Entity(id);
|
2019-12-22 09:15:58 +00:00
|
|
|
|
IDs.Add(id, entity);
|
2019-12-20 20:52:38 +00:00
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new EntityOverflowException("The number of entities has exceeded the entity capacity of {0}", entityCapacity);
|
|
|
|
|
}
|
2019-06-14 03:28:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 20:52:38 +00:00
|
|
|
|
public bool EntityExists(int id)
|
2019-07-13 00:37:31 +00:00
|
|
|
|
{
|
2019-12-22 09:15:58 +00:00
|
|
|
|
return IDs.ContainsKey(id);
|
2019-06-14 03:28:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
|
public void MarkForDestroy(Entity entity)
|
2019-06-16 01:05:56 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
entitiesMarkedForDestroy.Add(entity);
|
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-12-05 20:10:33 +00:00
|
|
|
|
foreach (var entity in entitiesMarkedForDestroy)
|
2019-06-16 01:05:56 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
componentManager.MarkAllComponentsOnEntityForRemoval(entity);
|
2019-12-16 10:17:39 +00:00
|
|
|
|
IDs.Remove(entity.ID);
|
2019-12-20 20:52:38 +00:00
|
|
|
|
idManager.Free(entity.ID);
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|