pruning empty entities

pull/2/head
Evan Hemsley 2020-03-17 17:40:11 -07:00
parent 4d67b4ddf7
commit f24ee21de0
5 changed files with 58 additions and 0 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using MoonTools.FastCollections;
namespace Encompass
{
@ -262,5 +263,10 @@ namespace Encompass
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
}
}
public bool UpToDateEntityIsEmpty(int entityID)
{
return upToDateComponentStore.EntityBitArray(entityID).AllFalse();
}
}
}

View File

@ -202,6 +202,14 @@ namespace Encompass
return entityManager.EntityExists(entity.ID);
}
/// <summary>
/// Returns true if an Entity with the specified ID exists.
/// </summary>
protected bool EntityExists(int entityID)
{
return entityManager.EntityExists(entityID);
}
/// <summary>
/// Returns an Entity containing the specified Component type.
/// </summary>

View File

@ -76,5 +76,17 @@ namespace Encompass
entitiesMarkedForDestroy.Clear();
}
// NOTE: this is very suboptimal
public void PruneEmptyEntities()
{
foreach (var id in EntityIDs)
{
if (componentManager.UpToDateEntityIsEmpty(id))
{
MarkForDestroy(id);
}
}
}
}
}

View File

@ -59,6 +59,7 @@ namespace Encompass
}
messageManager.ClearMessages();
entityManager.PruneEmptyEntities();
entityManager.DestroyMarkedEntities(enginesInOrder);
componentManager.RemoveMarkedComponents();

View File

@ -1387,6 +1387,37 @@ namespace Tests
getComponentResult.Should().Be(new MockComponentB(5));
}
static bool entityExistsResult;
class EntityExistsEngine : Engine
{
private int _id;
public EntityExistsEngine(int id)
{
_id = id;
}
public override void Update(double dt)
{
entityExistsResult = EntityExists(_id);
}
}
[Test]
public void PruneEmptyEntities()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
var id = entity.ID;
var world = worldBuilder.Build();
world.Update(0.01);
entityExistsResult.Should().BeFalse();
}
public class QueryTests
{
struct MockComponentB : IComponent { }