From b0f608bb50b9a58a3ea4ebc7eb7be95e771aa360 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 10 Apr 2016 23:08:47 +0200 Subject: Handle stop speed change events --- src/ch/epfl/xblast/server/GameState.java | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 364a564..65309d5 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -179,11 +179,10 @@ public final class GameState { Map> speedChangeEvents) { return players0.stream() - .map(p -> { - Optional speedChangeEvent = speedChangeEvents.get(p.id()); - Direction requestedDirection = speedChangeEvent != null ? speedChangeEvent.orElse(null) : null; - return GameState.nextPlayer(p, playerBonuses.get(p.id()), bombedCells1, board1, blastedCells1, requestedDirection); - }) + .map(p -> GameState.nextPlayer( + p, playerBonuses.get(p.id()), + bombedCells1, board1, blastedCells1, + speedChangeEvents.get(p.id()))) .collect(Collectors.toList()); } @@ -200,7 +199,7 @@ public final class GameState { */ private static Player nextPlayer(Player player0, Bonus playerBonus, Set bombedCells1, Board board1, Set blastedCells1, - Direction requestedDirection) { + Optional requestedDirection) { // 1. Compute the new path to follow Sq updatedPath = GameState.nextPath(player0.directedPositions(), requestedDirection); @@ -228,14 +227,17 @@ public final class GameState { * @param newDir the new requested Direction * @return the new path */ - private static Sq nextPath(Sq p0, Direction newDir) { + private static Sq nextPath(Sq p0, Optional newDir) { if (Objects.isNull(newDir)) return p0; - if (p0.head().direction().isPerpendicularTo(newDir)) - return GameState.pivotPath(Player.DirectedPosition.moving(p0.head()), newDir); + if (!newDir.isPresent()) + return Player.DirectedPosition.stopped(p0.head()); - return Player.DirectedPosition.moving(new Player.DirectedPosition(p0.head().position(), newDir)); + if (p0.head().direction().isPerpendicularTo(newDir.get())) + return GameState.pivotPath(Player.DirectedPosition.moving(p0.head()), newDir.get()); + + return Player.DirectedPosition.moving(new Player.DirectedPosition(p0.head().position(), newDir.get())); } /** @@ -304,7 +306,7 @@ public final class GameState { */ private static boolean isColliding(Sq path, Board board1, Set bombedCells1) { SubCell nextPos = path.tail().head().position(); - SubCell nextCentralPos = GameState.nextCentralPosition(path.tail()); + SubCell nextCentralPos = GameState.nextCentralPosition(Player.DirectedPosition.moving(path.tail().head())); if (!board1.blockAt(nextCentralPos.containingCell()).canHostPlayer()) return true; -- cgit v1.2.3