From 38711042dd2c39cf51cd2d063f2eb38d0bbb5a45 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 9 May 2016 16:37:36 +0200 Subject: Implement KeyboardEventHandler to the Graphical Simulation --- .../xblast/simulation/GraphicalSimulation.java | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'test/ch/epfl') 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 @@ package ch.epfl.xblast.simulation; +import ch.epfl.xblast.PlayerAction; import ch.epfl.xblast.PlayerID; import ch.epfl.xblast.client.*; import ch.epfl.xblast.server.*; @@ -8,7 +9,11 @@ import ch.epfl.xblast.server.debug.RandomEventGenerator; import ch.epfl.xblast.server.painter.BoardPainter; import javax.swing.*; +import java.awt.event.KeyEvent; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.function.Consumer; /** * @author Timothée FLOURE (257420) @@ -22,6 +27,18 @@ public class GraphicalSimulation { private static final int BOMB_PROB = 100; private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); + private static Map buildPlayerActionMap() { + Map playerActionMap = new HashMap<>(); + playerActionMap.put(KeyEvent.VK_UP, PlayerAction.MOVE_N); + playerActionMap.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E); + playerActionMap.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S); + playerActionMap.put(KeyEvent.VK_LEFT, PlayerAction.MOVE_W); + playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); + playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); + + return playerActionMap; + } + private static ch.epfl.xblast.client.GameState getClientData(GameState gs) { List serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); return GameStateDeserializer.deserialize(serializedGameState); @@ -48,11 +65,19 @@ public class GraphicalSimulation { } public static void main(String[] args) { + + // Build the window JFrame frame = buildFrame(); XBlastComponent xblast = new XBlastComponent(); frame.setContentPane(xblast); frame.pack(); + // Attach a KeyboardEventHandler + Consumer c = System.out::println; + frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), c)); + frame.requestFocusInWindow(); + + // Run the Simulation for (GameState gs = INITIAL_GAMESTATE; !isSimulationOver(gs); gs = nextGameState(gs)) displayGameState(getClientData(gs), xblast); } -- cgit v1.2.3