Added fullscreen toggles, improved keyboard controls (made new ones),

added a mouse visibility option for menus
This commit is contained in:
josua 2019-10-16 10:58:33 +11:00
parent 868b3481ee
commit 4a53342a73
11 changed files with 106 additions and 52 deletions

View File

@ -33,7 +33,7 @@ public class Main
public static World world;
public static AudioEngine audio;
public static Random rand = new Random();
public static Menu menu = new MenuGame();
public static Menu menu;
public static boolean game_paused = false;
public static void main(String[] args)
@ -80,6 +80,7 @@ public class Main
mainloop.register(EntityEventHandler.ENTITY_EVENT_HANDLER);
// Start the mainloop
menu = new MenuGame();
mainloop.start();
}
}

View File

@ -13,6 +13,7 @@ import shootergame.input.JoystickCallback;
import shootergame.input.KeyCallback;
import shootergame.input.KeyCharCallback;
import shootergame.input.MouseButtonCallback;
import shootergame.input.ScrollWheelCallback;
import shootergame.mainloop.MainloopEventHandler;
import shootergame.util.gl.GlHelpers;
import shootergame.util.gl.texture.AnimationEventHandler;
@ -21,8 +22,10 @@ public class DisplayWindow implements IMainloopTask
{
private String name;
private long window;
private long monitor;
private int width;
private int height;
private boolean fullscreen = true;
public int getWidth() {
return this.width;
@ -54,7 +57,7 @@ public class DisplayWindow implements IMainloopTask
// Get the monitor size
IntBuffer w = BufferUtils.createIntBuffer(1);
IntBuffer h = BufferUtils.createIntBuffer(1);
long monitor = GLFW.glfwGetPrimaryMonitor();
monitor = GLFW.glfwGetPrimaryMonitor();
GLFW.glfwGetMonitorPhysicalSize(monitor, w, h);
this.width = w.get()*4;
this.height = h.get()*4;
@ -79,6 +82,7 @@ public class DisplayWindow implements IMainloopTask
GLFW.glfwSetMouseButtonCallback(this.window, new MouseButtonCallback());
GLFW.glfwSetKeyCallback(this.window, new KeyCallback());
GLFW.glfwSetJoystickCallback(JoystickCallback.JOYSTICK_CALLBACK);
GLFW.glfwSetScrollCallback(this.window, new ScrollWheelCallback());
// Make the cursor invisible
GLFW.glfwSetInputMode(this.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN);
@ -112,6 +116,24 @@ public class DisplayWindow implements IMainloopTask
this.pollEvents();
}
public void toggleFullscreen() {
if(fullscreen) {
fullscreen = false;
GLFW.glfwSetWindowMonitor(window, 0, 1, 1, width, height, GLFW.GLFW_DONT_CARE);
} else {
fullscreen = true;
GLFW.glfwSetWindowMonitor(window, monitor, 0, 0, width, height, GLFW.GLFW_DONT_CARE);
}
}
public void setMouseVisibility(boolean status) {
if(status) {
GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_NORMAL);
} else {
GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN);
}
}
public void swapBuffers() {
GLFW.glfwSwapBuffers(this.window);
}

View File

@ -1,14 +1,28 @@
package shootergame.input;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWCursorPosCallbackI;
import shootergame.Main;
public class CursorPosCallback implements GLFWCursorPosCallbackI
{
private boolean keepMouse_last = true;
@Override
public void invoke(long arg0, double arg1, double arg2) {
// TODO Auto-generated method stub
public void invoke(long window, double x, double y) {
if(keepMouse_last != Main.menu.keepMouse) {
Main.window.setMouseVisibility(!Main.menu.keepMouse);
keepMouse_last = Main.menu.keepMouse;
} if(!Main.menu.keepMouse) {
return;
}
int wx = Main.window.getWidth();
int wy = Main.window.getHeight();
x = x / wx - 0.5;
y = y / wy - 0.5;
Main.menu.input.camera(true, x * 60);
GLFW.glfwSetCursorPos(window, wx / 2, wy / 2);
}
}

View File

@ -1,21 +1,6 @@
package shootergame.input;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_1;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_2;
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 static org.lwjgl.glfw.GLFW.*;
import static shootergame.input.GameInput.fireGun;
import static shootergame.input.GameInput.moveDown;
import static shootergame.input.GameInput.moveLeft;
@ -28,13 +13,14 @@ import org.lwjgl.glfw.GLFWKeyCallbackI;
import mainloop.task.IMainloopTask;
import shootergame.Main;
import shootergame.input.types.Input;
import shootergame.util.math.vec.Vec2d;
public class KeyCallback implements GLFWKeyCallbackI, IMainloopTask
{
private boolean itemUse_last = false;
private boolean itemDrop_last = false;
private boolean esc_last = false;
private boolean action_last = false;
private boolean fullscreen_last = false;
@Override
public void invoke(long window, int key, int scancode, int action, int mods)
@ -58,10 +44,6 @@ public class KeyCallback implements GLFWKeyCallbackI, IMainloopTask
moveRight = pressed;
}
if(key == GLFW_KEY_ENTER) {
fireGun = pressed;
}
if(key == GLFW_KEY_1 && pressed) {
input.hotbarGoto(true, 0);
}
@ -81,18 +63,6 @@ public class KeyCallback implements GLFWKeyCallbackI, IMainloopTask
input.hotbarGoto(true, 5);
}
if(key == GLFW_KEY_RIGHT_SHIFT) {
if(pressed) {
if(!itemUse_last) {
itemUse_last = true;
input.itemAction(true);
}
}
else if(itemUse_last) {
itemUse_last = false;
}
}
if(key == GLFW_KEY_E) {
if(pressed) {
if(!action_last) {
@ -128,6 +98,17 @@ public class KeyCallback implements GLFWKeyCallbackI, IMainloopTask
esc_last = false;
}
}
if(key == GLFW_KEY_F11) {
if(pressed) {
if(!fullscreen_last) {
fullscreen_last = true;
Main.window.toggleFullscreen();
}
} else if(fullscreen_last) {
fullscreen_last = false;
}
}
}
@Override
@ -145,22 +126,28 @@ public class KeyCallback implements GLFWKeyCallbackI, IMainloopTask
{
Input input = Main.menu.input;
Vec2d move_point = new Vec2d(0, 0);
if(moveUp) {
input.move(true, 0);
move_last = true;
move_point.x += 1;
}
if(moveDown) {
input.move(true, 180);
move_last = true;
move_point.x -= 1;
}
if(moveLeft) {
input.camera(true, -1);
move_point.y -= 1;
}
if(moveRight) {
input.camera(true, 1);
move_point.y += 1;
}
if(move_point.x != 0 || move_point.y != 0) {
move_last = true;
double angle = Math.toDegrees(Math.atan2(move_point.y, move_point.x));
input.move(true, angle);
}
if(fireGun) {

View File

@ -1,14 +1,24 @@
package shootergame.input;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWMouseButtonCallbackI;
import shootergame.Main;
public class MouseButtonCallback implements GLFWMouseButtonCallbackI
{
@Override
public void invoke(long arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
public void invoke(long window, int button, int action, int mods)
{
if(button == GLFW.GLFW_MOUSE_BUTTON_LEFT) {
GameInput.fireGun = action == 1;
}
if(button == GLFW.GLFW_MOUSE_BUTTON_RIGHT && action == 1) {
GameInput.activateItem_last = true;
Main.menu.input.itemAction(true);
}
}
}

View File

@ -0,0 +1,20 @@
package shootergame.input;
import org.lwjgl.glfw.GLFWScrollCallbackI;
import shootergame.Main;
public class ScrollWheelCallback implements GLFWScrollCallbackI
{
@Override
public void invoke(long window, double x, double y)
{
if(y > 0) {
Main.menu.input.hotbarShift(true, 1);
} if(y < 0) {
Main.menu.input.hotbarShift(true, -1);
}
}
}

View File

@ -6,6 +6,7 @@ public abstract class Menu
{
public boolean doGameloop;
public boolean doGameRender;
public boolean keepMouse = true;
public Input input;
public abstract void render();

View File

@ -14,6 +14,7 @@ public class MenuDeath extends Menu
public MenuDeath() {
this.doGameloop = false;
this.doGameRender = true;
this.keepMouse = false;
this.input = new InputDeath();
}

View File

@ -1,5 +1,6 @@
package shootergame.menu;
import shootergame.Main;
import shootergame.input.types.InputGame;
public class MenuGame extends Menu
@ -8,6 +9,7 @@ public class MenuGame extends Menu
this.doGameloop = true;
this.doGameRender = true;
this.input = new InputGame();
Main.window.setMouseVisibility(false);
}
@Override

View File

@ -13,6 +13,7 @@ public class MenuGamePause extends Menu
public MenuGamePause() {
this.doGameloop = false;
this.doGameRender = true;
this.keepMouse = false;
this.input = new InputGamePause();
}

View File

@ -4,7 +4,6 @@ import java.util.Random;
import shootergame.Main;
import shootergame.entity.Entity;
import shootergame.entity.EntityExplosion;
import shootergame.entity.EntityZombie;
import shootergame.init.Tiles;
import shootergame.time.GameTimer;
@ -33,10 +32,6 @@ public class LayerGenEarth extends LayerGen
// Get the noise generator
OpenSimplexNoise noisegen_n = new OpenSimplexNoise(new Random(seed + layer.id).nextLong());
if(c_pos.x == 0 && c_pos.y == 0) {
chunk.spawnEntity(new EntityExplosion(new Vec2d(1, 1), 10, 0));
}
// Loop over the layer dimensions and create land
for(int x=0;x<Chunk.CHUNK_SIZE.mx;x++) {
for(int y=0;y<Chunk.CHUNK_SIZE.my;y++)