aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ch/epfl/xblast/SubCell.java2
-rw-r--r--src/ch/epfl/xblast/server/GameState.java53
2 files changed, 27 insertions, 28 deletions
diff --git a/src/ch/epfl/xblast/SubCell.java b/src/ch/epfl/xblast/SubCell.java
index 531de01..adb0f9b 100644
--- a/src/ch/epfl/xblast/SubCell.java
+++ b/src/ch/epfl/xblast/SubCell.java
@@ -81,7 +81,7 @@ public final class SubCell {
81 * @param subCell the other SubCell 81 * @param subCell the other SubCell
82 * @return the distance 82 * @return the distance
83 */ 83 */
84 private int distanceTo(SubCell subCell) { 84 public int distanceTo(SubCell subCell) {
85 return Math.abs(this.x - subCell.x) + Math.abs(this.y - subCell.y); 85 return Math.abs(this.x - subCell.x) + Math.abs(this.y - subCell.y);
86 } 86 }
87 87
diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java
index 702fa37..2818f26 100644
--- a/src/ch/epfl/xblast/server/GameState.java
+++ b/src/ch/epfl/xblast/server/GameState.java
@@ -202,11 +202,11 @@ public final class GameState {
202 Set<Cell> bombedCells1, Board board1, Set<Cell> blastedCells1, 202 Set<Cell> bombedCells1, Board board1, Set<Cell> blastedCells1,
203 Direction requestedDirection) { 203 Direction requestedDirection) {
204 204
205 // 1. Compute the new path 205 // 1. Compute the new path to follow
206 Sq<Player.DirectedPosition> path1 = GameState.nextPath(player0, requestedDirection); 206 Sq<Player.DirectedPosition> updatedPath = GameState.nextPath(player0, requestedDirection);
207 207
208 // 2. Check possible collisions and update the Sequence if necessary 208 // 2. Follow the path if the Player can (moving life state and no collision)
209 Sq<Player.DirectedPosition> directedPos1 = GameState.handleCollisions(player0, path1, board1, bombedCells1); 209 Sq<Player.DirectedPosition> directedPos1 = GameState.moveAhead(player0, updatedPath, board1, bombedCells1);
210 210
211 // 3. Apply damages and generate a new LifeState Sequence 211 // 3. Apply damages and generate a new LifeState Sequence
212 Sq<Player.LifeState> lifeStates1 = GameState.nextLifeState(player0, directedPos1, blastedCells1); 212 Sq<Player.LifeState> lifeStates1 = GameState.nextLifeState(player0, directedPos1, blastedCells1);
@@ -240,11 +240,9 @@ public final class GameState {
240 * @return the next path 240 * @return the next path
241 */ 241 */
242 private static Sq<Player.DirectedPosition> nextPath(Player p0, Direction requestedDir) { 242 private static Sq<Player.DirectedPosition> nextPath(Player p0, Direction requestedDir) {
243 Sq<Player.DirectedPosition> path1 = GameState.newPath( 243 return GameState.newPath(
244 Player.DirectedPosition.moving(p0.directedPositions().head()), 244 Player.DirectedPosition.moving(p0.directedPositions().head()),
245 GameState.nextDirection(p0, requestedDir)); 245 GameState.nextDirection(p0, requestedDir));
246
247 return p0.lifeState().canMove() ? path1.tail() : path1;
248 } 246 }
249 247
250 /** 248 /**
@@ -299,39 +297,40 @@ public final class GameState {
299 /** 297 /**
300 * Checks for possible collisions and update the path if necessary. 298 * Checks for possible collisions and update the path if necessary.
301 * 299 *
302 * @param player0 the Player 300 * @param path the current path projection
303 * @param projectedPath the current path projection 301 * @param board1 the updated Board
304 * @param board1 the updated Board 302 * @param bombedCells1 the Set of bombed Cell-s
305 * @param bombedCells1 the Set of bombed Cell-s
306 * @return the corrected path 303 * @return the corrected path
307 */ 304 */
308 private static Sq<Player.DirectedPosition> handleCollisions(Player player0, 305 private static Sq<Player.DirectedPosition> moveAhead(Player player0,
309 Sq<Player.DirectedPosition> projectedPath, 306 Sq<Player.DirectedPosition> path,
310 Board board1, Set<Cell> bombedCells1) { 307 Board board1, Set<Cell> bombedCells1) {
308
309 if (!player0.lifeState().canMove())
310 return path;
311 311
312 Cell projectedCell = GameState.nextCentralPosition(projectedPath).containingCell(); 312 if (GameState.isColliding(path, board1, bombedCells1))
313 if (GameState.isColliding(player0, projectedCell, board1, bombedCells1)) 313 return path;
314 return Sq.repeat(1, player0.directedPositions().head())
315 .concat(projectedPath);
316 314
317 return projectedPath; 315 return path.tail();
318 } 316 }
319 317
320 /** 318 /**
321 * Returns T(the player is colliding with an object). 319 * Returns T(the player is colliding with an object).
322 * 320 *
323 * @param player0 the Player 321 * @param path the path
324 * @param projectedCell the projected next Cell 322 * @param board1 the updated Board
325 * @param board1 the updated Board 323 * @param bombedCells1 the Set of bombed Cell-s
326 * @param bombedCells1 the Set of bombed Cell-s
327 * @return T(the player is colliding with an object) 324 * @return T(the player is colliding with an object)
328 */ 325 */
329 private static boolean isColliding(Player player0, Cell projectedCell, Board board1, Set<Cell> bombedCells1) { 326 private static boolean isColliding(Sq<Player.DirectedPosition> path, Board board1, Set<Cell> bombedCells1) {
330 if (!board1.blockAt(projectedCell).canHostPlayer()) 327 SubCell nextPos = path.tail().head().position();
328 if (!board1.blockAt(nextPos.containingCell()).canHostPlayer())
331 return true; 329 return true;
332 330
333 if (bombedCells1.contains(projectedCell) && projectedCell.equals(player0.position().containingCell())) 331 SubCell nextCentralPos = GameState.nextCentralPosition(path.tail());
334 if (player0.position().distanceToCentral() <= Board.BOMB_BLOCKING_DISTANCE) 332 if (bombedCells1.contains(nextCentralPos.containingCell()))
333 if (nextPos.distanceTo(nextCentralPos) <= Board.BOMB_BLOCKING_DISTANCE)
335 return true; 334 return true;
336 335
337 return false; 336 return false;