aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlec Thomas <alec@swapoff.org>2014-12-21 11:39:56 +1100
committerAlec Thomas <alec@swapoff.org>2014-12-21 11:39:56 +1100
commit065a123aaac4f89658fb6fcfa902f89e8bc6e047 (patch)
tree7da1cabc85a980a192fe407ffb6f541629a584d8
parenta71d2eebe4ea615b3650d999f9c18110390455e1 (diff)
parent8769f0f5880712d78ee887004c2c1c53bf80acf0 (diff)
Merge pull request #76 from excaliburHisSheath/master
Add SystemManager::update_all() Method
-rw-r--r--entityx/System.cc7
-rw-r--r--entityx/System.h13
-rw-r--r--entityx/System_test.cc43
3 files changed, 63 insertions, 0 deletions
diff --git a/entityx/System.cc b/entityx/System.cc
index 278629e..009ac9c 100644
--- a/entityx/System.cc
+++ b/entityx/System.cc
@@ -17,6 +17,13 @@ BaseSystem::Family BaseSystem::family_counter_;
BaseSystem::~BaseSystem() {
}
+void SystemManager::update_all(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..7c90ef5 100644
--- a/entityx/System.h
+++ b/entityx/System.h
@@ -143,6 +143,19 @@ class SystemManager : entityx::help::NonCopyable {
}
/**
+ * 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.
*
* This is typically used to set up event handlers.
diff --git a/entityx/System_test.cc b/entityx/System_test.cc
index 312a733..5810357 100644
--- a/entityx/System_test.cc
+++ b/entityx/System_test.cc
@@ -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.update_all(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);
+ }
+}