]> code.bitgloo.com Git - clyne/entityx.git/commitdiff
Add entities_for_debugging() entity iterator.
authorAlec Thomas <alec@swapoff.org>
Mon, 7 Jul 2014 03:53:42 +0000 (13:53 +1000)
committerAlec Thomas <alec@swapoff.org>
Mon, 7 Jul 2014 03:54:47 +0000 (13:54 +1000)
entityx/Entity.h
entityx/Entity_test.cc

index 7f5a5cb0fa4659e4d0ebc93dac843b6ca0006c8c..cf6a506bdc898c8354a8aa7ea87d6aef8141c73c 100644 (file)
@@ -133,10 +133,10 @@ public:
 
   template <typename C>
   ComponentHandle<C> component();
-  
+
   template <typename C>
   const ComponentHandle<const C> component() const;
-    
+
   template <typename C>
   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 <typename C>
   void unpack(Entity::Id id, ComponentHandle<C> &a) {
     assert_valid(id);
@@ -660,6 +673,17 @@ class EntityManager : entityx::help::NonCopyable {
   template <typename C>
   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:
index f57bb957f0b9ef3844cf2ef0f9fb5497405a0646..1c474530f5e4c8526a15ec71630cde4116f4e93a 100644 (file)
@@ -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<Position>(1.0, 2.0);