From 2710c91b002f1b18a9852a41440e52564cdd9776 Mon Sep 17 00:00:00 2001 From: no Date: Thu, 27 May 2021 14:13:02 +0200 Subject: [PATCH] Added Handling of Inputs recognition. --- GamePlay/input/InputBuffer.java | 27 +++++++++------------------ GamePlay/input/Inputs.java | 31 ++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/GamePlay/input/InputBuffer.java b/GamePlay/input/InputBuffer.java index 570eabd..058e4b0 100644 --- a/GamePlay/input/InputBuffer.java +++ b/GamePlay/input/InputBuffer.java @@ -2,8 +2,6 @@ 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 @@ -11,7 +9,7 @@ public class InputBuffer { * if the value at the corresponding index is true, then the input is pressed * By default, no input is pressed. */ - private Boolean[][] inputList; + private Inputs[] inputList; /* * the size of the input buffer @@ -31,19 +29,17 @@ public class InputBuffer { public InputBuffer() { this.size = 1; this.pos = 0; - this.inputList = new Boolean[1][numberOfInputs]; - setLatestInputs(baseInputs); + this.inputList = new Inputs[1]; } public InputBuffer(int size) { this.size = size; 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 j = 0; j < numberOfInputs; j++) { - this.inputList[i][j] = baseInputs[j];} - } + this.inputList[i] = new Inputs(); + } } @@ -51,7 +47,7 @@ public class InputBuffer { /** * @return the latest added inputs */ - public Boolean[] getLatestInputs() { + public Inputs getLatestInputs() { return this.inputList[this.pos]; } @@ -60,13 +56,8 @@ public class InputBuffer { * 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 ? - } + private void setLatestInputs(Inputs inputs) { + this.inputList[pos] = inputs; } /** @@ -81,7 +72,7 @@ public class InputBuffer { * record a new input in the * @param inputs a size 8 tab of inputs */ - private void recordInputs(Boolean[] inputs) { + private void recordInputs(Inputs inputs) { this.nextPos(); this.setLatestInputs(inputs); } diff --git a/GamePlay/input/Inputs.java b/GamePlay/input/Inputs.java index c34934f..769a029 100644 --- a/GamePlay/input/Inputs.java +++ b/GamePlay/input/Inputs.java @@ -20,12 +20,9 @@ public class Inputs { * record one input * @param i the integer value corresponding to the tab location */ - public void recordOneInput(int i) { - try { + public void recordOneInput(Button b) { + int i = b.toInt(); 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()]; } + /** + * 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() { return this.tab; }