diff options
author | Pacien TRAN-GIRARD | 2016-05-08 20:26:28 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-05-08 20:26:28 +0200 |
commit | f19b688112598273ab5e1147c185c4ab2a3fe749 (patch) | |
tree | 4890e5c880e8861c3e979c1eee98159f72637a4d /src | |
parent | 085610b35b7e85cfbec43a1ceb7c38a4b15fe3c2 (diff) | |
download | xblast-f19b688112598273ab5e1147c185c4ab2a3fe749.tar.gz |
Refactor deserialization
Diffstat (limited to 'src')
-rw-r--r-- | src/ch/epfl/xblast/client/GameState.java | 70 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/GameStateDeserializer.java | 283 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/ImageCollection.java | 154 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/painter/ScorePainter.java | 107 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/painter/TimeLinePainter.java | 39 |
5 files changed, 440 insertions, 213 deletions
diff --git a/src/ch/epfl/xblast/client/GameState.java b/src/ch/epfl/xblast/client/GameState.java index 4906094..7652cd5 100644 --- a/src/ch/epfl/xblast/client/GameState.java +++ b/src/ch/epfl/xblast/client/GameState.java | |||
@@ -1,31 +1,29 @@ | |||
1 | package ch.epfl.xblast.client; | 1 | package ch.epfl.xblast.client; |
2 | 2 | ||
3 | import ch.epfl.xblast.ArgumentChecker; | ||
4 | import ch.epfl.xblast.Lists; | ||
3 | import ch.epfl.xblast.PlayerID; | 5 | import ch.epfl.xblast.PlayerID; |
4 | import ch.epfl.xblast.SubCell; | 6 | import ch.epfl.xblast.SubCell; |
7 | import ch.epfl.xblast.client.painter.ScorePainter; | ||
8 | import ch.epfl.xblast.client.painter.TimeLinePainter; | ||
5 | 9 | ||
6 | import java.awt.*; | 10 | import java.awt.*; |
7 | import java.util.List; | 11 | import java.util.List; |
12 | import java.util.Objects; | ||
8 | 13 | ||
9 | /** | 14 | /** |
15 | * The client representation of a game state. | ||
16 | * | ||
10 | * @author Timothée FLOURE (257420) | 17 | * @author Timothée FLOURE (257420) |
18 | * @author Pacien TRAN-GIRARD (261948) | ||
11 | */ | 19 | */ |
12 | public final class GameState { | 20 | public final class GameState { |
13 | /** | ||
14 | * GameState's parameters. | ||
15 | */ | ||
16 | private final List<Player> players; | ||
17 | private final List<Image> board; | ||
18 | private final List<Image> explosions; | ||
19 | private final List<Image> scores; | ||
20 | private final List<Image> ticks; | ||
21 | 21 | ||
22 | /** | 22 | /** |
23 | * A Player. | 23 | * A Player. |
24 | */ | 24 | */ |
25 | public static final class Player { | 25 | public static final class Player { |
26 | /** | 26 | |
27 | * Player's parameters. | ||
28 | */ | ||
29 | private final PlayerID id; | 27 | private final PlayerID id; |
30 | private final int lives; | 28 | private final int lives; |
31 | private final SubCell position; | 29 | private final SubCell position; |
@@ -34,16 +32,16 @@ public final class GameState { | |||
34 | /** | 32 | /** |
35 | * Instantiates a new Player. | 33 | * Instantiates a new Player. |
36 | * | 34 | * |
37 | * @param id id of the player | 35 | * @param id id of the player |
38 | * @param lives number of lives of the player | 36 | * @param lives number of lives of the player |
39 | * @param position position of the player | 37 | * @param position position of the player |
40 | * @param image image related to the actual state of the player | 38 | * @param image image related to the actual state of the player |
41 | */ | 39 | */ |
42 | public Player(PlayerID id, int lives, SubCell position, Image image) { | 40 | public Player(PlayerID id, int lives, SubCell position, Image image) { |
43 | this.id = id; | 41 | this.id = id; |
44 | this.lives = lives; | 42 | this.lives = lives; |
45 | this.position = position; | 43 | this.position = Objects.requireNonNull(position); |
46 | this.image = image; | 44 | this.image = Objects.requireNonNull(image); |
47 | } | 45 | } |
48 | 46 | ||
49 | /** | 47 | /** |
@@ -73,23 +71,42 @@ public final class GameState { | |||
73 | public Image image() { | 71 | public Image image() { |
74 | return this.image; | 72 | return this.image; |
75 | } | 73 | } |
74 | |||
75 | } | ||
76 | |||
77 | private final List<Player> players; | ||
78 | private final List<Image> board; | ||
79 | private final List<Image> explosions; | ||
80 | private final List<Image> scores; | ||
81 | private final List<Image> ticks; | ||
82 | |||
83 | /** | ||
84 | * Instantiates a new GameState. | ||
85 | * | ||
86 | * @param players list containing the players | ||
87 | * @param board list containing all the images composing the board | ||
88 | * @param explosions list containing all the images composing the blasts and the bombs | ||
89 | * @param scores list containing all the images composing the actual score | ||
90 | * @param ticks list containing all the images composing the time "line" | ||
91 | */ | ||
92 | public GameState(List<Player> players, List<Image> board, List<Image> explosions, List<Image> scores, List<Image> ticks) { | ||
93 | this.players = Lists.immutableList(ArgumentChecker.requireNonEmpty(players)); | ||
94 | this.board = Lists.immutableList(ArgumentChecker.requireNonEmpty(board)); | ||
95 | this.explosions = Lists.immutableList(Objects.requireNonNull(explosions)); | ||
96 | this.scores = Lists.immutableList(ArgumentChecker.requireNonEmpty(scores)); | ||
97 | this.ticks = Lists.immutableList(ArgumentChecker.requireNonEmpty(ticks)); | ||
76 | } | 98 | } |
77 | 99 | ||
78 | /** | 100 | /** |
79 | * Instantiates a new GameState. | 101 | * Instantiates a new GameState. |
80 | * | 102 | * |
81 | * @param players list containing the players | 103 | * @param players list containing the players |
82 | * @param board list containing all the images composing the board | 104 | * @param board list containing all the images composing the board |
83 | * @param explosions list containing all the images composing the blasts and the bombs | 105 | * @param explosions list containing all the images composing the blasts and the bombs |
84 | * @param scores list containing all the images composing the actual score | 106 | * @param ticks the ticks |
85 | * @param ticks list containing all the images composing the time "line" | ||
86 | */ | 107 | */ |
87 | public GameState(List<Player> players, List<Image> board, List<Image> explosions, List<Image> scores, List<Image> ticks) { | 108 | public GameState(List<Player> players, List<Image> board, List<Image> explosions, int ticks) { |
88 | this.players = players; | 109 | this(players, board, explosions, ScorePainter.buildScorePanel(players), TimeLinePainter.buildTimeLine(ticks)); |
89 | this.board = board; | ||
90 | this.explosions = explosions; | ||
91 | this.scores = scores; | ||
92 | this.ticks = ticks; | ||
93 | } | 110 | } |
94 | 111 | ||
95 | /** | 112 | /** |
@@ -126,4 +143,5 @@ public final class GameState { | |||
126 | public List<Image> ticks() { | 143 | public List<Image> ticks() { |
127 | return this.ticks; | 144 | return this.ticks; |
128 | } | 145 | } |
146 | |||
129 | } | 147 | } |
diff --git a/src/ch/epfl/xblast/client/GameStateDeserializer.java b/src/ch/epfl/xblast/client/GameStateDeserializer.java index f1327e3..20b44f9 100644 --- a/src/ch/epfl/xblast/client/GameStateDeserializer.java +++ b/src/ch/epfl/xblast/client/GameStateDeserializer.java | |||
@@ -1,164 +1,170 @@ | |||
1 | package ch.epfl.xblast.client; | 1 | package ch.epfl.xblast.client; |
2 | 2 | ||
3 | import ch.epfl.xblast.Lists; | ||
3 | import ch.epfl.xblast.PlayerID; | 4 | import ch.epfl.xblast.PlayerID; |
4 | import ch.epfl.xblast.RunLengthEncoder; | 5 | import ch.epfl.xblast.RunLengthEncoder; |
5 | import ch.epfl.xblast.SubCell; | 6 | import ch.epfl.xblast.SubCell; |
6 | import ch.epfl.xblast.client.GameState.Player; | 7 | import ch.epfl.xblast.client.GameState.Player; |
7 | import ch.epfl.xblast.server.Ticks; | ||
8 | 8 | ||
9 | import java.util.ArrayList; | 9 | import java.awt.*; |
10 | import java.util.*; | ||
10 | import java.util.List; | 11 | import java.util.List; |
11 | import java.awt.Image; | ||
12 | import java.util.stream.Collectors; | 12 | import java.util.stream.Collectors; |
13 | 13 | ||
14 | /** | 14 | /** |
15 | * The client game state deserializer. | ||
16 | * | ||
17 | * @author Pacien TRAN-GIRARD (261948) | ||
15 | * @author Timothée FLOURE (257420) | 18 | * @author Timothée FLOURE (257420) |
16 | */ | 19 | */ |
17 | public final class GameStateDeserializer { | 20 | public final class GameStateDeserializer { |
18 | /** | ||
19 | * Image collections folders | ||
20 | */ | ||
21 | private final static String BOARD_IMAGES_FOLDER = "block"; | ||
22 | private final static String EXPLOSION_IMAGES_FOLDER = "explosion"; | ||
23 | private final static String PLAYER_IMAGES_FOLDER = "player"; | ||
24 | private final static String TIME_IMAGES_FOLDER = "score"; | ||
25 | private final static String SCORE_IMAGES_FOLDER = "score"; | ||
26 | 21 | ||
27 | /** | 22 | private static final int SERIALIZED_PLAYER_LENGTH = 4; |
28 | * Serialized data chunks | 23 | private static final int SERIALIZED_PLAYERS_LENGTH = PlayerID.values().length * SERIALIZED_PLAYER_LENGTH; |
29 | */ | 24 | private static final int SERIALIZED_TICKS_LENGTH = 1; |
30 | private final static int NUMBER_OF_PLAYERS = 4; | ||
31 | private final static int PLAYER_CHUNK_SIZE = 4; | ||
32 | private final static int PLAYER_TOTAL_SIZE = NUMBER_OF_PLAYERS * PLAYER_CHUNK_SIZE; | ||
33 | 25 | ||
34 | /** | 26 | /** |
35 | * Scores & Time | 27 | * Represents a length-prefixed chunk of byte-encoded data. |
36 | */ | 28 | */ |
37 | private final static int TIME_LINE_SIZE = (Ticks.GAME_DURATION / 2); | 29 | private static class VariableLengthChunk { |
38 | private final static int SCORE_SEPARATION_SIZE = 8; | ||
39 | 30 | ||
40 | /** | 31 | private static final int PREFIX_SHIFT = 1; |
41 | * Images ID | ||
42 | */ | ||
43 | private final static int TIME_LINE_LED_ON_IMAGE_ID = 21; | ||
44 | private final static int TIME_LINE_LED_OFF_IMAGE_ID = 20; | ||
45 | private final static int SCORE_TEXT_MIDDLE_IMAGE_ID = 10; | ||
46 | private final static int SCORE_TEXT_RIGHT_IMAGE_ID = 11; | ||
47 | private final static int SCORE_TILE_VOID_IMAGE_ID = 12; | ||
48 | 32 | ||
49 | /** | 33 | private final List<Byte> bytes; |
50 | * Deserialize an encoded chunk of data and link it to images. | ||
51 | * | ||
52 | * @param data serialized data | ||