From 36a991d4c6d9ae5515d4e8e3a2c784ed87b65b4b Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Mon, 7 Jul 2014 13:53:42 +1000 Subject: Add entities_for_debugging() entity iterator. --- entityx/Entity.h | 31 +++++++++++++++++++++++++++---- entityx/Entity_test.cc | 13 +++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/entityx/Entity.h b/entityx/Entity.h index 7f5a5cb..cf6a506 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -133,10 +133,10 @@ public: template ComponentHandle component(); - + template const ComponentHandle component() const; - + template bool has_component() const; @@ -626,6 +626,19 @@ class EntityManager : entityx::help::NonCopyable { .unpack_to(c, args ...); } + /** + * Iterate over all *valid* entities (ie. not in the free list). Not fast, + * so should only be used for debugging. + * + * @code + * for (Entity entity : entity_manager.entities_for_debugging()) {} + * + * @return An iterator view over all valid entities. + */ + View entities_for_debugging() { + return View(this, ValidEntityPredicate()); + } + template void unpack(Entity::Id id, ComponentHandle &a) { assert_valid(id); @@ -660,6 +673,17 @@ class EntityManager : entityx::help::NonCopyable { template friend class ComponentHandle; + // Only returns entities that are valid (ie. not in the free list). Should + // only be used for debugging. + struct ValidEntityPredicate { + bool operator()(const EntityManager &entities, const Entity::Id &entity) { + for (uint32_t i : entities.free_list_) { + if (entity.index() == i) return false; + } + return true; + } + }; + /// A predicate that matches valid entities with the given component mask. class ComponentMaskPredicate { public: @@ -667,8 +691,7 @@ class EntityManager : entityx::help::NonCopyable { : entity_component_masks_(entity_component_masks), mask_(mask) {} bool operator()(const EntityManager &entities, const Entity::Id &entity) { - return entities.entity_version_[entity.index()] == entity.version() - && (entity_component_masks_[entity.index()] & mask_) == mask_; + return (entity_component_masks_[entity.index()] & mask_) == mask_; } private: diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index f57bb95..1c47453 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -237,6 +237,19 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponentAndUnpacking REQUIRE(1 == i); } +TEST_CASE_METHOD(EntityManagerFixture, "TestIterateAllEntitiesSkipsDestroyed") { + Entity a = em.create(); + Entity b = em.create(); + Entity c = em.create(); + + b.destroy(); + + EntityManager::View::Iterator it = em.entities_for_debugging().begin(); + REQUIRE(a.id() == (*it).id()); + ++it; + REQUIRE(c.id() == (*it).id()); +} + TEST_CASE_METHOD(EntityManagerFixture, "TestUnpack") { Entity e = em.create(); auto p = e.assign(1.0, 2.0); -- cgit v1.2.3