Skip to content

Commit

Permalink
Update docs, add step(dt) to Manager.
Browse files Browse the repository at this point in the history
Fixes #19.
  • Loading branch information
alecthomas committed Dec 2, 2013
1 parent 49df16c commit 402d84b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
46 changes: 37 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
Expand Down Expand Up @@ -161,7 +161,7 @@ struct MovementSystem : public System<MovementSystem> {
position->x += direction->x * dt;
position->y += direction->y * dt;
}
}
};
};
```
Expand Down Expand Up @@ -200,7 +200,7 @@ class CollisionSystem : public System<CollisionSystem> {
}
}
}
}
};
};
```
Expand Down Expand Up @@ -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);
Expand All @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion entityx/Manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ void Manager::run() {
}
}

void Manager::step(double dt) {
update(dt);
}


void Manager::stop() {
running_ = false;
}

}
} // namespace entityx
18 changes: 18 additions & 0 deletions entityx/Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 402d84b

Please sign in to comment.