jeu-de-combat/src/engine/input/KeyboardInput.java
2021-06-02 23:20:40 +02:00

114 lines
3.7 KiB
Java

package engine.input;
import engine.Engine;
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.*;
public class KeyboardInput extends GLFWKeyCallback {
public static boolean[] keys = new boolean[65536];
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
keys[key] = action != GLFW.GLFW_RELEASE;
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
else if(key == GLFW_KEY_SPACE && action == GLFW_PRESS) //Switch to wireframe
if (glGetInteger(GL_POLYGON_MODE) == GL_FILL) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
public static boolean isKeyDown(int keyCode) {
return glfwGetKey(Engine.getWindow(), keyCode) == 1;
}
public static void gamepadInput(ObjectGl token, int speed) {
ByteBuffer gamepadButton = glfwGetJoystickButtons(GLFW_JOYSTICK_1);
FloatBuffer gamepadAxes = glfwGetJoystickAxes(GLFW_JOYSTICK_1);
assert gamepadAxes != 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)
token.translate(new Vector3f( 0.0f, speed * 5.0f, 0.0f));
}
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));
if ( gamepadAxes.get(2) < -0.1 ){
token.setTextureWrap(121,0,57,80, ObjectGl.DEFAULT);
}else if (gamepadAxes.get(2) > 0.1) {
token.setTextureWrap(178,0,62,82, ObjectGl.DEFAULT);
}
}
if ( (gamepadAxes.get(3) < -0.1 || gamepadAxes.get(3) > 0.1) ) { // de haut en bas //joystick gauche
token.translate(new Vector3f (0.0f, -5 * speed * gamepadAxes.get(3), 0.0f));
}
/* 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 static void keyboardInput(ObjectGl token, int speed) {
boolean keyPressed = false;
if (KeyboardInput.isKeyDown(GLFW.GLFW_KEY_S)) {
token.setTextureWrap(161,260,56,59, ObjectGl.STICK_BOTTOM);
keyPressed = true;
} else if (KeyboardInput.isKeyDown(GLFW.GLFW_KEY_W)) {
keyPressed = true;
token.scale(new Vector3f(1.001f,1.001f,1.0f));
}
if (KeyboardInput.isKeyDown(GLFW.GLFW_KEY_A)) {
token.translate(new Vector3f (speed * -1.0f, 0.0f, 0.0f));
token.setTextureWrap(121,0,57,82, ObjectGl.STICK_TOP);
keyPressed = true;
}
else if (KeyboardInput.isKeyDown(GLFW.GLFW_KEY_D)) {
token.translate(new Vector3f (speed * 1.0f, 0.0f, 0.0f));
token.setTextureWrap(178,0,62,82, ObjectGl.STICK_TOP);
keyPressed = true;
}
if (!keyPressed) token.setTextureWrap(58,0,62,82, ObjectGl.STICK_TOP);
token.flipTextureWrapH();
}
}