-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputHandler.java
48 lines (40 loc) · 1.36 KB
/
InputHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* e-LEMON-ators */
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class InputHandler implements KeyListener
{
public InputHandler(GamePanel game) {
game.addKeyListener(this);
}
public class Key {
private boolean pressed = false;
public boolean isPressed() {
return pressed;
}
public void toggle(boolean isPressed) {
pressed = isPressed;
}
}
public Key up = new Key();
public Key down = new Key();
public Key left = new Key();
public Key right = new Key();
public Key sprint = new Key();
public Key barrage = new Key();
public void keyPressed(KeyEvent key) {
toggleKey(key.getKeyCode(), true);
}
public void keyReleased(KeyEvent key) {
toggleKey(key.getKeyCode(), false);
}
public void keyTyped(KeyEvent key) {}
public void toggleKey(int keyCode, boolean isPressed)
{
if (keyCode == KeyEvent.VK_W) { up.toggle(isPressed); }
if (keyCode == KeyEvent.VK_S) { down.toggle(isPressed); }
if (keyCode == KeyEvent.VK_A) { left.toggle(isPressed); }
if (keyCode == KeyEvent.VK_D) { right.toggle(isPressed); }
if (keyCode == KeyEvent.VK_SHIFT) { sprint.toggle(isPressed); }
if (keyCode == KeyEvent.VK_SPACE) { barrage.toggle(isPressed); }
}
}