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.