aboutsummaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
authorTimothée Floure2016-04-05 09:37:10 +0200
committerTimothée Floure2016-04-05 09:37:10 +0200
commit0ce2048d2c34efb6335722a8d09c37afca78c583 (patch)
treed9dcb59580210c4ce7d75cfb7127d6cd757e7cd2 /src/ch
parent4d75a239dbffb6bd73d261c2cd5804d6e77834b4 (diff)
downloadxblast-0ce2048d2c34efb6335722a8d09c37afca78c583.tar.gz
GameState: Fix movement in nextPlayer()
Diffstat (limited to 'src/ch')
-rw-r--r--src/ch/epfl/xblast/Direction.java19
-rw-r--r--src/ch/epfl/xblast/server/GameState.java18
2 files changed, 27 insertions, 10 deletions
diff --git a/src/ch/epfl/xblast/Direction.java b/src/ch/epfl/xblast/Direction.java
index 030038b..e2df042 100644
--- a/src/ch/epfl/xblast/Direction.java
+++ b/src/ch/epfl/xblast/Direction.java
@@ -68,6 +68,25 @@ public enum Direction {
68 } 68 }
69 69
70 /** 70 /**
71 * T(the current and given Directions are perpendicular).
72 *
73 * @param that a Direction to compare
74 * @return T(the current and given Directions are perpendicular)
75 */
76 public boolean isPerpendicularTo(Direction that) {
77 switch (this) {
78 case N:
79 case S:
80 return that == E || that == W;
81 case E:
82 case W:
83 return that == N || that == S;
84 default:
85 return false;
86 }
87 }
88
89 /**
71 * Returns the x-coordinate of the normed vector representation of the Direction. 90 * Returns the x-coordinate of the normed vector representation of the Direction.
72 * 91 *
73 * @return the x-coordinate 92 * @return the x-coordinate
diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java
index 035493d..d648cef 100644
--- a/src/ch/epfl/xblast/server/GameState.java
+++ b/src/ch/epfl/xblast/server/GameState.java
@@ -167,31 +167,29 @@ public final class GameState {
167 167
168 for (Player player0 : players0) { 168 for (Player player0 : players0) {
169 Optional<Direction> event = speedChangeEvents.get(player0.id()); 169 Optional<Direction> event = speedChangeEvents.get(player0.id());
170 Sq<Player.DirectedPosition> directedPositions1; 170 // By default the path won't change
171 Sq<Player.DirectedPosition> directedPositions1 = player0.directedPositions();
171 172
172 // Get the requested direction 173 // Get the (possibly) requested direction
173 Direction requestedDirection = null; 174 Direction requestedDirection = null;
174 if (event != null) { 175 if (event != null) {
175 requestedDirection = event.orElse(null); 176 requestedDirection = event.orElse(null);
176 } 177 }
177 178
178 // Generate the new Sequence of Directed Positions (kinda ugly right now) 179 // Generate the new Sequence of Directed Positions (kinda ugly right now)
179 if (requestedDirection == null || !player0.lifeState().canMove()) { 180 if (!player0.lifeState().canMove() && requestedDirection != null) { // if the player can't movex
180 // The player does not move
181 directedPositions1 = Player.DirectedPosition.stopped(player0.directedPositions().head()); 181 directedPositions1 = Player.DirectedPosition.stopped(player0.directedPositions().head());
182 } else if (player0.direction() == requestedDirection.opposite() || player0.direction() == requestedDirection) { 182 } else if (player0.direction().isParallelTo(requestedDirection)) { // if the player want to go to a parallel directionx
183 // The player keep its actual position or go to the opposite direction
184 Player.DirectedPosition requestedDirectedPosition = new Player.DirectedPosition(player0.position(), requestedDirection); 183 Player.DirectedPosition requestedDirectedPosition = new Player.DirectedPosition(player0.position(), requestedDirection);
185 directedPositions1 = Sq.constant(requestedDirectedPosition); 184 directedPositions1 = Player.DirectedPosition.moving(requestedDirectedPosition);
186 } else { 185 } else if (player0.direction().isPerpendicularTo(requestedDirection)) { // if the player want to go to a perpendicular direction
187 // The player go to a perpendicular position.
188 Player.DirectedPosition nextCentralSubCell = player0.directedPositions().findFirst(dp -> dp.position().isCentral()); 186 Player.DirectedPosition nextCentralSubCell = player0.directedPositions().findFirst(dp -> dp.position().isCentral());
189 Sq<Player.DirectedPosition> toNextCentralSubCell = player0.directedPositions().takeWhile(dp -> !dp.position().isCentral()); 187 Sq<Player.DirectedPosition> toNextCentralSubCell = player0.directedPositions().takeWhile(dp -> !dp.position().isCentral());
190 Sq<Player.DirectedPosition> pastNextCentralSubCell = Player.DirectedPosition.moving(new Player.DirectedPosition(nextCentralSubCell.position(), requestedDirection)); 188 Sq<Player.DirectedPosition> pastNextCentralSubCell = Player.DirectedPosition.moving(new Player.DirectedPosition(nextCentralSubCell.position(), requestedDirection));
191 directedPositions1 = toNextCentralSubCell.concat(pastNextCentralSubCell); 189 directedPositions1 = toNextCentralSubCell.concat(pastNextCentralSubCell);
192 } 190 }
193 191
194 // Advance of one SubCell on the newly computed path 192 // Advance of one SubCell on the player's path
195 directedPositions1 = directedPositions1.tail(); 193 directedPositions1 = directedPositions1.tail();
196 194
197 // Check possible collisions and update the Sequence if necessary (kinda ugly right now) 195 // Check possible collisions and update the Sequence if necessary (kinda ugly right now)