diff options
Diffstat (limited to 'test/ch/epfl/xblast/simulation/GraphicalSimulation.java')
-rw-r--r-- | test/ch/epfl/xblast/simulation/GraphicalSimulation.java | 78 |
1 files changed, 39 insertions, 39 deletions
diff --git a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java index 7e4454d..8009d9f 100644 --- a/test/ch/epfl/xblast/simulation/GraphicalSimulation.java +++ b/test/ch/epfl/xblast/simulation/GraphicalSimulation.java | |||
@@ -2,33 +2,37 @@ package ch.epfl.xblast.simulation; | |||
2 | 2 | ||
3 | import ch.epfl.xblast.PlayerAction; | 3 | import ch.epfl.xblast.PlayerAction; |
4 | import ch.epfl.xblast.PlayerID; | 4 | import ch.epfl.xblast.PlayerID; |
5 | import ch.epfl.xblast.client.*; | 5 | import ch.epfl.xblast.client.GameStateDeserializer; |
6 | import ch.epfl.xblast.server.*; | 6 | import ch.epfl.xblast.client.KeyboardEventHandler; |
7 | import ch.epfl.xblast.client.XBlastComponent; | ||
7 | import ch.epfl.xblast.server.GameState; | 8 | import ch.epfl.xblast.server.GameState; |
8 | import ch.epfl.xblast.server.debug.RandomEventGenerator; | 9 | import ch.epfl.xblast.server.GameStateSerializer; |
10 | import ch.epfl.xblast.server.Level; | ||
9 | import ch.epfl.xblast.server.painter.BoardPainter; | 11 | import ch.epfl.xblast.server.painter.BoardPainter; |
10 | 12 | ||
11 | import javax.swing.*; | 13 | import javax.swing.*; |
12 | import java.awt.event.KeyEvent; | 14 | import java.awt.event.KeyEvent; |
15 | import java.util.Collections; | ||
13 | import java.util.HashMap; | 16 | import java.util.HashMap; |
14 | import java.util.List; | 17 | import java.util.List; |
15 | import java.util.Map; | 18 | import java.util.Map; |
16 | import java.util.function.Consumer; | 19 | import java.util.function.Consumer; |
17 | 20 | ||
18 | /** | 21 | /** |
22 | * Graphical game simulation. | ||
23 | * | ||
19 | * @author Timothée FLOURE (257420) | 24 | * @author Timothée FLOURE (257420) |
25 | * @author Pacien TRAN-GIRARD (261948) | ||
20 | */ | 26 | */ |
21 | public class GraphicalSimulation { | 27 | public class GraphicalSimulation extends Simulation { |
22 | private static final GameState INITIAL_GAMESTATE = Level.DEFAULT_LEVEL.initialState(); | ||
23 | private static final BoardPainter BOARD_PAINTER = Level.DEFAULT_LEVEL.painter(); | ||
24 | 28 | ||
25 | private static final int SEED = 2016; | 29 | private static final BoardPainter BOARD_PAINTER = Level.DEFAULT_LEVEL.painter(); |
26 | private static final int SPEED_CHANGE_PROB = 30; | 30 | private static final Consumer<PlayerAction> PLAYER_ACTION_CONSUMER = System.out::println; |
27 | private static final int BOMB_PROB = 100; | 31 | private static final PlayerID MAIN_PLAYER = PlayerID.PLAYER_1; |
28 | private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); | ||
29 | 32 | ||
30 | private static Map<Integer,PlayerAction> buildPlayerActionMap() { | 33 | private static Map<Integer, PlayerAction> buildPlayerActionMap() { |
31 | Map<Integer, PlayerAction> playerActionMap = new HashMap<>(); | 34 | Map<Integer, PlayerAction> playerActionMap = new HashMap<>(); |
35 | |||
32 | playerActionMap.put(KeyEvent.VK_UP, PlayerAction.MOVE_N); | 36 | playerActionMap.put(KeyEvent.VK_UP, PlayerAction.MOVE_N); |
33 | playerActionMap.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E); | 37 | playerActionMap.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E); |
34 | playerActionMap.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S); | 38 | playerActionMap.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S); |
@@ -36,49 +40,45 @@ public class GraphicalSimulation { | |||
36 | playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); | 40 | playerActionMap.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB); |
37 | playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); | 41 | playerActionMap.put(KeyEvent.VK_SHIFT, PlayerAction.STOP); |
38 | 42 | ||
39 | return playerActionMap; | 43 | return Collections.unmodifiableMap(playerActionMap); |
40 | } | 44 | } |
41 | 45 | ||
42 | private static ch.epfl.xblast.client.GameState getClientData(GameState gs) { | 46 | private static ch.epfl.xblast.client.GameState translateToClientData(GameState gs) { |
43 | List<Byte> serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); | 47 | List<Byte> serializedGameState = GameStateSerializer.serialize(BOARD_PAINTER, gs); |
44 | return GameStateDeserializer.deserialize(serializedGameState); | 48 | return GameStateDeserializer.deserialize(serializedGameState); |
45 | } | 49 | } |
46 | 50 | ||
47 | private static boolean isSimulationOver(GameState gs) { | 51 | private static JFrame buildFrame(XBlastComponent gui) { |
48 | return gs == null || gs.isGameOver(); | ||
49 | } | ||
50 | |||
51 | private static GameState nextGameState(GameState gs) { | ||
52 | return gs.next(RANDOM_EVENT_GENERATOR.randomSpeedChangeEvents(), RANDOM_EVENT_GENERATOR.randomBombDropEvents()); | ||
53 | } | ||
54 | |||
55 | private static void displayGameState(ch.epfl.xblast.client.GameState gs, XBlastComponent gui) { | ||
56 | gui.setGameState(gs, PlayerID.PLAYER_1); | ||
57 | } | ||
58 | |||
59 | private static JFrame buildFrame() { | ||
60 | JFrame frame = new JFrame(); | 52 | JFrame frame = new JFrame(); |
61 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 53 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
62 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 54 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
63 | frame.setVisible(true); | 55 | frame.setVisible(true); |
56 | |||
57 | frame.setContentPane(gui); | ||
58 | frame.pack(); | ||
64 | return frame; | 59 | return frame; |
65 | } | 60 | } |
66 | 61 | ||
67 | public static void main(String[] args) { | 62 | private void attachKeyboardHandler(JFrame frame) { |
63 | frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), PLAYER_ACTION_CONSUMER)); | ||
64 | frame.requestFocusInWindow(); | ||
65 | } | ||
68 | 66 | ||
69 | // Build the window | 67 | private final XBlastComponent gui; |
70 | JFrame frame = buildFrame(); | ||
71 | XBlastComponent xblast = new XBlastComponent(); | ||
72 | frame.setContentPane(xblast); | ||
73 | frame.pack(); | ||
74 | 68 | ||
75 | // Attach a KeyboardEventHandler | 69 | private GraphicalSimulation() { |
76 | Consumer<PlayerAction> c = System.out::println; | 70 | this.gui = new XBlastComponent(); |
77 | frame.addKeyListener(new KeyboardEventHandler(buildPlayerActionMap(), c)); | 71 | attachKeyboardHandler(buildFrame(this.gui)); |
78 | frame.requestFocusInWindow(); | 72 | } |
79 | 73 | ||
80 | // Run the Simulation | 74 | @Override |
81 | for (GameState gs = INITIAL_GAMESTATE; !isSimulationOver(gs); gs = nextGameState(gs)) | 75 | void displayGameState(GameState gs) { |
82 | displayGameState(getClientData(gs), xblast); | 76 | this.gui.setGameState(translateToClientData(gs), MAIN_PLAYER); |
77 | delay(); | ||
83 | } | 78 | } |
79 | |||
80 | public static void main(String[] args) { | ||
81 | (new GraphicalSimulation()).runSimulation(); | ||
82 | } | ||
83 | |||
84 | } | 84 | } |