From 7d9c28e25d5b0965171f4b347852a11fdf01482e Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 10 May 2016 19:27:18 +0200 Subject: Move player comparators to the corresponding class --- src/ch/epfl/xblast/client/GameState.java | 10 ++++--- src/ch/epfl/xblast/client/XBlastComponent.java | 36 ++++++++++++-------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/ch/epfl/xblast/client/GameState.java b/src/ch/epfl/xblast/client/GameState.java index 480c6a1..9a4d37b 100644 --- a/src/ch/epfl/xblast/client/GameState.java +++ b/src/ch/epfl/xblast/client/GameState.java @@ -8,10 +8,8 @@ import ch.epfl.xblast.client.painter.ScorePainter; import ch.epfl.xblast.client.painter.TimeLinePainter; import java.awt.*; -import java.util.Collections; +import java.util.*; import java.util.List; -import java.util.Map; -import java.util.Objects; import java.util.stream.Collectors; /** @@ -27,6 +25,12 @@ public final class GameState { */ public static final class Player { + static final Comparator POSITION_COMPARATOR = (p1, p2) -> p1.position().compareTo(p2.position()); + + static Comparator idPushingComparator(PlayerID firstClassID) { + return (p1, p2) -> p1.id() == firstClassID ? -1 : 0; + } + private final PlayerID id; private final int lives; private final SubCell position; diff --git a/src/ch/epfl/xblast/client/XBlastComponent.java b/src/ch/epfl/xblast/client/XBlastComponent.java index fe5bf5a..0ef8c5c 100644 --- a/src/ch/epfl/xblast/client/XBlastComponent.java +++ b/src/ch/epfl/xblast/client/XBlastComponent.java @@ -107,27 +107,25 @@ public final class XBlastComponent extends JComponent { drawString(g, e.getKey(), e.getValue().toString()); } - } - - private static Comparator buildPlayerComparator(PlayerID priorityPlayer) { - Comparator coordinateComparator = - (p1, p2) -> p1.position().compareTo(p2.position()); - - Comparator idComparator = - (p1, p2) -> p1.id() == priorityPlayer ? -1 : 0; + /** + * Draws the players on the graphic context. + * + * @param g the graphic context + * @param players the list of players to be displayed + */ + private static void drawPlayers(Graphics2D g, List players) { + for (GameState.Player p : players) + drawImage(g, Grid.positionForPlayer(p), p.image()); + } - return coordinateComparator.thenComparing(idComparator); } - /** - * Draws the players on the graphic context. - * - * @param g the graphic context - * @param players the list of players to be displayed - */ - private static void drawPlayers(Graphics2D g, List players) { - for (GameState.Player p : players) - Painter.drawImage(g, Grid.positionForPlayer(p), p.image()); + private static List sortPlayers(List players, PlayerID currentPlayerID) { + return Lists.sorted( + players, + GameState.Player + .POSITION_COMPARATOR + .thenComparing(GameState.Player.idPushingComparator(currentPlayerID))); } private GameState gameState; @@ -172,7 +170,7 @@ public final class XBlastComponent extends JComponent { 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))); + Painter.drawPlayers(g, sortPlayers(this.gameState.players(), this.playerID)); } } -- cgit v1.2.3