Improved the controls; made them more changable and fixed issues with

the keyboard and the controller being used together to cheat (fast speed
and fast gun)
This commit is contained in:
josua 2019-09-22 10:55:00 +10:00
parent d88b433b11
commit ef6fe56c89
15 changed files with 614 additions and 376 deletions

View File

@ -15,9 +15,10 @@ import shootergame.init.Resources;
import shootergame.init.Sounds; import shootergame.init.Sounds;
import shootergame.init.Textures; import shootergame.init.Textures;
import shootergame.input.JoystickCallback; import shootergame.input.JoystickCallback;
import shootergame.input.KeyCallback;
import shootergame.mainloop.MainloopEventHandler; import shootergame.mainloop.MainloopEventHandler;
import shootergame.menu.Menu; import shootergame.menu.Menu;
import shootergame.menu.MenuNone; import shootergame.menu.MenuGame;
import shootergame.tiles.LightLevelNoise; import shootergame.tiles.LightLevelNoise;
import shootergame.time.GameTimer; import shootergame.time.GameTimer;
import shootergame.world.World; import shootergame.world.World;
@ -31,7 +32,7 @@ public class Main
public static World world; public static World world;
public static AudioEngine audio; public static AudioEngine audio;
public static Random rand = new Random(); public static Random rand = new Random();
public static Menu menu = new MenuNone(); public static Menu menu = new MenuGame();
public static boolean game_paused = false; public static boolean game_paused = false;
public static void main(String[] args) public static void main(String[] args)
@ -58,6 +59,7 @@ public class Main
mainloop.register(JoystickCallback.JOYSTICK_CALLBACK); mainloop.register(JoystickCallback.JOYSTICK_CALLBACK);
mainloop.register(new LightLevelNoise()); mainloop.register(new LightLevelNoise());
mainloop.register(new GameTimer()); mainloop.register(new GameTimer());
mainloop.register(new KeyCallback());
// Create the display // Create the display
window = new DisplayWindow("ShooterGame"); window = new DisplayWindow("ShooterGame");

View File

@ -49,6 +49,9 @@ public class DisplayRender
// Bind the texmap // Bind the texmap
Textures.texmap.bindTexture(); Textures.texmap.bindTexture();
if(Main.menu.doGameRender)
{
// Push the matrix
GlHelpers.pushMatrix(); GlHelpers.pushMatrix();
{ {
@ -96,6 +99,7 @@ public class DisplayRender
GlHelpers.popMatrix(); GlHelpers.popMatrix();
} }
}
// Render the user interface // Render the user interface
DisplayRenderUI.render(); DisplayRenderUI.render();

View File

@ -27,12 +27,19 @@ public class DisplayRenderUI
// Get some text settings // Get some text settings
Vec2d text_size = new Vec2d(0.5, 0.2); Vec2d text_size = new Vec2d(0.5, 0.2);
// Render the fps and the position // Render the fps
GlHelpers.pushMatrix(); GlHelpers.pushMatrix();
GlHelpers.translate(-9.5, 9.5, 0); GlHelpers.translate(-9.5, 9.5, 0);
GlHelpers.color3(1, 1, 0); GlHelpers.color3(1, 1, 0);
Text.render("FPS: " + DisplayStatsEventHandler.fps, text_size); Text.render("FPS: " + DisplayStatsEventHandler.fps, text_size);
GlHelpers.translate(0, -0.5, 0); GlHelpers.popMatrix();
if(Main.menu.doGameRender)
{
// Render the fps and the position
GlHelpers.pushMatrix();
GlHelpers.translate(-9.5, 9, 0);
GlHelpers.color3(1, 1, 0);
Text.render("x: " + (int) player.pos.x + ", y: " + (int) player.pos.y, text_size); Text.render("x: " + (int) player.pos.x + ", y: " + (int) player.pos.y, text_size);
GlHelpers.color3(1, 1, 1); GlHelpers.color3(1, 1, 1);
GlHelpers.popMatrix(); GlHelpers.popMatrix();
@ -142,6 +149,7 @@ public class DisplayRenderUI
// Render the boss bars // Render the boss bars
BossBars.render(); BossBars.render();
}
// Render the loaded menu // Render the loaded menu
Main.menu.render(); Main.menu.render();

View File

@ -0,0 +1,20 @@
package shootergame.input;
class GameInput
{
static boolean activateItem_last = false;
static boolean activateTile_last = false;
static boolean dropItem_last = false;
static boolean move_last = false;
static boolean hotbar_l = false;
static boolean hotbar_r = false;
static boolean startButton_last = false;
static boolean moveLeft = false;
static boolean moveRight = false;
static boolean moveUp = false;
static boolean moveDown = false;
static boolean fireGun = false;
}

View File

@ -4,29 +4,20 @@ import java.nio.ByteBuffer;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import static shootergame.input.GameInput.*;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWJoystickCallbackI; import org.lwjgl.glfw.GLFWJoystickCallbackI;
import mainloop.task.IMainloopTask; import mainloop.task.IMainloopTask;
import shootergame.Main; import shootergame.Main;
import shootergame.menu.MenuNone; import shootergame.input.types.Input;
import shootergame.menu.MenuPause;
import shootergame.util.math.MathHelpers;
public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
{ {
public static final JoystickCallback JOYSTICK_CALLBACK = new JoystickCallback(); public static final JoystickCallback JOYSTICK_CALLBACK = new JoystickCallback();
private ArrayList<Integer> connections = new ArrayList<Integer>(); private ArrayList<Integer> connections = new ArrayList<Integer>();
private static boolean activateItem_last = false;
private static boolean activateTile_last = false;
private static boolean dropItem_last = false;
private static boolean hotbar_l = false;
private static boolean hotbar_r = false;
private static boolean startButton_last = false;
@Override @Override
public void invoke(int jid, int event) public void invoke(int jid, int event)
{ {
@ -150,33 +141,31 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
dpad_down = buttons.get(14) == 1 || buttons.get(17) == 1 || dpad_down; dpad_down = buttons.get(14) == 1 || buttons.get(17) == 1 || dpad_down;
} }
// Only check for these input commands if the game isn't paused Input input = Main.menu.input;
if(!Main.game_paused)
{
// Is the left stick moved into a position (movement stick) // 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) if((left_x > 0.3 || left_x < -0.3 || left_y > 0.3 || left_y < -0.3) && !moveUp && !moveDown)
{ {
// Get the the angle // Get the the angle
double angle = Math.toDegrees(Math.atan2(left_y, left_x)) + 90; double angle = Math.toDegrees(Math.atan2(left_y, left_x)) + 90;
input.move(true, angle);
// Move the player in the left sticks angle move_last = true;
Main.player.moveTowards(angle);
// Set the players moving to true
Main.player.moving = true;
} }
// Set the players moving to false // Set the players moving to false
else Main.player.moving = false; else if(move_last && !moveUp && !moveDown) {
move_last = false;
input.move(false, 0);
}
// Is the right x axis stick moved into a position (camera stick) // Is the right x axis stick moved into a position (camera stick)
if(right_x > 0.3 || right_x < -0.3) { if(right_x > 0.3 || right_x < -0.3) {
Main.player.angle += right_x; input.camera(true, right_x);
} }
// Gun trigger // Gun trigger
if(right_trigger > 0.3) { if(right_trigger > 0.3 && !fireGun) {
Main.player.fireBullet(0); input.fire(true);
} }
// Item trigger // Item trigger
@ -184,7 +173,7 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
if(!activateItem_last) if(!activateItem_last)
{ {
activateItem_last = true; activateItem_last = true;
Main.player.activateItem(); input.itemAction(true);
} }
} }
@ -195,8 +184,7 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
if(shoulder_left) { if(shoulder_left) {
if(!hotbar_l) { if(!hotbar_l) {
hotbar_l = true; hotbar_l = true;
Main.player.inventory_hand -= 1; input.hotbarShift(true, -1);
Main.player.inventory_hand = MathHelpers.mod(Main.player.inventory_hand, 6);
} }
} }
@ -207,8 +195,7 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
if(shoulder_right) { if(shoulder_right) {
if(!hotbar_r) { if(!hotbar_r) {
hotbar_r = true; hotbar_r = true;
Main.player.inventory_hand += 1; input.hotbarShift(true, 1);
Main.player.inventory_hand = MathHelpers.mod(Main.player.inventory_hand, 6);
} }
} }
@ -219,7 +206,7 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
// Activate tile // Activate tile
if(button_x) { if(button_x) {
if(!activateTile_last) { if(!activateTile_last) {
Main.player.activateTile(); input.activateTile(true);
activateTile_last = true; activateTile_last = true;
} }
} }
@ -231,7 +218,7 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
// Drop item // Drop item
if(button_b) { if(button_b) {
if(!dropItem_last) { if(!dropItem_last) {
Main.player.dropItem(); input.itemDrop(true);
dropItem_last = true; dropItem_last = true;
} }
} }
@ -239,22 +226,12 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
else if(dropItem_last) { else if(dropItem_last) {
dropItem_last = false; dropItem_last = false;
} }
}
// Pause and unpause the game // Pause and unpause the game
if(button_start) { if(button_start) {
if(!startButton_last) { if(!startButton_last) {
startButton_last = true; startButton_last = true;
input.pause(true);
if(Main.game_paused) {
Main.menu = new MenuNone();
Main.game_paused = false;
}
else {
Main.menu = new MenuPause();
Main.game_paused = true;
}
} }
} }

View File

@ -1,29 +1,15 @@
package shootergame.input; package shootergame.input;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_1; import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_2; import static shootergame.input.GameInput.*;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_3;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_4;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_5;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_6;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_A;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_D;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_E;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_ENTER;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_Q;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT_SHIFT;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_S;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_W;
import static org.lwjgl.glfw.GLFW.GLFW_RELEASE;
import org.lwjgl.glfw.GLFWKeyCallbackI; import org.lwjgl.glfw.GLFWKeyCallbackI;
import mainloop.task.IMainloopTask;
import shootergame.Main; import shootergame.Main;
import shootergame.menu.MenuNone; import shootergame.input.types.Input;
import shootergame.menu.MenuPause;
public class KeyCallback implements GLFWKeyCallbackI public class KeyCallback implements GLFWKeyCallbackI, IMainloopTask
{ {
private boolean itemUse_last = false; private boolean itemUse_last = false;
private boolean itemDrop_last = false; private boolean itemDrop_last = false;
@ -33,55 +19,53 @@ public class KeyCallback implements GLFWKeyCallbackI
@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)
{ {
Input input = Main.menu.input;
boolean pressed = ! ( action == GLFW_RELEASE ); boolean pressed = ! ( action == GLFW_RELEASE );
if(!Main.game_paused && !Main.player.dead && !Main.player.in_animation)
{
if(key == GLFW_KEY_W) { if(key == GLFW_KEY_W) {
Main.player.MOVE_FORWARD = pressed; moveUp = pressed;
} }
if(key == GLFW_KEY_S) { if(key == GLFW_KEY_S) {
Main.player.MOVE_BACKWARD = pressed; moveDown = pressed;
} }
if(key == GLFW_KEY_A) { if(key == GLFW_KEY_A) {
Main.player.MOVE_LEFT = pressed; moveLeft = pressed;
} }
if(key == GLFW_KEY_D) { if(key == GLFW_KEY_D) {
Main.player.MOVE_RIGHT = pressed; moveRight = pressed;
} }
if(key == GLFW_KEY_ENTER) { if(key == GLFW_KEY_ENTER) {
Main.player.GUN = pressed; fireGun = pressed;
} }
if(key == GLFW_KEY_1 && pressed) { if(key == GLFW_KEY_1 && pressed) {
Main.player.inventory_hand = 0; input.hotbarGoto(true, 0);
} }
if(key == GLFW_KEY_2 && pressed) { if(key == GLFW_KEY_2 && pressed) {
Main.player.inventory_hand = 1; input.hotbarGoto(true, 1);
} }
if(key == GLFW_KEY_3 && pressed) { if(key == GLFW_KEY_3 && pressed) {
Main.player.inventory_hand = 2; input.hotbarGoto(true, 2);
} }
if(key == GLFW_KEY_4 && pressed) { if(key == GLFW_KEY_4 && pressed) {
Main.player.inventory_hand = 3; input.hotbarGoto(true, 3);
} }
if(key == GLFW_KEY_5 && pressed) { if(key == GLFW_KEY_5 && pressed) {
Main.player.inventory_hand = 4; input.hotbarGoto(true, 4);
} }
if(key == GLFW_KEY_6 && pressed) { if(key == GLFW_KEY_6 && pressed) {
Main.player.inventory_hand = 5; input.hotbarGoto(true, 5);
} }
if(key == GLFW_KEY_RIGHT_SHIFT) { if(key == GLFW_KEY_RIGHT_SHIFT) {
if(pressed) { if(pressed) {
if(!itemUse_last) { if(!itemUse_last) {
itemUse_last = true; itemUse_last = true;
Main.player.activateItem(); input.itemAction(true);
} }
} }
else if(itemUse_last) { else if(itemUse_last) {
@ -93,7 +77,7 @@ public class KeyCallback implements GLFWKeyCallbackI
if(pressed) { if(pressed) {
if(!action_last) { if(!action_last) {
action_last = true; action_last = true;
Main.player.activateTile(); input.activateTile(true);
} }
} }
else if(action_last) { else if(action_last) {
@ -105,40 +89,19 @@ public class KeyCallback implements GLFWKeyCallbackI
if(pressed) { if(pressed) {
if(!itemDrop_last) { if(!itemDrop_last) {
itemDrop_last = true; itemDrop_last = true;
Main.player.dropItem(); input.itemDrop(true);
} }
} }
else if(itemDrop_last) { else if(itemDrop_last) {
itemDrop_last = false; itemDrop_last = false;
} }
} }
}
else {
itemUse_last = false;
esc_last = false;
action_last = false;
itemDrop_last = false;
Main.player.MOVE_BACKWARD = false;
Main.player.MOVE_FORWARD = false;
Main.player.MOVE_LEFT = false;
Main.player.MOVE_RIGHT = false;
}
if(key == GLFW_KEY_ESCAPE) { if(key == GLFW_KEY_ESCAPE) {
if(pressed) { if(pressed) {
if(!esc_last) { if(!esc_last) {
esc_last = true; esc_last = true;
input.pause(true);
if(Main.game_paused) {
Main.game_paused = false;
Main.menu = new MenuNone();
}
else {
Main.game_paused = true;
Main.menu = new MenuPause();
}
} }
} }
else if(esc_last) { else if(esc_last) {
@ -147,4 +110,42 @@ public class KeyCallback implements GLFWKeyCallbackI
} }
} }
@Override
public boolean MainLoopDelay(long millis) {
return millis > 10;
}
@Override
public boolean MainLoopRepeat() {
return true;
}
@Override
public void MainLoopUpdate()
{
Input input = Main.menu.input;
if(moveUp) {
input.move(true, 0);
move_last = true;
}
if(moveDown) {
input.move(true, 180);
move_last = true;
}
if(moveLeft) {
input.camera(true, -1);
}
if(moveRight) {
input.camera(true, 1);
}
if(fireGun) {
input.fire(true);
}
}
} }

View File

@ -0,0 +1,14 @@
package shootergame.input.types;
public interface Input
{
public void move(boolean state, double angle);
public void fire(boolean state);
public void activateTile(boolean state);
public void camera(boolean state, double amount);
public void itemAction(boolean state);
public void itemDrop(boolean state);
public void pause(boolean state);
public void hotbarGoto(boolean state, int pos);
public void hotbarShift(boolean state, int amount);
}

View File

@ -0,0 +1,70 @@
package shootergame.input.types;
import shootergame.Main;
import shootergame.menu.MenuGame;
import shootergame.menu.MenuGamePause;
import shootergame.util.math.MathHelpers;
public class InputGame implements Input
{
@Override
public void move(boolean state, double angle)
{
if(state)
{
// Move the player in the left sticks angle
Main.player.moveTowards(angle);
// Set the players moving to true
Main.player.moving = true;
}
else {
Main.player.moving = false;
}
}
@Override
public void fire(boolean state) {
Main.player.fireBullet(0);
}
@Override
public void activateTile(boolean state) {
Main.player.activateTile();
}
@Override
public void camera(boolean state, double amount) {
Main.player.angle += amount;
}
@Override
public void itemAction(boolean state) {
Main.player.activateItem();
}
@Override
public void itemDrop(boolean state) {
Main.player.dropItem();
}
@Override
public void pause(boolean state) {
Main.menu = new MenuGamePause();
Main.game_paused = true;
}
@Override
public void hotbarGoto(boolean state, int pos) {
Main.player.inventory_hand = pos;
}
@Override
public void hotbarShift(boolean state, int amount) {
Main.player.inventory_hand += amount;
Main.player.inventory_hand = MathHelpers.mod(Main.player.inventory_hand, 6);
}
}

View File

@ -0,0 +1,47 @@
package shootergame.input.types;
import shootergame.Main;
import shootergame.menu.MenuGame;
public class InputGamePause implements Input
{
@Override
public void move(boolean state, double angle) {
}
@Override
public void fire(boolean state) {
}
@Override
public void activateTile(boolean state) {
}
@Override
public void camera(boolean state, double amount) {
}
@Override
public void itemAction(boolean state) {
}
@Override
public void itemDrop(boolean state) {
}
@Override
public void pause(boolean state) {
Main.menu = new MenuGame();
Main.game_paused = false;
}
@Override
public void hotbarGoto(boolean state, int pos) {
}
@Override
public void hotbarShift(boolean state, int amount) {
}
}

View File

@ -0,0 +1,62 @@
package shootergame.input.types;
import shootergame.Main;
public class InputMenu implements Input
{
@Override
public void move(boolean state, double angle) {
// TODO
}
@Override
public void fire(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void activateTile(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void camera(boolean state, double amount) {
// TODO Auto-generated method stub
}
@Override
public void itemAction(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void itemDrop(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void pause(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void hotbarGoto(boolean state, int pos) {
// TODO Auto-generated method stub
}
@Override
public void hotbarShift(boolean state, int amount) {
// TODO Auto-generated method stub
}
}

View File

@ -1,8 +1,12 @@
package shootergame.menu; package shootergame.menu;
import shootergame.input.types.Input;
public abstract class Menu public abstract class Menu
{ {
public boolean doGameloop = false; public boolean doGameloop;
public boolean doGameRender;
public Input input;
public abstract void render(); public abstract void render();
} }

View File

@ -0,0 +1,16 @@
package shootergame.menu;
import shootergame.input.types.InputGame;
public class MenuGame extends Menu
{
public MenuGame() {
this.doGameloop = true;
this.doGameRender = true;
this.input = new InputGame();
}
@Override
public void render() {
}
}

View File

@ -1,11 +1,18 @@
package shootergame.menu; package shootergame.menu;
import shootergame.input.types.InputGamePause;
import shootergame.text.Text; import shootergame.text.Text;
import shootergame.util.gl.GlHelpers; import shootergame.util.gl.GlHelpers;
import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2d;
public class MenuPause extends Menu public class MenuGamePause extends Menu
{ {
public MenuGamePause() {
this.doGameloop = false;
this.doGameRender = true;
this.input = new InputGamePause();
}
@Override @Override
public void render() public void render()
{ {

View File

@ -0,0 +1,18 @@
package shootergame.menu;
import shootergame.input.types.InputMenu;
public class MenuMain extends Menu
{
public MenuMain() {
this.doGameloop = false;
this.doGameRender = false;
this.input = new InputMenu();
}
@Override
public void render() {
}
}

View File

@ -1,12 +0,0 @@
package shootergame.menu;
public class MenuNone extends Menu
{
public MenuNone() {
this.doGameloop = true;
}
@Override
public void render() {
}
}