I am trying to map multiple input types to single in-game controls using libGDX's
ControllerListener interfaces. Currently, I have created a single
InputType interface which is inherited by the following enumerations:
- Buttons (Gamepad)
- Axis (Gamepad)
The InputType interface declares a single method
getCode(); as all inheriting enumerations have their own codes.
I have created a
Keyboard class which implements
InputProcessor and a
Gamepad class which implements
ControllerListener. Both classes extend from a generic
InputHandler class which stores a
Set<T extends InputType>. I also created a Mouse class and
AxisHandler class which work similarly, although as touch events are handled within the
InputProcessor and axis events handled within the
Gamepad classes contain instances of these classes respectively.
Finally, to map multiple input types to single controls, I created a
ControlMap class which stores an
EnumMap<Controls, InputType>, where
Controls is an enumeration containing all controls that are present within the game (MOVE_UP, MOVE_DOWN, ATTACK, etc.)
The obvious problem with this is that I'm going from an event-driven approach to polling. Every time input is processed, I'm storing the associated
InputType accordingly, and this can create an overhead after some time.
My game uses a component-based architecture, so in my MovementSystem I'd check whether controls are down by using
if (Controls.controlDown(Controls.MOVE_DOWN)). I can't seem to find any other way of approaching this problem just yet. There is no apparent drop in performance; when input isn't being handled, nothing is stored. Is my method fine as it is and am I over thinking the problem too much?
My UML is terrible but here's how I can best describe the process: