ProjectZombie/src/shootergame/input/JoystickCallback.java

213 lines
5.2 KiB
Java

package shootergame.input;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWJoystickCallbackI;
import mainloop.task.IMainloopTask;
import shootergame.Main;
public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
{
public static final JoystickCallback JOYSTICK_CALLBACK = new JoystickCallback();
private ArrayList<Integer> connections = new ArrayList<Integer>();
private static boolean throwTnt_last = false;
private static boolean activate_last = false;
@Override
public void invoke(int jid, int event)
{
// Gamepad connect
if(event == GLFW.GLFW_CONNECTED)
{
// Log the event and add the connection
System.out.println("Gamepad "+jid+" connected");
connections.add(jid);
}
// Gamepad disconnect
else if(event == GLFW.GLFW_DISCONNECTED)
{
// Log the event
System.out.println("Gamepad "+jid+" disconnected");
// Loop over the connections
for(int i=0;i<connections.size();i++)
{
// Is this connection the connection that disconnected
if(connections.get(i).intValue() == jid)
{
// Remove the connection and reduce i
connections.remove(i);
i -= 1;
}
}
}
}
public void init()
{
// Loop over all the gamepads
for(int i=0;i<=GLFW.GLFW_JOYSTICK_LAST;i++)
{
// Is this gamepad present; add the gamepad
if(GLFW.glfwJoystickPresent(i)) {
if(GLFW.glfwJoystickIsGamepad(i)) {
System.out.println("Gamepad "+i+" connected");
connections.add(i);
}
}
}
}
@Override
public boolean MainLoopDelay(long millis) {
return millis > 10;
}
@Override
public boolean MainLoopRepeat() {
return true;
}
private float combineJoystickAxis(float a, float b)
{
if(b > 0.3 || b < -0.3) a += b;
return a;
}
@Override
public void MainLoopUpdate()
{
// Gamepad properties
float left_x = 0;
float left_y = 0;
float left_trigger = 0;
float right_x = 0;
float right_y = 0;
float right_trigger = 0;
boolean left_stick_button = false;
boolean right_stick_button = false;
boolean button_a = false;
boolean button_b = false;
boolean button_x = false;
boolean button_y = false;
boolean button_start = false;
boolean button_back = false;
boolean button_home = false;
boolean dpad_up = false;
boolean dpad_down = false;
boolean dpad_left = false;
boolean dpad_right = false;
boolean shoulder_left = false;
boolean shoulder_right = false;
// Loop over all the connected gamepads
for(int jid : connections)
{
// Get all the axes
FloatBuffer axes = GLFW.glfwGetJoystickAxes(jid);
// Store all the axes data
left_x = combineJoystickAxis(left_x, axes.get(0));
left_y = combineJoystickAxis(left_y, axes.get(1));
right_x = combineJoystickAxis(right_x, axes.get(3));
right_y = combineJoystickAxis(right_y, axes.get(4));
left_trigger = combineJoystickAxis(left_trigger, axes.get(2));
right_trigger = combineJoystickAxis(right_trigger, axes.get(5));
// Get all the buttons
ByteBuffer buttons = GLFW.glfwGetJoystickButtons(jid);
// Store all the button data
button_a = buttons.get(0) == 1 || button_a;
button_b = buttons.get(1) == 1 || button_b;
button_x = buttons.get(2) == 1 || button_x;
button_y = buttons.get(3) == 1 || button_y;
shoulder_left = buttons.get(4) == 1 || shoulder_left;
shoulder_right = buttons.get(5) == 1 || shoulder_right;
button_back = buttons.get(6) == 1 || button_back;
button_start = buttons.get(7) == 1 || button_start;
button_home = buttons.get(8) == 1 || button_home;
left_stick_button = buttons.get(9) == 1 || left_stick_button;
right_stick_button = buttons.get(10) == 1 || right_stick_button;
dpad_left = buttons.get(11) == 1 || buttons.get(18) == 1 || dpad_left;
dpad_right = buttons.get(12) == 1 || buttons.get(16) == 1 || dpad_right;
dpad_up = buttons.get(13) == 1 || buttons.get(15) == 1 || dpad_up;
dpad_down = buttons.get(14) == 1 || buttons.get(17) == 1 || dpad_down;
}
// Is the left stick moved into a position (movement stick)
if(left_x > 0.3 || left_x < -0.3 || left_y > 0.3 || left_y < -0.3)
{
// Get the the angle
double angle = Math.toDegrees(Math.atan2(left_y, left_x)) + 90;
// Move the player in the left sticks angle
Main.player.moveTowards(angle);
// Set the players moving to true
Main.player.moving = true;
}
// Set the players moving to false
else Main.player.moving = false;
// Is the right x axis stick moved into a position (camera stick)
if(right_x > 0.3 || right_x < -0.3) {
Main.player.angle += right_x;
}
// Gun trigger
if(right_trigger > 0.3) {
Main.player.fireBullet(0);
}
// Tnt trigger
if(left_trigger > 0.3) {
if(!throwTnt_last)
{
throwTnt_last = true;
Main.player.throwTnt(0);
}
}
else {
throwTnt_last = false;
}
if(dpad_up) {
Main.player.fireBullet(0);
}
if(dpad_down) {
Main.player.fireBullet(180);
}
if(dpad_left) {
Main.player.fireBullet(270);
}
if(dpad_right) {
Main.player.fireBullet(90);
}
// Activate block
if(button_x) {
if(!activate_last) {
Main.player.activateTile();
activate_last = true;
}
}
else if(activate_last) {
activate_last = false;
}
}
}