11package com .redomar .game ;
22
3- import java .awt .event .KeyEvent ;
4- import java .awt .event .KeyListener ;
5- import java .awt .im .InputContext ;
6-
73import com .redomar .game .lib .SleepThread ;
84import com .redomar .game .script .PopUp ;
95import com .redomar .game .script .PrintTypes ;
106import com .redomar .game .script .Printing ;
117
8+ import java .awt .event .KeyEvent ;
9+ import java .awt .event .KeyListener ;
10+ import java .awt .im .InputContext ;
11+
1212public class InputHandler implements KeyListener {
1313
1414 private boolean isAzertyCountry ;
15-
16- public InputHandler (Game game ) {
17- InputContext context = InputContext .getInstance ();
18- // Important to know whether the keyboard is in Azerty or Qwerty.
19- // Azerty countries used QZSD instead of WASD keys.
20- isAzertyCountry = context .getLocale ().getCountry ().equals ("BE" )
21- || context .getLocale ().getCountry ().equals ("FR" );
22- game .addKeyListener (this );
23- }
24-
25- public class Key {
26- private int numTimesPressed = 0 ;
27- private boolean pressed = false ;
28-
29- public int getNumTimesPressed () {
30- return numTimesPressed ;
31- }
32-
33- public boolean isPressed () {
34- return pressed ;
35- }
36-
37- public void toggle (boolean isPressed ) {
38- pressed = isPressed ;
39- if (isPressed ) {
40- numTimesPressed ++;
41- }
42- }
43-
44- public void off () {
45- pressed = false ;
46- numTimesPressed = 0 ;
47- }
48- }
49-
5015 private Key up = new Key ();
5116 private Key down = new Key ();
5217 private Key left = new Key ();
@@ -57,6 +22,15 @@ public void off() {
5722 private boolean ignoreInput = false ;
5823 private PopUp popup = new PopUp ();
5924
25+ public InputHandler (Game game ) {
26+ InputContext context = InputContext .getInstance ();
27+ // Important to know whether the keyboard is in Azerty or Qwerty.
28+ // Azerty countries used QZSD instead of WASD keys.
29+ isAzertyCountry = context .getLocale ().getCountry ().equals ("BE" )
30+ || context .getLocale ().getCountry ().equals ("FR" );
31+ game .addKeyListener (this );
32+ }
33+
6034 public void keyPressed (KeyEvent e ) {
6135 toggleKey (e .getKeyCode (), true );
6236 }
@@ -69,24 +43,16 @@ public void keyTyped(KeyEvent e) {
6943
7044 }
7145
72- public void toggleKey (int keyCode , boolean isPressed ) {
73- if (isIgnoreInput () == false ) {
74- if (isAzertyCountry ) {
75- if (keyCode == KeyEvent .VK_Z || keyCode == KeyEvent .VK_UP ) {
76- up .toggle (isPressed );
77- }
78-
79- if (keyCode == KeyEvent .VK_Q || keyCode == KeyEvent .VK_LEFT ) {
80- left .toggle (isPressed );
81- }
82- } else {
83- if (keyCode == KeyEvent .VK_W || keyCode == KeyEvent .VK_UP ) {
84- up .toggle (isPressed );
85- }
46+ private void toggleKey (int keyCode , boolean isPressed ) {
47+ if (!isIgnoreInput ()) {
48+ if (keyCode == KeyEvent .VK_Z && isAzertyCountry || keyCode == KeyEvent .VK_W && !isAzertyCountry
49+ || keyCode == KeyEvent .VK_UP ) {
50+ up .toggle (isPressed );
51+ }
8652
87- if (keyCode == KeyEvent .VK_A || keyCode == KeyEvent .VK_LEFT ) {
88- left . toggle ( isPressed );
89- }
53+ if (keyCode == KeyEvent .VK_Q && isAzertyCountry || keyCode == KeyEvent .VK_A && ! isAzertyCountry
54+ || keyCode == KeyEvent . VK_LEFT ) {
55+ left . toggle ( isPressed );
9056 }
9157
9258 if (keyCode == KeyEvent .VK_S || keyCode == KeyEvent .VK_DOWN ) {
@@ -97,7 +63,7 @@ public void toggleKey(int keyCode, boolean isPressed) {
9763 right .toggle (isPressed );
9864 }
9965 }
100- if (isIgnoreInput () == true ) {
66+ if (isIgnoreInput ()) {
10167 up .toggle (false );
10268 down .toggle (false );
10369 left .toggle (false );
@@ -107,33 +73,17 @@ public void toggleKey(int keyCode, boolean isPressed) {
10773 this .setPlayMusic (true );
10874 }
10975
110- if (isAzertyCountry ) {
111- if (keyCode == KeyEvent .VK_W ) {
112- // if (map == 0){
113- // Game.getGame().setMap("/levels/water_level.png");
114- // map++;
115- // } else{
116- // Game.getGame().setMap("/levels/custom_level.png");
117- // map--;
118- // }
119- if (Game .getMap () == 2 ) {
120- Game .setChangeLevel (true );
121- Game .setNpc (false );
122- }
123- }
124- } else {
125- if (keyCode == KeyEvent .VK_Z ) {
126- // if (map == 0){
127- // Game.getGame().setMap("/levels/water_level.png");
128- // map++;
129- // } else{
130- // Game.getGame().setMap("/levels/custom_level.png");
131- // map--;
132- // }
133- if (Game .getMap () == 2 ) {
134- Game .setChangeLevel (true );
135- Game .setNpc (false );
136- }
76+ if (keyCode == KeyEvent .VK_W && isAzertyCountry || keyCode == KeyEvent .VK_Z && !isAzertyCountry ) {
77+ // if (map == 0){
78+ // Game.getGame().setMap("/levels/water_level.png");
79+ // map++;
80+ // } else{
81+ // Game.getGame().setMap("/levels/custom_level.png");
82+ // map--;
83+ // }
84+ if (Game .getMap () == 2 ) {
85+ Game .setChangeLevel (true );
86+ if (Game .isNpc ()) Game .setNpc (false );
13787 }
13888 }
13989 if (keyCode == KeyEvent .VK_N ) {
@@ -145,57 +95,45 @@ public void toggleKey(int keyCode, boolean isPressed) {
14595 }
14696 return ;
14797 }
148- if (Game .isNpc () == false ) {
98+ if (! Game .isNpc ()) {
14999 Game .setNpc (true );
150100 Game .npcSpawn ();
151101 print .print ("Dummy has been spawned" , PrintTypes .GAME );
152102 }
153103 }
154104 if (keyCode == KeyEvent .VK_K ) {
155- if (Game .isNpc () == true ) {
105+ if (Game .isNpc ()) {
156106 Game .setNpc (false );
157107 Game .npcKill ();
158108 print .print ("Dummy has been removed" , PrintTypes .GAME );
159109 }
160110 }
161- if (isAzertyCountry ) {
162- if (keyCode == KeyEvent .VK_A ) {
163- Game .setClosing (true );
164- try {
165- Thread .sleep (1000 );
166- } catch (InterruptedException e ) {
167- e .printStackTrace ();
168- }
169- Game .getLevel ().removeEntity (
170- Game .getPlayer ().getSantizedUsername ());
171- Game .setRunning (false );
172- Game .getFrame ().dispose ();
173- System .exit (1 );
174- }
175- } else {
176- if (keyCode == KeyEvent .VK_Q ) {
177- Game .setClosing (true );
178- try {
179- Thread .sleep (1000 );
180- } catch (InterruptedException e ) {
181- e .printStackTrace ();
182- }
183- Game .getLevel ().removeEntity (
184- Game .getPlayer ().getSantizedUsername ());
185- Game .setRunning (false );
186- Game .getFrame ().dispose ();
187- System .exit (1 );
188- }
189- }
111+
112+ if (keyCode == KeyEvent .VK_A && isAzertyCountry || keyCode == KeyEvent .VK_Q && !isAzertyCountry )
113+ this .quitGame ();
190114
191115 if (keyCode == KeyEvent .VK_BACK_QUOTE ) {
192- if (Game .isClosing () == false && Game .isDevMode () == false ) {
116+ if (! Game .isClosing () && ! Game .isDevMode ()) {
193117 Game .setDevMode (true );
194118 new Thread (new SleepThread ());
195119 }
196120 }
197121 }
198122
123+ private void quitGame () {
124+ Game .setClosing (true );
125+ try {
126+ Thread .sleep (1000 );
127+ } catch (InterruptedException e ) {
128+ e .printStackTrace ();
129+ }
130+ Game .getLevel ().removeEntity (
131+ Game .getPlayer ().getSantizedUsername ());
132+ Game .setRunning (false );
133+ Game .getFrame ().dispose ();
134+ System .exit (1 );
135+ }
136+
199137 public void untoggle (boolean toggle ) {
200138 this .ignoreInput = toggle ;
201139 }
@@ -208,11 +146,11 @@ public void setMap(int map) {
208146 this .map = map ;
209147 }
210148
211- public boolean isPlayMusic () {
149+ boolean isPlayMusic () {
212150 return PlayMusic ;
213151 }
214152
215- public void setPlayMusic (boolean playMusic ) {
153+ void setPlayMusic (boolean playMusic ) {
216154 PlayMusic = playMusic ;
217155 }
218156
@@ -252,8 +190,33 @@ public boolean isIgnoreInput() {
252190 return ignoreInput ;
253191 }
254192
255- public void setIgnoreInput (boolean ignoreInput ) {
193+ private void setIgnoreInput (boolean ignoreInput ) {
256194 this .ignoreInput = ignoreInput ;
257195 }
258196
197+ public class Key {
198+ private int numTimesPressed = 0 ;
199+ private boolean pressed = false ;
200+
201+ public int getNumTimesPressed () {
202+ return numTimesPressed ;
203+ }
204+
205+ public boolean isPressed () {
206+ return pressed ;
207+ }
208+
209+ void toggle (boolean isPressed ) {
210+ pressed = isPressed ;
211+ if (isPressed ) {
212+ numTimesPressed ++;
213+ }
214+ }
215+
216+ public void off () {
217+ pressed = false ;
218+ numTimesPressed = 0 ;
219+ }
220+ }
221+
259222}
0 commit comments