aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-09 22:15:12 +0200
committerPacien TRAN-GIRARD2016-05-09 22:15:12 +0200
commit596ce316680de8a82fbe5622f830b2a985a84644 (patch)
tree072da4ffc00a585c2aa66d1442a3be339fea4b31
parent0a04160109c59dc4c257a8363a4098a45ba257aa (diff)
downloadxblast-596ce316680de8a82fbe5622f830b2a985a84644.tar.gz
Implement player painting order
-rw-r--r--src/ch/epfl/xblast/Lists.java14
-rw-r--r--src/ch/epfl/xblast/client/XBlastComponent.java19
2 files changed, 27 insertions, 6 deletions
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java
index 073e0cc..da3f919 100644
--- a/src/ch/epfl/xblast/Lists.java
+++ b/src/ch/epfl/xblast/Lists.java
@@ -167,4 +167,18 @@ public final class Lists {
167 .collect(Collectors.toList())); 167 .collect(Collectors.toList()));
168 } 168 }
169 169
170 /**
171 * Returns an immutable copy of the given list, sorted using the supplied comparator.
172 *
173 * @param l the list to sort
174 * @param cmp the Comparator to use
175 * @param <T> the type of the list's elements
176 * @return the sorted list
177 */
178 public static <T> List<T> sorted(List<T> l, Comparator<T> cmp) {
179 List<T> r = new ArrayList<>(l);
180 Collections.sort(r, cmp);
181 return Collections.unmodifiableList(r);
182 }
183
170} 184}
diff --git a/src/ch/epfl/xblast/client/XBlastComponent.java b/src/ch/epfl/xblast/client/XBlastComponent.java
index cab6fde..0b4f0bf 100644
--- a/src/ch/epfl/xblast/client/XBlastComponent.java
+++ b/src/ch/epfl/xblast/client/XBlastComponent.java
@@ -1,15 +1,14 @@
1package ch.epfl.xblast.client; 1package ch.epfl.xblast.client;
2 2
3import ch.epfl.xblast.Cell; 3import ch.epfl.xblast.Cell;
4import ch.epfl.xblast.Lists;
4import ch.epfl.xblast.PlayerID; 5import ch.epfl.xblast.PlayerID;
5 6
6import javax.swing.*; 7import javax.swing.*;
7import java.awt.*; 8import java.awt.*;
8import java.awt.image.ImageObserver; 9import java.awt.image.ImageObserver;
9import java.util.Arrays; 10import java.util.*;
10import java.util.Collections;
11import java.util.List; 11import java.util.List;
12import java.util.Objects;
13import java.util.stream.Collectors; 12import java.util.stream.Collectors;
14import java.util.stream.IntStream; 13import java.util.stream.IntStream;
15 14
@@ -64,6 +63,16 @@ public final class XBlastComponent extends JComponent {
64 new Point(912, vShift)); 63 new Point(912, vShift));
65 } 64 }
66 65
66 private static Comparator<GameState.Player> buildPlayerComparator(PlayerID blackSheep) {
67 Comparator<GameState.Player> coordinateComparator =
68 (p1, p2) -> p2.position().y() - p1.position().y();
69
70 Comparator<GameState.Player> idComparator =
71 (p1, p2) -> p1.id() == blackSheep ? -1 : 0;
72
73 return coordinateComparator.thenComparing(idComparator);
74 }
75
67 private static void drawImage(Graphics2D g, Image img, Point pos) { 76 private static void drawImage(Graphics2D g, Image img, Point pos) {
68 g.drawImage(img, pos.x, pos.y, IMG_OBSERVER); 77 g.drawImage(img, pos.x, pos.y, IMG_OBSERVER);
69 } 78 }
@@ -89,9 +98,7 @@ public final class XBlastComponent extends JComponent {
89 * @param players the list of players to be displayed 98 * @param players the list of players to be displayed
90 */ 99 */
91 private void drawPlayers(Graphics2D g, List<GameState.Player> players) { 100 private void drawPlayers(Graphics2D g, List<GameState.Player> players) {
92 // TODO: draw in preferred order 101 for (GameState.Player p : Lists.sorted(players, buildPlayerComparator(this.playerID)))
93
94 for (GameState.Player p : players)
95 drawImage(g, p.image(), playerPosition(p)); 102 drawImage(g, p.image(), playerPosition(p));
96 } 103 }
97 104