aboutsummaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-09 19:50:58 +0200
committerPacien TRAN-GIRARD2016-05-09 19:50:58 +0200
commit83c7a8c1b46ca173dfcb3252d9136dfe15d3376d (patch)
treee5c44018e65fe6e21ebbde3b84a52fa85dd308df /src/ch
parente9b85677fd581791bb1b5dffbe7261a876879084 (diff)
downloadxblast-83c7a8c1b46ca173dfcb3252d9136dfe15d3376d.tar.gz
Clean keyboard handler
Diffstat (limited to 'src/ch')
-rw-r--r--src/ch/epfl/xblast/client/KeyboardEventHandler.java27
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 @@
1package ch.epfl.xblast.client; 1package ch.epfl.xblast.client;
2 2
3import ch.epfl.xblast.Lists;
3import ch.epfl.xblast.PlayerAction; 4import ch.epfl.xblast.PlayerAction;
4 5
5import java.awt.event.KeyAdapter; 6import java.awt.event.KeyAdapter;
6import java.awt.event.KeyEvent; 7import java.awt.event.KeyEvent;
7import java.util.Map; 8import java.util.Map;
9import java.util.Objects;
8import java.util.function.Consumer; 10import 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 */
13public class KeyboardEventHandler extends KeyAdapter{ 18public 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}