Add and apply editorconfig file.
parent
3f4eafbef8
commit
eaebc0a56c
|
@ -0,0 +1,11 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.yml]
|
||||
indent_size = 2
|
|
@ -8,28 +8,28 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
public final class ControllerListener extends ControllerAdapter {
|
||||
private final List<Controller> controllers = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void connected(final Controller controller) {
|
||||
System.err.println("Controller connected: " + controller.getName());
|
||||
|
||||
// Only add controllers that we know how to handle.
|
||||
if (controller.getName().equals(XBOX360_WIRELESS_RECEIVER)) {
|
||||
controllers.add(controller);
|
||||
} else {
|
||||
System.err.println("Unknown controller type.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected(final Controller controller) {
|
||||
System.err.println("Controller disconnected: " + controller.getName());
|
||||
|
||||
controllers.remove(controller);
|
||||
}
|
||||
|
||||
public List<Controller> getControllers() {
|
||||
return controllers;
|
||||
}
|
||||
private final List<Controller> controllers = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void connected(final Controller controller) {
|
||||
System.err.println("Controller connected: " + controller.getName());
|
||||
|
||||
// Only add controllers that we know how to handle.
|
||||
if (controller.getName().equals(XBOX360_WIRELESS_RECEIVER)) {
|
||||
controllers.add(controller);
|
||||
} else {
|
||||
System.err.println("Unknown controller type.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected(final Controller controller) {
|
||||
System.err.println("Controller disconnected: " + controller.getName());
|
||||
|
||||
controllers.remove(controller);
|
||||
}
|
||||
|
||||
public List<Controller> getControllers() {
|
||||
return controllers;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
package sovereignstudios.helhalla.input;
|
||||
|
||||
public interface InputSource {
|
||||
/**
|
||||
* Some inputs are continuous (like movement keys or joystick position),
|
||||
* but some are discrete (like attacking).
|
||||
* Since one press of the attack key/button should correspond with one attack rather than continuous auto-attacking,
|
||||
* the input should only be read as the attack button being pressed a single time.
|
||||
* Thus, this method will have the side effect of resetting all discrete input events.
|
||||
* For this reason, make sure you only read the input once per frame,
|
||||
* or you risk losing inputs like that.
|
||||
*
|
||||
* Furthermore, if you've been ignoring the input for a while for whatever reason,
|
||||
* make sure you read it once to clear discrete events so that the player
|
||||
* doesn't e.g. accidentally attack after coming back from a pause
|
||||
* because they accidentally pressed the attack button when they put down the controller two hours ago
|
||||
* (or whatever).
|
||||
*/
|
||||
InputState readInput();
|
||||
/**
|
||||
* Some inputs are continuous (like movement keys or joystick position),
|
||||
* but some are discrete (like attacking).
|
||||
* Since one press of the attack key/button should correspond with one attack rather than continuous auto-attacking,
|
||||
* the input should only be read as the attack button being pressed a single time.
|
||||
* Thus, this method will have the side effect of resetting all discrete input events.
|
||||
* For this reason, make sure you only read the input once per frame,
|
||||
* or you risk losing inputs like that.
|
||||
*
|
||||
* Furthermore, if you've been ignoring the input for a while for whatever reason,
|
||||
* make sure you read it once to clear discrete events so that the player
|
||||
* doesn't e.g. accidentally attack after coming back from a pause
|
||||
* because they accidentally pressed the attack button when they put down the controller two hours ago
|
||||
* (or whatever).
|
||||
*/
|
||||
InputState readInput();
|
||||
}
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
package sovereignstudios.helhalla.input;
|
||||
|
||||
public interface InputState {
|
||||
/**
|
||||
* The orientation of the player relative to the world, in radians.
|
||||
*
|
||||
* With keyboard input, this will always be up, down, left, right, or a diagonal;
|
||||
* with a joystick, this can be any orientation.
|
||||
*/
|
||||
float getOrientation();
|
||||
|
||||
/**
|
||||
* What proportion (0.0-1.0) of the player's maximum acceleration are they trying to use.
|
||||
*
|
||||
* This essentially corresponds with how hard the player is pressing the 'move' input.
|
||||
* In the case of a keyboard, the button is either pressed or not, so this will always be 0.0 or 1.0.
|
||||
* With a joystick, this corresponds with how far they are pushing the joystick as a proportion of its maximum range.
|
||||
*/
|
||||
float getAcceleration();
|
||||
|
||||
/**
|
||||
* Has the player pressed the 'attack' button.
|
||||
*/
|
||||
boolean isAttacking();
|
||||
/**
|
||||
* The orientation of the player relative to the world, in radians.
|
||||
*
|
||||
* With keyboard input, this will always be up, down, left, right, or a diagonal;
|
||||
* with a joystick, this can be any orientation.
|
||||
*/
|
||||
float getOrientation();
|
||||
|
||||
/**
|
||||
* What proportion (0.0-1.0) of the player's maximum acceleration are they trying to use.
|
||||
*
|
||||
* This essentially corresponds with how hard the player is pressing the 'move' input.
|
||||
* In the case of a keyboard, the button is either pressed or not, so this will always be 0.0 or 1.0.
|
||||
* With a joystick, this corresponds with how far they are pushing the joystick as a proportion of its maximum range.
|
||||
*/
|
||||
float getAcceleration();
|
||||
|
||||
/**
|
||||
* Has the player pressed the 'attack' button.
|
||||
*/
|
||||
boolean isAttacking();
|
||||
}
|
||||
|
|
|
@ -4,100 +4,124 @@ import com.badlogic.gdx.Input.Keys;
|
|||
import com.badlogic.gdx.InputAdapter;
|
||||
|
||||
public final class KeyboardInputSource extends InputAdapter implements InputSource {
|
||||
/** Left and down are negative, right and up are positive. */
|
||||
private int x = 0, y = 0;
|
||||
private boolean attacking = false;
|
||||
|
||||
@Override
|
||||
public boolean keyDown(final int keycode) {
|
||||
switch (keycode) {
|
||||
case Keys.W:
|
||||
case Keys.UP:
|
||||
if (y < 0) { y = 0; }
|
||||
else if (y == 0) { y = 1; }
|
||||
break;
|
||||
|
||||
case Keys.S:
|
||||
case Keys.DOWN:
|
||||
if (y > 0) { y = 0; }
|
||||
else if (y == 0) { y = -1; }
|
||||
break;
|
||||
|
||||
case Keys.A:
|
||||
case Keys.LEFT:
|
||||
if (x > 0) { x = 0; }
|
||||
else if (x == 0) { x = -1; }
|
||||
break;
|
||||
|
||||
case Keys.D:
|
||||
case Keys.RIGHT:
|
||||
if (x < 0) { x = 0; }
|
||||
else if (x == 0) { x = 1; }
|
||||
break;
|
||||
|
||||
case Keys.SPACE:
|
||||
this.attacking = true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
/** Left and down are negative, right and up are positive. */
|
||||
private int x = 0, y = 0;
|
||||
private boolean attacking = false;
|
||||
|
||||
@Override
|
||||
public boolean keyUp(final int keycode) {
|
||||
switch (keycode) {
|
||||
case Keys.W:
|
||||
case Keys.UP:
|
||||
if (y > 0) { y = 0; }
|
||||
else if (y == 0) { y = -1; }
|
||||
break;
|
||||
|
||||
case Keys.S:
|
||||
case Keys.DOWN:
|
||||
if (y < 0) { y = 0; }
|
||||
else if (y == 0) { y = 1; }
|
||||
break;
|
||||
|
||||
case Keys.A:
|
||||
case Keys.LEFT:
|
||||
if (x < 0) { x = 0; }
|
||||
else if (x == 0) { x = 1; }
|
||||
break;
|
||||
|
||||
case Keys.D:
|
||||
case Keys.RIGHT:
|
||||
if (x > 0) { x = 0; }
|
||||
else if (x == 0) { x = -1; }
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputState readInput() {
|
||||
// Either the button is pressed, or it isn't, so the magnitude is always either 1 or 0.
|
||||
final float magnitude = Math.max(Math.abs(x), Math.abs(y));
|
||||
final float orientation = (float) Math.atan2(y, x);
|
||||
final boolean attacking = this.attacking;
|
||||
|
||||
// 1 press = 1 attack. We don't do auto-attacking. Button mashing for the win.
|
||||
this.attacking = false;
|
||||
|
||||
return new InputState() {
|
||||
@Override
|
||||
public float getOrientation() {
|
||||
return orientation;
|
||||
}
|
||||
@Override
|
||||
public boolean keyDown(final int keycode) {
|
||||
switch (keycode) {
|
||||
case Keys.W:
|
||||
case Keys.UP:
|
||||
if (y < 0) {
|
||||
y = 0;
|
||||
} else if (y == 0) {
|
||||
y = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
@Override
|
||||
public float getAcceleration() {
|
||||
return magnitude;
|
||||
}
|
||||
case Keys.S:
|
||||
case Keys.DOWN:
|
||||
if (y > 0) {
|
||||
y = 0;
|
||||
} else if (y == 0) {
|
||||
y = -1;
|
||||
}
|
||||
break;
|
||||
|
||||
@Override
|
||||
public boolean isAttacking() {
|
||||
return attacking;
|
||||
}
|
||||
};
|
||||
}
|
||||
case Keys.A:
|
||||
case Keys.LEFT:
|
||||
if (x > 0) {
|
||||
x = 0;
|
||||
} else if (x == 0) {
|
||||
x = -1;
|
||||
}
|
||||
break;
|
||||
|
||||
case Keys.D:
|
||||
case Keys.RIGHT:
|
||||
if (x < 0) {
|
||||
x = 0;
|
||||
} else if (x == 0) {
|
||||
x = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case Keys.SPACE:
|
||||
this.attacking = true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyUp(final int keycode) {
|
||||
switch (keycode) {
|
||||
case Keys.W:
|
||||
case Keys.UP:
|
||||
if (y > 0) {
|
||||
y = 0;
|
||||
} else if (y == 0) {
|
||||
y = -1;
|
||||
}
|
||||
break;
|
||||
|
||||
case Keys.S:
|
||||
case Keys.DOWN:
|
||||
if (y < 0) {
|
||||
y = 0;
|
||||
} else if (y == 0) {
|
||||
y = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case Keys.A:
|
||||
case Keys.LEFT:
|
||||
if (x < 0) {
|
||||
x = 0;
|
||||
} else if (x == 0) {
|
||||
x = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case Keys.D:
|
||||
case Keys.RIGHT:
|
||||
if (x > 0) {
|
||||
x = 0;
|
||||
} else if (x == 0) {
|
||||
x = -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputState readInput() {
|
||||
// Either the button is pressed, or it isn't, so the magnitude is always either 1 or 0.
|
||||
final float magnitude = Math.max(Math.abs(x), Math.abs(y));
|
||||
final float orientation = (float) Math.atan2(y, x);
|
||||
final boolean attacking = this.attacking;
|
||||
|
||||
// 1 press = 1 attack. We don't do auto-attacking. Button mashing for the win.
|
||||
this.attacking = false;
|
||||
|
||||
return new InputState() {
|
||||
@Override
|
||||
public float getOrientation() {
|
||||
return orientation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getAcceleration() {
|
||||
return magnitude;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAttacking() {
|
||||
return attacking;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,36 +1,37 @@
|
|||
package sovereignstudios.helhalla.input;
|
||||
|
||||
public class MutableInputState implements InputState {
|
||||
private float orientation = 0.0f;
|
||||
private float acceleration = 0.0f;
|
||||
private boolean attacking = false;
|
||||
private float orientation = 0.0f;
|
||||
private float acceleration = 0.0f;
|
||||
private boolean attacking = false;
|
||||
|
||||
@Override
|
||||
public float getOrientation() {
|
||||
return orientation;
|
||||
}
|
||||
@Override
|
||||
public float getOrientation() {
|
||||
return orientation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getAcceleration() {
|
||||
return acceleration;
|
||||
}
|
||||
@Override
|
||||
public float getAcceleration() {
|
||||
return acceleration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAttacking() {
|
||||
return attacking;
|
||||
}
|
||||
|
||||
public MutableInputState() {}
|
||||
|
||||
public void setOrientation(final float orientation) {
|
||||
this.orientation = orientation;
|
||||
}
|
||||
|
||||
public void setAcceleration(final float acceleration) {
|
||||
this.acceleration = acceleration;
|
||||
}
|
||||
|
||||
public void setAttacking() {
|
||||
this.attacking = true;
|
||||
}
|
||||
@Override
|
||||
public boolean isAttacking() {
|
||||
return attacking;
|
||||
}
|
||||
|
||||
public MutableInputState() {
|
||||
}
|
||||
|
||||
public void setOrientation(final float orientation) {
|
||||
this.orientation = orientation;
|
||||
}
|
||||
|
||||
public void setAcceleration(final float acceleration) {
|
||||
this.acceleration = acceleration;
|
||||
}
|
||||
|
||||
public void setAttacking() {
|
||||
this.attacking = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,33 +1,34 @@
|
|||
package sovereignstudios.helhalla.input;
|
||||
|
||||
public final class Xbox360Controller {
|
||||
public static final String XBOX360_WIRELESS_RECEIVER = "Xbox 360 Wireless Receiver";
|
||||
|
||||
public static final int BUTTON_A = 0;
|
||||
public static final int BUTTON_B = 1;
|
||||
public static final int BUTTON_X = 2;
|
||||
public static final int BUTTON_Y = 3;
|
||||
public static final int BUTTON_LEFT_BUMPER = 4;
|
||||
public static final int BUTTON_RIGHT_BUMPER = 5;
|
||||
public static final int BUTTON_BACK = 6;
|
||||
public static final int BUTTON_START = 7;
|
||||
public static final int BUTTON_XBOX = 8;
|
||||
/** Triggered when you press down on the joystick, not when it's moved. */
|
||||
public static final int BUTTON_LEFT_JOYSTICK = 9;
|
||||
public static final int BUTTON_RIGHT_JOYSTICK = 10;
|
||||
public static final int BUTTON_DPAD_LEFT = 11;
|
||||
public static final int BUTTON_DPAD_RIGHT = 12;
|
||||
public static final int BUTTON_DPAD_UP = 13;
|
||||
public static final int BUTTON_DPAD_DOWN = 14;
|
||||
|
||||
public static final int AXIS_LEFT_JOYSTICK_X = 0;
|
||||
public static final int AXIS_LEFT_JOYSTICK_Y = 1;
|
||||
public static final int AXIS_LEFT_TRIGGER = 2;
|
||||
public static final int AXIS_RIGHT_JOYSTICK_X = 3;
|
||||
public static final int AXIS_RIGHT_JOYSTICK_Y = 4;
|
||||
public static final int AXIS_RIGHT_TRIGGER = 5;
|
||||
|
||||
public static final float AXIS_PRESS_THRESHOLD = 0.2f;
|
||||
|
||||
private Xbox360Controller() {}
|
||||
public static final String XBOX360_WIRELESS_RECEIVER = "Xbox 360 Wireless Receiver";
|
||||
|
||||
public static final int BUTTON_A = 0;
|
||||
public static final int BUTTON_B = 1;
|
||||
public static final int BUTTON_X = 2;
|
||||
public static final int BUTTON_Y = 3;
|
||||
public static final int BUTTON_LEFT_BUMPER = 4;
|
||||
public static final int BUTTON_RIGHT_BUMPER = 5;
|
||||
public static final int BUTTON_BACK = 6;
|
||||
public static final int BUTTON_START = 7;
|
||||
public static final int BUTTON_XBOX = 8;
|
||||
/** Triggered when you press down on the joystick, not when it's moved. */
|
||||
public static final int BUTTON_LEFT_JOYSTICK = 9;
|
||||
public static final int BUTTON_RIGHT_JOYSTICK = 10;
|
||||
public static final int BUTTON_DPAD_LEFT = 11;
|
||||
public static final int BUTTON_DPAD_RIGHT = 12;
|
||||
public static final int BUTTON_DPAD_UP = 13;
|
||||
public static final int BUTTON_DPAD_DOWN = 14;
|
||||
|
||||
public static final int AXIS_LEFT_JOYSTICK_X = 0;
|
||||
public static final int AXIS_LEFT_JOYSTICK_Y = 1;
|
||||
public static final int AXIS_LEFT_TRIGGER = 2;
|
||||
public static final int AXIS_RIGHT_JOYSTICK_X = 3;
|
||||
public static final int AXIS_RIGHT_JOYSTICK_Y = 4;
|
||||
public static final int AXIS_RIGHT_TRIGGER = 5;
|
||||
|
||||
public static final float AXIS_PRESS_THRESHOLD = 0.2f;
|
||||
|
||||
private Xbox360Controller() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,58 +5,58 @@ import com.badlogic.gdx.controllers.ControllerAdapter;
|
|||
import static sovereignstudios.helhalla.input.Xbox360Controller.*;
|
||||
|
||||
public final class Xbox360WirelessControllerInputSource extends ControllerAdapter implements InputSource {
|
||||
/**
|
||||
* Joysticks are a bit sticky, so when the joystick is released it doesn't return to exactly 0.0f.
|
||||
* This is the minimum magnitude the joystick must be pushed to consider it being pushed at all.
|
||||
* This applies to the total magnitude of the joystick, not the magnitude of either individual axis.
|
||||
*
|
||||
* This value is based on my own controller. The highest number I managed to get it to stick at was ~0.2,
|
||||
* but it is important that the player *never* gets stuck moving after the joystick is released,
|
||||
* so I made it a bit higher than that.
|
||||
* There are certainly controllers both better and worse than mine,
|
||||
* so this value probably ought to be configurable.
|
||||
*/
|
||||
private static final float JOYSTICK_PUSH_THRESHOLD = 0.25f;
|
||||
|
||||
private final Controller controller;
|
||||
private MutableInputState inputState = new MutableInputState();
|
||||
|
||||
public Xbox360WirelessControllerInputSource(final Controller controller) {
|
||||
if (!controller.getName().equals(XBOX360_WIRELESS_RECEIVER)) {
|
||||
throw new IllegalArgumentException("Controller was not a valid known Xbox360 controller.");
|
||||
}
|
||||
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean buttonDown(final Controller controller, final int buttonCode) {
|
||||
System.out.println("Button: " + buttonCode);
|
||||
switch (buttonCode) {
|
||||
case BUTTON_A:
|
||||
inputState.setAttacking();
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final InputState readInput() {
|
||||
final MutableInputState state = inputState;
|
||||
|
||||
// For joysticks, up and left are negative. For us, down and left are negative.
|
||||
// The y input must be inverted.
|
||||
final float x = controller.getAxis(AXIS_LEFT_JOYSTICK_X);
|
||||
final float y = -controller.getAxis(AXIS_LEFT_JOYSTICK_Y);
|
||||
final float magnitude = Math.min(1.0f, Math.abs(x) + Math.abs(y));
|
||||
final float orientation = (float) Math.atan2(y, x);
|
||||
/**
|
||||
* Joysticks are a bit sticky, so when the joystick is released it doesn't return to exactly 0.0f.
|
||||
* This is the minimum magnitude the joystick must be pushed to consider it being pushed at all.
|
||||
* This applies to the total magnitude of the joystick, not the magnitude of either individual axis.
|
||||
*
|
||||
* This value is based on my own controller. The highest number I managed to get it to stick at was ~0.2,
|
||||
* but it is important that the player *never* gets stuck moving after the joystick is released,
|
||||
* so I made it a bit higher than that.
|
||||
* There are certainly controllers both better and worse than mine,
|
||||
* so this value probably ought to be configurable.
|
||||
*/
|
||||
private static final float JOYSTICK_PUSH_THRESHOLD = 0.25f;
|
||||
|
||||
if (magnitude > JOYSTICK_PUSH_THRESHOLD) {
|
||||
state.setAcceleration(magnitude);
|
||||
}
|
||||
state.setOrientation(orientation);
|
||||
|
||||
inputState = new MutableInputState();
|
||||
return state;
|
||||
}
|
||||
private final Controller controller;
|
||||
private MutableInputState inputState = new MutableInputState();
|
||||
|
||||
public Xbox360WirelessControllerInputSource(final Controller controller) {
|
||||
if (!controller.getName().equals(XBOX360_WIRELESS_RECEIVER)) {
|
||||
throw new IllegalArgumentException("Controller was not a valid known Xbox360 controller.");
|
||||
}
|
||||
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean buttonDown(final Controller controller, final int buttonCode) {
|
||||
System.out.println("Button: " + buttonCode);
|
||||
switch (buttonCode) {
|
||||
case BUTTON_A:
|
||||
inputState.setAttacking();
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final InputState readInput() {
|
||||
final MutableInputState state = inputState;
|
||||
|
||||
// For joysticks, up and left are negative. For us, down and left are negative.
|
||||
// The y input must be inverted.
|
||||
final float x = controller.getAxis(AXIS_LEFT_JOYSTICK_X);
|
||||
final float y = -controller.getAxis(AXIS_LEFT_JOYSTICK_Y);
|
||||
final float magnitude = Math.min(1.0f, Math.abs(x) + Math.abs(y));
|
||||
final float orientation = (float) Math.atan2(y, x);
|
||||
|
||||
if (magnitude > JOYSTICK_PUSH_THRESHOLD) {
|
||||
state.setAcceleration(magnitude);
|
||||
}
|
||||
state.setOrientation(orientation);
|
||||
|
||||
inputState = new MutableInputState();
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue