Refactor KEYS array so it's no longer accessible to prevent bad practices and deprecated setting of the variable.

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
Nic0Nic0Nii 2022-06-06 17:57:31 +00:00
parent a6e6b92f7b
commit 8f13a24fd4
4 changed files with 16 additions and 9 deletions

View File

@ -99,14 +99,14 @@ public class RabiClone{
}*/ }*/
} }
if (p.KEYS.getOrDefault(KeyEvent.VK_F1,false)) { if (Key.isKeyHeld(KeyEvent.VK_F1)) {
if (level_renderer instanceof EditorRenderer) { if (level_renderer instanceof EditorRenderer) {
OBJ.remove(level_renderer); OBJ.remove(level_renderer);
OBJ.add(level_renderer=new LevelRenderer(p)); OBJ.add(level_renderer=new LevelRenderer(p));
StartGame(); StartGame();
} }
} }
if (p.KEYS.getOrDefault(KeyEvent.VK_F2,false)) { if (Key.isKeyHeld(KeyEvent.VK_F2)) {
if (!(level_renderer instanceof EditorRenderer)) { if (!(level_renderer instanceof EditorRenderer)) {
OBJ.clear(); OBJ.clear();
ResetGame(); ResetGame();

View File

@ -3,19 +3,27 @@ package sig.engine;
import java.util.HashMap; import java.util.HashMap;
import net.java.games.input.Component; import net.java.games.input.Component;
import sig.RabiClone;
public class Key implements Component{ public class Key implements Component{
public static HashMap<Integer,Identifier.Key> KEY_CONVERSION_MAP = new HashMap<>(); public static HashMap<Integer,Identifier.Key> KEY_CONVERSION_MAP = new HashMap<>();
static HashMap<Component.Identifier.Key,Boolean> KEYS = new HashMap<>();
int keycode; int keycode;
Key(int keycode) { Key(int keycode) {
this.keycode=keycode; this.keycode=keycode;
} }
public static void setKeyHeld(int keycode,boolean pressed) {
KEYS.put(KEY_CONVERSION_MAP.get(keycode),pressed);
}
public static boolean isKeyHeld(int keycode) {
return KEYS.getOrDefault(KEY_CONVERSION_MAP.get(keycode),false);
}
public boolean isKeyHeld() { public boolean isKeyHeld() {
return RabiClone.p.KEYS.get(getIdentifier()); return KEYS.getOrDefault(getIdentifier(),false);
} }
@Override @Override

View File

@ -90,7 +90,7 @@ public abstract class Object implements GameEntity{
} }
protected boolean KeyHeld(int key) { protected boolean KeyHeld(int key) {
return panel.KEYS.getOrDefault(Key.KEY_CONVERSION_MAP.get(key),false); return Key.isKeyHeld(key);
} }
protected void KeyPressed(int key) { protected void KeyPressed(int key) {

View File

@ -47,7 +47,6 @@ public class Panel extends JPanel implements Runnable,KeyListener {
public double nanaY = 0; public double nanaY = 0;
public Point mousePos=new Point(0,0); public Point mousePos=new Point(0,0);
public int button = 0; public int button = 0;
public HashMap<Component.Identifier.Key,Boolean> KEYS = new HashMap<>();
public HashMap<Integer,Boolean> MOUSE = new HashMap<>(); public HashMap<Integer,Boolean> MOUSE = new HashMap<>();
public static byte[] generalPalette = new byte[]{ public static byte[] generalPalette = new byte[]{
(byte)0x5b,(byte)0xa6,(byte)0x75, (byte)0x5b,(byte)0xa6,(byte)0x75,
@ -414,8 +413,8 @@ public class Panel extends JPanel implements Runnable,KeyListener {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
if (!KEYS.getOrDefault(e.getKeyCode(),false)) { if (!Key.isKeyHeld(e.getKeyCode())) {
KEYS.put(Key.KEY_CONVERSION_MAP.get(e.getKeyCode()),true); Key.setKeyHeld(e.getKeyCode(), true);
for (int i=0;i<RabiClone.OBJ.size();i++) { for (int i=0;i<RabiClone.OBJ.size();i++) {
RabiClone.OBJ.get(i).KeyPressed(e.getKeyCode()); RabiClone.OBJ.get(i).KeyPressed(e.getKeyCode());
} }
@ -425,7 +424,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
KEYS.put(Key.KEY_CONVERSION_MAP.get(e.getKeyCode()),false); Key.setKeyHeld(e.getKeyCode(), false);
for (int i=0;i<RabiClone.OBJ.size();i++) { for (int i=0;i<RabiClone.OBJ.size();i++) {
RabiClone.OBJ.get(i).KeyReleased(e.getKeyCode()); RabiClone.OBJ.get(i).KeyReleased(e.getKeyCode());
} }