Using the LibGDX InputProcessor, what can I do with the keycode integer values generated in keyDown() and keyUp() to handle input correctly? To expand, what I mean by handling input correctly is to handle it in such a way so that GameObjects which need to be controlled by user input, do not themselves actually handle the user input or hold any input related values such as key down/up flags, and are instead influenced indirectly by something else which handles the input.
One way in which I feel this can be done is with a Controller object which can act as a mediator between the input values and the object you want to be influenced by the input. The Controller would hold a reference to the object and methods to receive the keycode input value. Depending on this value, the Controller would then, using a switch case or set of if-else's, call relevant methods on the object such as move(x, y) or attack(). This decouples the object from the input and allows you to control it in a top down way.
The problem with this approach, in my case at least, is that it does not provide a fine enough level of control over the object so that it responds appropriately to different sets of keys. A concrete example of this would be if the object is moving in a certain direction because a key is held and another key is pressed detailing that it should move in the opposite direction, I would want the key that would make the object move in the opposite direction to be ignored so long as the first key remains held. With the Controller setup that wouldn't happen. The second input would override the first, causing the object to move in the opposite direction.
The way that I have managed to achieve the level of control I need over how input is acted on by GameObjects, which in my case is primarily the Player object, has been by making the Player receive the keycode directly from the InputProcessor and handle it through the use of flags and states in the form of State objects. The flags represent the state of input which is relevant to it and the states determine explicitly the state of the Player and what it can do at any one time, including how it handles input state. The current state updates every Player update and based on a set of conditions, including the state of the flags, it either changes to another appropriate state or continues updating.
This setup is bad for a couple of reasons. It makes it so that the Player is responsible for maintaining key state in the form of flags, essentially becoming an input handler. It also tightly couples the States it uses to itself, making it so that they can only be used by the Player and not another kind of object.
The problem I am facing is that I don't know how to change the setup so that GameObjects which should act on input can do so while remaining decoupled from the input, while also maintaining the fine level of control that states and flags provide over how the input is processed.
What can I do to achieve this? Thank you.
↧