From f24ee21de01633fe2ea3c81256fd204483989674 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Tue, 17 Mar 2020 17:40:11 -0700 Subject: [PATCH] pruning empty entities --- encompass-cs/ComponentManager.cs | 6 ++++++ encompass-cs/Engine.cs | 8 ++++++++ encompass-cs/EntityManager.cs | 12 ++++++++++++ encompass-cs/World.cs | 1 + test/EngineTest.cs | 31 +++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 8c45186..046704e 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using MoonTools.FastCollections; namespace Encompass { @@ -262,5 +263,10 @@ namespace Encompass drawLayerManager.UnRegisterComponentWithLayer(entityID); } } + + public bool UpToDateEntityIsEmpty(int entityID) + { + return upToDateComponentStore.EntityBitArray(entityID).AllFalse(); + } } } diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 8be53dd..a7ba186 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -202,6 +202,14 @@ namespace Encompass return entityManager.EntityExists(entity.ID); } + /// + /// Returns true if an Entity with the specified ID exists. + /// + protected bool EntityExists(int entityID) + { + return entityManager.EntityExists(entityID); + } + /// /// Returns an Entity containing the specified Component type. /// diff --git a/encompass-cs/EntityManager.cs b/encompass-cs/EntityManager.cs index 483b36b..c800281 100644 --- a/encompass-cs/EntityManager.cs +++ b/encompass-cs/EntityManager.cs @@ -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); + } + } + } } } diff --git a/encompass-cs/World.cs b/encompass-cs/World.cs index fcc3384..b611d96 100644 --- a/encompass-cs/World.cs +++ b/encompass-cs/World.cs @@ -59,6 +59,7 @@ namespace Encompass } messageManager.ClearMessages(); + entityManager.PruneEmptyEntities(); entityManager.DestroyMarkedEntities(enginesInOrder); componentManager.RemoveMarkedComponents(); diff --git a/test/EngineTest.cs b/test/EngineTest.cs index 29da00c..77ceeb1 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -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 { }