added gamepadInputClass

This commit is contained in:
Léo 2021-05-31 15:36:40 +02:00
parent f2b2fa9e9b
commit 888e5825ba
3 changed files with 83 additions and 12 deletions

View File

@ -24,7 +24,7 @@ public class Engine {
private final List<ObjectGl> objectsGl; private final List<ObjectGl> objectsGl;
private final static int buttonA=0, buttonB=1, buttonX=3, buttonY=4;
private boolean running; private boolean running;
@ -210,16 +210,13 @@ public class Engine {
boolean nextFrame = false; boolean nextFrame = false;
boolean present = glfwJoystickPresent(GLFW_JOYSTICK_1); boolean present = glfwJoystickPresent(GLFW_JOYSTICK_1);
int buttonA=0;
int buttonB=1;
int buttonX=3;
int buttonY=4;
while (engine.isRunning()) { while (engine.isRunning()) {
lastFrame = System.currentTimeMillis(); lastFrame = System.currentTimeMillis();
// Game logic should fit here // Game logic should fit here
if (present) { if (present) { //sprite //bindings
gamepadInput.gamepadInput(zangief, speed, buttonA, buttonB, buttonX, buttonY); gamepadInput.gamepadInput(zangief, speed, buttonA, buttonB, buttonX, buttonY);
} }

View File

@ -15,6 +15,7 @@ import static org.lwjgl.opengl.GL11.*;
public class Input extends GLFWKeyCallback { public class Input extends GLFWKeyCallback {
public static boolean[] keys = new boolean[65536]; public static boolean[] keys = new boolean[65536];
public static boolean keyPressed = false;
@Override @Override
public void invoke(long window, int key, int scancode, int action, int mods) { public void invoke(long window, int key, int scancode, int action, int mods) {
@ -37,9 +38,6 @@ public class Input extends GLFWKeyCallback {
assert gamepadAxes != null; assert gamepadAxes != null;
assert gamepadButton != null; assert gamepadButton != null;
String name = GLFW.glfwGetJoystickName(GLFW_JOYSTICK_1);
System.out.println("GamePad Name :" + name);
if (gamepadButton.get(0) ==1 ) { // appuie sur croix(PlayStation) A (Xbox) if (gamepadButton.get(0) ==1 ) { // appuie sur croix(PlayStation) A (Xbox)
@ -48,9 +46,9 @@ public class Input extends GLFWKeyCallback {
if ( (gamepadAxes.get(2) < -0.1 || gamepadAxes.get(2) > 0.1) ) { // de droite à gauche //joystick gauche if ( (gamepadAxes.get(2) < -0.1 || gamepadAxes.get(2) > 0.1) ) { // de droite à gauche //joystick gauche
token.translate(new Vector3f (5 * speed * gamepadAxes.get(2), 0.0f, 0.0f)); token.translate(new Vector3f (5 * speed * gamepadAxes.get(2), 0.0f, 0.0f));
if ( gamepadAxes.get(2) < -0.1 ){ if ( gamepadAxes.get(2) < -0.1 ){
token.setTextureWrap(121,0,57,80, ObjectGl.DEFAULT); token.setTextureWrap(121,0,57,80, ObjectGl.STICK_TOP);
}else if (gamepadAxes.get(2) > 0.1) { }else if (gamepadAxes.get(2) > 0.1) {
token.setTextureWrap(178,0,62,82, ObjectGl.DEFAULT); token.setTextureWrap(178,0,62,82, ObjectGl.STICK_TOP);
} }
} }
@ -88,7 +86,7 @@ public class Input extends GLFWKeyCallback {
} }
public static void keyboardInput(ObjectGl token, int speed) { public static void keyboardInput(ObjectGl token, int speed) {
boolean keyPressed = false;
if (Input.isKeyDown(GLFW.GLFW_KEY_S)) { if (Input.isKeyDown(GLFW.GLFW_KEY_S)) {
token.setTextureWrap(161,260,56,59, ObjectGl.STICK_BOTTOM); token.setTextureWrap(161,260,56,59, ObjectGl.STICK_BOTTOM);
keyPressed = true; keyPressed = true;

View File

@ -0,0 +1,76 @@
package engine.input;
import engine.math.Vector3f;
import engine.object.ObjectGl;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWKeyCallback;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
/* Buttons
0 : Croix / A
1: rond /B
2: carré / X
3: triangle / Y
4: L1 / LB
5: R1 / RB
6:select
7:start
8:L3
9:R3
10: haut
11: droite
12: bas
13: gauche
*/
/* Axes
0 : left X axe ( right : 1 left -1)
1: left Y axe ( down : 1 , Up -1)
2: right X axe ( right : 1 left -1)
3: right Y axe ( down : 1 , Up -1)
4:L2 / LT : 1 active, -1 unactive
5: R2 /RT : 1 active, -1 unactive
*/
public class gamepadInput{
//à définir //à définir
public static void gamepadInput(ObjectGl token, int speed,int jump, int attack, int act3, int act4) { //to update for more controlBinding
ByteBuffer gamepadButton = glfwGetJoystickButtons(GLFW_JOYSTICK_1);
FloatBuffer gamepadAxes = glfwGetJoystickAxes(GLFW_JOYSTICK_1);
assert gamepadAxes != null;
assert gamepadButton != null;
if (gamepadButton.get(jump)==1 ) { // appuie sur croix(PlayStation) A (Xbox)
jump( token,speed);
}
if (gamepadButton.get(attack)==1 ) { // appuie sur rond(PlayStation) B (Xbox)
attack( token,speed);
}
if (gamepadAxes.get(3) > 0.1) {
crouch(token,speed);
}
}
public static void jump(ObjectGl token,int speed) {
token.translate(new Vector3f( 0.0f, speed * 20.0f, 0.0f));
}
public static void attack(ObjectGl token,int speed) {
}
public static void crouch(ObjectGl token,int speed) {
token.setTextureWrap(161,260,56,59, ObjectGl.STICK_BOTTOM);
}
}