Added functions to link gamepad inputs (as recognized by the engine) and the inputs defined in the gameplay classes.

This commit is contained in:
no 2021-06-02 16:00:20 +02:00
commit e83aabf56f
5 changed files with 156 additions and 16 deletions

View File

@ -178,7 +178,7 @@ public class GamepadInput {
float magnitude = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
float angle = (float) Math.toDegrees(2 * Math.atan(y /(x + magnitude)));
if (magnitude < 0.1) return CENTER;
if (magnitude < 0.3) return CENTER;
if (angle < 22.5 && angle > -22.5) return RIGHT;
else if (angle > -67.5 && angle < -22.5) return UPPER_RIGHT;
else if (angle > -112.5 && angle < -67.5) return UP;

View File

@ -16,4 +16,28 @@ public enum Button {
default : return -1;
}
}
public String toString() {
switch (this) {
case UP : return "UP";
case DOWN : return "DOWN";
case LEFT : return "LEFT";
case RIGHT : return "RIGHT";
case A : return "A";
case B : return "B";
case C : return "C";
case D : return "D";
default : return "???";
}
}
public static Button intToButton(int i) {
Button[] b = {UP, DOWN, LEFT, RIGHT, A, B, C, D};
try {
return b[i];
} catch (ArrayIndexOutOfBoundsException e) {
//TODO: put error message here
return null;
}
}
}

View File

@ -1,5 +1,7 @@
package gameplay.input;
import engine.input.GamepadInput;
public class InputBuffer {
/**
@ -72,9 +74,19 @@ public class InputBuffer {
* record a new input in the
* @param inputs a size 8 tab of inputs
*/
private void recordInputs(Inputs inputs) {
public void recordInputs(Inputs inputs) {
this.nextPos();
this.setLatestInputs(inputs);
}
/**
* records the new Inputs from a gamepad in the gamepad buffer
* @param pad the gamepad to record
*/
public void recordInputsFromGamepad(GamepadInput pad) {
Inputs in = new Inputs();
in.recordFromGamepad(pad);
this.recordInputs(in);
}
}

View File

@ -1,5 +1,8 @@
package gameplay.input;
import engine.input.GamepadInput;
import engine.input.InputConst;
/**
* The class handling the parsing of one input.
* @author Victor
@ -18,7 +21,7 @@ public class Inputs {
/**
* record one input
* @param i the integer value corresponding to the tab location
* @param b the input to be recorded
*/
public void recordOneInput(Button b) {
int i = b.toInt();
@ -27,7 +30,7 @@ public class Inputs {
/**
* Check if a specific input (for example UP) has been recorded
* @param i the integer value corresponding to the input
* @param b the button to be checked
* @return
*/
public boolean containsInput(Button b) {
@ -61,6 +64,24 @@ public class Inputs {
public boolean[] getInputs() {
return this.tab;
}
public void recordFromGamepad(GamepadInput pad) {
this.tab[Button.UP.toInt()] = pad.checkPressed(InputConst.up);
this.tab[Button.DOWN.toInt()] = pad.checkPressed(InputConst.down);
this.tab[Button.LEFT.toInt()] = pad.checkPressed(InputConst.left);
this.tab[Button.RIGHT.toInt()] = pad.checkPressed(InputConst.right);
this.tab[Button.A.toInt()] = pad.checkPressed(InputConst.buttonX);
this.tab[Button.B.toInt()] = pad.checkPressed(InputConst.buttonA);
this.tab[Button.C.toInt()] = pad.checkPressed(InputConst.buttonY);
this.tab[Button.D.toInt()] = pad.checkPressed(InputConst.buttonB);
}
public String toString() {
String s = "";
for(int i = 0; i < numberOfInputs; i++) {
if(this.tab[i]) {s = s + Button.intToButton(i).toString() + " "; }
}
return s;
}
}

View File

@ -1,9 +1,18 @@
package gameplay.match;
import engine.Engine;
import engine.input.Button;
import engine.input.GamepadInput;
import engine.input.InputConst;
import engine.object.ObjectGl;
import gameplay.input.InputBuffer;
import gameplay.entities.*;
import gameplay.entities.Character;
import engine.Engine;
import java.util.ArrayList;
import java.util.List;
import static org.lwjgl.glfw.GLFW.*;
/**
* Main class that describes the base structure of the match, with characters, timer and such
@ -39,18 +48,92 @@ public class match {
this.roundsWonP2 = 0;
}
public static void main(String[] args) throws Exception {
/*
Engine engine = new Engine(800, 600, 3.0f / 4.0f);
engine.init();
/**
* Main function of a match. Contains the main gameplay loop.
* @param args arguments
*/
public static void main(String args[]) {
Character p1 = new Character(); //TODO : change to not a blank
Character p2 = new Character(); //TODO : same as above
match match = new match(p1,p2);
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;
Button jump = null;
if (Joystick1Present) {
gamepad1 = new GamepadInput(GLFW_JOYSTICK_1);
gamepad1.inputRefresh();
List<Integer> listJump = new ArrayList<>();
listJump.add(InputConst.buttonA);
jump = new Button("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);
} */
long framestart = System.currentTimeMillis();
long frameend = System.currentTimeMillis();
int frame = 0;
boolean goToNextFrame = true;
boolean Joystick1Present = glfwJoystickPresent(GLFW_JOYSTICK_1);
GamepadInput gamepad1 = null;
match match = new match(new Character(),new Character());
if (Joystick1Present) {
gamepad1 = new GamepadInput(GLFW_JOYSTICK_1);
gamepad1.inputRefresh();
}
while(goToNextFrame) {
framestart = System.currentTimeMillis();
frame++;
gamepad1.inputRefresh();
match.inputsP1.recordInputsFromGamepad(gamepad1);
System.out.println("Frame " + frame + " " + match.inputsP1.getLatestInputs().toString());
frameend = System.currentTimeMillis();
while(frameend-framestart < (1000/60)) {
frameend = System.currentTimeMillis();
}
}
}
}