jeu-de-combat/GamePlay/input/InputBuffer.java

81 lines
1.6 KiB
Java

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.
*/
private Inputs[] 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 Inputs[1];
}
public InputBuffer(int size) {
this.size = size;
this.pos = 0;
this.inputList = new Inputs[this.size];
for(int i = 0; i < this.size; i++) {
this.inputList[i] = new Inputs();
}
}
/**
* @return the latest added inputs
*/
public Inputs getLatestInputs() {
return this.inputList[this.pos];
}
/**
* Sets the last input without moving the current position
* @param inputs
*/
private void setLatestInputs(Inputs inputs) {
this.inputList[pos] = inputs;
}
/**
* 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(Inputs inputs) {
this.nextPos();
this.setLatestInputs(inputs);
}
}