From 38a9e1d2b4d297bd4e3302cccea56d97b84326a4 Mon Sep 17 00:00:00 2001 From: David LeGare Date: Fri, 19 Dec 2014 21:52:47 -0600 Subject: Add Non-Templated SystemManager::update() Method - Added a non-templated updateAll() method to the SystemManager that updates all systems in the manager. - Adds a test for updateAll(). --- entityx/System.cc | 7 +++++++ entityx/System.h | 2 ++ entityx/System_test.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/entityx/System.cc b/entityx/System.cc index 278629e..fb020fd 100644 --- a/entityx/System.cc +++ b/entityx/System.cc @@ -17,6 +17,13 @@ BaseSystem::Family BaseSystem::family_counter_; BaseSystem::~BaseSystem() { } +void SystemManager::updateAll(TimeDelta dt) { + assert(initialized_ && "SystemManager::configure() not called"); + for (auto &pair : systems_) { + pair.second->update(entity_manager_, event_manager_, dt); + } +} + void SystemManager::configure() { for (auto &pair : systems_) { pair.second->configure(event_manager_); diff --git a/entityx/System.h b/entityx/System.h index 0f56215..3aad9ea 100644 --- a/entityx/System.h +++ b/entityx/System.h @@ -142,6 +142,8 @@ class SystemManager : entityx::help::NonCopyable { s->update(entity_manager_, event_manager_, dt); } + void updateAll(TimeDelta dt); + /** * Configure the system. Call after adding all Systems. * diff --git a/entityx/System_test.cc b/entityx/System_test.cc index 312a733..dabb8e8 100644 --- a/entityx/System_test.cc +++ b/entityx/System_test.cc @@ -32,6 +32,12 @@ struct Direction : Component { float x, y; }; +struct Counter : Component { + explicit Counter(int counter = 0) : counter(counter) {} + + int counter; +}; + class MovementSystem : public System { public: explicit MovementSystem(string label = "") : label(label) {} @@ -51,6 +57,19 @@ class MovementSystem : public System { string label; }; +class CounterSystem : public System { +public: + void update(EntityManager &es, EventManager &events, TimeDelta) override { + EntityManager::View entities = + es.entities_with_components(); + Counter::Handle counter; + for (auto entity : entities) { + entity.unpack(counter); + counter->counter++; + } + } +}; + class EntitiesFixture : public EntityX { public: std::vector created_entities; @@ -61,6 +80,8 @@ class EntitiesFixture : public EntityX { created_entities.push_back(e); if (i % 2 == 0) e.assign(1, 2); if (i % 3 == 0) e.assign(1, 1); + + e.assign(0); } } }; @@ -90,3 +111,25 @@ TEST_CASE_METHOD(EntitiesFixture, "TestApplySystem") { } } } + +TEST_CASE_METHOD(EntitiesFixture, "TestApplyAllSystems") { + systems.add(); + systems.add(); + systems.configure(); + + systems.updateAll(0.0); + Position::Handle position; + Direction::Handle direction; + Counter::Handle counter; + for (auto entity : created_entities) { + entity.unpack(position, direction, counter); + if (position && direction) { + REQUIRE(2.0 == Approx(position->x)); + REQUIRE(3.0 == Approx(position->y)); + } else if (position) { + REQUIRE(1.0 == Approx(position->x)); + REQUIRE(2.0 == Approx(position->y)); + } + REQUIRE(1 == counter->counter); + } +} -- cgit v1.2.3 From 8769f0f5880712d78ee887004c2c1c53bf80acf0 Mon Sep 17 00:00:00 2001 From: David LeGare Date: Sat, 20 Dec 2014 10:25:47 -0600 Subject: Rename SystemManager::update_all() and Add Documentation - Renamed SystemManager::updateAll() to SystemManager::update_all() to make it consistent with the existing naming convention. - Added documentation for SystemManager::update_all(). --- entityx/System.cc | 2 +- entityx/System.h | 13 ++++++++++++- entityx/System_test.cc | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/entityx/System.cc b/entityx/System.cc index fb020fd..009ac9c 100644 --- a/entityx/System.cc +++ b/entityx/System.cc @@ -17,7 +17,7 @@ BaseSystem::Family BaseSystem::family_counter_; BaseSystem::~BaseSystem() { } -void SystemManager::updateAll(TimeDelta dt) { +void SystemManager::update_all(TimeDelta dt) { assert(initialized_ && "SystemManager::configure() not called"); for (auto &pair : systems_) { pair.second->update(entity_manager_, event_manager_, dt); diff --git a/entityx/System.h b/entityx/System.h index 3aad9ea..7c90ef5 100644 --- a/entityx/System.h +++ b/entityx/System.h @@ -142,7 +142,18 @@ class SystemManager : entityx::help::NonCopyable { s->update(entity_manager_, event_manager_, dt); } - void updateAll(TimeDelta dt); + /** + * Call System::update() on all registered systems. + * + * The order which the registered systems are updated is arbitrary but consistent, + * meaning the order which they will be updated cannot be specified, but that order + * will stay the same as long no systems are added or removed. + * + * If the order in which systems update is important, use SystemManager::update() + * to manually specify the update order. EntityX does not yet support a way of + * specifying priority for update_all(). + */ + void update_all(TimeDelta dt); /** * Configure the system. Call after adding all Systems. diff --git a/entityx/System_test.cc b/entityx/System_test.cc index dabb8e8..5810357 100644 --- a/entityx/System_test.cc +++ b/entityx/System_test.cc @@ -117,7 +117,7 @@ TEST_CASE_METHOD(EntitiesFixture, "TestApplyAllSystems") { systems.add(); systems.configure(); - systems.updateAll(0.0); + systems.update_all(0.0); Position::Handle position; Direction::Handle direction; Counter::Handle counter; -- cgit v1.2.3