From 180aad05decc7eefa87e4e45d6747c48f40e5361 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 17 Apr 2023 12:12:01 -0400 Subject: save --- .../tylermurphy/Minecraft/Input/CommandInput.java | 57 ++++++++ .../net/tylermurphy/Minecraft/Input/CoreInput.java | 25 ++++ .../net/tylermurphy/Minecraft/Input/GameInput.java | 85 ++++++++++++ .../net/tylermurphy/Minecraft/Input/IInput.java | 12 ++ .../net/tylermurphy/Minecraft/Input/Input.java | 154 +++++++++++++++++++++ 5 files changed, 333 insertions(+) create mode 100755 src/main/java/net/tylermurphy/Minecraft/Input/CommandInput.java create mode 100755 src/main/java/net/tylermurphy/Minecraft/Input/CoreInput.java create mode 100755 src/main/java/net/tylermurphy/Minecraft/Input/GameInput.java create mode 100755 src/main/java/net/tylermurphy/Minecraft/Input/IInput.java create mode 100755 src/main/java/net/tylermurphy/Minecraft/Input/Input.java (limited to 'src/main/java/net/tylermurphy/Minecraft/Input') diff --git a/src/main/java/net/tylermurphy/Minecraft/Input/CommandInput.java b/src/main/java/net/tylermurphy/Minecraft/Input/CommandInput.java new file mode 100755 index 0000000..85540ab --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Input/CommandInput.java @@ -0,0 +1,57 @@ +package net.tylermurphy.Minecraft.Input; + +import org.lwjgl.glfw.GLFW; + +import net.tylermurphy.Minecraft.Command.CommandHandler; +import net.tylermurphy.Minecraft.Scene.World; + +import static net.tylermurphy.Minecraft.UI.UIMaster.*; + +public class CommandInput extends IInput { + + public void keyPressed(int keyCode) { + switch(keyCode) { + case GLFW.GLFW_KEY_ESCAPE: + Input.setEnabled("CommandInput", false); + Input.setEnabled("GameInput", true); + bindUI(3); + setEnabled(false); + getText("commandBar").setText(""); + World.player.isPaused = false; + break; + case GLFW.GLFW_KEY_BACKSPACE: + bindUI(3); + String currentText = getText("commandBar").getTextString(); + if(currentText.length() > 0) + getText("commandBar").setText(currentText.substring(0,currentText.length()-1)); + break; + case GLFW.GLFW_KEY_ENTER: + bindUI(3); + setEnabled(false); + + String command = getText("commandBar").getTextString(); + CommandHandler.handleCommand(command); + + Input.setEnabled("CommandInput", false); + Input.setEnabled("GameInput", true); + getText("commandBar").setText(""); + World.player.isPaused = false; + break; + } + } + + public void keyRelesed(int keyCode) {} + + public void mousePressed(int mouseButton) {} + + public void mouseRelesed(int mouseButton) {} + + public void charAction(char c) { + bindUI(3); + String currentText = getText("commandBar").getTextString(); + + currentText += c; + getText("commandBar").setText(currentText); + } + +} diff --git a/src/main/java/net/tylermurphy/Minecraft/Input/CoreInput.java b/src/main/java/net/tylermurphy/Minecraft/Input/CoreInput.java new file mode 100755 index 0000000..2732805 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Input/CoreInput.java @@ -0,0 +1,25 @@ +package net.tylermurphy.Minecraft.Input; + +import org.lwjgl.glfw.GLFW; + +import net.tylermurphy.Minecraft.Render.Data.Display; + +public class CoreInput extends IInput { + + public void keyPressed(int keyCode) { + switch(keyCode) { + case GLFW.GLFW_KEY_F11: + Display.setFullScreen(!Display.fullscreen); + break; + } + } + + public void keyRelesed(int keyCode) {} + + public void mousePressed(int mouseButton) {} + + public void mouseRelesed(int mouseButton) {} + + public void charAction(char c) {} + +} diff --git a/src/main/java/net/tylermurphy/Minecraft/Input/GameInput.java b/src/main/java/net/tylermurphy/Minecraft/Input/GameInput.java new file mode 100755 index 0000000..5da40a6 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Input/GameInput.java @@ -0,0 +1,85 @@ +package net.tylermurphy.Minecraft.Input; + +import static net.tylermurphy.Minecraft.UI.UIMaster.*; + +import net.tylermurphy.Minecraft.Scene.Objects.WorldOrigin; +import org.lwjgl.glfw.GLFW; +import org.joml.Vector3f; + +import net.tylermurphy.Minecraft.Chunk.Cube; +import net.tylermurphy.Minecraft.Scene.World; +import net.tylermurphy.Minecraft.Util.Flags; +import net.tylermurphy.Minecraft.Util.MousePicker; + +public class GameInput extends IInput { + + byte blockId = 1; + + public void keyPressed(int keyCode) { + switch(keyCode) { + case GLFW.GLFW_KEY_ESCAPE: + Flags.actionForceClose = true; break; + case GLFW.GLFW_KEY_F3: + bindUI(1); + setEnabled(!isEnabled()); + break; + case GLFW.GLFW_KEY_EQUAL: + do { + blockId++; + if (blockId > (byte) (Cube.blocks.size() - 1)) blockId = 0; + } while (!Cube.getBlock(blockId).placeable); + bindUI(1); + getText("block").setText("Block Selected: minecraft:"+Cube.getBlock(blockId).name); + break; + case GLFW.GLFW_KEY_MINUS: + do { + blockId--; + if (blockId < 0) blockId = (byte) (Cube.blocks.size() - 1); + } while (!Cube.getBlock(blockId).placeable); + bindUI(1); + getText("block").setText("Block Selected: minecraft:"+Cube.getBlock(blockId).name); + break; + case GLFW.GLFW_KEY_F2: + World.player.isFlying = !World.player.isFlying; + break; + case GLFW.GLFW_KEY_K: + if(World.player.isDead) { + World.player.isDead = false; + World.player.health = 20; + World.world_origin = new WorldOrigin(0,0); + World.player.getTransform().setPosition(new Vector3f(0, World.getHighestBlock(0, 0)+1,0)); + bindUI(0); + getText("dead").setEnabled(false); + getImage("crosshair").setEnabled(true); + } + break; + case GLFW.GLFW_KEY_SLASH: + this.enabled = false; + Input.setEnabled("CommandInput", true); + bindUI(3); + setEnabled(true); + World.player.isPaused = true; + break; + } + } + + public void keyRelesed(int keyCode) {} + + public void mousePressed(int mouseButton) { + switch(mouseButton) { + case GLFW.GLFW_MOUSE_BUTTON_1: + if(MousePicker.breakPos!=null) + World.setBlock((int)MousePicker.breakPos.x,(int)MousePicker.breakPos.y,(int)MousePicker.breakPos.z, Cube.AIR); + break; + case GLFW.GLFW_MOUSE_BUTTON_2: + if(MousePicker.placePos!=null && !World.player.collides(MousePicker.placePos.x,MousePicker.placePos.y,MousePicker.placePos.z)) + World.setBlock((int)MousePicker.placePos.x,(int)MousePicker.placePos.y,(int)MousePicker.placePos.z, blockId); + break; + } + } + + public void mouseRelesed(int mouseButton) {} + + public void charAction(char c) {} + +} diff --git a/src/main/java/net/tylermurphy/Minecraft/Input/IInput.java b/src/main/java/net/tylermurphy/Minecraft/Input/IInput.java new file mode 100755 index 0000000..0eb48e7 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Input/IInput.java @@ -0,0 +1,12 @@ +package net.tylermurphy.Minecraft.Input; + +public abstract class IInput { + + public boolean enabled = true; + + public abstract void keyPressed(int keyCode); + public abstract void keyRelesed(int keyCode); + public abstract void mousePressed(int mouseButton); + public abstract void mouseRelesed(int mouseButton); + public abstract void charAction(char c); +} \ No newline at end of file diff --git a/src/main/java/net/tylermurphy/Minecraft/Input/Input.java b/src/main/java/net/tylermurphy/Minecraft/Input/Input.java new file mode 100755 index 0000000..86deb12 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Input/Input.java @@ -0,0 +1,154 @@ +package net.tylermurphy.Minecraft.Input; + +import java.util.ArrayList; +import java.util.List; + +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWCursorPosCallback; +import org.lwjgl.glfw.GLFWKeyCallback; +import org.lwjgl.glfw.GLFWMouseButtonCallback; + +import org.lwjgl.glfw.GLFWCharCallback; + +public class Input { + + private static List inputs = new ArrayList(); + + private static boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST]; + private static boolean[] mouseButtons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST]; + private static double mouseX, mouseY; + private static double mouseX_last, mouseY_last; + + private static GLFWKeyCallback keyboard; + private static GLFWCursorPosCallback mouseMove; + private static GLFWMouseButtonCallback mouseButton; + private static GLFWCharCallback character; + + public static void init() { + + keyboard = new GLFWKeyCallback() { + public void invoke(long window, int key, int scancode, int action, int mods) { + switch(action) { + case GLFW.GLFW_PRESS: + keys[key] = true; + for(IInput input : inputs) { + if(!input.enabled) continue; + input.keyPressed(key); + } + break; + case GLFW.GLFW_RELEASE: + keys[key] = false; + for(IInput input : inputs) { + if(!input.enabled) continue; + input.keyRelesed(key); + } + break; + } + + } + }; + + mouseMove = new GLFWCursorPosCallback() { + public void invoke(long window, double xPos, double yPos) { + mouseX = xPos; mouseY = yPos; + } + }; + + mouseButton = new GLFWMouseButtonCallback() { + public void invoke(long window, int button, int action, int mods) { + switch(action) { + case GLFW.GLFW_PRESS: + mouseButtons[button] = true; + for(IInput input : inputs) { + if(!input.enabled) continue; + input.mousePressed(button); + } + break; + case GLFW.GLFW_RELEASE: + mouseButtons[button] = false; + for(IInput input : inputs) { + if(!input.enabled) continue; + input.mouseRelesed(button); + } + break; + } + } + }; + + character = new GLFWCharCallback() { + public void invoke(long window, int key) { + for(IInput input : inputs) { + if(!input.enabled) continue; + input.charAction((char)key); + } + } + }; + } + + public static void addInput(IInput input) { + inputs.add(input); + } + + public static void destroy() { + keyboard.free(); + mouseMove.free(); + mouseButton.free(); + character.free(); + } + + public static boolean isKeyDown(int key) { + return keys[key]; + } + + public static boolean isButtonDown(int button) { + return mouseButtons[button]; + } + + public static double getMouseX() { + return mouseX; + } + + public static double getMouseY() { + return mouseY; + } + + public static double getMouseDX() { + double d = -(mouseX_last - mouseX); + mouseX_last = mouseX; + return d; + } + + public static double getMouseDY() { + double d = mouseY_last - mouseY; + mouseY_last = mouseY; + return d; + } + + public static GLFWKeyCallback getKeyboardCallback() { + return keyboard; + } + + public static GLFWCursorPosCallback getMouseMoveCallback() { + return mouseMove; + } + + public static GLFWMouseButtonCallback getMouseButtonCallback() { + return mouseButton; + } + + public static GLFWCharCallback getCharCallback() { + return character; + } + + public static void setEnabled(String className, boolean enabled) { + for(IInput input : inputs) { + String fullName = input.getClass().getTypeName(); + fullName = fullName.substring(fullName.lastIndexOf(".")+1,fullName.length()); + if(className.equals(fullName)) { + input.enabled = enabled; + } + } + } + + +} -- cgit v1.2.3-freya