aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-10 19:27:18 +0200
committerPacien TRAN-GIRARD2016-05-10 19:27:18 +0200
commit7d9c28e25d5b0965171f4b347852a11fdf01482e (patch)
tree99a30bb9602bef52cdbe5f6d68e442df3f1f4428
parente3e9e9a8696f1604c4b3590c142ead48167210b4 (diff)
downloadxblast-7d9c28e25d5b0965171f4b347852a11fdf01482e.tar.gz
Move player comparators to the corresponding class
-rw-r--r--src/ch/epfl/xblast/client/GameState.java10
-rw-r--r--src/ch/epfl/xblast/client/XBlastComponent.java36
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;
8import ch.epfl.xblast.client.painter.TimeLinePainter; 8import ch.epfl.xblast.client.painter.TimeLinePainter;
9 9
10import java.awt.*; 10import java.awt.*;
11import java.util.Collections; 11import java.util.*;
12import java.util.List; 12import java.util.List;
13import java.util.Map;
14import java.util.Objects;
15import java.util.stream.Collectors; 13import java.util.stream.Collectors;
16 14
17/** 15/**
@@ -27,6 +25,12 @@ public final class GameState {
27 */ 25 */
28 public static final class Player { 26 public static final class Player {
29 27
28 static final Comparator<Player> POSITION_COMPARATOR = (p1, p2) -> p1.position().compareTo(p2.position());
29
30 static Comparator<Player> idPushingComparator(PlayerID firstClassID) {
31 return (p1, p2) -> p1.id() == firstClassID ? -1 : 0;
32 }
33
30 private final PlayerID id; 34 private final PlayerID id;
31 private final int lives; 35 private final int lives;
32 private final SubCell position; 36 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 {
107 drawString(g, e.getKey(), e.getValue().toString()); 107 drawString(g, e.getKey(), e.getValue().toString());
108 } 108 }
109 109
110 } 110 /**
111 111 * Draws the players on the graphic context.
112 private static Comparator<GameState.Player> buildPlayerComparator(PlayerID priorityPlayer) { 112 *
113 Comparator<GameState.Player> coordinateComparator = 113 * @param g the graphic context
114 (p1, p2) -> p1.position().compareTo(p2.position()); 114 * @param players the list of players to be displayed
115 115 */
116 Comparator<GameState.Player> idComparator = 116 private static void drawPlayers(Graphics2D g, List<GameState.Player> players) {
117 (p1, p2) -> p1.id() == priorityPlayer ? -1 : 0; 117 for (GameState.Player p : players)
118 drawImage(g, Grid.positionForPlayer(p), p.image());
119 }
118 120
119 return coordinateComparator.thenComparing(idComparator);
120 } 121 }
121 122
122 /** 123 private static List<GameState.Player> sortPlayers(List<GameState.Player> players, PlayerID currentPlayerID) {
123 * Draws the players on the graphic context. 124 return Lists.sorted(
124 * 125 players,
125 * @param g the graphic context 126 GameState.Player
126 * @param players the list of players to be displayed 127 .POSITION_COMPARATOR
127 */ 128 .thenComparing(GameState.Player.idPushingComparator(currentPlayerID)));
128 private static void drawPlayers(Graphics2D g, List<GameState.Player> players) {
129 for (GameState.Player p : players)
130 Painter.drawImage(g, Grid.positionForPlayer(p), p.image());
131 } 129 }
132 130
133 private GameState gameState; 131 private GameState gameState;
@@ -172,7 +170,7 @@ public final class XBlastComponent extends JComponent {
172 Painter.drawImageMap(g, Lists.linearMap(Grid.SCORE_BG_POSITIONS, this.gameState.scores())); 170 Painter.drawImageMap(g, Lists.linearMap(Grid.SCORE_BG_POSITIONS, this.gameState.scores()));
173 Painter.drawImageMap(g, Lists.linearMap(Grid.TIME_LINE_POSITIONS, this.gameState.ticks())); 171 Painter.drawImageMap(g, Lists.linearMap(Grid.TIME_LINE_POSITIONS, this.gameState.ticks()));
174 Painter.drawStringMap(g, Lists.traverseMaps(Lists.invertMap(Grid.SCORE_TXT_POSITIONS), this.gameState.playerScores())); 172 Painter.drawStringMap(g, Lists.traverseMaps(Lists.invertMap(Grid.SCORE_TXT_POSITIONS), this.gameState.playerScores()));
175 drawPlayers(g, Lists.sorted(this.gameState.players(), buildPlayerComparator(this.playerID))); 173 Painter.drawPlayers(g, sortPlayers(this.gameState.players(), this.playerID));
176 } 174 }
177 175
178} 176}