ecs
is the entity-component-system library I have created for my game Vagabond.
ecs
aims to be:
- easy to use
- fast
- lightweight
- header only
- implemented with modern C++ features (C++17)
Firstly, you should define some components:
struct Position : public Component<Position>
{
float x;
float y;
};
struct Velocity : public Component<Velocity>
{
float x;
float y;
};
A component of type T
must inherit Component<T>
.
Now, let us create an entity manager:
auto manager = EntityManager();
Next, let us create an entity with both components:
auto entity = manager.createEntity();
manager.addComponent<Position>(entity);
manager.addComponent<Velocity>(entity);
Finally, we can use getEntitySet
to query all entities that have both components and update their positions:
auto dt = 1.0f / 60.0f;
for (auto [entity, components] : manager.getEntitySet<Position, Velocity>())
{
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 folder.
I have written several articles on my blog describing the design of the library. They are available here.
Otherwise, just look at the EntityManager.h file, it is simple to understand.
Distributed under the MIT License.