From a6e6b92f7b08cc90bdacbf9dbfdf558f9cc26b5a Mon Sep 17 00:00:00 2001 From: Nic0Nic0Nii Date: Mon, 6 Jun 2022 17:42:11 +0000 Subject: [PATCH] Implement keyboard keys as a component Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 --- src/sig/RabiClone.java | 108 ++++++++++++++++++++++++++++++++++++- src/sig/engine/Key.java | 51 ++++++++++++++++++ src/sig/engine/Object.java | 2 +- src/sig/engine/Panel.java | 8 +-- 4 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 src/sig/engine/Key.java diff --git a/src/sig/RabiClone.java b/src/sig/RabiClone.java index b9a43b5..ca555ed 100644 --- a/src/sig/RabiClone.java +++ b/src/sig/RabiClone.java @@ -9,6 +9,7 @@ import net.java.games.input.ControllerEvent; import net.java.games.input.ControllerListener; import net.java.games.input.Event; import net.java.games.input.EventQueue; +import net.java.games.input.Component.Identifier; import java.util.ArrayList; import java.util.Arrays; @@ -21,6 +22,7 @@ import sig.objects.EditorRenderer; import sig.objects.Erinoah; import sig.objects.LevelRenderer; import sig.objects.Player; +import sig.engine.Key; import sig.engine.Object; import java.awt.Toolkit; import java.awt.event.KeyEvent; @@ -46,6 +48,9 @@ public class RabiClone{ public static Maps CURRENT_MAP = Maps.WORLD1; public static Controller[] CONTROLLERS = new Controller[]{}; public static void main(String[] args) { + + InitializeKeyConversionMap(); + f = new JFrame(PROGRAM_NAME); f.setResizable(false); f.setUndecorated(true); @@ -76,7 +81,7 @@ public class RabiClone{ lastGameTime=System.nanoTime(); double updateMult = Math.min(1/60d,timePassed/1000000000d); - CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().getControllers() + CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().getControllers(); for (int i=0;i KEY_CONVERSION_MAP = new HashMap<>(); + int keycode; + + Key(int keycode) { + this.keycode=keycode; + } + + public boolean isKeyHeld() { + return RabiClone.p.KEYS.get(getIdentifier()); + } + + @Override + public Identifier getIdentifier() { + return KEY_CONVERSION_MAP.get(keycode); + } + + @Override + public boolean isRelative() { + return false; + } + + @Override + public boolean isAnalog() { + return false; + } + + @Override + public float getDeadZone() { + return 0; + } + + @Override + public float getPollData() { + return isKeyHeld()?1.0f:0.0f; + } + + @Override + public String getName() { + return "Java System Keyboard"; + } + +} diff --git a/src/sig/engine/Object.java b/src/sig/engine/Object.java index 75a40c7..f204310 100644 --- a/src/sig/engine/Object.java +++ b/src/sig/engine/Object.java @@ -90,7 +90,7 @@ public abstract class Object implements GameEntity{ } protected boolean KeyHeld(int key) { - return panel.KEYS.getOrDefault(key,false); + return panel.KEYS.getOrDefault(Key.KEY_CONVERSION_MAP.get(key),false); } protected void KeyPressed(int key) { diff --git a/src/sig/engine/Panel.java b/src/sig/engine/Panel.java index 2982cd2..6dbbf9d 100644 --- a/src/sig/engine/Panel.java +++ b/src/sig/engine/Panel.java @@ -18,6 +18,8 @@ import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.event.MouseInputListener; +import net.java.games.input.Component; + import java.awt.event.KeyListener; import sig.DrawLoop; @@ -45,7 +47,7 @@ public class Panel extends JPanel implements Runnable,KeyListener { public double nanaY = 0; public Point mousePos=new Point(0,0); public int button = 0; - public HashMap KEYS = new HashMap<>(); + public HashMap KEYS = new HashMap<>(); public HashMap MOUSE = new HashMap<>(); public static byte[] generalPalette = new byte[]{ (byte)0x5b,(byte)0xa6,(byte)0x75, @@ -413,7 +415,7 @@ public class Panel extends JPanel implements Runnable,KeyListener { @Override public void keyPressed(KeyEvent e) { if (!KEYS.getOrDefault(e.getKeyCode(),false)) { - KEYS.put(e.getKeyCode(),true); + KEYS.put(Key.KEY_CONVERSION_MAP.get(e.getKeyCode()),true); for (int i=0;i