From 99cae200fa62af1a2a6695129413691698f27184 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 15 May 2014 12:56:25 -0400 Subject: Only delete assigned components. Fixes #31. --- entityx/Entity.h | 7 +++++-- entityx/Entity_test.cc | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/entityx/Entity.h b/entityx/Entity.h index 7362f69..5f51b50 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -456,9 +456,12 @@ class EntityManager : entityx::help::NonCopyable { void destroy(Entity::Id entity) { assert_valid(entity); int index = entity.index(); + auto mask = entity_component_mask_[entity.index()]; event_manager_.emit(Entity(this, entity)); - for (BasePool *pool : component_pools_) { - if (pool) pool->destroy(index); + for (size_t i = 0; i < component_pools_.size(); i++) { + BasePool *pool = component_pools_[i]; + if (pool && mask.test(i)) + pool->destroy(index); } entity_component_mask_[index] = 0; entity_version_[index]++; diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index f40a9bc..1f87473 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -449,3 +449,10 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenCompon a.remove(); REQUIRE(!position); } + +TEST_CASE_METHOD(EntityManagerFixture, "TestDeleteEntityWithNoComponents") { + Entity a = em.create(); + a.assign(1, 2); + Entity b = em.create(); + b.destroy(); +} -- cgit v1.2.3