diff options
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/xblast/server/GameStateSerializer.java | 81 |
1 files changed, 52 insertions, 29 deletions
diff --git a/src/ch/epfl/xblast/server/GameStateSerializer.java b/src/ch/epfl/xblast/server/GameStateSerializer.java index 4a133e5..998ff21 100644 --- a/src/ch/epfl/xblast/server/GameStateSerializer.java +++ b/src/ch/epfl/xblast/server/GameStateSerializer.java | |||
@@ -14,9 +14,11 @@ import java.util.stream.Collectors; | |||
14 | public final class GameStateSerializer { | 14 | public final class GameStateSerializer { |
15 | 15 | ||
16 | /** | 16 | /** |
17 | * @param painter | 17 | * Serialize a Board. |
18 | * @param board | 18 | * |
19 | * @return | 19 | * @param painter the painter used to serialize de board's blocks |
20 | * @param board the board to be serialized | ||
21 | * @return a list of byte corresponding to the blocks' images | ||
20 | */ | 22 | */ |
21 | private static List<Byte> serializeBoard(BoardPainter painter, Board board) { | 23 | private static List<Byte> serializeBoard(BoardPainter painter, Board board) { |
22 | return RunLengthEncoder.encode(Cell.SPIRAL_ORDER.stream() | 24 | return RunLengthEncoder.encode(Cell.SPIRAL_ORDER.stream() |
@@ -24,6 +26,13 @@ public final class GameStateSerializer { | |||
24 | .collect(Collectors.toList())); | 26 | .collect(Collectors.toList())); |
25 | } | 27 | } |
26 | 28 | ||
29 | /** | ||
30 | * Set the image ID of the explosion on a cell given its neighbors blasts. | ||
31 | * | ||
32 | * @param blastedCells cells containing a blast | ||
33 | * @param cell given cell | ||
34 | * @return the block image id related to the given cell | ||
35 | */ | ||
27 | private static byte serializeBlast(Set<Cell> blastedCells, Cell cell) { | 36 | private static byte serializeBlast(Set<Cell> blastedCells, Cell cell) { |
28 | return ExplosionPainter.byteForBlast( | 37 | return ExplosionPainter.byteForBlast( |
29 | blastedCells.contains(cell.neighbor(Direction.N)), | 38 | blastedCells.contains(cell.neighbor(Direction.N)), |
@@ -32,6 +41,14 @@ public final class GameStateSerializer { | |||
32 | blastedCells.contains(cell.neighbor(Direction.W))); | 41 | blastedCells.contains(cell.neighbor(Direction.W))); |
33 | } | 42 | } |
34 | 43 | ||
44 | /** | ||
45 | * Serialize the explosion on a given cell. | ||
46 | * | ||
47 | * @param bombedCells cells containing a bomb | ||
48 | * @param blastedCells cells containing a blast | ||
49 | * @param cell cell hosting the explosion | ||
50 | * @return the serialized explosion | ||
51 | */ | ||
35 | private static byte serializeExplosion(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Cell cell) { | 52 | private static byte serializeExplosion(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Cell cell) { |
36 | if (bombedCells.containsKey(cell)) | 53 | if (bombedCells.containsKey(cell)) |
37 | return ExplosionPainter.byteForBomb(bombedCells.get(cell)); | 54 | return ExplosionPainter.byteForBomb(bombedCells.get(cell)); |
@@ -40,9 +57,11 @@ public final class GameStateSerializer { | |||
40 | } | 57 | } |
41 | 58 | ||
42 | /** | 59 | /** |
43 | * @param bombedCells | 60 | * Serialize the explosions (Blasts and Bombs). |
44 | * @param blastedCells | 61 | * |
45 | * @return | 62 | * @param bombedCells cells containing a bomb |
63 | * @param blastedCells cells containing a blast | ||
64 | * @return the serialized explosions | ||
46 | */ | 65 | */ |
47 | private static List<Byte> serializeExplosions(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells) { | 66 | private static List<Byte> serializeExplosions(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells) { |
48 | return RunLengthEncoder.encode(Cell.ROW_MAJOR_ORDER.stream() | 67 | return RunLengthEncoder.encode(Cell.ROW_MAJOR_ORDER.stream() |
@@ -51,39 +70,43 @@ public final class GameStateSerializer { | |||
51 | } | 70 | } |
52 | 71 | ||
53 | /** | 72 | /** |
54 | * Serialize a whole game state. | 73 | * Serialize a Player. |
55 | * | 74 | * |
56 | * @param boardPainter board painter related to the actual level | 75 | * @param player player to be serialized |
76 | * @param tick tick related to the GameState | ||
77 | * @return the serialized player | ||
78 | */ | ||
79 | private static List<Byte> serializePlayer(Player player, int tick) { | ||
80 | List<Byte> serializedPlayer = new ArrayList<>(); | ||
81 | |||
82 | serializedPlayer.add((byte) player.lives()); | ||
83 | serializedPlayer.add((byte) player.position().x()); | ||
84 | serializedPlayer.add((byte) player.position().y()); | ||
85 | serializedPlayer.add(PlayerPainter.byteForPlayer(player, tick)); | ||
86 | |||
87 | return serializedPlayer; | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Serialize a whole GameState. | ||
92 | * | ||
93 | * @param boardPainter board painter used to serialize the board | ||
57 | * @param gameState GameState to be serialized | 94 | * @param gameState GameState to be serialized |
58 | * @return the serialized game state | 95 | * @return the serialized GameState |
59 | */ | 96 | */ |
60 | public static List<Byte> serialize(BoardPainter boardPainter, GameState gameState) { | 97 | public static List<Byte> serialize(BoardPainter boardPainter, GameState gameState) { |
61 | List<Byte> output = new ArrayList<>(); | 98 | List<Byte> output = new ArrayList<>(); |
62 | List<Byte> data = new ArrayList<>(); | 99 | List<Byte> data = new ArrayList<>(); |
63 | 100 | ||
64 | // Board | 101 | data.addAll(serializeBoard(boardPainter, gameState.board())); // Serialize the Board |
65 | data.addAll(serializeBoard(boardPainter, gameState.board())); | 102 | data.addAll(serializeExplosions(gameState.bombedCells(), gameState.blastedCells())); // Serialize Explosions |
66 | |||
67 | // Blasts and Bombs | ||
68 | data.addAll(serializeExplosions(gameState.bombedCells(), gameState.blastedCells())); | ||
69 | |||
70 | // Players | ||
71 | for (Player player : gameState.players()) { | 103 | for (Player player : gameState.players()) { |
72 | List<Byte> serializedPlayer = new ArrayList<>(); | 104 | data.addAll(serializePlayer(player, gameState.ticks())); // Serialize each Player |
73 | serializedPlayer.add((byte) player.lives()); | ||
74 | serializedPlayer.add((byte) player.position().x()); | ||
75 | serializedPlayer.add((byte) player.position().y()); | ||
76 | serializedPlayer.add(PlayerPainter.byteForPlayer(player, gameState.ticks())); | ||
77 | |||
78 | data.addAll(serializedPlayer); | ||
79 | } | 105 | } |
80 | 106 | ||
81 | // Ticks | 107 | data.add((byte) (gameState.remainingTime() / 2)); // Add the state Tick to the data |
82 | data.add((byte) (gameState.remainingTime() / 2)); | 108 | output.add((byte) data.size()); // Get the size of the whole data chunk and add it as first element of the output |
83 | 109 | output.addAll(data); // Add all the data to the output | |
84 | // Build output | ||
85 | output.add((byte) data.size()); | ||
86 | output.addAll(data); | ||
87 | 110 | ||
88 | return output; | 111 | return output; |
89 | } | 112 | } |