54 lines
1.3 KiB
C#
54 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal class EntityManager
|
|
{
|
|
private readonly HashSet<Guid> IDs = new HashSet<Guid>();
|
|
|
|
private readonly HashSet<Entity> entitiesMarkedForDestroy = new HashSet<Entity>();
|
|
|
|
private readonly ComponentManager componentManager;
|
|
|
|
public EntityManager(ComponentManager componentManager)
|
|
{
|
|
this.componentManager = componentManager;
|
|
}
|
|
|
|
public Entity CreateEntity()
|
|
{
|
|
var id = NextID();
|
|
var entity = new Entity(id);
|
|
IDs.Add(id);
|
|
return entity;
|
|
}
|
|
|
|
public bool EntityExists(Guid 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);
|
|
}
|
|
|
|
entitiesMarkedForDestroy.Clear();
|
|
}
|
|
|
|
private Guid NextID()
|
|
{
|
|
return Guid.NewGuid();
|
|
}
|
|
}
|
|
}
|