diff options
author | Timothée Floure | 2016-05-09 16:37:36 +0200 |
---|---|---|
committer | Timothée Floure | 2016-05-09 16:37:36 +0200 |
commit | 38711042dd2c39cf51cd2d063f2eb38d0bbb5a45 (patch) | |
tree | 95ede85ed5baf84ccebc361f43024991bc120abb /test/ch | |
parent | aa7cd18ee5b13ee4373db58dc8ab9157a348c72e (diff) | |
download | xblast-38711042dd2c39cf51cd2d063f2eb38d0bbb5a45.tar.gz |
Implement KeyboardEventHandler to the Graphical Simulation
Diffstat (limited to 'test/ch')
-rw-r--r-- | test/ch/epfl/xblast/simulation/GraphicalSimulation.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java index 5706577..7e4454d 100644 --- a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java +++ b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java | |||
@@ -1,5 +1,6 @@ | |||
1 | package ch.epfl.xblast.simulation; | 1 | package ch.epfl.xblast.simulation; |
2 | 2 | ||
3 | import ch.epfl.xblast.PlayerAction; | ||
3 | import ch.epfl.xblast.PlayerID; | 4 | import ch.epfl.xblast.PlayerID; |
4 | import ch.epfl.xblast.client.*; | 5 | import ch.epfl.xblast.client.*; |
5 | import ch.epfl.xblast.server.*; | 6 | import ch.epfl.xblast.server.*; |
@@ -8,7 +9,11 @@ import ch.epfl.xblast.server.debug.RandomEventGenerator; | |||
8 | import ch.epfl.xblast.server.painter.BoardPainter; | 9 | import ch.epfl.xblast.server.painter.BoardPainter; |
9 | 10 | ||
10 | import javax.swing.*; | 11 | import javax.swing.*; |
12 | import java.awt.event.KeyEvent; | ||
13 | import java.util.HashMap; | ||
11 | import java.util.List; | 14 | import java.util.List; |
15 | import java.util.Map; | ||
16 | import java.util.function.Consumer; | ||
12 | 17 | ||
13 | /** | 18 | /** |
14 | * @author Timothée FLOURE (257420) | 19 | * @author Timothée FLOURE (257420) |
@@ -22,6 +27,18 @@ public class GraphicalSimulation { | |||
22 | private static final int BOMB_PROB = 100; | 27 | private static final int BOMB_PROB = 100; |
23 | private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); | 28 | private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); |
24 | 29 | ||
30 | private static Map<Integer,PlayerAction> buildPlayerActionMap() { | ||
31 | Map<Integer, PlayerAction> playerActionMap = new HashMap<>(); | ||
32 | playerActionMap.put(KeyEvent.VK_UP, PlayerAction.MOVE_N); | ||
33 | playerActionMap.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E); | ||
34 | playerActionMap.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S); | ||
35 | playerActionMap.put(KeyEvent.VK_LEFT, PlayerAction.MOVE_W); | ||
36 | playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); | ||
37 | playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); | ||
38 | |||
39 | return playerActionMap; | ||
40 | } | ||
41 | |||
25 | private static ch.epfl.xblast.client.GameState getClientData(GameState gs) { | 42 | private static ch.epfl.xblast.client.GameState getClientData(GameState gs) { |
26 | List<Byte> serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); | 43 | List<Byte> serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); |
27 | return GameStateDeserializer.deserialize(serializedGameState); | 44 | return GameStateDeserializer.deserialize(serializedGameState); |
@@ -48,11 +65,19 @@ public class GraphicalSimulation { | |||
48 | } | 65 | } |
49 | 66 | ||
50 | public static void main(String[] args) { | 67 | public static void main(String[] args) { |
68 | |||
69 | // Build the window | ||
51 | JFrame frame = buildFrame(); | 70 | JFrame frame = buildFrame(); |
52 | XBlastComponent xblast = new XBlastComponent(); | 71 | XBlastComponent xblast = new XBlastComponent(); |
53 | frame.setContentPane(xblast); | 72 | frame.setContentPane(xblast); |
54 | frame.pack(); | 73 | frame.pack(); |
55 | 74 | ||
75 | // Attach a KeyboardEventHandler | ||
76 | Consumer<PlayerAction> c = System.out::println; | ||
77 | frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), c)); | ||
78 | frame.requestFocusInWindow(); | ||
79 | |||
80 | // Run the Simulation | ||
56 | for (GameState gs = INITIAL_GAMESTATE; !isSimulationOver(gs); gs = nextGameState(gs)) | 81 | for (GameState gs = INITIAL_GAMESTATE; !isSimulationOver(gs); gs = nextGameState(gs)) |
57 | displayGameState(getClientData(gs), xblast); | 82 | displayGameState(getClientData(gs), xblast); |
58 | } | 83 | } |