diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ch/epfl/xblast/client/KeyboardEventHandler.java | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/client/KeyboardEventHandler.java b/src/ch/epfl/xblast/client/KeyboardEventHandler.java new file mode 100644 index 0000000..bebc4a5 --- /dev/null +++ b/src/ch/epfl/xblast/client/KeyboardEventHandler.java | |||
@@ -0,0 +1,38 @@ | |||
1 | package ch.epfl.xblast.client; | ||
2 | |||
3 | import ch.epfl.xblast.PlayerAction; | ||
4 | |||
5 | import java.awt.event.KeyAdapter; | ||
6 | import java.awt.event.KeyEvent; | ||
7 | import java.util.Map; | ||
8 | import java.util.function.Consumer; | ||
9 | |||
10 | /** | ||
11 | * @author Timothée FLOURE (257420) | ||
12 | */ | ||
13 | public class KeyboardEventHandler extends KeyAdapter{ | ||
14 | private final Map<Integer,PlayerAction> playerActionMap; | ||
15 | private final Consumer consumer; | ||
16 | |||
17 | /** | ||
18 | * Instantiates a new KeyboardEventHandler. | ||
19 | * | ||
20 | * @param playerActionMap the map of key codes to PlayerAction. | ||
21 | * @param consumer consumer related to the EventHandler | ||
22 | */ | ||
23 | public KeyboardEventHandler(Map<Integer,PlayerAction> playerActionMap, Consumer<PlayerAction> consumer) { | ||
24 | this.playerActionMap = playerActionMap; | ||
25 | this.consumer = consumer; | ||
26 | } | ||
27 | |||
28 | /** | ||
29 | * Executes the command related to the given keycode. | ||
30 | * | ||
31 | * @param e event (pressed key) | ||
32 | */ | ||
33 | @Override | ||
34 | public void keyTyped(KeyEvent e) { | ||
35 | if (playerActionMap.containsKey(e.getKeyCode())) | ||
36 | consumer.accept(playerActionMap.get(e.getKeyCode())); | ||
37 | } | ||
38 | } | ||