A new entry in the learning class!!
Subject : What method do you use for your input in a game?
Description
I use 2 differents way to handle input in a game, the first one is the traditionnal input listener and the second one is an input that keep track of every key pressed, key released, mouse down, mouse up and the mouse position. For now, I will only explain how to do the second option.
So, what do you need to make a custom input container? :
- First, you need to have input listener that will catch the event for you
- Then, you send theses event to the InputRecorder.
- Finally, you can access those datas through the Input interface.
Code
Here is a part of the code that does that.
The listener that receive input from the player and send them to the InputRecorder
private class KeyControl implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
inputRecorder.setKeyPressed(e.getKeyCode(), true);
}
@Override
public void keyReleased(KeyEvent e) {
inputRecorder.setKeyPressed(e.getKeyCode(), false);
}
@Override
public void keyTyped(KeyEvent e) {
}
}
The interface that the programmer use to get the information about the player input
public interface Input {
public boolean isKeyPressed(int code);
public boolean isMouseDown(int code);
public int getMouseX();
public int getMouseY();
}
The class the record every input from the player
public class InputRecorder implements Input{
private ArrayList<Boolean> keyPressed = new ArrayList<Boolean>();
private ArrayList<Boolean> mouseDown = new ArrayList<Boolean>();
private int mouseX = -1;
private int mouseY = -1;
@Override
public synchronized boolean isKeyPressed(int code){
if(code >= keyPressed.size()){
increaseSize(keyPressed, code+1);
}
return keyPressed.get(code);
}
public void setKeyPressed(int code, boolean value){
if(code >= keyPressed.size()){
increaseSize(keyPressed, code+1);
}
keyPressed.set(code, value);
}
@Override
public synchronized boolean isMouseDown(int code){
if(code >= mouseDown.size()){
increaseSize(mouseDown, code+1);
}
return mouseDown.get(code);
}
public synchronized void setMouseDown(int code, boolean value){
if(code >= mouseDown.size()){
increaseSize(mouseDown, code+1);
}
mouseDown.set(code, value);
}
@Override
public synchronized int getMouseX(){
return mouseX;
}
@Override
public synchronized int getMouseY(){
return mouseY;
}
public synchronized void setMouseX(int pos){
mouseX = pos;
}
public synchronized void setMouseY(int pos){
mouseY = pos;
}
public synchronized void reset(){
mouseX = -1;
mouseY = -1;
for(int i=0; i<keyPressed.size(); i++){
keyPressed.set(i, false);
}
for(int i=0; i<mouseDown.size(); i++){
mouseDown.set(i, false);
}
}
//****************************** HELPING METHODS ********************
private void increaseSize(ArrayList<Boolean> list, int newSize) {
while(list.size() < newSize){
list.add(false);
}
}
}
Finally, here is an example of how to use it
if(input.isKeyPressed(KeyEvent.VK_D){
//execute code
}
Notes
The advantage of using this kind of input is that you can put the logic that depends on input anywhere in your code. You don’t have to put everything in the listener subclass or creating a lot of variable for keeping track of the changes.