From 402d84bea00728896638b9121a9604a5269f9cb8 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Mon, 2 Dec 2013 14:55:20 -0500 Subject: [PATCH] Update docs, add step(dt) to Manager. Fixes #19. --- README.md | 46 +++++++++++++++++++++++++++++++++++++--------- entityx/Manager.cc | 7 ++++++- entityx/Manager.h | 18 ++++++++++++++++++ 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4e6d20f..4a7b191 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ entity.assign(1.0f, 2.0f); You can also assign existing instances of components: ```c++ -entityx::ptr position = new Position(1.0f, 2.0f); +entityx::ptr position(new Position(1.0f, 2.0f)); entity.assign(position); ``` @@ -161,7 +161,7 @@ struct MovementSystem : public System { position->x += direction->x * dt; position->y += direction->y * dt; } - } + }; }; ``` @@ -200,7 +200,7 @@ class CollisionSystem : public System { } } } - } + }; }; ``` @@ -248,21 +248,24 @@ Several events are emitted by EntityX itself: Managing systems, components and entities can be streamlined by subclassing `Manager`. It is not necessary, but it provides callbacks for configuring systems, initializing entities, and so on. -To use it, subclass `Manager` and implement `configure()`, `initialize()` and `update()`: +To use it, subclass `Manager` and implement `configure()`, `initialize()` and `update()`. In this example a new `Manager` is created for each level. ```c++ -class GameManager : public Manager { +class Level : public Manager { +public: + explicit Level(filename string) : filename_(filename) {} + protected: void configure() { system_manager->add(); system_manager->add(); system_manager->add(); - system_manager->configure(); - } + }; void initialize() { - // Create some entities in random locations heading in random directions - for (int i = 0; i < 100; ++i) { + level_.load(filename_); + + for (auto e : level.entity_data()) { entityx::Entity entity = entity_manager->create(); entity.assign(rand() % 100, rand() % 100); entity.assign((rand() % 10) - 5, (rand() % 10) - 5); @@ -273,9 +276,34 @@ class GameManager : public Manager { system_manager->update(dt); system_manager->update(dt); } + + string filename_; + Level level_; }; ``` + +Once created, start the manager: + +```c++ +Level level("mylevel.dat"); +level.start(); +``` + +You can then either start the (simplistic) main loop: + +```c++ +level.run(); +``` + +Or step the entities explicitly inside your own game loop (recommended): + +```c++ +while (true) { + level.step(0.1); +} +``` + ## Installation EntityX has the following build and runtime requirements: diff --git a/entityx/Manager.cc b/entityx/Manager.cc index 6ed7512..a86ee56 100644 --- a/entityx/Manager.cc +++ b/entityx/Manager.cc @@ -29,8 +29,13 @@ void Manager::run() { } } +void Manager::step(double dt) { + update(dt); +} + + void Manager::stop() { running_ = false; } -} +} // namespace entityx diff --git a/entityx/Manager.h b/entityx/Manager.h index 3a7e99a..2da482e 100644 --- a/entityx/Manager.h +++ b/entityx/Manager.h @@ -21,8 +21,26 @@ class Manager { public: virtual ~Manager() {} + /** + * Call start() to initialize the Manager. + */ void start(); + + + /** + * Run the main loop. To explicitly manage your own main loop use step(dt); + */ void run(); + + /** + * Step the system by dt. + * @param dt Delta time since last frame. + */ + void step(double dt); + + /** + * Stop the manager. + */ void stop(); protected: