0

I am creating a game using EntityX library which handles Entity-Component-System model, and SFML.

Right now, I just have four basic components: position, direction, velocity and renderable, plus two systems: a movement system and a render system.

I will separate my question in two:

  • How can I make basic controls such as moving my character? With SFML, I can get the events this way:

    sf::Event event;
    while(window.pollEvent(event))
    {
        if(event.type == sf::Event::Closed)
            window.close();
        // other events ...
    }
    

So I could create a ControllerSystem which targets entities having a Controller component, and give a reference to my window to the system so that it handles the events. But the only controllable entity would be my character so is that really efficient? Plus, it means I would have to loop through the events in my system to get KeyPressed events. Thus I can't for example do the condition to close the window except if I put it along with the events for the character.

Finally, when I get - wherever it is - a KeyPressed event and I want to do an action, how and where should I send this action so that it get realised? I mean, my controller system can deplace my character and a AI system could deplace a mob. I'm not going to write the deplacement twice in my systems, even if it's just changing direction and velocity. I could have more complex actions such as launching spells etc.

  • Coming to my second point, how to handle different spells/weapons? Suppose I have three keys A, B, C each one launching a totally different spell. In my Controller Component, when I would get an event saying one of these key has been pressed, I would want to launch the spell. But where should I have this spell "stored" in the code? Assuming the three spells must be coded differently as they have a different comportment, but there needs to a common pattern between them, no?

So shall spells be systems, or just normal classes out of the ECS model, having access on entity manager and components to create projectiles or I don't know what?

It remains quite blur to me and I can't really find specific enough tutorials. Thanks for your help.

Anders Evensen
  • 579
  • 5
  • 15
  • It seems that your asking about how to implement a callback routine for controller/keyboard input. I use freeglut myself, as I find it very reliable. – Charlie Jul 19 '16 at 08:52
  • Full-blown engines (rather than games that don't try to abstract the hardware very much - you see these simpler switch-statement designs a lot in homebrew games) tend to take hardware events and translate them into some sort of "game system" events. One benefit of this level of abstraction is that you can do key binding in your mapping system. Another benefit is that your game system can emit its own events that aren't triggered off hardware events. They also tend to use the https://en.wikipedia.org/wiki/Observer_pattern so that they can efficiently decouple event listeners from event emitters – Merlyn Morgan-Graham Jul 22 '16 at 22:29
  • As for efficiency - people often decouple their render, physics, and "game logic" loops, and run them at different intervals. High level game logic is often considered "okay" to be a bit slower (though it might have to be a bit quicker for physics-related events). An extra function pointer dereference or event queue to process isn't typically going to break the bank when it comes to higher level game events. In fact, a lot of people use a scripting engine for their game logic instead of C++, and that's going to be *much* slower. – Merlyn Morgan-Graham Jul 22 '16 at 22:33

0 Answers0