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
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
private readonly int _entityCapacity;
|
|
|
|
|
private readonly IDManager _idManager = new IDManager();
|
|
|
|
|
private readonly HashSet<int> _ids = new HashSet<int>();
|
2020-03-17 02:00:42 +00:00
|
|
|
|
|
2020-03-20 07:09:57 +00:00
|
|
|
|
private readonly HashSet<int> _entitiesMarkedForDestroy = new HashSet<int>();
|
2019-06-19 23:13:02 +00:00
|
|
|
|
|
2020-03-20 07:09:57 +00:00
|
|
|
|
private readonly ComponentManager _componentManager;
|
2019-06-14 03:28:26 +00:00
|
|
|
|
|
2019-12-29 21:54:08 +00:00
|
|
|
|
public IEnumerable<int> EntityIDs
|
2019-12-22 09:15:58 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
get { return _ids; }
|
2019-12-22 09:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 20:52:38 +00:00
|
|
|
|
public EntityManager(ComponentManager componentManager, int entityCapacity)
|
2019-06-16 01:05:56 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
_componentManager = componentManager;
|
|
|
|
|
_entityCapacity = entityCapacity;
|
2019-12-20 20:52:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int NextID()
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
return _idManager.NextID();
|
2019-06-14 03:28:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
|
public Entity CreateEntity()
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
if (_ids.Count < _entityCapacity)
|
2019-12-20 20:52:38 +00:00
|
|
|
|
{
|
|
|
|
|
var id = NextID();
|
|
|
|
|
var entity = new Entity(id);
|
2020-03-20 07:09:57 +00:00
|
|
|
|
_ids.Add(id);
|
2019-12-20 20:52:38 +00:00
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
throw new EntityOverflowException("The number of entities has exceeded the entity capacity of {0}", _entityCapacity);
|
2019-12-20 20:52:38 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
return _ids.Contains(id);
|
2019-06-14 03:28:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 02:45:49 +00:00
|
|
|
|
public Entity GetEntity(int id)
|
|
|
|
|
{
|
|
|
|
|
if (!EntityExists(id))
|
|
|
|
|
{
|
|
|
|
|
throw new Encompass.Exceptions.EntityNotFoundException("Entity with id {0} does not exist.", id);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 21:54:08 +00:00
|
|
|
|
return new Entity(id);
|
2019-12-24 02:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 21:54:08 +00:00
|
|
|
|
public void MarkForDestroy(int entityID)
|
2019-06-16 01:05:56 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
_entitiesMarkedForDestroy.Add(entityID);
|
2019-06-14 03:28:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 00:16:21 +00:00
|
|
|
|
public void DestroyMarkedEntities(IEnumerable<Engine> engines)
|
2019-06-16 01:05:56 +00:00
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
foreach (var entityID in _entitiesMarkedForDestroy)
|
2019-06-16 01:05:56 +00:00
|
|
|
|
{
|
2019-12-29 21:54:08 +00:00
|
|
|
|
foreach (var engine in engines) { engine.RegisterDestroyedEntity(entityID); }
|
2020-03-20 07:09:57 +00:00
|
|
|
|
_componentManager.MarkAllComponentsOnEntityForRemoval(entityID);
|
|
|
|
|
_ids.Remove(entityID);
|
|
|
|
|
_idManager.Free(entityID);
|
2019-06-14 03:28:26 +00:00
|
|
|
|
}
|
2019-06-19 23:13:02 +00:00
|
|
|
|
|
2020-03-20 07:09:57 +00:00
|
|
|
|
_entitiesMarkedForDestroy.Clear();
|
2019-06-14 03:28:26 +00:00
|
|
|
|
}
|
2020-03-18 00:40:11 +00:00
|
|
|
|
|
|
|
|
|
// NOTE: this is very suboptimal
|
|
|
|
|
public void PruneEmptyEntities()
|
|
|
|
|
{
|
|
|
|
|
foreach (var id in EntityIDs)
|
|
|
|
|
{
|
2020-03-20 07:09:57 +00:00
|
|
|
|
if (_componentManager.UpToDateEntityIsEmpty(id))
|
2020-03-18 00:40:11 +00:00
|
|
|
|
{
|
|
|
|
|
MarkForDestroy(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-14 03:28:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|