Handling inputs the right way

Started by
1 comment, last by Firnael 11 years, 6 months ago
Hi,
I'm (ultra) confuse about input managment. I've the global idea, but I can figure out how to implement something which fit my design (maybe because it's a lame one).
The context is a state based game. For the example, we wil define 3 states : MAIN_MENU, GAME_RUNNING, and GAME_CASTING_SPELL.
In this 3 states, the action "left-button mouse clic" will be handled differently :
- In the MAIN_MENU state, the input will change the state, from MAIN_MENU to GAME_RUNNING.
- In the GAME_RUNNING state, the input triggers a player action : cast a spell (let's say firebolt), which change the state from GAME_RUNNING to CASTING_SPELL.
- In the CASTING_SPELL state, the input gives informations for the spell, for example his target. You clic somewhere and the firebolt goes there. This input changes the state from CASTING_SPELL to GAME_RUNNING.
Somewhere I'll need a class (let's call it InputManager) which needs to know the state the game currently is, like this (pseudo-code):

(method called every tick :)
update(GameState state) {
switch(state) {
case MAIN_MENU :
if(mouse_leftbutton_clic)
*go to GAME state*
break;

case GAME_RUNNING :
if(mouse_leftbutton_clic)
*go to CASTING_SPELL state*
break;
case CASTING_SPELL :
if(mouse_leftbutton_clic)
*launch firebolt and go to GAME_RUNNING state*
break;
}
}

How do i link my classes together ? My input manager needs to know the existence of the other objects and classes to update their logic.
Plus, what happens when I want an event to trigger when the player presses 2 buttons at the same time ?
Should my input manager return an array defining the state of my keyboard and mouse input and instant T ?
What I said may be huge crap, I need a teacher tongue.png
Thanks !
Advertisement
Something I've used in the past was having something like
[source lang="java"]interface State {
void processInput(InputEvent event);
State getNextState();
}[/source]

The gameloop then forwards all events it doesn't handle directly to the currently active state and at the end of the game loop queries the next state and sets that as active. That avoids this global switch construct which will get out of hand and unmaintainable pretty quickly.
Alright, that sounds like a good idea.
Now how do you update your logic with your inputs ?
Does your input manager (let's say you have one for each state) needs to know your logic (so you have to pass it references of your logic objects so he can manipulate them) or does it simply return some infos on what input have been triggered, infos that the logic is able to decode and modify itself ?
I would say the second one, since your input manager shouldn't be doing something else than managing inputs.

This topic is closed to new replies.

Advertisement