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>
main
Nic0Nic0Nii 3 years ago
parent a6e6b92f7b
commit 8f13a24fd4
  1. 4
      src/sig/RabiClone.java
  2. 12
      src/sig/engine/Key.java
  3. 2
      src/sig/engine/Object.java
  4. 7
      src/sig/engine/Panel.java

@ -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) {
OBJ.remove(level_renderer);
OBJ.add(level_renderer=new LevelRenderer(p));
StartGame();
}
}
if (p.KEYS.getOrDefault(KeyEvent.VK_F2,false)) {
if (Key.isKeyHeld(KeyEvent.VK_F2)) {
if (!(level_renderer instanceof EditorRenderer)) {
OBJ.clear();
ResetGame();

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

@ -90,7 +90,7 @@ public abstract class Object implements GameEntity{
}
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) {

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

Loading…
Cancel
Save