Entt: EnTT-Pacman

Created on 4 Oct 2018  ·  5Comments  ·  Source: skypjack/entt

EnTT-Pacman is an example of how to make a game (in this case: Pacman) with EnTT. Reading the source might help beginners in understanding how to apply the ECS architecture to a game. I think this deserves a mention in the README of EnTT.

Link: https://github.com/Kerndog73/EnTT-Pacman
Description: An example of how to make Pacman with EnTT. Read the source and learn!

That last part “Read the source and learn!” might be a bit much. I’ll let you decide whether or not that part is included. 😉

Most helpful comment

That's good!! I'm merging meta on v3 during these days and then I'm porting everything on master.
I'd add it directly after these changes if it's fine for you, even though this isn't using C++17.

All 5 comments

btw, the core example at main page helped me a lot already,
I modified it a bit with cout to see what was going on,
had to compile like g++ test.cpp -o test -I. -std=c++14 tho
(some includes are not used yet btw)

#include <entt/entt.hpp>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

struct Position {
    float x;
    float y;
};

struct Velocity {
    float dx;
    float dy;
};

void update(entt::DefaultRegistry &registry) {
    auto view = registry.view<Position, Velocity>();

    for(auto entity: view) {
        // gets only the components that are going to be used ...

        auto &velocity = view.get<Velocity>(entity);

        velocity.dx = 0.;
        velocity.dy = 0.;

        // ...
        std::cout << entity << ":" << velocity.dx << std::endl;
    }
}

void update(std::uint64_t dt, entt::DefaultRegistry &registry) {
    registry.view<Position, Velocity>().each([dt](auto entity, auto &position, auto &velocity) {
        // gets all the components of the view at once ...

        position.x += velocity.dx * dt;
        position.y += velocity.dy * dt;

        // ...
        std::cout << entity << ":" << velocity.dx << "," << position.x << std::endl;
    });
}

int main() {
    entt::DefaultRegistry registry;
    std::uint64_t dt = 16;

    for(auto i = 0; i < 10; ++i) {
        auto entity = registry.create();
        registry.assign<Position>(entity, i * 1.f, i * 1.f);
        if(i % 2 == 0) { registry.assign<Velocity>(entity, i * .1f, i * .1f); }
    }

    update(dt, registry);
    update(registry);

    // ...
}

but I will take a look on pacman to see more stuff :)

EDIT:
I think a good game is implemented isolating the parts so we can plugin them w/o compromising the core code like:

  • code ECS, all the game logic, separate from the rest
  • code specific implementation to provide a bridge to renderers (3D, 2D, textMode) and other media (music,sfx etc)
    So when we need to change the rendering end, is less troubling (if troubling at all).

@AquariusPower I believe that I have done that with Pacman. Changing the rendering backend would involve rewriting sys/render.cpp (to use the new renderer) and modifying core/app.cpp (to initialize the new renderer). Apart from that, the parameter types of a few functions will need to be tweaked but that's about it.

For example, the code that does pathfinding has no idea how the entities are being rendered. I find that the ECS architecture makes separation of concerns really easy.

For a brilliant example of the power that ECS gives you, see comp/chase_target.hpp.

could be
sys/render3D.cpp
sys/render2D.cpp
sys/renderTM.cpp
configurable thru core/app.cpp :)

so, such functions would be overridden and only available to 3D, 2D or TM,
I mean, the moment I start coding to 3D (as I will begin tests in TM), the idea, if possible, is to have not to change anything else other than create the code to let it render in 3D :)

but that all is a personal preference xD

That's good!! I'm merging meta on v3 during these days and then I'm porting everything on master.
I'd add it directly after these changes if it's fine for you, even though this isn't using C++17.

Ok, added to the README file of branch v3.
I'm closing the issue right now and hopefully merging v3 on master during the week. I've been a bit busy last week.

Was this page helpful?
0 / 5 - 0 ratings