encompass-cs/encompass-cs/EntityManager.cs

54 lines
1.3 KiB
C#
Raw Normal View History

2019-12-17 04:40:15 +00:00
using System;
using System.Collections.Generic;
2019-06-16 01:05:56 +00:00
namespace Encompass
{
internal class EntityManager
{
2019-12-16 10:17:39 +00:00
private readonly HashSet<Guid> IDs = new HashSet<Guid>();
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;
public EntityManager(ComponentManager componentManager)
2019-06-16 01:05:56 +00:00
{
this.componentManager = componentManager;
}
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-12-16 10:17:39 +00:00
IDs.Add(id);
2019-06-19 23:13:02 +00:00
return entity;
}
public bool EntityExists(Guid id)
{
2019-12-16 10:17:39 +00:00
return IDs.Contains(id);
}
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-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-06-19 23:13:02 +00:00
entitiesMarkedForDestroy.Clear();
}
private Guid NextID()
2019-06-16 01:05:56 +00:00
{
return Guid.NewGuid();
}
}
}