diff options
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/xblast/client/GameState.java | 13 | ||||
-rw-r--r-- | src/ch/epfl/xblast/client/XBlastComponent.java | 194 |
2 files changed, 114 insertions, 93 deletions
diff --git a/src/ch/epfl/xblast/client/GameState.java b/src/ch/epfl/xblast/client/GameState.java index 2ac27e3..480c6a1 100644 --- a/src/ch/epfl/xblast/client/GameState.java +++ b/src/ch/epfl/xblast/client/GameState.java | |||
@@ -8,8 +8,11 @@ import ch.epfl.xblast.client.painter.ScorePainter; | |||
8 | import ch.epfl.xblast.client.painter.TimeLinePainter; | 8 | import ch.epfl.xblast.client.painter.TimeLinePainter; |
9 | 9 | ||
10 | import java.awt.*; | 10 | import java.awt.*; |
11 | import java.util.Collections; | ||
11 | import java.util.List; | 12 | import java.util.List; |
13 | import java.util.Map; | ||
12 | import java.util.Objects; | 14 | import java.util.Objects; |
15 | import java.util.stream.Collectors; | ||
13 | 16 | ||
14 | /** | 17 | /** |
15 | * The client representation of a game state. | 18 | * The client representation of a game state. |
@@ -131,7 +134,7 @@ public final class GameState { | |||
131 | } | 134 | } |
132 | 135 | ||
133 | /** | 136 | /** |
134 | * @return list containing all the images composing the actual score | 137 | * @return list containing all the images composing the score |
135 | */ | 138 | */ |
136 | public List<Image> scores() { | 139 | public List<Image> scores() { |
137 | return this.scores; | 140 | return this.scores; |
@@ -144,4 +147,12 @@ public final class GameState { | |||
144 | return this.ticks; | 147 | return this.ticks; |
145 | } | 148 | } |
146 | 149 | ||
150 | /** | ||
151 | * @return map of players' scores | ||
152 | */ | ||
153 | Map<PlayerID, Integer> playerScores() { | ||
154 | return Collections.unmodifiableMap(this.players.stream() | ||
155 | .collect(Collectors.toMap(Player::id, Player::lives))); | ||
156 | } | ||
157 | |||
147 | } | 158 | } |
diff --git a/src/ch/epfl/xblast/client/XBlastComponent.java b/src/ch/epfl/xblast/client/XBlastComponent.java index 0b4f0bf..388a208 100644 --- a/src/ch/epfl/xblast/client/XBlastComponent.java +++ b/src/ch/epfl/xblast/client/XBlastComponent.java | |||
@@ -20,52 +20,98 @@ import java.util.stream.IntStream; | |||
20 | */ | 20 | */ |
21 | public final class XBlastComponent extends JComponent { | 21 | public final class XBlastComponent extends JComponent { |
22 | 22 | ||
23 | private static final ImageObserver IMG_OBSERVER = null; | 23 | private static class Grid { |
24 | 24 | ||
25 | private static final Dimension CELL_DIMENSION = new Dimension(64, 48); | 25 | private static final Dimension CELL_DIMENSION = new Dimension(64, 48); |
26 | private static final Dimension SCORE_IMG_DIMENSION = new Dimension(48, 48); | 26 | private static final Dimension SCORE_IMG_DIMENSION = new Dimension(48, 48); |
27 | private static final Dimension TIME_LED_DIMENSION = new Dimension(16, 16); | 27 | private static final Dimension TIME_LED_DIMENSION = new Dimension(16, 16); |
28 | private static final Dimension PREFERRED_WINDOW_DIMENSION = frameDimension(); | 28 | private static final Dimension FRAME_DIMENSION = totalDimension(); |
29 | 29 | ||
30 | private static final List<Point> CELL_POSITIONS = buildCellGrid(CELL_DIMENSION); | 30 | private static final List<Point> CELL_POSITIONS = buildCellGrid(CELL_DIMENSION); |
31 | private static final List<Point> SCORE_TXT_POSITIONS = buildScorePositionList(659); | 31 | private static final List<Point> SCORE_BG_POSITIONS = buildScoreGrid(); |
32 | private static final List<Point> SCORE_BG_POSITIONS = | 32 | private static final List<Point> TIME_LINE_POSITIONS = buildTimeLineGrid(); |
33 | buildLine(SCORE_IMG_DIMENSION, 20, new Point(0, CELL_DIMENSION.height * Cell.ROWS)); | 33 | private static final Map<PlayerID, Point> SCORE_TXT_POSITIONS = buildScoreMap(659); |
34 | private static final List<Point> TIME_LINE_POSITIONS = | 34 | |
35 | buildLine(TIME_LED_DIMENSION, 60, new Point(0, CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height)); | 35 | private static Dimension totalDimension() { |
36 | 36 | return new Dimension( | |
37 | private static final Font TXT_FONT = new Font("Arial", Font.BOLD, 25); | 37 | CELL_DIMENSION.width * Cell.COLUMNS, |
38 | private static final Color TXT_COLOR = Color.WHITE; | 38 | CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height + TIME_LED_DIMENSION.height); |
39 | 39 | } | |
40 | private static Dimension frameDimension() { | 40 | |
41 | return new Dimension( | 41 | private static List<Point> buildCellGrid(Dimension elementDim) { |
42 | CELL_DIMENSION.width * Cell.COLUMNS, | 42 | return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream() |
43 | CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height + TIME_LED_DIMENSION.height); | 43 | .map(c -> new Point(c.x() * elementDim.width, c.y() * elementDim.height)) |
44 | } | 44 | .collect(Collectors.toList())); |
45 | } | ||
46 | |||
47 | private static List<Point> buildLine(Dimension elementDim, int len, Point shift) { | ||
48 | return Collections.unmodifiableList(IntStream.range(0, len) | ||
49 | .mapToObj(x -> new Point(x * elementDim.width + shift.x, shift.y)) | ||
50 | .collect(Collectors.toList())); | ||
51 | } | ||
52 | |||
53 | private static List<Point> buildScoreGrid() { | ||
54 | return buildLine( | ||
55 | SCORE_IMG_DIMENSION, | ||
56 | FRAME_DIMENSION.width / SCORE_IMG_DIMENSION.width, | ||
57 | new Point(0, CELL_DIMENSION.height * Cell.ROWS)); | ||
58 | } | ||
59 | |||
60 | private static List<Point> buildTimeLineGrid() { | ||
61 | return buildLine( | ||
62 | TIME_LED_DIMENSION, | ||
63 | FRAME_DIMENSION.width / TIME_LED_DIMENSION.width, | ||
64 | new Point(0, CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height)); | ||
65 | } | ||
66 | |||
67 | private static Map<PlayerID, Point> buildScoreMap(int vShift) { | ||
68 | Map<PlayerID, Point> m = new EnumMap<>(PlayerID.class); | ||
69 | m.put(PlayerID.PLAYER_1, new Point(96, vShift)); | ||
70 | m.put(PlayerID.PLAYER_2, new Point(240, vShift)); | ||
71 | m.put(PlayerID.PLAYER_3, new Point(768, vShift)); | ||
72 | m.put(PlayerID.PLAYER_4, new Point(912, vShift)); | ||
73 | return m; | ||
74 | } | ||
75 | |||
76 | private static Point positionForPlayer(GameState.Player p) { | ||
77 | return new Point( | ||
78 | 4 * p.position().x() - 24, | ||
79 | 3 * p.position().y() - 52); | ||
80 | } | ||
45 | 81 | ||
46 | private static List<Point> buildCellGrid(Dimension elementDim) { | ||
47 | return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream() | ||
48 | .map(c -> new Point(c.x() * elementDim.width, c.y() * elementDim.height)) | ||
49 | .collect(Collectors.toList())); | ||
50 | } | 82 | } |
51 | 83 | ||
52 | private static List<Point> buildLine(Dimension elementDim, int len, Point shift) { | 84 | private static class Painter { |
53 | return Collections.unmodifiableList(IntStream.range(0, len) | 85 | |
54 | .mapToObj(x -> new Point(x * elementDim.width + shift.x, shift.y)) | 86 | private static final ImageObserver IMG_OBSERVER = null; |
55 | .collect(Collectors.toList())); | 87 | private static final Font TXT_FONT = new Font("Arial", Font.BOLD, 25); |
56 | } | 88 | private static final Color TXT_COLOR = Color.WHITE; |
89 | |||
90 | private static void drawString(Graphics2D g, Point pos, String str) { | ||
91 | g.setColor(TXT_COLOR); | ||
92 | g.setFont(TXT_FONT); | ||
93 | g.drawString(str, pos.x, pos.y); | ||
94 | } | ||
95 | |||
96 | private static void drawImage(Graphics2D g, Point pos, Image img) { | ||
97 | g.drawImage(img, pos.x, pos.y, IMG_OBSERVER); | ||
98 | } | ||
99 | |||
100 | private static void drawImageMap(Graphics2D g, Map<Point, Image> m) { | ||
101 | for (Map.Entry<Point, Image> e : m.entrySet()) | ||
102 | drawImage(g, e.getKey(), e.getValue()); | ||
103 | } | ||
104 | |||
105 | private static <T> void drawStringMap(Graphics2D g, Map<Point, T> m) { | ||
106 | for (Map.Entry<Point, T> e : m.entrySet()) | ||
107 | drawString(g, e.getKey(), e.getValue().toString()); | ||
108 | } | ||
57 | 109 | ||
58 | private static List<Point> buildScorePositionList(int vShift) { | ||
59 | return Arrays.asList( | ||
60 | new Point(96, vShift), | ||
61 | new Point(240, vShift), | ||
62 | new Point(768, vShift), | ||
63 | new Point(912, vShift)); | ||
64 | } | 110 | } |
65 | 111 | ||
66 | private static Comparator<GameState.Player> buildPlayerComparator(PlayerID blackSheep) { | 112 | private static Comparator<GameState.Player> buildPlayerComparator(PlayerID blackSheep) { |
67 | Comparator<GameState.Player> coordinateComparator = | 113 | Comparator<GameState.Player> coordinateComparator = |
68 | (p1, p2) -> p2.position().y() - p1.position().y(); | 114 | (p1, p2) -> p1.position().compareTo(p2.position()); |
69 | 115 | ||
70 | Comparator<GameState.Player> idComparator = | 116 | Comparator<GameState.Player> idComparator = |
71 | (p1, p2) -> p1.id() == blackSheep ? -1 : 0; | 117 | (p1, p2) -> p1.id() == blackSheep ? -1 : 0; |
@@ -73,55 +119,10 @@ public final class XBlastComponent extends JComponent { | |||
73 | return coordinateComparator.thenComparing(idComparator); | 119 | return coordinateComparator.thenComparing(idComparator); |
74 | } | 120 | } |
75 | 121 | ||
76 | private static void drawImage(Graphics2D g, Image img, Point pos) { | ||
77 | g.drawImage(img, pos.x, pos.y, IMG_OBSERVER); | ||
78 | } | ||
79 | |||
80 | private static void drawGrid(Graphics2D g, List<Point> grid, List<Image> imgs) { | ||
81 | for (int i = 0; i < grid.size() && i < imgs.size(); ++i) | ||
82 | drawImage(g, imgs.get(i), grid.get(i)); | ||
83 | } | ||
84 | |||
85 | private static Point playerPosition(GameState.Player p) { | ||
86 | return new Point( | ||
87 | 4 * p.position().x() - 24, | ||
88 | 3 * p.position().y() - 52); | ||
89 | } | ||
90 | |||
91 | private GameState gameState; | 122 | private GameState gameState; |
92 | private PlayerID playerID; | 123 | private PlayerID playerID; |
93 | 124 | ||
94 | /** | 125 | /** |
95 | * Draw the players on the graphic context. | ||
96 | * | ||
97 | * @param g the graphic context | ||
98 | * @param players the list of players to be displayed | ||
99 | */ | ||
100 | private void drawPlayers(Graphics2D g, List<GameState.Player> players) { | ||
101 | for (GameState.Player p : Lists.sorted(players, buildPlayerComparator(this.playerID))) | ||
102 | drawImage(g, p.image(), playerPosition(p)); | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * Writes the remaining lives of each playerID on the graphic context. | ||
107 | * | ||
108 | * @param g the graphic context | ||
109 | * @param players list of players | ||
110 | */ | ||
111 | private void writeScores(Graphics2D g, List<GameState.Player> players) { |