forked from redomar/JavaGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputHandler.java
More file actions
222 lines (183 loc) · 4.66 KB
/
Copy pathInputHandler.java
File metadata and controls
222 lines (183 loc) · 4.66 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.redomar.game;
import com.redomar.game.lib.SleepThread;
import com.redomar.game.script.PopUp;
import com.redomar.game.script.PrintTypes;
import com.redomar.game.script.Printing;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.im.InputContext;
public class InputHandler implements KeyListener {
private boolean isAzertyCountry;
private Key up = new Key();
private Key down = new Key();
private Key left = new Key();
private Key right = new Key();
private Printing print = new Printing();
private int map;
private boolean ignoreInput = false;
private PopUp popup = new PopUp();
public InputHandler(Game game) {
InputContext context = InputContext.getInstance();
// Important to know whether the keyboard is in Azerty or Qwerty.
// Azerty countries used QZSD instead of WASD keys.
isAzertyCountry = context.getLocale().getCountry().equals("BE")
|| context.getLocale().getCountry().equals("FR");
game.addKeyListener(this);
}
public void keyPressed(KeyEvent e) {
toggleKey(e.getKeyCode(), true);
}
public void keyReleased(KeyEvent e) {
toggleKey(e.getKeyCode(), false);
}
public void keyTyped(KeyEvent e) {
}
private void toggleKey(int keyCode, boolean isPressed) {
if (!isIgnoreInput()) {
if (keyCode == KeyEvent.VK_Z && isAzertyCountry || keyCode == KeyEvent.VK_W && !isAzertyCountry
|| keyCode == KeyEvent.VK_UP) {
up.toggle(isPressed);
}
if (keyCode == KeyEvent.VK_Q && isAzertyCountry || keyCode == KeyEvent.VK_A && !isAzertyCountry
|| keyCode == KeyEvent.VK_LEFT) {
left.toggle(isPressed);
}
if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) {
down.toggle(isPressed);
}
if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) {
right.toggle(isPressed);
}
}
if (isIgnoreInput()) {
up.toggle(false);
down.toggle(false);
left.toggle(false);
right.toggle(false);
}
if (keyCode == KeyEvent.VK_M) {
Game.getBackgroundMusic().play();
}
if (keyCode == KeyEvent.VK_COMMA) {
Game.getBackgroundMusic().stop();
}
if (keyCode == KeyEvent.VK_W && isAzertyCountry || keyCode == KeyEvent.VK_Z && !isAzertyCountry) {
// if (map == 0){
// Game.getGame().setMap("/levels/water_level.png");
// map++;
// } else{
// Game.getGame().setMap("/levels/custom_level.png");
// map--;
// }
if (Game.getMap() == 2) {
if (Game.isNpc()) {
Game.setNpc(false);
}
Game.setChangeLevel(true);
}
}
if (keyCode == KeyEvent.VK_N) {
if (Game.getPlayer().isMoving()) {
setIgnoreInput(true);
int n = popup.Warn("Stop moving before spawning dummy AI");
if (n == 0) {
setIgnoreInput(false);
}
return;
}
if (!Game.isNpc()) {
Game.setNpc(true);
Game.npcSpawn();
print.print("Dummy has been spawned", PrintTypes.GAME);
}
}
if (keyCode == KeyEvent.VK_K) {
if (Game.isNpc()) {
Game.setNpc(false);
Game.npcKill();
print.print("Dummy has been removed", PrintTypes.GAME);
}
}
if (keyCode == KeyEvent.VK_A && isAzertyCountry || keyCode == KeyEvent.VK_Q && !isAzertyCountry)
this.quitGame();
if (keyCode == KeyEvent.VK_BACK_QUOTE) {
if (!Game.isClosing() && !Game.isDevMode()) {
Game.setDevMode(true);
new Thread(new SleepThread());
}
}
}
private void quitGame() {
Game.setClosing(true);
print.removeLog();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Game.getLevel().removeEntity(
Game.getPlayer().getSanitisedUsername());
Game.setRunning(false);
Game.getFrame().dispose();
System.exit(1);
}
public void untoggle(boolean toggle) {
this.ignoreInput = toggle;
}
public int getMap() {
return map;
}
public void setMap(int map) {
this.map = map;
}
public Key getUp() {
return up;
}
public void setUp(Key up) {
this.up = up;
}
public Key getDown() {
return down;
}
public void setDown(Key down) {
this.down = down;
}
public Key getLeft() {
return left;
}
public void setLeft(Key left) {
this.left = left;
}
public Key getRight() {
return right;
}
public void setRight(Key right) {
this.right = right;
}
public boolean isIgnoreInput() {
return ignoreInput;
}
private void setIgnoreInput(boolean ignoreInput) {
this.ignoreInput = ignoreInput;
}
public class Key {
private int numTimesPressed = 0;
private boolean pressed = false;
public int getNumTimesPressed() {
return numTimesPressed;
}
public boolean isPressed() {
return pressed;
}
void toggle(boolean isPressed) {
pressed = isPressed;
if (isPressed) {
numTimesPressed++;
}
}
public void off() {
pressed = false;
numTimesPressed = 0;
}
}
}