87 lines
1.9 KiB
Java
87 lines
1.9 KiB
Java
|
package input;
|
||
|
|
||
|
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
|
||
|
* Each element is a tab where each element represent a possible input
|
||
|
* (UP, Down, Right, Left, A, B, C, D)
|
||
|
* if the value at the corresponding index is true, then the input is pressed
|
||
|
* By default, no input is pressed.
|
||
|
*/
|
||
|
private Boolean[][] inputList;
|
||
|
|
||
|
/*
|
||
|
* the size of the input buffer
|
||
|
*/
|
||
|
private int size;
|
||
|
|
||
|
/*
|
||
|
* the current position in the tab of the last newest input recorded
|
||
|
* This should never bee accessed outside of this class.
|
||
|
*/
|
||
|
private int pos;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* base constructor of the InputBuffer. Creates a buffer of size 1, with every input empty
|
||
|
*/
|
||
|
public InputBuffer() {
|
||
|
this.size = 1;
|
||
|
this.pos = 0;
|
||
|
this.inputList = new Boolean[1][8];
|
||
|
setLatestInputs(baseInputs);
|
||
|
}
|
||
|
|
||
|
public InputBuffer(int size) {
|
||
|
this.size = size;
|
||
|
this.pos = 0;
|
||
|
this.inputList = new Boolean[this.size][8];
|
||
|
|
||
|
for(int i = 0; i < this.size; i++) {this.inputList[i] = baseInputs;}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @return the latest added inputs
|
||
|
*/
|
||
|
public Boolean[] getLatestInputs() {
|
||
|
return this.inputList[this.pos];
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Sets the last input without moving the current position
|
||
|
* @param inputs
|
||
|
*/
|
||
|
private void setLatestInputs(Boolean[] inputs) {
|
||
|
try {
|
||
|
for(int i = 0; i < 8; i++) {
|
||
|
this.inputList[pos][i] = inputs[i];
|
||
|
}
|
||
|
} catch (ArrayIndexOutOfBoundsException e) { //TODO : what do we do in this case ?
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* advances the current position to the next one (goes back to the first if at the end
|
||
|
*/
|
||
|
private void nextPos() {
|
||
|
this.pos++;
|
||
|
if(this.pos == size) {this.pos = 0;}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* record a new input in the
|
||
|
* @param inputs a size 8 tab of inputs
|
||
|
*/
|
||
|
private void recordInputs(Boolean[] inputs) {
|
||
|
this.nextPos();
|
||
|
this.setLatestInputs(inputs);
|
||
|
}
|
||
|
}
|
||
|
|