diff options
Diffstat (limited to 'src/ch/epfl/xblast/client/KeyboardEventHandler.java')
-rw-r--r-- | src/ch/epfl/xblast/client/KeyboardEventHandler.java | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/client/KeyboardEventHandler.java b/src/ch/epfl/xblast/client/KeyboardEventHandler.java index 2060959..ab64c80 100644 --- a/src/ch/epfl/xblast/client/KeyboardEventHandler.java +++ b/src/ch/epfl/xblast/client/KeyboardEventHandler.java | |||
@@ -5,6 +5,8 @@ import ch.epfl.xblast.PlayerAction; | |||
5 | 5 | ||
6 | import java.awt.event.KeyAdapter; | 6 | import java.awt.event.KeyAdapter; |
7 | import java.awt.event.KeyEvent; | 7 | import java.awt.event.KeyEvent; |
8 | import java.util.Collections; | ||
9 | import java.util.HashMap; | ||
8 | import java.util.Map; | 10 | import java.util.Map; |
9 | import java.util.Objects; | 11 | import java.util.Objects; |
10 | import java.util.function.Consumer; | 12 | import java.util.function.Consumer; |
@@ -17,6 +19,24 @@ import java.util.function.Consumer; | |||
17 | */ | 19 | */ |
18 | public class KeyboardEventHandler extends KeyAdapter { | 20 | public class KeyboardEventHandler extends KeyAdapter { |
19 | 21 | ||
22 | /** | ||
23 | * The default key->action mapping. | ||
24 | */ | ||
25 | private static final Map<Integer, PlayerAction> DEFAULT_KEY_MAP = buildDefaultKeyMap(); | ||
26 | |||
27 | private static Map<Integer, PlayerAction> buildDefaultKeyMap() { | ||
28 | Map<Integer, PlayerAction> playerActionMap = new HashMap<>(); | ||
29 | |||
30 | playerActionMap.put(KeyEvent.VK_UP, PlayerAction.MOVE_N); | ||
31 | playerActionMap.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E); | ||
32 | playerActionMap.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S); | ||
33 | playerActionMap.put(KeyEvent.VK_LEFT, PlayerAction.MOVE_W); | ||
34 | playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); | ||
35 | playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); | ||
36 | |||
37 | return Collections.unmodifiableMap(playerActionMap); | ||
38 | } | ||
39 | |||
20 | private final Map<Integer, PlayerAction> playerActionMap; | 40 | private final Map<Integer, PlayerAction> playerActionMap; |
21 | private final Consumer<PlayerAction> consumer; | 41 | private final Consumer<PlayerAction> consumer; |
22 | 42 | ||
@@ -32,6 +52,15 @@ public class KeyboardEventHandler extends KeyAdapter { | |||
32 | } | 52 | } |
33 | 53 | ||
34 | /** | 54 | /** |
55 | * Instantiates a new KeyboardEventHandler with the default key mapping. | ||
56 | * | ||
57 | * @param consumer consumer related to the EventHandler | ||
58 | */ | ||
59 | public KeyboardEventHandler(Consumer<PlayerAction> consumer) { | ||
60 | this(DEFAULT_KEY_MAP, consumer); | ||
61 | } | ||
62 | |||
63 | /** | ||
35 | * Executes the command related to the given key code. | 64 | * Executes the command related to the given key code. |
36 | * | 65 | * |
37 | * @param e event (pressed key) | 66 | * @param e event (pressed key) |