Added Handling of Inputs recognition.

This commit is contained in:
no 2021-05-27 14:13:02 +02:00
parent e02acbcf5c
commit 2710c91b00
2 changed files with 35 additions and 23 deletions

View File

@ -2,8 +2,6 @@ package input;
public class InputBuffer { public class InputBuffer {
private static final Boolean[] baseInputs = {false,false,false,false,false,false,false,false};
/** /**
* a list of various inputs being recorded, such as inputs pressed at each frame * a list of various inputs being recorded, such as inputs pressed at each frame
* Each element is a tab where each element represent a possible input * Each element is a tab where each element represent a possible input
@ -11,7 +9,7 @@ public class InputBuffer {
* if the value at the corresponding index is true, then the input is pressed * if the value at the corresponding index is true, then the input is pressed
* By default, no input is pressed. * By default, no input is pressed.
*/ */
private Boolean[][] inputList; private Inputs[] inputList;
/* /*
* the size of the input buffer * the size of the input buffer
@ -31,18 +29,16 @@ public class InputBuffer {
public InputBuffer() { public InputBuffer() {
this.size = 1; this.size = 1;
this.pos = 0; this.pos = 0;
this.inputList = new Boolean[1][numberOfInputs]; this.inputList = new Inputs[1];
setLatestInputs(baseInputs);
} }
public InputBuffer(int size) { public InputBuffer(int size) {
this.size = size; this.size = size;
this.pos = 0; this.pos = 0;
this.inputList = new Boolean[this.size][numberOfInputs]; this.inputList = new Inputs[this.size];
for(int i = 0; i < this.size; i++) { for(int i = 0; i < this.size; i++) {
for(int j = 0; j < numberOfInputs; j++) { this.inputList[i] = new Inputs();
this.inputList[i][j] = baseInputs[j];}
} }
} }
@ -51,7 +47,7 @@ public class InputBuffer {
/** /**
* @return the latest added inputs * @return the latest added inputs
*/ */
public Boolean[] getLatestInputs() { public Inputs getLatestInputs() {
return this.inputList[this.pos]; return this.inputList[this.pos];
} }
@ -60,13 +56,8 @@ public class InputBuffer {
* Sets the last input without moving the current position * Sets the last input without moving the current position
* @param inputs * @param inputs
*/ */
private void setLatestInputs(Boolean[] inputs) { private void setLatestInputs(Inputs inputs) {
try { this.inputList[pos] = inputs;
for(int i = 0; i < 8; i++) {
this.inputList[pos][i] = inputs[i];
}
} catch (ArrayIndexOutOfBoundsException e) { //TODO : what do we do in this case ?
}
} }
/** /**
@ -81,7 +72,7 @@ public class InputBuffer {
* record a new input in the * record a new input in the
* @param inputs a size 8 tab of inputs * @param inputs a size 8 tab of inputs
*/ */
private void recordInputs(Boolean[] inputs) { private void recordInputs(Inputs inputs) {
this.nextPos(); this.nextPos();
this.setLatestInputs(inputs); this.setLatestInputs(inputs);
} }

View File

@ -20,12 +20,9 @@ public class Inputs {
* record one input * record one input
* @param i the integer value corresponding to the tab location * @param i the integer value corresponding to the tab location
*/ */
public void recordOneInput(int i) { public void recordOneInput(Button b) {
try { int i = b.toInt();
this.tab[i] = true; this.tab[i] = true;
} catch (ArrayIndexOutOfBoundsException e) {
//TODO : what do we do here ? Error message ?
}
} }
/** /**
@ -37,6 +34,30 @@ public class Inputs {
return this.tab[b.toInt()]; return this.tab[b.toInt()];
} }
/**
* Check if a number of inputs are contained simultaneously
* @param in a number of inputs. Check if those are containes in this
* @return true if all inputs of in are also in this
*/
public boolean containsInputs(Inputs in) {
for(int i = 0; i < numberOfInputs; i++) {
if(this.tab[i] != in.getInputs()[i]) {return false;}
}
return true;
}
/**
* Check if a number of inputs are contained simultaneously, in the form of an array of Buttons
* @param bs a number of inputs. Check if those are contained in this
* @return true if all inputs of in are also in this
*/
public boolean containsButtonTab(Button[] bs) {
for(int i = 0; i < bs.length; i++) {
if(!this.containsInput(bs[i])) { return false;}
}
return true;
}
public boolean[] getInputs() { public boolean[] getInputs() {
return this.tab; return this.tab;
} }