From 816f502f129562fce5001a76819dc8109be021c5 Mon Sep 17 00:00:00 2001 From: torlunjen Date: Mon, 16 Mar 2020 17:31:54 +0100 Subject: [PATCH] Adds partial functionality that moves robots standing on conveyor belts. --- .../fiasko/roborally/objects/Board.java | 12 ++- .../roborally/objects/RoboRallyGame.java | 87 ++++++++++++++++++- 2 files changed, 91 insertions(+), 8 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/objects/Board.java b/src/main/java/inf112/fiasko/roborally/objects/Board.java index 5ab22dc..467a1c1 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Board.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Board.java @@ -1,9 +1,6 @@ package inf112.fiasko.roborally.objects; import inf112.fiasko.roborally.element_properties.*; -import inf112.fiasko.roborally.utility.TextureConverterUtil; - -import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -296,7 +293,7 @@ public class Board { * @param direction The direction to move the element * @return The new position of the element */ - private Position getNewPosition(Position oldPosition, Direction direction) { + Position getNewPosition(Position oldPosition, Direction direction) { switch (direction) { case NORTH: return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() - 1); @@ -327,6 +324,13 @@ public class Board { return elements; } + public Tile getTileOnPosition(Position position) { + if (!isValidPosition(position)) { + throw new IllegalArgumentException("Position is not on the board!"); + } + return tiles.getElement(position.getXCoordinate(), position.getYCoordinate()); + } + /** * Gets a list of BoardElementContainers, containing all tiles and positions of given tile types * @param tiles The tiles you want all positions for diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index 74ae131..ab36e2c 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -1,9 +1,6 @@ package inf112.fiasko.roborally.objects; -import inf112.fiasko.roborally.element_properties.Action; -import inf112.fiasko.roborally.element_properties.Position; -import inf112.fiasko.roborally.element_properties.RobotID; -import inf112.fiasko.roborally.element_properties.TileType; +import inf112.fiasko.roborally.element_properties.*; import inf112.fiasko.roborally.utility.BoardLoaderUtil; import java.io.IOException; @@ -17,6 +14,7 @@ import java.util.concurrent.TimeUnit; public class RoboRallyGame implements IDrawableGame { private Board gameBoard; List> cogwheels; + List> conveyorBelts; public RoboRallyGame(boolean debug) { if (debug) { @@ -90,6 +88,13 @@ public class RoboRallyGame implements IDrawableGame { gameBoard = BoardLoaderUtil.loadBoard("boards/Checkmate.txt", robots); cogwheels = gameBoard.getPositionsOfTileOnBoard(TileType.COGWHEEL_RIGHT, TileType.COGWHEEL_LEFT); + conveyorBelts = gameBoard.getPositionsOfTileOnBoard(TileType.TRANSPORT_BAND_FAST, + TileType.TRANSPORT_BAND_SLOW, TileType.TRANSPORT_BAND_FAST_SIDE_ENTRANCE_RIGHT, + TileType.TRANSPORT_BAND_FAST_RIGHT, TileType.TRANSPORT_BAND_SLOW_RIGHT, + TileType.TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_RIGHT, TileType.TRANSPORT_BAND_FAST_SIDE_ENTRANCE_LEFT, + TileType.TRANSPORT_BAND_FAST_LEFT, TileType.TRANSPORT_BAND_SLOW_LEFT, + TileType.TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_LEFT); + new Thread(() -> { try { runGameLoop(); @@ -204,4 +209,78 @@ public class RoboRallyGame implements IDrawableGame { } } } + + /** + * Moves robots standing on conveyor belts in the direction of the conveyor belt. + * Rotates robots being moved to a turn on the conveyor belt. + * @throws InterruptedException If disturbed during sleep. + */ + private void moveConveyorBelts() throws InterruptedException { + for (BoardElementContainer conveyorBelt : conveyorBelts) { + if (!gameBoard.hasRobotOnPosition(conveyorBelt.getPosition())) { + continue; + } + Position newPosition = gameBoard.getNewPosition(conveyorBelt.getPosition(), + conveyorBelt.getObject().getDirection()); + Tile nextTile = gameBoard.getTileOnPosition(newPosition); + Direction currentDirection = conveyorBelt.getObject().getDirection(); + Direction nextDirection = nextTile.getDirection(); + RobotID robot = gameBoard.getRobotOnPosition(conveyorBelt.getPosition()); + if (conveyorBelts.contains(nextTile) && currentDirection != nextDirection) { + if (currentDirection.equals(Direction.NORTH)) { + if (nextDirection.equals(Direction.WEST)) { + sleep(); + gameBoard.moveRobot(robot, currentDirection); + sleep(); + gameBoard.rotateRobotLeft(robot); + } else { + sleep(); + gameBoard.moveRobot(robot, currentDirection); + sleep(); + gameBoard.rotateRobotRight(robot); + } + } else if (currentDirection.equals(Direction.WEST)) { + if (nextDirection.equals(Direction.SOUTH)) { + sleep(); + gameBoard.moveRobot(robot, currentDirection); + sleep(); + gameBoard.rotateRobotLeft(robot); + } else { + sleep(); + gameBoard.moveRobot(robot, currentDirection); + sleep(); + gameBoard.rotateRobotLeft(robot); + } + } else if (currentDirection.equals(Direction.SOUTH)) { + if (nextDirection.equals(Direction.EAST)) { + sleep(); + gameBoard.moveRobot(robot, currentDirection); + sleep(); + gameBoard.rotateRobotLeft(robot); + } else { + sleep(); + gameBoard.moveRobot(robot, currentDirection); + sleep(); + gameBoard.rotateRobotRight(robot); + } + } else if (currentDirection.equals(Direction.EAST)) { + if (nextDirection.equals(Direction.NORTH)) { + sleep(); + gameBoard.moveRobot(robot, currentDirection); + sleep(); + gameBoard.rotateRobotLeft(robot); + } else { + sleep(); + gameBoard.moveRobot(robot, currentDirection); + sleep(); + gameBoard.rotateRobotRight(robot); + } + } + } else { + sleep(); + gameBoard.moveRobot(gameBoard.getRobotOnPosition(conveyorBelt.getPosition()), + conveyorBelt.getObject().getDirection()); + } + } + } } \ No newline at end of file