aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlec Thomas <alec@swapoff.org>2014-07-07 13:53:42 +1000
committerAlec Thomas <alec@swapoff.org>2014-07-07 13:54:47 +1000
commit36a991d4c6d9ae5515d4e8e3a2c784ed87b65b4b (patch)
tree40ff314da8cd58379cd13570c4aac43bfe4b5657
parentf65ec82158625f6144751c803d2601b02c53b431 (diff)
Add entities_for_debugging() entity iterator.
-rw-r--r--entityx/Entity.h31
-rw-r--r--entityx/Entity_test.cc13
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 <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:
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<Position>(1.0, 2.0);