2021-05-27 17:45:02 +02:00
|
|
|
package gameplay.input;
|
2021-05-27 00:06:12 +02:00
|
|
|
|
2021-06-02 16:00:20 +02:00
|
|
|
import engine.input.GamepadInput;
|
|
|
|
|
2021-05-27 00:06:12 +02:00
|
|
|
public class InputBuffer {
|
2021-06-07 17:38:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of past frames to check for a certain input pas another one.
|
|
|
|
* For example, if you need to input DOWN, then FORWARD, and we know FORWARD has been input on frame 25,
|
|
|
|
* this indicates that you need to check for DOWN on frames 20 to 24
|
|
|
|
*/
|
|
|
|
private static final int pastFramesToCheck = 5;
|
2021-05-27 00:06:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-06-02 16:00:20 +02:00
|
|
|
public void recordInputs(Inputs inputs) {
|
2021-05-27 00:06:12 +02:00
|
|
|
this.nextPos();
|
|
|
|
this.setLatestInputs(inputs);
|
|
|
|
}
|
2021-06-02 16:00:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* records the new Inputs from a gamepad in the gamepad buffer
|
|
|
|
* @param pad the gamepad to record
|
2021-06-07 15:55:04 +02:00
|
|
|
* @param facesRight whether the character faces right or not, to know which way is forward
|
2021-06-02 16:00:20 +02:00
|
|
|
*/
|
2021-06-07 15:55:04 +02:00
|
|
|
public void recordInputsFromGamepad(GamepadInput pad, boolean facesRight) {
|
2021-06-02 16:00:20 +02:00
|
|
|
Inputs in = new Inputs();
|
2021-06-07 15:55:04 +02:00
|
|
|
in.recordFromGamepad(pad, facesRight);
|
2021-06-02 16:00:20 +02:00
|
|
|
this.recordInputs(in);
|
|
|
|
}
|
2021-06-07 17:38:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for a command to be recognized. The last input of the command has to have been input on the current frame
|
|
|
|
* @param command
|
|
|
|
* @return true if the command is recognized,false if not
|
|
|
|
*/
|
|
|
|
public boolean commandRecognized(ButtonIG[][] command) {
|
|
|
|
boolean ret = true;
|
|
|
|
int backCounter;
|
|
|
|
int startFrameCount = this.pos;
|
|
|
|
int frameToCheck;
|
|
|
|
try {
|
|
|
|
ret = this.inputList[pos].containsButtonTab(command[command.length - 1]);
|
|
|
|
} catch (ArrayIndexOutOfBoundsException e ) {
|
2021-06-17 14:26:38 +02:00
|
|
|
return false;
|
2021-06-07 17:38:35 +02:00
|
|
|
}
|
2021-06-10 14:06:15 +02:00
|
|
|
for(int i = command.length - 2; i >= 0 && ret; i--) {
|
2021-06-07 17:38:35 +02:00
|
|
|
backCounter = 1;
|
|
|
|
if(startFrameCount - backCounter < 0) {frameToCheck = this.size - (backCounter - startFrameCount);}
|
|
|
|
else {frameToCheck = startFrameCount - backCounter;}
|
|
|
|
boolean search = true;
|
|
|
|
while(ret && search) {
|
2021-06-16 12:27:49 +02:00
|
|
|
if(this.inputList[frameToCheck].equalsButtonTab(command[i])) {
|
2021-06-07 17:38:35 +02:00
|
|
|
ret = true;
|
|
|
|
search = false;
|
|
|
|
} else {
|
2021-06-10 13:54:16 +02:00
|
|
|
if(backCounter >= pastFramesToCheck) {
|
2021-06-07 17:38:35 +02:00
|
|
|
ret = false;
|
|
|
|
search = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
backCounter++;
|
|
|
|
if(startFrameCount - backCounter < 0) {frameToCheck = this.size - (backCounter - startFrameCount);}
|
|
|
|
else {frameToCheck = startFrameCount - backCounter;}
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:54:16 +02:00
|
|
|
|
2021-06-07 17:38:35 +02:00
|
|
|
}
|
2021-06-10 13:54:16 +02:00
|
|
|
startFrameCount = frameToCheck;
|
2021-06-07 17:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2021-06-09 13:14:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the inputbuffer. Clears every recorded input and puts the position back to 0.
|
|
|
|
*/
|
|
|
|
public void clear(){
|
|
|
|
for(int i = 0; i < this.size; i++) {
|
|
|
|
this.inputList[i].clear();
|
|
|
|
}
|
|
|
|
this.pos = 0;
|
|
|
|
}
|
2021-05-27 00:06:12 +02:00
|
|
|
}
|
|
|
|
|