package gameplay.input; import engine.input.GamepadInput; import engine.input.InputConst; import gameplay.input.ButtonIG; import static gameplay.input.ButtonIG.*; /** * The class handling the parsing of one input. * @author Victor * */ public class Inputs { private static final int numberOfInputs = 8; private static boolean[] tab; public Inputs() { this.tab = new boolean[numberOfInputs]; for(int i = 0; i < numberOfInputs; i ++) { this.tab[i] = false; } } /** * record one input * @param b the input to be recorded */ public void recordOneInput(ButtonIG b) { int i = b.toInt(); this.tab[i] = true; } /** * Check if a specific input (for example UP) has been recorded * @param b the button to be checked * @return */ public boolean containsInput(ButtonIG b) { 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(ButtonIG[] bs) { for(int i = 0; i < bs.length; i++) { if(!this.containsInput(bs[i])) { return false;} } return true; } /** * Check if a number of inputs are contained simultaneously, in the form of an array of Buttons. * Contrary to the other containsButtonTab, will check that only these specified buttons are pressed. * @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 equalsButtonTab(ButtonIG[] bs) { ButtonIG[] directions = {DOWN,FORWARD,UP,BACK}; ButtonIG[] buttons = {A,B,C,D}; Inputs inp = new Inputs(); for(int i = 0; i < bs.length; i++) { inp.recordOneInput(bs[i]); } if(!this.containsButtonTab(bs)){return false;} for(ButtonIG d : directions) { if(!this.containsInput(d) && inp.containsInput(d)) { return false;} if(this.containsInput(d) && !inp.containsInput(d)) { return false;} } for(ButtonIG d : buttons) { if(!this.containsInput(d) && inp.containsInput(d)) { return false;} if(this.containsInput(d) && !inp.containsInput(d)) { return false;} } return true; } public boolean[] getInputs() { return this.tab; } public void recordFromGamepad(GamepadInput pad, boolean facesRight) { this.tab[ButtonIG.UP.toInt()] = pad.checkPressed(InputConst.up); this.tab[DOWN.toInt()] = pad.checkPressed(InputConst.down); this.tab[ButtonIG.BACK.toInt()] = (pad.checkPressed(InputConst.left) && facesRight) || (pad.checkPressed(InputConst.right)&&!facesRight); this.tab[FORWARD.toInt()] = (pad.checkPressed(InputConst.right) && facesRight) || (pad.checkPressed(InputConst.left) && !facesRight); this.tab[ButtonIG.A.toInt()] = pad.checkPressed(InputConst.buttonX); this.tab[ButtonIG.B.toInt()] = pad.checkPressed(InputConst.buttonA); this.tab[ButtonIG.C.toInt()] = pad.checkPressed(InputConst.buttonY); this.tab[ButtonIG.D.toInt()] = pad.checkPressed(InputConst.buttonB); } public String toString() { String s = ""; for(int i = 0; i < numberOfInputs; i++) { if(this.tab[i]) {s = s + ButtonIG.intToButton(i).toString() + " "; } } return s; } /** * Clears the input (puts them all to false) */ public void clear(){ for(int i = 0; i < numberOfInputs; i++) { this.tab[i] = false; } } }