2019-08-22 22:20:10 +00:00
|
|
|
using System.Linq;
|
2019-06-17 18:33:38 +00:00
|
|
|
using System;
|
2019-06-14 03:28:26 +00:00
|
|
|
using System.Collections.Generic;
|
2019-08-22 22:20:10 +00:00
|
|
|
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-06-20 17:46:15 +00:00
|
|
|
private readonly Dictionary<Guid, Entity> IDToEntity = new Dictionary<Guid, Entity>();
|
2019-06-14 03:28:26 +00:00
|
|
|
|
2019-07-15 03:43:24 +00:00
|
|
|
private readonly HashSet<Guid> entitiesMarkedForDestroy = new HashSet<Guid>();
|
2019-06-19 23:13:02 +00:00
|
|
|
|
2019-06-20 17:46:15 +00:00
|
|
|
private readonly ComponentManager componentManager;
|
2019-08-01 23:24:57 +00:00
|
|
|
private readonly ComponentMessageManager componentMessageManager;
|
2019-06-14 03:28:26 +00:00
|
|
|
|
2019-08-01 23:24:57 +00:00
|
|
|
public EntityManager(ComponentManager componentManager, ComponentMessageManager componentMessageManager)
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-06-14 03:28:26 +00:00
|
|
|
this.componentManager = componentManager;
|
2019-08-01 23:24:57 +00:00
|
|
|
this.componentMessageManager = componentMessageManager;
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
public Entity CreateEntity()
|
|
|
|
{
|
2019-06-19 23:13:02 +00:00
|
|
|
var id = NextID();
|
2019-07-17 18:24:21 +00:00
|
|
|
var entity = new Entity(id);
|
2019-06-19 23:13:02 +00:00
|
|
|
IDToEntity[id] = entity;
|
2019-07-13 00:37:31 +00:00
|
|
|
componentManager.RegisterEntity(id);
|
2019-08-02 06:09:41 +00:00
|
|
|
componentMessageManager.RegisterEntity(entity);
|
2019-06-19 23:13:02 +00:00
|
|
|
return entity;
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 00:37:31 +00:00
|
|
|
public bool EntityExists(Guid id)
|
|
|
|
{
|
|
|
|
return IDToEntity.ContainsKey(id);
|
|
|
|
}
|
|
|
|
|
2019-06-17 18:33:38 +00:00
|
|
|
public Entity GetEntity(Guid id)
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-08-22 22:20:10 +00:00
|
|
|
if (IDToEntity.ContainsKey(id))
|
|
|
|
{
|
|
|
|
return IDToEntity[id];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new EntityNotFoundException("Entity with ID {0} does not exist.", id);
|
|
|
|
}
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:13:02 +00:00
|
|
|
public void MarkForDestroy(Guid entityID)
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-06-19 23:13:02 +00:00
|
|
|
entitiesMarkedForDestroy.Add(entityID);
|
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-06-19 23:13:02 +00:00
|
|
|
foreach (var entityID in entitiesMarkedForDestroy)
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-08-01 23:24:57 +00:00
|
|
|
componentMessageManager.RegisterDestroyedEntity(GetEntity(entityID));
|
2019-07-18 01:12:29 +00:00
|
|
|
componentManager.MarkAllComponentsOnEntityForRemoval(entityID);
|
2019-06-19 23:13:02 +00:00
|
|
|
IDToEntity.Remove(entityID);
|
2019-06-20 03:37:46 +00:00
|
|
|
componentManager.RegisterDestroyedEntity(entityID);
|
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
|
|
|
}
|
|
|
|
|
2019-06-17 18:33:38 +00:00
|
|
|
private Guid NextID()
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-06-17 18:33:38 +00:00
|
|
|
return Guid.NewGuid();
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|