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