From 0ce2048d2c34efb6335722a8d09c37afca78c583 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Tue, 5 Apr 2016 09:37:10 +0200 Subject: GameState: Fix movement in nextPlayer() --- src/ch/epfl/xblast/Direction.java | 19 +++++++++++++++++++ src/ch/epfl/xblast/server/GameState.java | 18 ++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'src') 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 @@ -67,6 +67,25 @@ public enum Direction { return that == this || that == this.opposite(); } + /** + * T(the current and given Directions are perpendicular). + * + * @param that a Direction to compare + * @return T(the current and given Directions are perpendicular) + */ + public boolean isPerpendicularTo(Direction that) { + switch (this) { + case N: + case S: + return that == E || that == W; + case E: + case W: + return that == N || that == S; + default: + return false; + } + } + /** * Returns the x-coordinate of the normed vector representation of the Direction. * 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 { for (Player player0 : players0) { Optional event = speedChangeEvents.get(player0.id()); - Sq directedPositions1; + // By default the path won't change + Sq directedPositions1 = player0.directedPositions(); - // Get the requested direction + // Get the (possibly) requested direction Direction requestedDirection = null; if (event != null) { requestedDirection = event.orElse(null); } // Generate the new Sequence of Directed Positions (kinda ugly right now) - if (requestedDirection == null || !player0.lifeState().canMove()) { - // The player does not move + if (!player0.lifeState().canMove() && requestedDirection != null) { // if the player can't movex directedPositions1 = Player.DirectedPosition.stopped(player0.directedPositions().head()); - } else if (player0.direction() == requestedDirection.opposite() || player0.direction() == requestedDirection) { - // The player keep its actual position or go to the opposite direction + } else if (player0.direction().isParallelTo(requestedDirection)) { // if the player want to go to a parallel directionx Player.DirectedPosition requestedDirectedPosition = new Player.DirectedPosition(player0.position(), requestedDirection); - directedPositions1 = Sq.constant(requestedDirectedPosition); - } else { - // The player go to a perpendicular position. + directedPositions1 = Player.DirectedPosition.moving(requestedDirectedPosition); + } else if (player0.direction().isPerpendicularTo(requestedDirection)) { // if the player want to go to a perpendicular direction Player.DirectedPosition nextCentralSubCell = player0.directedPositions().findFirst(dp -> dp.position().isCentral()); Sq toNextCentralSubCell = player0.directedPositions().takeWhile(dp -> !dp.position().isCentral()); Sq pastNextCentralSubCell = Player.DirectedPosition.moving(new Player.DirectedPosition(nextCentralSubCell.position(), requestedDirection)); directedPositions1 = toNextCentralSubCell.concat(pastNextCentralSubCell); } - // Advance of one SubCell on the newly computed path + // Advance of one SubCell on the player's path directedPositions1 = directedPositions1.tail(); // Check possible collisions and update the Sequence if necessary (kinda ugly right now) -- cgit v1.2.3