diff options
author | Alec Thomas <alec@swapoff.org> | 2013-12-02 14:55:20 -0500 |
---|---|---|
committer | Alec Thomas <alec@swapoff.org> | 2013-12-02 14:55:55 -0500 |
commit | 402d84bea00728896638b9121a9604a5269f9cb8 (patch) | |
tree | d7c9dc6512d328b84d9dc416a442717e25a2a240 | |
parent | 49df16c349c2ac756d203015bb8f3edf3b342a88 (diff) |
Update docs, add step(dt) to Manager.
Fixes #19.
-rw-r--r-- | README.md | 46 | ||||
-rw-r--r-- | entityx/Manager.cc | 7 | ||||
-rw-r--r-- | entityx/Manager.h | 18 |
3 files changed, 61 insertions, 10 deletions
@@ -102,7 +102,7 @@ entity.assign<Position>(1.0f, 2.0f); You can also assign existing instances of components: ```c++ -entityx::ptr<Position> position = new Position(1.0f, 2.0f); +entityx::ptr<Position> position(new Position(1.0f, 2.0f)); entity.assign(position); ``` @@ -161,7 +161,7 @@ struct MovementSystem : public System<MovementSystem> { position->x += direction->x * dt; position->y += direction->y * dt; } - } + }; }; ``` @@ -200,7 +200,7 @@ class CollisionSystem : public System<CollisionSystem> { } } } - } + }; }; ``` @@ -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<DebugSystem>(); system_manager->add<MovementSystem>(); system_manager->add<CollisionSystem>(); - 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<Position>(rand() % 100, rand() % 100); entity.assign<Direction>((rand() % 10) - 5, (rand() % 10) - 5); @@ -273,9 +276,34 @@ class GameManager : public Manager { system_manager->update<MovementSystem>(dt); system_manager->update<CollisionSystem>(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: |