From 32d00b913c713856f52e932dc885a44ef44586d4 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 10 Apr 2016 15:41:30 +0200 Subject: Simplify path evolution and enable bombed cell exit on opposite direction --- src/ch/epfl/xblast/SubCell.java | 2 +- src/ch/epfl/xblast/server/GameState.java | 53 ++++++++++++++++---------------- 2 files changed, 27 insertions(+), 28 deletions(-) (limited to 'src') 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 { * @param subCell the other SubCell * @return the distance */ - private int distanceTo(SubCell subCell) { + public int distanceTo(SubCell subCell) { return Math.abs(this.x - subCell.x) + Math.abs(this.y - subCell.y); } 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 { Set bombedCells1, Board board1, Set blastedCells1, Direction requestedDirection) { - // 1. Compute the new path - Sq path1 = GameState.nextPath(player0, requestedDirection); + // 1. Compute the new path to follow + Sq updatedPath = GameState.nextPath(player0, requestedDirection); - // 2. Check possible collisions and update the Sequence if necessary - Sq directedPos1 = GameState.handleCollisions(player0, path1, board1, bombedCells1); + // 2. Follow the path if the Player can (moving life state and no collision) + Sq directedPos1 = GameState.moveAhead(player0, updatedPath, board1, bombedCells1); // 3. Apply damages and generate a new LifeState Sequence Sq lifeStates1 = GameState.nextLifeState(player0, directedPos1, blastedCells1); @@ -240,11 +240,9 @@ public final class GameState { * @return the next path */ private static Sq nextPath(Player p0, Direction requestedDir) { - Sq path1 = GameState.newPath( + return GameState.newPath( Player.DirectedPosition.moving(p0.directedPositions().head()), GameState.nextDirection(p0, requestedDir)); - - return p0.lifeState().canMove() ? path1.tail() : path1; } /** @@ -299,39 +297,40 @@ public final class GameState { /** * Checks for possible collisions and update the path if necessary. * - * @param player0 the Player - * @param projectedPath the current path projection - * @param board1 the updated Board - * @param bombedCells1 the Set of bombed Cell-s + * @param path the current path projection + * @param board1 the updated Board + * @param bombedCells1 the Set of bombed Cell-s * @return the corrected path */ - private static Sq handleCollisions(Player player0, - Sq projectedPath, - Board board1, Set bombedCells1) { + private static Sq moveAhead(Player player0, + Sq path, + Board board1, Set bombedCells1) { + + if (!player0.lifeState().canMove()) + return path; - Cell projectedCell = GameState.nextCentralPosition(projectedPath).containingCell(); - if (GameState.isColliding(player0, projectedCell, board1, bombedCells1)) - return Sq.repeat(1, player0.directedPositions().head()) - .concat(projectedPath); + if (GameState.isColliding(path, board1, bombedCells1)) + return path; - return projectedPath; + return path.tail(); } /** * Returns T(the player is colliding with an object). * - * @param player0 the Player - * @param projectedCell the projected next Cell - * @param board1 the updated Board - * @param bombedCells1 the Set of bombed Cell-s + * @param path the path + * @param board1 the updated Board + * @param bombedCells1 the Set of bombed Cell-s * @return T(the player is colliding with an object) */ - private static boolean isColliding(Player player0, Cell projectedCell, Board board1, Set bombedCells1) { - if (!board1.blockAt(projectedCell).canHostPlayer()) + private static boolean isColliding(Sq path, Board board1, Set bombedCells1) { + SubCell nextPos = path.tail().head().position(); + if (!board1.blockAt(nextPos.containingCell()).canHostPlayer()) return true; - if (bombedCells1.contains(projectedCell) && projectedCell.equals(player0.position().containingCell())) - if (player0.position().distanceToCentral() <= Board.BOMB_BLOCKING_DISTANCE) + SubCell nextCentralPos = GameState.nextCentralPosition(path.tail()); + if (bombedCells1.contains(nextCentralPos.containingCell())) + if (nextPos.distanceTo(nextCentralPos) <= Board.BOMB_BLOCKING_DISTANCE) return true; return false; -- cgit v1.2.3