slight entity manager optimization

pull/5/head
Evan Hemsley 2019-12-16 02:17:39 -08:00
parent db8964b54c
commit f6d93c7579
3 changed files with 6 additions and 34 deletions

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Reflection; using System.Reflection;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -151,17 +151,6 @@ namespace Encompass
return entityManager.EntityExists(entity.ID); return entityManager.EntityExists(entity.ID);
} }
/// <summary>
/// Returns the Entity with the specified ID.
/// </summary>
/// <exception cref="Encompass.Exceptions.EntityNotFoundException">
/// Thrown when an Entity with the given ID does not exist.
/// </exception>
internal Entity GetEntity(Guid entityID)
{
return entityManager.GetEntity(entityID);
}
/// <summary> /// <summary>
/// Returns an Entity containing the specified Component type. /// Returns an Entity containing the specified Component type.
/// </summary> /// </summary>

View File

@ -1,4 +1,4 @@
using System.Linq; using System.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Encompass.Exceptions; using Encompass.Exceptions;
@ -7,7 +7,7 @@ namespace Encompass
{ {
internal class EntityManager internal class EntityManager
{ {
private readonly Dictionary<Guid, Entity> IDToEntity = new Dictionary<Guid, Entity>(1024); private readonly HashSet<Guid> IDs = new HashSet<Guid>();
private readonly HashSet<Entity> entitiesMarkedForDestroy = new HashSet<Entity>(); private readonly HashSet<Entity> entitiesMarkedForDestroy = new HashSet<Entity>();
@ -22,25 +22,13 @@ namespace Encompass
{ {
var id = NextID(); var id = NextID();
var entity = new Entity(id); var entity = new Entity(id);
IDToEntity[id] = entity; IDs.Add(id);
return entity; return entity;
} }
public bool EntityExists(Guid id) public bool EntityExists(Guid id)
{ {
return IDToEntity.ContainsKey(id); return IDs.Contains(id);
}
public Entity GetEntity(Guid id)
{
if (IDToEntity.ContainsKey(id))
{
return IDToEntity[id];
}
else
{
throw new EntityNotFoundException("Entity with ID {0} does not exist.", id);
}
} }
public void MarkForDestroy(Entity entity) public void MarkForDestroy(Entity entity)
@ -53,7 +41,7 @@ namespace Encompass
foreach (var entity in entitiesMarkedForDestroy) foreach (var entity in entitiesMarkedForDestroy)
{ {
componentManager.MarkAllComponentsOnEntityForRemoval(entity); componentManager.MarkAllComponentsOnEntityForRemoval(entity);
IDToEntity.Remove(entity.ID); IDs.Remove(entity.ID);
} }
entitiesMarkedForDestroy.Clear(); entitiesMarkedForDestroy.Clear();

View File

@ -19,11 +19,6 @@ namespace Encompass
this.componentManager = componentManager; this.componentManager = componentManager;
} }
internal Entity GetEntity(Guid entityID)
{
return entityManager.GetEntity(entityID);
}
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{ {
return ReadComponentsIncludingEntity<TComponent>().Select(tuple => tuple.Item2); return ReadComponentsIncludingEntity<TComponent>().Select(tuple => tuple.Item2);