diff options
Diffstat (limited to 'src/ch/epfl/xblast/client/KeyboardEventHandler.java')
-rw-r--r-- | src/ch/epfl/xblast/client/KeyboardEventHandler.java | 47 |
1 files changed, 47 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..2060959 --- /dev/null +++ b/src/ch/epfl/xblast/client/KeyboardEventHandler.java | |||
@@ -0,0 +1,47 @@ | |||
1 | package ch.epfl.xblast.client; | ||
2 | |||
3 | import ch.epfl.xblast.Lists; | ||
4 | import ch.epfl.xblast.PlayerAction; | ||
5 | |||
6 | import java.awt.event.KeyAdapter; | ||
7 | import java.awt.event.KeyEvent; | ||
8 | import java.util.Map; | ||
9 | import java.util.Objects; | ||
10 | import java.util.function.Consumer; | ||
11 | |||
12 | /** | ||
13 | * The game action keyboard event handler. | ||
14 | * | ||
15 | * @author Timothée FLOURE (257420) | ||
16 | * @author Pacien TRAN-GIRARD (261948) | ||
17 | */ | ||
18 | public class KeyboardEventHandler extends KeyAdapter { | ||
19 | |||
20 | private final Map<Integer, PlayerAction> playerActionMap; | ||
21 | private final Consumer<PlayerAction> consumer; | ||
22 | |||
23 | /** | ||
24 | * Instantiates a new KeyboardEventHandler. | ||
25 | * | ||
26 | * @param playerActionMap the map of key codes to PlayerAction. | ||
27 | * @param consumer consumer related to the EventHandler | ||
28 | */ | ||
29 | public KeyboardEventHandler(Map<Integer, PlayerAction> playerActionMap, Consumer<PlayerAction> consumer) { | ||
30 | this.playerActionMap = Lists.immutableMap(playerActionMap); | ||
31 | this.consumer = consumer; | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Executes the command related to the given key code. | ||
36 | * | ||
37 | * @param e event (pressed key) | ||
38 | */ | ||
39 | @Override | ||
40 | public void keyPressed(KeyEvent e) { | ||
41 | PlayerAction act = this.playerActionMap.get(e.getKeyCode()); | ||
42 | |||
43 | if (Objects.nonNull(act)) | ||
44 | this.consumer.accept(act); | ||
45 | } | ||
46 | |||
47 | } | ||