]> code.bitgloo.com Git - clyne/entityx.git/commitdiff
Add Non-Templated SystemManager::update() Method
authorDavid LeGare <excaliburhissheath@gmail.com>
Sat, 20 Dec 2014 03:52:47 +0000 (21:52 -0600)
committerDavid LeGare <excaliburhissheath@gmail.com>
Sat, 20 Dec 2014 03:52:47 +0000 (21:52 -0600)
- Added a non-templated updateAll() method to the SystemManager that
updates all systems in the manager.
- Adds a test for updateAll().

entityx/System.cc
entityx/System.h
entityx/System_test.cc

index 278629e271ec8bb5bcfc14228dfa8c17f2f323bd..fb020fdbb68e1ba702c2e5fbc8a4e993e719af57 100644 (file)
@@ -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_);
index 0f56215ad2cb5e6287e0a50831acb86c3bfb5658..3aad9ea40e9f9e9fa9c8b08db9799fbb4b955c7d 100644 (file)
@@ -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.
    *
index 312a7336624bedb01a1d7da25e6dbc3434d46eab..dabb8e8b22804ac1b3f5b5551ab92fe89afcb770 100644 (file)
@@ -32,6 +32,12 @@ struct Direction : Component<Direction> {
   float x, y;
 };
 
+struct Counter : Component<Counter> {
+  explicit Counter(int counter = 0) : counter(counter) {}
+
+  int counter;
+};
+
 class MovementSystem : public System<MovementSystem> {
  public:
   explicit MovementSystem(string label = "") : label(label) {}
@@ -51,6 +57,19 @@ class MovementSystem : public System<MovementSystem> {
   string label;
 };
 
+class CounterSystem : public System<CounterSystem> {
+public:
+  void update(EntityManager &es, EventManager &events, TimeDelta) override {
+    EntityManager::View entities =
+        es.entities_with_components<Counter>();
+    Counter::Handle counter;
+    for (auto entity : entities) {
+      entity.unpack<Counter>(counter);
+      counter->counter++;
+    }
+  }
+};
+
 class EntitiesFixture : public EntityX {
  public:
   std::vector<Entity> created_entities;
@@ -61,6 +80,8 @@ class EntitiesFixture : public EntityX {
       created_entities.push_back(e);
       if (i % 2 == 0) e.assign<Position>(1, 2);
       if (i % 3 == 0) e.assign<Direction>(1, 1);
+
+      e.assign<Counter>(0);
     }
   }
 };
@@ -90,3 +111,25 @@ TEST_CASE_METHOD(EntitiesFixture, "TestApplySystem") {
     }
   }
 }
+
+TEST_CASE_METHOD(EntitiesFixture, "TestApplyAllSystems") {
+  systems.add<MovementSystem>();
+  systems.add<CounterSystem>();
+  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>(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);
+  }
+}