11package com .redomar .game ;
22
33import com .redomar .game .lib .SleepThread ;
4- import com .redomar .game .script .PopUp ;
54import com .redomar .game .script .PrintTypes ;
6- import com .redomar .game .script .Printing ;
5+ import com .redomar .game .script .Printer ;
76
87import java .awt .event .KeyEvent ;
98import java .awt .event .KeyListener ;
109import java .awt .im .InputContext ;
10+ import java .util .HashMap ;
11+ import java .util .Map ;
1112
1213public class InputHandler implements KeyListener {
1314
14- private final boolean isAzertyCountry ;
15- private final Printing print = new Printing ();
16- private final PopUp popup = new PopUp ();
15+ private final boolean frenchKeyboardLayout ;
16+ private final Printer inputPrinter = new Printer (PrintTypes .INPUT );
1717 private final Key UP_KEY = new Key ();
1818 private final Key DOWN_KEY = new Key ();
1919 private final Key LEFT_KEY = new Key ();
2020 private final Key RIGHT_KEY = new Key ();
21- private boolean ignoreInput = false ;
22- private boolean toggleMusic = false ;
21+ private boolean musicPlaying = false ;
22+
2323
2424 public InputHandler (Game game ) {
2525 InputContext context = InputContext .getInstance ();
26- // Important to know whether the keyboard is in Azerty or Qwerty.
27- // Azerty countries used QZSD instead of WASD keys.
28- isAzertyCountry = context .getLocale ().getCountry ().equals ("BE" ) || context .getLocale ().getCountry ().equals ("FR" );
26+ frenchKeyboardLayout = context .getLocale ().getCountry ().equals ("BE" ) || context .getLocale ().getCountry ().equals ("FR" );
2927 game .addKeyListener (this );
3028 }
3129
@@ -34,120 +32,94 @@ public void keyPressed(KeyEvent e) {
3432 }
3533
3634 public void keyReleased (KeyEvent e ) {
37- toggleKey (e .getKeyCode (), false );
38- }
39-
40- public void keyTyped (KeyEvent e ) {
41-
42- }
43-
44- private void toggleKey (int keyCode , boolean isPressed ) {
45- if (!isIgnoreInput ()) {
46- if (keyCode == KeyEvent .VK_Z && isAzertyCountry || keyCode == KeyEvent .VK_W && !isAzertyCountry || keyCode == KeyEvent .VK_UP ) {
47- UP_KEY .toggle (isPressed );
48- }
49-
50- if (keyCode == KeyEvent .VK_Q && isAzertyCountry || keyCode == KeyEvent .VK_A && !isAzertyCountry || keyCode == KeyEvent .VK_LEFT ) {
51- LEFT_KEY .toggle (isPressed );
52- }
53-
54- if (keyCode == KeyEvent .VK_S || keyCode == KeyEvent .VK_DOWN ) {
55- DOWN_KEY .toggle (isPressed );
56- }
57-
58- if (keyCode == KeyEvent .VK_D || keyCode == KeyEvent .VK_RIGHT ) {
59- RIGHT_KEY .toggle (isPressed );
35+ int keyCode = e .getKeyCode ();
36+ toggleKey (keyCode , false );
37+ if (keyCode == KeyEvent .VK_BACK_QUOTE ) {
38+ if (!Game .isClosing ()) {
39+ Game .setDevMode (!Game .isDevMode ());
40+ new Thread (new SleepThread ());
41+ inputPrinter .print (String .format ("Debug Mode %s" , Game .isDevMode () ? "Enabled" : "Disabled" ));
6042 }
6143 }
62- if (isIgnoreInput ()) {
63- UP_KEY .toggle (false );
64- DOWN_KEY .toggle (false );
65- LEFT_KEY .toggle (false );
66- RIGHT_KEY .toggle (false );
67- }
6844
6945 if (keyCode == KeyEvent .VK_M ) {
70- if (!toggleMusic ) {
46+ if (!musicPlaying ) {
7147 Game .getBackgroundMusic ().play ();
72- print .print ("Playing Music" , PrintTypes .MUSIC );
73- toggleMusic = true ;
48+ musicPlaying = true ;
49+ } else {
50+ Game .getBackgroundMusic ().stop ();
51+ musicPlaying = false ;
52+ }
53+ }
54+
55+ if (keyCode == KeyEvent .VK_N ) {
56+ if (!Game .isNpc ()) {
57+ Game .setNpc (true );
58+ Game .npcSpawn ();
59+ inputPrinter .print ("Dummy has been spawned" , PrintTypes .GAME );
7460 }
7561 }
62+ }
7663
77- if (keyCode == KeyEvent .VK_COMMA ) {
78- Game .getBackgroundMusic ().stop ();
79- if (toggleMusic ) print .print ("Stopping Music" , PrintTypes .MUSIC );
80- toggleMusic = false ;
64+ public void keyTyped (KeyEvent e ) {
65+
66+ }
67+
68+ private void toggleKey (int keyCode , boolean isPressed ) {
69+ Map <Integer , Runnable > keyCodeActions = new HashMap <>();
70+
71+ keyCodeActions .put (KeyEvent .VK_S , () -> DOWN_KEY .toggle (isPressed ));
72+ keyCodeActions .put (KeyEvent .VK_D , () -> RIGHT_KEY .toggle (isPressed ));
73+ keyCodeActions .put (KeyEvent .VK_UP , () -> UP_KEY .toggle (isPressed ));
74+ keyCodeActions .put (KeyEvent .VK_LEFT , () -> LEFT_KEY .toggle (isPressed ));
75+ keyCodeActions .put (KeyEvent .VK_DOWN , () -> DOWN_KEY .toggle (isPressed ));
76+ keyCodeActions .put (KeyEvent .VK_RIGHT , () -> RIGHT_KEY .toggle (isPressed ));
77+
78+ if (frenchKeyboardLayout ) {
79+ keyCodeActions .put (KeyEvent .VK_Q , () -> LEFT_KEY .toggle (isPressed ));
80+ keyCodeActions .put (KeyEvent .VK_Z , () -> UP_KEY .toggle (isPressed ));
81+ keyCodeActions .put (KeyEvent .VK_A , this ::quitGame );
82+ } else {
83+ keyCodeActions .put (KeyEvent .VK_A , () -> LEFT_KEY .toggle (isPressed ));
84+ keyCodeActions .put (KeyEvent .VK_W , () -> UP_KEY .toggle (isPressed ));
85+ keyCodeActions .put (KeyEvent .VK_Q , this ::quitGame );
8186 }
8287
88+ if (keyCodeActions .containsKey (keyCode )) keyCodeActions .get (keyCode ).run ();
8389
84- if (keyCode == KeyEvent .VK_W && isAzertyCountry || keyCode == KeyEvent .VK_Z && !isAzertyCountry ) {
85- // if (map == 0){
86- // Game.getGame().setMap("/levels/water_level.png");
87- // map++;
88- // } else{
89- // Game.getGame().setMap("/levels/custom_level.png");
90- // map--;
91- // }
90+ if (keyCode == KeyEvent .VK_W && frenchKeyboardLayout || keyCode == KeyEvent .VK_Z && !frenchKeyboardLayout ) {
9291 if (Game .getMap () == 2 ) {
9392 if (Game .isNpc ()) {
9493 Game .setNpc (false );
9594 }
9695 Game .setChangeLevel (true );
9796 }
9897 }
99- if (keyCode == KeyEvent .VK_N ) {
100- if (Game .getPlayer ().isMoving ()) {
101- setIgnoreInput (true );
102- int n = popup .Warn ("Stop moving before spawning dummy AI" );
103- if (n == 0 ) {
104- setIgnoreInput (false );
105- }
106- return ;
107- }
108- if (!Game .isNpc ()) {
109- Game .setNpc (true );
110- Game .npcSpawn ();
111- print .print ("Dummy has been spawned" , PrintTypes .GAME );
112- }
113- }
98+
11499 if (keyCode == KeyEvent .VK_K ) {
115100 if (Game .isNpc ()) {
116101 Game .setNpc (false );
117102 Game .npcKill ();
118- print .print ("Dummy has been removed" , PrintTypes .GAME );
103+ inputPrinter .print ("Dummy has been removed" , PrintTypes .GAME );
119104 }
120105 }
121106
122- if (keyCode == KeyEvent .VK_A && isAzertyCountry || keyCode == KeyEvent .VK_Q && !isAzertyCountry )
123- this .quitGame ();
124-
125- if (keyCode == KeyEvent .VK_BACK_QUOTE ) {
126- if (!Game .isClosing () && !Game .isDevMode ()) {
127- Game .setDevMode (true );
128- new Thread (new SleepThread ());
129- }
130- }
131107 }
132108
133109 private void quitGame () {
134110 Game .setClosing (true );
135- if (!print .removeLog ()) System .err .println ("Could not delete Log file" );
111+ if (!inputPrinter .removeLog ()) System .err .println ("Could not delete Log file" );
136112 try {
137113 Thread .sleep (1000 );
138114 } catch (InterruptedException e ) {
139115 e .printStackTrace ();
140116 }
141- Game .getLevel ().removeEntity (Game .getPlayer ().getSanitisedUsername ());
142- Game .setRunning ( false );
117+ Game .getLevel ().removeEntity (Game .getPlayer ().getName ());
118+ Game .getGame (). stop ( );
143119 Game .getFrame ().dispose ();
144120 System .exit (0 );
145121 }
146122
147- public void untoggle (boolean toggle ) {
148- this .ignoreInput = toggle ;
149- }
150-
151123 public Key getUP_KEY () {
152124 return UP_KEY ;
153125 }
@@ -164,14 +136,6 @@ public Key getRIGHT_KEY() {
164136 return RIGHT_KEY ;
165137 }
166138
167- public boolean isIgnoreInput () {
168- return ignoreInput ;
169- }
170-
171- private void setIgnoreInput (boolean ignoreInput ) {
172- this .ignoreInput = ignoreInput ;
173- }
174-
175139 public static class Key {
176140 private int numTimesPressed = 0 ;
177141 private boolean pressed = false ;
0 commit comments