2021-05-27 00:06:12 +02:00
|
|
|
package input;
|
|
|
|
|
|
|
|
public class InputBuffer {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-05-27 14:13:02 +02:00
|
|
|
private Inputs[] inputList;
|
2021-05-27 00:06:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2021-05-27 14:13:02 +02:00
|
|
|
this.inputList = new Inputs[1];
|
2021-05-27 00:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public InputBuffer(int size) {
|
|
|
|
this.size = size;
|
|
|
|
this.pos = 0;
|
2021-05-27 14:13:02 +02:00
|
|
|
this.inputList = new Inputs[this.size];
|
2021-05-27 00:06:12 +02:00
|
|
|
|
2021-05-27 13:03:29 +02:00
|
|
|
for(int i = 0; i < this.size; i++) {
|
2021-05-27 14:13:02 +02:00
|
|
|
this.inputList[i] = new Inputs();
|
|
|
|
}
|
2021-05-27 00:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the latest added inputs
|
|
|
|
*/
|
2021-05-27 14:13:02 +02:00
|
|
|
public Inputs getLatestInputs() {
|
2021-05-27 00:06:12 +02:00
|
|
|
return this.inputList[this.pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the last input without moving the current position
|
|
|
|
* @param inputs
|
|
|
|
*/
|
2021-05-27 14:13:02 +02:00
|
|
|
private void setLatestInputs(Inputs inputs) {
|
|
|
|
this.inputList[pos] = inputs;
|
2021-05-27 00:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-05-27 14:13:02 +02:00
|
|
|
private void recordInputs(Inputs inputs) {
|
2021-05-27 00:06:12 +02:00
|
|
|
this.nextPos();
|
|
|
|
this.setLatestInputs(inputs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|