Context of the question :
When developing a game using Game State Management I came across a problematic when it came to handling user input : I want my game to retrieve user input a single way, but to handle it in a completely different way, depending on the context.
For example, the [Up] key will always mean "Menu Up" when you're in a menu, but if you're in the game, it might do different things, depending on the key bindings. Another difference would be, for example, that in a menu, a .5sec KeyDown
on a key should be interpreted as a single command, but when you're in game, a .5sec KeyDown
should often be seen as a .5sec movement / burst / charge shot / etc...
Then way I though about to achieve this result is the following :
- Have an
InputState
class that keeps the current and previous Keyboard/Gamepad input states, just like the usual InputState class. Have thisInputState
class implement all the methods I would need in all the different contexts. - Create an Interface per context. For example,
IMenuInputState
,IGameInputState
,ICutsceneInputState
, etc. These interfaces would only contain the method definitions related to the context. - Have the
InputState
implement all theI[...]InputState
interfaces. If two or more context need the same method, the same implementation would be used. - In the
ScreenManager
's Update method, theInputState
would be Updated, but depending on which screen must handle theInputState
, a differentI[...]InputState
would be sent. Even though the screen could access the other methods by casting the interface, only the contextual methods would directly be usable.
My question is the following : Is my idea a proper / common way of handling this issue?
If it is, could you tell me the name of that "pattern" or direct me to some online resources so I can read on that and avoid the known pitfalls. If it's not, how would you suggest I handle the problem?
Note: I know this question is borderline since the answer is partially subjective but I'd appreciate some help on this issue since I think it's a critical decision in my project.