From 53bd5fc4c164eb45d5e0aa34dc103579c9619a32 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 10 May 2016 17:12:01 +0200 Subject: Refactor XBlastComponent --- src/ch/epfl/xblast/client/GameState.java | 13 +- src/ch/epfl/xblast/client/XBlastComponent.java | 194 +++++++++++++------------ 2 files changed, 114 insertions(+), 93 deletions(-) (limited to 'src/ch') 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; import ch.epfl.xblast.client.painter.TimeLinePainter; import java.awt.*; +import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; /** * The client representation of a game state. @@ -131,7 +134,7 @@ public final class GameState { } /** - * @return list containing all the images composing the actual score + * @return list containing all the images composing the score */ public List scores() { return this.scores; @@ -144,4 +147,12 @@ public final class GameState { return this.ticks; } + /** + * @return map of players' scores + */ + Map playerScores() { + return Collections.unmodifiableMap(this.players.stream() + .collect(Collectors.toMap(Player::id, Player::lives))); + } + } 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; */ public final class XBlastComponent extends JComponent { - private static final ImageObserver IMG_OBSERVER = null; - - private static final Dimension CELL_DIMENSION = new Dimension(64, 48); - private static final Dimension SCORE_IMG_DIMENSION = new Dimension(48, 48); - private static final Dimension TIME_LED_DIMENSION = new Dimension(16, 16); - private static final Dimension PREFERRED_WINDOW_DIMENSION = frameDimension(); - - private static final List CELL_POSITIONS = buildCellGrid(CELL_DIMENSION); - private static final List SCORE_TXT_POSITIONS = buildScorePositionList(659); - private static final List SCORE_BG_POSITIONS = - buildLine(SCORE_IMG_DIMENSION, 20, new Point(0, CELL_DIMENSION.height * Cell.ROWS)); - private static final List TIME_LINE_POSITIONS = - buildLine(TIME_LED_DIMENSION, 60, new Point(0, CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height)); - - private static final Font TXT_FONT = new Font("Arial", Font.BOLD, 25); - private static final Color TXT_COLOR = Color.WHITE; - - private static Dimension frameDimension() { - return new Dimension( - CELL_DIMENSION.width * Cell.COLUMNS, - CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height + TIME_LED_DIMENSION.height); - } + private static class Grid { + + private static final Dimension CELL_DIMENSION = new Dimension(64, 48); + private static final Dimension SCORE_IMG_DIMENSION = new Dimension(48, 48); + private static final Dimension TIME_LED_DIMENSION = new Dimension(16, 16); + private static final Dimension FRAME_DIMENSION = totalDimension(); + + private static final List CELL_POSITIONS = buildCellGrid(CELL_DIMENSION); + private static final List SCORE_BG_POSITIONS = buildScoreGrid(); + private static final List TIME_LINE_POSITIONS = buildTimeLineGrid(); + private static final Map SCORE_TXT_POSITIONS = buildScoreMap(659); + + private static Dimension totalDimension() { + return new Dimension( + CELL_DIMENSION.width * Cell.COLUMNS, + CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height + TIME_LED_DIMENSION.height); + } + + private static List buildCellGrid(Dimension elementDim) { + return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream() + .map(c -> new Point(c.x() * elementDim.width, c.y() * elementDim.height)) + .collect(Collectors.toList())); + } + + private static List buildLine(Dimension elementDim, int len, Point shift) { + return Collections.unmodifiableList(IntStream.range(0, len) + .mapToObj(x -> new Point(x * elementDim.width + shift.x, shift.y)) + .collect(Collectors.toList())); + } + + private static List buildScoreGrid() { + return buildLine( + SCORE_IMG_DIMENSION, + FRAME_DIMENSION.width / SCORE_IMG_DIMENSION.width, + new Point(0, CELL_DIMENSION.height * Cell.ROWS)); + } + + private static List buildTimeLineGrid() { + return buildLine( + TIME_LED_DIMENSION, + FRAME_DIMENSION.width / TIME_LED_DIMENSION.width, + new Point(0, CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height)); + } + + private static Map buildScoreMap(int vShift) { + Map m = new EnumMap<>(PlayerID.class); + m.put(PlayerID.PLAYER_1, new Point(96, vShift)); + m.put(PlayerID.PLAYER_2, new Point(240, vShift)); + m.put(PlayerID.PLAYER_3, new Point(768, vShift)); + m.put(PlayerID.PLAYER_4, new Point(912, vShift)); + return m; + } + + private static Point positionForPlayer(GameState.Player p) { + return new Point( + 4 * p.position().x() - 24, + 3 * p.position().y() - 52); + } - private static List buildCellGrid(Dimension elementDim) { - return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream() - .map(c -> new Point(c.x() * elementDim.width, c.y() * elementDim.height)) - .collect(Collectors.toList())); } - private static List buildLine(Dimension elementDim, int len, Point shift) { - return Collections.unmodifiableList(IntStream.range(0, len) - .mapToObj(x -> new Point(x * elementDim.width + shift.x, shift.y)) - .collect(Collectors.toList())); - } + private static class Painter { + + private static final ImageObserver IMG_OBSERVER = null; + private static final Font TXT_FONT = new Font("Arial", Font.BOLD, 25); + private static final Color TXT_COLOR = Color.WHITE; + + private static void drawString(Graphics2D g, Point pos, String str) { + g.setColor(TXT_COLOR); + g.setFont(TXT_FONT); + g.drawString(str, pos.x, pos.y); + } + + private static void drawImage(Graphics2D g, Point pos, Image img) { + g.drawImage(img, pos.x, pos.y, IMG_OBSERVER); + } + + private static void drawImageMap(Graphics2D g, Map m) { + for (Map.Entry e : m.entrySet()) + drawImage(g, e.getKey(), e.getValue()); + } + + private static void drawStringMap(Graphics2D g, Map m) { + for (Map.Entry e : m.entrySet()) + drawString(g, e.getKey(), e.getValue().toString()); + } - private static List buildScorePositionList(int vShift) { - return Arrays.asList( - new Point(96, vShift), - new Point(240, vShift), - new Point(768, vShift), - new Point(912, vShift)); } private static Comparator buildPlayerComparator(PlayerID blackSheep) { Comparator coordinateComparator = - (p1, p2) -> p2.position().y() - p1.position().y(); + (p1, p2) -> p1.position().compareTo(p2.position()); Comparator idComparator = (p1, p2) -> p1.id() == blackSheep ? -1 : 0; @@ -73,54 +119,9 @@ public final class XBlastComponent extends JComponent { return coordinateComparator.thenComparing(idComparator); } - private static void drawImage(Graphics2D g, Image img, Point pos) { - g.drawImage(img, pos.x, pos.y, IMG_OBSERVER); - } - - private static void drawGrid(Graphics2D g, List grid, List imgs) { - for (int i = 0; i < grid.size() && i < imgs.size(); ++i) - drawImage(g, imgs.get(i), grid.get(i)); - } - - private static Point playerPosition(GameState.Player p) { - return new Point( - 4 * p.position().x() - 24, - 3 * p.position().y() - 52); - } - private GameState gameState; private PlayerID playerID; - /** - * Draw the players on the graphic context. - * - * @param g the graphic context - * @param players the list of players to be displayed - */ - private void drawPlayers(Graphics2D g, List players) { - for (GameState.Player p : Lists.sorted(players, buildPlayerComparator(this.playerID))) - drawImage(g, p.image(), playerPosition(p)); - } - - /** - * Writes the remaining lives of each playerID on the graphic context. - * - * @param g the graphic context - * @param players list of players - */ - private void writeScores(Graphics2D g, List players) { - g.setColor(TXT_COLOR); - g.setFont(TXT_FONT); - - players.stream().forEach(p -> writeScore(g, p)); - } - - private void writeScore(Graphics2D g, GameState.Player p) { - String score = Integer.toString(p.lives()); - Point pos = SCORE_TXT_POSITIONS.get(p.id().ordinal()); - g.drawString(score, pos.x, pos.y); - } - /** * Display the given GameState from the point of view of the given playerID. * @@ -130,8 +131,7 @@ public final class XBlastComponent extends JComponent { public void setGameState(GameState gs, PlayerID pid) { this.gameState = gs; this.playerID = pid; - - repaint(); + this.repaint(); } /** @@ -141,7 +141,7 @@ public final class XBlastComponent extends JComponent { */ @Override public Dimension getPreferredSize() { - return PREFERRED_WINDOW_DIMENSION; + return Grid.FRAME_DIMENSION; } /** @@ -156,13 +156,23 @@ public final class XBlastComponent extends JComponent { if (Objects.isNull(this.gameState)) return; - drawGrid(g, CELL_POSITIONS, this.gameState.board()); - drawGrid(g, CELL_POSITIONS, this.gameState.explosions()); - drawGrid(g, SCORE_BG_POSITIONS, this.gameState.scores()); - drawGrid(g, TIME_LINE_POSITIONS, this.gameState.ticks()); + Painter.drawImageMap(g, Lists.linearMap(Grid.CELL_POSITIONS, this.gameState.board())); + Painter.drawImageMap(g, Lists.linearMap(Grid.CELL_POSITIONS, this.gameState.explosions())); + Painter.drawImageMap(g, Lists.linearMap(Grid.SCORE_BG_POSITIONS, this.gameState.scores())); + Painter.drawImageMap(g, Lists.linearMap(Grid.TIME_LINE_POSITIONS, this.gameState.ticks())); + Painter.drawStringMap(g, Lists.traverseMaps(Lists.invertMap(Grid.SCORE_TXT_POSITIONS), this.gameState.playerScores())); + drawPlayers(g, Lists.sorted(this.gameState.players(), buildPlayerComparator(this.playerID))); + } - drawPlayers(g, this.gameState.players()); - writeScores(g, this.gameState.players()); + /** + * Draw the players on the graphic context. + * + * @param g the graphic context + * @param players the list of players to be displayed + */ + private void drawPlayers(Graphics2D g, List players) { + for (GameState.Player p : players) + Painter.drawImage(g, Grid.positionForPlayer(p), p.image()); } } -- cgit v1.2.3