package gameplay.match; import engine.input.Button; import engine.input.GamepadInput; import gameplay.actions.Attack; import gameplay.entities.Status; import gameplay.input.InputBuffer; import gameplay.entities.Character; import gameplay.input.Inputs; import gameplay.input.ButtonIG; import static org.lwjgl.glfw.GLFW.*; /** * Main class that describes the base structure of the match, with characters, timer and such * @author Victor Azra * */ public class match { /** * the number of inputs read for each character, a.k.a. for how many frames the inputs are saved in memory. */ private static final int inputBufferSize = 120; private int timer; private InputBuffer inputsP1, inputsP2; private int hpP1, hpP2; private int roundsWonP1, roundsWonP2; private Character p1, p2; //characters of player 1 and 2 private static long timeStamp1; private static long timeStamp2; private static int frameCount; private static GamepadInput gamepad1 = null; private static GamepadInput gamepad2 = null; /** * base constructor of the match class. * Initiates a new match with with two given characters */ public match(Character p1, Character p2) { this.timer = 99; this.inputsP1 = new InputBuffer(inputBufferSize); this.inputsP2 = new InputBuffer(inputBufferSize); this.p1 = p1; this.p2 = p2; this.hpP1 = p1.getMaxHP(); this.hpP2 = p2.getMaxHP(); this.roundsWonP1 = 0; this.roundsWonP2 = 0; } /** * Starts a new round, by placing the timer back at base value, characters back at full hp and such. */ private void startNewRound() { this.timer = 99; this.inputsP1 = new InputBuffer(inputBufferSize); this.inputsP2 = new InputBuffer(inputBufferSize); this.hpP1 = p1.getMaxHP(); this.hpP2 = p2.getMaxHP(); this.p1.setPos(-500, 250); //TODO : change to better values if needed this.p2.setPos(500, 250); //TODO : change to better values if needed } /** * Ends the round. * Used for playing animations and such. * TODO : Implement this once we know what to do. */ private void endRound() { } /** * Ends the match. * Used for playing animations and such. * TODO : Implement this once we know what to do. */ private void endMatch() { } public static void main(String[] args) throws Exception { /* Engine engine = new Engine(800, 600, 3.0f / 4.0f); engine.init(); String path = "textures/zangief_sprite.png"; ObjectGl zangief = new ObjectGl(0f, 60f, 80f, 10f, path, null); zangief.setTextureWrap(58, 0, 62, 84, ObjectGl.STICK_TOP); engine.add_objectGl(zangief); long timer = System.currentTimeMillis(); long lastFrame; int frame = 0; boolean nextFrame = false; boolean Joystick1Present = glfwJoystickPresent(GLFW_JOYSTICK_1); GamepadInput gamepad1 = null; ButtonIG jump = null; if (Joystick1Present) { gamepad1 = new GamepadInput(GLFW_JOYSTICK_1); gamepad1.inputRefresh(); List listJump = new ArrayList<>(); listJump.add(InputConst.buttonA); jump = new ButtonIG("jump", listJump, gamepad1); } while (engine.isRunning()) { lastFrame = System.currentTimeMillis(); // Game logic should fit here if (Joystick1Present) { gamepad1.inputRefresh(); System.out.println(gamepad1.getAxisDiscreet(GLFW_GAMEPAD_AXIS_LEFT_X)); // Check si le personnage a sauté if (jump.isButtonPressed()) { // Le personnage saute System.out.println(" JE SAUTE "); } } engine.update(); engine.render(); frame++; if (System.currentTimeMillis() - timer > 1000) { timer += 1000; System.out.println("FPS: " + frame); frame = 0; } while (!nextFrame) { nextFrame = System.currentTimeMillis() - lastFrame >= 16.66f; } nextFrame = false; if (engine.shouldClose()) engine.setRunning(false); } */ int frame = 0; boolean goToNextFrame = true; boolean Joystick1Present = glfwJoystickPresent(GLFW_JOYSTICK_1); boolean Joystick2Present = glfwJoystickPresent(GLFW_JOYSTICK_2); match match = new match(new Character(),new Character()); //TOD0 : Change to not empty chars if (Joystick1Present) { gamepad1 = new GamepadInput(GLFW_JOYSTICK_1); gamepad1.inputRefresh(); } if(Joystick2Present) { gamepad2 = new GamepadInput(GLFW_JOYSTICK_2); gamepad2.inputRefresh(); } } private void ac(int i) { switch (i) { //initiate a round case 0 : startNewRound(); timeStamp1 = System.currentTimeMillis(); frameCount = 0; ac(10); break; //checks if one or both of the chars are out of health case 10: if(this.hpP1 <= 0 && hpP2 <= 0) { ac(11);} else if(this.hpP1 <= 0) { ac(12);} else if(this.hpP2 <= 0) { ac(13);} else { ac(20);} break; //end round case 11: endRound(); if(roundsWonP1 >= 2||roundsWonP2 >= 2) { endMatch();} //TODO : will probably need to specify more else{ac(0);} break; //if p1 is at 0 health case 12: roundsWonP2++; ac(11); break; //if p2 is at 0 health case 13: roundsWonP1++; ac(11); break; //read both players inputs case 20: gamepad1.inputRefresh(); gamepad2.inputRefresh(); inputsP1.recordInputsFromGamepad(gamepad1, p1.getPosX() < p2.getPosX()); inputsP2.recordInputsFromGamepad(gamepad2, p2.getPosX() <= p1.getPosX()); handleInputs(p1, inputsP1); handleInputs(p2, inputsP2); } } /** * Will handle the inputs recorder and have the character do a corresponding action if possible * Order of priority is Throw > Special > Normal > Jump > Dash > Crouch > Move > do nothing * @param c * @param input */ private static void handleInputs(Character c, InputBuffer input) { Inputs latestIn = input.getLatestInputs(); boolean actionSet = false; if(latestIn.containsButtonTab(c.getNormalthrow().getCommand()[0]) && c.getCurrentframe().isNormalCancellable()) { c.clearNextFrames(); c.addNextFramesList(c.getNormalthrow().getFrame()); actionSet = true; } else { int atkCount = 0; //do an attack if possible while(atkCount < c.getAttacks().length && !actionSet) { Attack atk = c.getAttacks()[atkCount]; Boolean attackIsPossible = input.commandRecognized(atk.getCommand()) && atk.getRequiredStatus().equals(c.getStatus()) && ((atk.isSpecial() && c.getCurrentframe().isSpecialCancellable()) || (!atk.isSpecial() && c.getCurrentframe().isNormalCancellable())); if(attackIsPossible) { c.clearNextFrames(); c.addNextFramesList(atk.getFrame()); actionSet = true; } atkCount++; } if(c.getCurrentframe().isJumpCancellable() && !actionSet) { if (input.commandRecognized(c.getForwardJump().getCommand())) { c.clearNextFrames(); c.addNextFramesList(c.getForwardJump().getFrame()); actionSet = true; c.setStatus(Status.JUMPING); } else if (input.commandRecognized(c.getBackJump().getCommand())) { c.clearNextFrames(); c.addNextFramesList(c.getForwardJump().getFrame()); actionSet = true; c.setStatus(Status.JUMPING); } else if (input.commandRecognized(c.getNeutralJump().getCommand())) { c.clearNextFrames(); c.addNextFramesList(c.getNeutralJump().getFrame()); actionSet = true; c.setStatus(Status.JUMPING); } } if(c.getCurrentframe().isDashCancellable() && !actionSet) { if (input.commandRecognized(c.getForwardDash().getCommand())) { c.clearNextFrames(); c.addNextFramesList(c.getForwardDash().getFrame()); actionSet = true; } else if (input.commandRecognized(c.getBackDash().getCommand())) { c.clearNextFrames(); c.addNextFramesList(c.getBackDash().getFrame()); actionSet = true; } } if(c.getCurrentframe().isMoveCancellable() && !actionSet) { if(input.getLatestInputs().containsInput(ButtonIG.DOWN)) { c.clearNextFrames(); c.addNextFrames(c.getDefaultCrouchingFrames()); } else if(input.getLatestInputs().containsInput(ButtonIG.BACK)) { c.clearNextFrames(); c.addNextFrames(c.getBackWalkFrames()); } if(input.getLatestInputs().containsInput(ButtonIG.FORWARD)) { c.clearNextFrames(); c.addNextFrames(c.getForwardWalkFrames()); } } } } }