Added functions to link gamepad inputs (as recognized by the engine) and the inputs defined in the gameplay classes.

This commit is contained in:
no
2021-06-02 16:00:20 +02:00
5 changed files with 156 additions and 16 deletions

View File

@ -16,4 +16,28 @@ public enum Button {
default : return -1;
}
}
public String toString() {
switch (this) {
case UP : return "UP";
case DOWN : return "DOWN";
case LEFT : return "LEFT";
case RIGHT : return "RIGHT";
case A : return "A";
case B : return "B";
case C : return "C";
case D : return "D";
default : return "???";
}
}
public static Button intToButton(int i) {
Button[] b = {UP, DOWN, LEFT, RIGHT, A, B, C, D};
try {
return b[i];
} catch (ArrayIndexOutOfBoundsException e) {
//TODO: put error message here
return null;
}
}
}

View File

@ -1,5 +1,7 @@
package gameplay.input;
import engine.input.GamepadInput;
public class InputBuffer {
/**
@ -72,9 +74,19 @@ public class InputBuffer {
* record a new input in the
* @param inputs a size 8 tab of inputs
*/
private void recordInputs(Inputs inputs) {
public void recordInputs(Inputs inputs) {
this.nextPos();
this.setLatestInputs(inputs);
}
/**
* records the new Inputs from a gamepad in the gamepad buffer
* @param pad the gamepad to record
*/
public void recordInputsFromGamepad(GamepadInput pad) {
Inputs in = new Inputs();
in.recordFromGamepad(pad);
this.recordInputs(in);
}
}

View File

@ -1,5 +1,8 @@
package gameplay.input;
import engine.input.GamepadInput;
import engine.input.InputConst;
/**
* The class handling the parsing of one input.
* @author Victor
@ -18,7 +21,7 @@ public class Inputs {
/**
* record one input
* @param i the integer value corresponding to the tab location
* @param b the input to be recorded
*/
public void recordOneInput(Button b) {
int i = b.toInt();
@ -27,7 +30,7 @@ public class Inputs {
/**
* Check if a specific input (for example UP) has been recorded
* @param i the integer value corresponding to the input
* @param b the button to be checked
* @return
*/
public boolean containsInput(Button b) {
@ -61,6 +64,24 @@ public class Inputs {
public boolean[] getInputs() {
return this.tab;
}
public void recordFromGamepad(GamepadInput pad) {
this.tab[Button.UP.toInt()] = pad.checkPressed(InputConst.up);
this.tab[Button.DOWN.toInt()] = pad.checkPressed(InputConst.down);
this.tab[Button.LEFT.toInt()] = pad.checkPressed(InputConst.left);
this.tab[Button.RIGHT.toInt()] = pad.checkPressed(InputConst.right);
this.tab[Button.A.toInt()] = pad.checkPressed(InputConst.buttonX);
this.tab[Button.B.toInt()] = pad.checkPressed(InputConst.buttonA);
this.tab[Button.C.toInt()] = pad.checkPressed(InputConst.buttonY);
this.tab[Button.D.toInt()] = pad.checkPressed(InputConst.buttonB);
}
public String toString() {
String s = "";
for(int i = 0; i < numberOfInputs; i++) {
if(this.tab[i]) {s = s + Button.intToButton(i).toString() + " "; }
}
return s;
}
}