I'm writting a game in C++ and I'm struggling with getting events and game states work together.
By game states I mean something like this
class GameState
{
public:
virtual void onPause();
virtual void draw();
virtual void update();
};
class PauseScreen : public GameState
{
//stuff
};
class Game : public GameState
{
//stuff
};
And I've created dope event observer based system where I have simple EventObserver class which has register function to what Event I want to subscribe and EventManager where I have map of vectors of all possible EventObservers (everything made with templates) and callback functions. No pointers, no inheritance - everything works like charm.
So there is my problem - how to connect these? In my main loop I'm getting current active game state - and then updating and drawing it. But for example when state is not active its still receiving event. This is pretty bad beacuse if I have MainMenu gamestate and for example if escape key is pressed it would just exit the game even if I'm currently in Game.
I could only came with some bool checking - if current state is not more active it would disable all EventListeners but I would need to manually call EventListener::disable in all game states for every EventListeners I have in onPause() methods and I don't like this solution (and since it's my hobby project I would like things that I like). I could store EventListeners in vector in GameState interface class so I wouldn't need to disable it manually and just iterate thought it and disable it 'automatically' but I would have to deal with A LOT of templates and naaah I don't wanna do it (just keep it simple).
On the other hand I could add method to GameState handleEvent(Event* event) (well since this is now dynamic memory I would probably prefer to use smart pointers so I don't need to worry when I have to delete event) but then I would have giant switch statement for every game state that I have and this also doesn't seems like good solution. Now I'm just binding one method per one event so everything is nice divided and simple.
So any tips how to design this? Can event observers (or subscribers) work with game states?