https://github.com/pvigier/ecs
A simple and easy to use entity-component-system C++ library
https://github.com/pvigier/ecs
cpp ecs entity-component entity-component-system game-development header-only modern-cpp
Last synced: 6 months ago
JSON representation
A simple and easy to use entity-component-system C++ library
- Host: GitHub
- URL: https://github.com/pvigier/ecs
- Owner: pvigier
- License: mit
- Created: 2019-07-05T15:14:58.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-02T14:33:34.000Z (over 6 years ago)
- Last Synced: 2025-05-13T01:07:17.746Z (10 months ago)
- Topics: cpp, ecs, entity-component, entity-component-system, game-development, header-only, modern-cpp
- Language: C++
- Homepage: https://pvigier.github.io/2019/07/07/entity-component-system-part1.html
- Size: 68.4 KB
- Stars: 65
- Watchers: 3
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ecs
[](https://travis-ci.org/pvigier/ecs)
[](https://codecov.io/gh/pvigier/ecs)
`ecs` is the [entity-component-system](https://en.wikipedia.org/wiki/Entity_component_system) library I have created for my game [Vagabond](https://pvigier.github.io/tag/vagabond).
`ecs` aims to be:
* easy to use
* fast
* lightweight
* header only
* implemented with modern C++ features (C++17)
## Example
Firstly, you should define some components:
```cpp
struct Position : public Component
{
float x;
float y;
};
struct Velocity : public Component
{
float x;
float y;
};
```
A component of type `T` must inherit `Component`.
Now, let us create an entity manager:
```cpp
auto manager = EntityManager();
```
Next, let us create an entity with both components:
```cpp
auto entity = manager.createEntity();
manager.addComponent(entity);
manager.addComponent(entity);
```
Finally, we can use `getEntitySet` to query all entities that have both components and update their positions:
```cpp
auto dt = 1.0f / 60.0f;
for (auto [entity, components] : manager.getEntitySet())
{
auto [position, velocity] = components;
// Update the position
position.x += velocity.x * dt;
position.y += velocity.y * dt;
}
```
It is that easy!
If you want more examples, look at the [examples](https://github.com/pvigier/ecs/tree/master/examples) folder.
## Documentation
I have written several articles on my blog describing the design of the library. They are available [here](https://pvigier.github.io/2019/07/07/entity-component-system-part1.html).
Otherwise, just look at the [EntityManager.h](https://github.com/pvigier/ecs/blob/master/include/ecs/EntityManager.h) file, it is simple to understand.
## License
Distributed under the MIT License.