adds EntityExists method + fixes crash when destroying an entity that has no components
parent
5ec8c4da82
commit
2d929e4665
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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];
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue