adds EntityExists method + fixes crash when destroying an entity that has no components

pull/5/head
Evan Hemsley 2019-07-12 17:37:31 -07:00
parent 5ec8c4da82
commit 2d929e4665
6 changed files with 61 additions and 6 deletions

View File

@ -37,6 +37,11 @@ namespace Encompass
this.entitiesWithRemovedComponents = entitiesWithRemovedComponents;
}
internal void RegisterEntity(Guid entityID)
{
entityIDToComponentIDs.Add(entityID, new List<Guid>());
}
internal Guid AddComponent<TComponent>(Guid entityID, TComponent component) where TComponent : struct, IComponent
{
var componentID = Guid.NewGuid();
@ -51,11 +56,6 @@ namespace Encompass
typeToComponentIDs[typeof(TComponent)].Add(componentID);
if (!entityIDToComponentIDs.ContainsKey(entityID))
{
entityIDToComponentIDs.Add(entityID, new List<Guid>());
}
entityIDToComponentIDs[entityID].Add(componentID);
componentIDToEntityID[componentID] = entityID;

View File

@ -59,6 +59,11 @@ namespace Encompass
return entityManager.CreateEntity();
}
protected bool EntityExists(Guid entityID)
{
return entityManager.EntityExists(entityID);
}
protected Entity GetEntity(Guid entityID)
{
return entityManager.GetEntity(entityID);

View File

@ -33,9 +33,15 @@ namespace Encompass
var id = NextID();
var entity = new Entity(id, componentManager);
IDToEntity[id] = entity;
componentManager.RegisterEntity(id);
return entity;
}
public bool EntityExists(Guid id)
{
return IDToEntity.ContainsKey(id);
}
public Entity GetEntity(Guid id)
{
return IDToEntity[id];

View File

@ -54,7 +54,7 @@ namespace Encompass
var renderer = drawComponentTypeToEntityRenderer[componentType];
var entityID = componentManager.GetEntityIDByComponentID(componentID);
if (renderer.IsTracking(entityID))
if (renderer.IsTracking(entityID) && entityManager.EntityExists(entityID))
{
var entity = entityManager.GetEntity(entityID);
renderer.Render(entity);

View File

@ -19,6 +19,11 @@ namespace Encompass
this.componentManager = componentManager;
}
protected bool EntityExists(Guid entityID)
{
return entityManager.EntityExists(entityID);
}
protected Entity GetEntity(Guid entityID)
{
return entityManager.GetEntity(entityID);

View File

@ -556,5 +556,44 @@ namespace Tests
Assert.Throws<ComponentTypeMismatchException>(() => world.Update(0.01f));
}
struct EntityIDComponent : IComponent { public Guid entityID; }
static bool hasEntity;
class HasEntityTestEngine : Engine
{
public override void Update(double dt)
{
foreach (var (mockComponentID, mockComponent) in ReadComponents<EntityIDComponent>())
{
hasEntity = EntityExists(mockComponent.entityID);
if (hasEntity) { Destroy(mockComponent.entityID); }
}
}
}
[Test]
public void EntityExists()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new HasEntityTestEngine());
var entity = worldBuilder.CreateEntity();
var entityTwo = worldBuilder.CreateEntity();
EntityIDComponent entityIDComponent;
entityIDComponent.entityID = entityTwo.id;
entity.AddComponent(entityIDComponent);
var world = worldBuilder.Build();
world.Update(0.01);
Assert.IsTrue(hasEntity);
world.Update(0.01);
Assert.IsFalse(hasEntity);
}
}
}