mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Deler opp moveRobot medtoden slik at den blir mer håndterlig
This commit is contained in:
parent
014122f384
commit
d413e1c965
@ -93,16 +93,11 @@ public class Board {
|
|||||||
Position robotPosition = robot.getPosition();
|
Position robotPosition = robot.getPosition();
|
||||||
Position newPosition = getNewPosition(robotPosition, direction);
|
Position newPosition = getNewPosition(robotPosition, direction);
|
||||||
//Robot tried to go outside of the map. Kill it.
|
//Robot tried to go outside of the map. Kill it.
|
||||||
if (newPosition.getXCoordinate() < 0
|
if (killRobotIfGoesOutsideMap(robot, newPosition)) {
|
||||||
|| newPosition.getXCoordinate() >= boardWidth
|
|
||||||
|| newPosition.getYCoordinate() < 0
|
|
||||||
|| newPosition.getYCoordinate() >= boardHeight) {
|
|
||||||
killRobot(robot);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//There is a wall blocking the robot. It can't proceed.
|
//There is a wall blocking the robot. It can't proceed.
|
||||||
if (hasWallFacing(robotPosition, direction) ||
|
if (robotMoveIsStoppedByWall(robotPosition, newPosition, direction)) {
|
||||||
hasWallFacing(newPosition, Direction.getReverseDirection(direction))) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//If another robot is blocking this robot's path, try to shove it.
|
//If another robot is blocking this robot's path, try to shove it.
|
||||||
@ -113,6 +108,49 @@ public class Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Some tiles may kill the robot if stepped on.
|
//Some tiles may kill the robot if stepped on.
|
||||||
|
if (killRobotIfStepsOnDangerousTile(robot, newPosition)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
robot.setPosition(newPosition);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a potential robot move would be blocked by a wall
|
||||||
|
* @param robotPosition The current position of the robot
|
||||||
|
* @param newPosition The position the robot is trying to move to
|
||||||
|
* @param direction The direction the robot is going
|
||||||
|
* @return True if a wall would stop its path
|
||||||
|
*/
|
||||||
|
private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) {
|
||||||
|
return hasWallFacing(robotPosition, direction) ||
|
||||||
|
hasWallFacing(newPosition, Direction.getReverseDirection(direction));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the robot is about to step outside of the board, and kills it if it does
|
||||||
|
* @param robot The robot attempting to move
|
||||||
|
* @param newPosition The position the robot is attempting to move to
|
||||||
|
* @return True if the robot was killed for leaving the board
|
||||||
|
*/
|
||||||
|
private boolean killRobotIfGoesOutsideMap(Robot robot, Position newPosition) {
|
||||||
|
if (newPosition.getXCoordinate() < 0
|
||||||
|
|| newPosition.getXCoordinate() >= boardWidth
|
||||||
|
|| newPosition.getYCoordinate() < 0
|
||||||
|
|| newPosition.getYCoordinate() >= boardHeight) {
|
||||||
|
killRobot(robot);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks the tile the robot is about to step on and kills it if the tile is dangerous
|
||||||
|
* @param robot The robot attempting to move
|
||||||
|
* @param newPosition The position the robot is attempting to move to
|
||||||
|
* @return True if the robot was killed by the tile
|
||||||
|
*/
|
||||||
|
private boolean killRobotIfStepsOnDangerousTile(Robot robot, Position newPosition) {
|
||||||
Tile tileRobotStepsOn = tiles.getElement(newPosition.getXCoordinate(), newPosition.getYCoordinate());
|
Tile tileRobotStepsOn = tiles.getElement(newPosition.getXCoordinate(), newPosition.getYCoordinate());
|
||||||
if (tileRobotStepsOn == null) {
|
if (tileRobotStepsOn == null) {
|
||||||
throw new IllegalArgumentException("The game board is missing a tile. This should not happen.");
|
throw new IllegalArgumentException("The game board is missing a tile. This should not happen.");
|
||||||
@ -122,8 +160,7 @@ public class Board {
|
|||||||
killRobot(robot);
|
killRobot(robot);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
robot.setPosition(newPosition);
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -183,9 +220,10 @@ public class Board {
|
|||||||
return direction == Direction.SOUTH || direction == Direction.WEST;
|
return direction == Direction.SOUTH || direction == Direction.WEST;
|
||||||
case SOUTH_EAST:
|
case SOUTH_EAST:
|
||||||
return direction == Direction.SOUTH || direction == Direction.EAST;
|
return direction == Direction.SOUTH || direction == Direction.EAST;
|
||||||
}
|
default:
|
||||||
return wall.getDirection() == direction;
|
return wall.getDirection() == direction;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the position 1 unit in a specific direction from another position
|
* Gets the position 1 unit in a specific direction from another position
|
||||||
|
Loading…
x
Reference in New Issue
Block a user