diff options
author | Pacien TRAN-GIRARD | 2016-05-09 19:50:58 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-05-09 19:50:58 +0200 |
commit | 83c7a8c1b46ca173dfcb3252d9136dfe15d3376d (patch) | |
tree | e5c44018e65fe6e21ebbde3b84a52fa85dd308df /src/ch | |
parent | e9b85677fd581791bb1b5dffbe7261a876879084 (diff) | |
download | xblast-83c7a8c1b46ca173dfcb3252d9136dfe15d3376d.tar.gz |
Clean keyboard handler
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/xblast/client/KeyboardEventHandler.java | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/src/ch/epfl/xblast/client/KeyboardEventHandler.java b/src/ch/epfl/xblast/client/KeyboardEventHandler.java index bebc4a5..a0d3848 100644 --- a/src/ch/epfl/xblast/client/KeyboardEventHandler.java +++ b/src/ch/epfl/xblast/client/KeyboardEventHandler.java | |||
@@ -1,38 +1,47 @@ | |||
1 | package ch.epfl.xblast.client; | 1 | package ch.epfl.xblast.client; |
2 | 2 | ||
3 | import ch.epfl.xblast.Lists; | ||
3 | import ch.epfl.xblast.PlayerAction; | 4 | import ch.epfl.xblast.PlayerAction; |
4 | 5 | ||
5 | import java.awt.event.KeyAdapter; | 6 | import java.awt.event.KeyAdapter; |
6 | import java.awt.event.KeyEvent; | 7 | import java.awt.event.KeyEvent; |
7 | import java.util.Map; | 8 | import java.util.Map; |
9 | import java.util.Objects; | ||
8 | import java.util.function.Consumer; | 10 | import java.util.function.Consumer; |
9 | 11 | ||
10 | /** | 12 | /** |
13 | * The game action keyboard event handler. | ||
14 | * | ||
11 | * @author Timothée FLOURE (257420) | 15 | * @author Timothée FLOURE (257420) |
16 | * @author Pacien TRAN-GIRARD (261948) | ||
12 | */ | 17 | */ |
13 | public class KeyboardEventHandler extends KeyAdapter{ | 18 | public class KeyboardEventHandler extends KeyAdapter { |
14 | private final Map<Integer,PlayerAction> playerActionMap; | 19 | |
15 | private final Consumer consumer; | 20 | private final Map<Integer, PlayerAction> playerActionMap; |
21 | private final Consumer<PlayerAction> consumer; | ||
16 | 22 | ||
17 | /** | 23 | /** |
18 | * Instantiates a new KeyboardEventHandler. | 24 | * Instantiates a new KeyboardEventHandler. |
19 | * | 25 | * |
20 | * @param playerActionMap the map of key codes to PlayerAction. | 26 | * @param playerActionMap the map of key codes to PlayerAction. |
21 | * @param consumer consumer related to the EventHandler | 27 | * @param consumer consumer related to the EventHandler |
22 | */ | 28 | */ |
23 | public KeyboardEventHandler(Map<Integer,PlayerAction> playerActionMap, Consumer<PlayerAction> consumer) { | 29 | public KeyboardEventHandler(Map<Integer, PlayerAction> playerActionMap, Consumer<PlayerAction> consumer) { |
24 | this.playerActionMap = playerActionMap; | 30 | this.playerActionMap = Lists.immutableMap(playerActionMap); |
25 | this.consumer = consumer; | 31 | this.consumer = consumer; |
26 | } | 32 | } |
27 | 33 | ||
28 | /** | 34 | /** |
29 | * Executes the command related to the given keycode. | 35 | * Executes the command related to the given key code. |
30 | * | 36 | * |
31 | * @param e event (pressed key) | 37 | * @param e event (pressed key) |
32 | */ | 38 | */ |
33 | @Override | 39 | @Override |
34 | public void keyTyped(KeyEvent e) { | 40 | public void keyTyped(KeyEvent e) { |
35 | if (playerActionMap.containsKey(e.getKeyCode())) | 41 | PlayerAction act = this.playerActionMap.get(e.getKeyCode()); |
36 | consumer.accept(playerActionMap.get(e.getKeyCode())); | 42 | |
43 | if (Objects.nonNull(act)) | ||
44 | this.consumer.accept(act); | ||
37 | } | 45 | } |
46 | |||
38 | } | 47 | } |