I thinking about how to do an input system.
I am going to use Win32API (anyone got better solution?) to capture the input events (mouse and keyboard, no plan for controllers or joysticks yet).
So from what I understood I should have some array of key states (and mouse state) and every frame I call my input system to check if something is pressed.
Correct me if I'm wrong up to this point.
This works nice for a demo where you don't have different game states. So this brings me to GameStateOperator. GSO (as I call it) is simply a wrapper upon stack DS that holds various GameState objects. The top most GameState in the stack is the current state of the game and the only one who can parse the input events.
Example:
Let there be two GameStates:
1. MainMenuState
2. InGameState
Both states can parse the ESC button press however they acts differently. MainMenuState is the default state that loaded when the game initialized. If current state is InGameState, pressing ESC will pop this state therefore the current state will be MainMenuState and pressing ESC here will quit the application.
This is nice and cool, but lets add entities. Entity (dont yell at me if I define it wrong) as I see it is something that can be present in the scene (be it visible and dynamic like car, visible and static like wall or invisible like trigger). Some entities would like to handle input as well (Player entity for example).
What I tough about is to do an Interface like IKeyboardListener and IMouseListener and everyone who wishes to receive keyboard or mouse events will implements them and registers itself within InputManager.
The same goes for GameState class. It will by default inherit from IKeyboardListener and IMouseListener.
For the input manager to know if the event was parsed withing specific listener, the listener must return either true or false.
Pseudo code:
[source lang=cpp]InputManager::handle(){ foreach(kb in this->mKeyboardListeners){ if(kb->handle(this->kbState)) break; }}
So if the listener returned true, the InputManager knows that that current state was handled and no more listener needs to be called.
This is a brief of how I see it. Somethings were tough about for a long time, others just came into my head right now. It looks like a flexible system for me but I would you to comment on it and show me were I went wrong, if at all.
Thanks!