pvigier / ecs

A simple and easy to use entity-component-system C++ library

Home Page:https://pvigier.github.io/2019/07/07/entity-component-system-part1.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ecs

Build Status codecov

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)

Example

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.

Documentation

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.

License

Distributed under the MIT License.

About

A simple and easy to use entity-component-system C++ library

https://pvigier.github.io/2019/07/07/entity-component-system-part1.html

License:MIT License


Languages

Language:C++ 95.4%Language:CMake 4.6%