Skip to content

Commit 402d84b

Browse files
committed
Update docs, add step(dt) to Manager.
Fixes #19.
1 parent 49df16c commit 402d84b

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

README.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ entity.assign<Position>(1.0f, 2.0f);
102102
You can also assign existing instances of components:
103103

104104
```c++
105-
entityx::ptr<Position> position = new Position(1.0f, 2.0f);
105+
entityx::ptr<Position> position(new Position(1.0f, 2.0f));
106106
entity.assign(position);
107107
```
108108
@@ -161,7 +161,7 @@ struct MovementSystem : public System<MovementSystem> {
161161
position->x += direction->x * dt;
162162
position->y += direction->y * dt;
163163
}
164-
}
164+
};
165165
};
166166
```
167167
@@ -200,7 +200,7 @@ class CollisionSystem : public System<CollisionSystem> {
200200
}
201201
}
202202
}
203-
}
203+
};
204204
};
205205
```
206206
@@ -248,21 +248,24 @@ Several events are emitted by EntityX itself:
248248

249249
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.
250250

251-
To use it, subclass `Manager` and implement `configure()`, `initialize()` and `update()`:
251+
To use it, subclass `Manager` and implement `configure()`, `initialize()` and `update()`. In this example a new `Manager` is created for each level.
252252

253253
```c++
254-
class GameManager : public Manager {
254+
class Level : public Manager {
255+
public:
256+
explicit Level(filename string) : filename_(filename) {}
257+
255258
protected:
256259
void configure() {
257260
system_manager->add<DebugSystem>();
258261
system_manager->add<MovementSystem>();
259262
system_manager->add<CollisionSystem>();
260-
system_manager->configure();
261-
}
263+
};
262264

263265
void initialize() {
264-
// Create some entities in random locations heading in random directions
265-
for (int i = 0; i < 100; ++i) {
266+
level_.load(filename_);
267+
268+
for (auto e : level.entity_data()) {
266269
entityx::Entity entity = entity_manager->create();
267270
entity.assign<Position>(rand() % 100, rand() % 100);
268271
entity.assign<Direction>((rand() % 10) - 5, (rand() % 10) - 5);
@@ -273,9 +276,34 @@ class GameManager : public Manager {
273276
system_manager->update<MovementSystem>(dt);
274277
system_manager->update<CollisionSystem>(dt);
275278
}
279+
280+
string filename_;
281+
Level level_;
276282
};
277283
```
278284
285+
286+
Once created, start the manager:
287+
288+
```c++
289+
Level level("mylevel.dat");
290+
level.start();
291+
```
292+
293+
You can then either start the (simplistic) main loop:
294+
295+
```c++
296+
level.run();
297+
```
298+
299+
Or step the entities explicitly inside your own game loop (recommended):
300+
301+
```c++
302+
while (true) {
303+
level.step(0.1);
304+
}
305+
```
306+
279307
## Installation
280308

281309
EntityX has the following build and runtime requirements:

entityx/Manager.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ void Manager::run() {
2929
}
3030
}
3131

32+
void Manager::step(double dt) {
33+
update(dt);
34+
}
35+
36+
3237
void Manager::stop() {
3338
running_ = false;
3439
}
3540

36-
}
41+
} // namespace entityx

entityx/Manager.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,26 @@ class Manager {
2121
public:
2222
virtual ~Manager() {}
2323

24+
/**
25+
* Call start() to initialize the Manager.
26+
*/
2427
void start();
28+
29+
30+
/**
31+
* Run the main loop. To explicitly manage your own main loop use step(dt);
32+
*/
2533
void run();
34+
35+
/**
36+
* Step the system by dt.
37+
* @param dt Delta time since last frame.
38+
*/
39+
void step(double dt);
40+
41+
/**
42+
* Stop the manager.
43+
*/
2644
void stop();
2745

2846
protected:

0 commit comments

Comments
 (0)