diff --git a/src/main/java/inf112/fiasko/roborally/element_properties/TileType.java b/src/main/java/inf112/fiasko/roborally/element_properties/TileType.java index f28a2fb..57c4a4c 100644 --- a/src/main/java/inf112/fiasko/roborally/element_properties/TileType.java +++ b/src/main/java/inf112/fiasko/roborally/element_properties/TileType.java @@ -8,18 +8,18 @@ public enum TileType { HOLE (2), COGWHEEL_RIGHT (3), COGWHEEL_LEFT (4), - TRANSPORT_BAND_SLOW (5), - TRANSPORT_BAND_SLOW_RIGHT (6), - TRANSPORT_BAND_SLOW_LEFT (7), - TRANSPORT_BAND_SLOW_SIDE_ENTRANCES (8), - TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_LEFT (9), - TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_RIGHT (10), - TRANSPORT_BAND_FAST (11), - TRANSPORT_BAND_FAST_RIGHT (12), - TRANSPORT_BAND_FAST_LEFT (13), - TRANSPORT_BAND_FAST_SIDE_ENTRANCES (14), - TRANSPORT_BAND_FAST_SIDE_ENTRANCE_LEFT (15), - TRANSPORT_BAND_FAST_SIDE_ENTRANCE_RIGHT (16), + CONVEYOR_BELT_SLOW(5), + CONVEYOR_BELT_SLOW_RIGHT(6), + CONVEYOR_BELT_SLOW_LEFT(7), + CONVEYOR_BELT_SLOW_SIDE_ENTRANCES(8), + CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_LEFT(9), + CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_RIGHT(10), + CONVEYOR_BELT_FAST(11), + CONVEYOR_BELT_FAST_RIGHT(12), + CONVEYOR_BELT_FAST_LEFT(13), + CONVEYOR_BELT_FAST_SIDE_ENTRANCES(14), + CONVEYOR_BELT_FAST_SIDE_ENTRANCE_LEFT(15), + CONVEYOR_BELT_FAST_SIDE_ENTRANCE_RIGHT(16), FLAG_1 (17), FLAG_2 (18), FLAG_3 (19), diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index a322d89..66c898e 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.function.Predicate; /** * This class represent a game which is drawable using libgdx @@ -16,6 +17,7 @@ public class RoboRallyGame implements IDrawableGame { private Board gameBoard; private List> cogwheels; private List> conveyorBelts; + private List> fastConveyorBelts; public RoboRallyGame(boolean debug) { if (debug) { @@ -89,12 +91,18 @@ 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); + fastConveyorBelts = gameBoard.getPositionsOfTileOnBoard(TileType.CONVEYOR_BELT_FAST, + TileType.CONVEYOR_BELT_FAST_RIGHT, TileType.CONVEYOR_BELT_FAST_LEFT, + TileType.CONVEYOR_BELT_FAST_SIDE_ENTRANCE_RIGHT, + TileType.CONVEYOR_BELT_FAST_SIDE_ENTRANCE_LEFT, + TileType.CONVEYOR_BELT_FAST_SIDE_ENTRANCES); + conveyorBelts = new ArrayList<>(); + conveyorBelts.addAll(fastConveyorBelts); + conveyorBelts.addAll(gameBoard.getPositionsOfTileOnBoard(TileType.CONVEYOR_BELT_SLOW, + TileType.CONVEYOR_BELT_SLOW_RIGHT, TileType.CONVEYOR_BELT_SLOW_LEFT, + TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_RIGHT, + TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_LEFT, + TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCES)); new Thread(() -> { try { @@ -211,87 +219,65 @@ public class RoboRallyGame implements IDrawableGame { } } - private Boolean listContainsTile(Tile tile) { - boolean containsTile = false; - for (BoardElementContainer conveyorBelt : conveyorBelts) { - if (conveyorBelt.getObject() == tile) { - containsTile = true; - break; + /** + * Checks whether a given list has at least one element as defined by the predicate + * @param list The list to check + * @param predicate The predicate to test + * @param The type of the list + * @return True if the list has at least one element passing the test of the predicate + */ + private static boolean testPredicate(List list, Predicate predicate) { + for (T object : list) { + if (predicate.test(object)) { + return true; } } - return containsTile; + return false; } /** - * 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. + * Moves robots standing on conveyor belts in the direction of the conveyor belt + * + * In addition, the function rotates appropriately when arriving at any non-straight conveyor belt + * + * @throws InterruptedException If disturbed during sleep */ - private void moveConveyorBelts() throws InterruptedException { + private void moveAllConveyorBelts() throws InterruptedException { + moveConveyorBelts(fastConveyorBelts); + moveConveyorBelts(conveyorBelts); + } + + /** + * Moves all conveyor belts in the input list + * @param conveyorBelts A list of conveyor belts to move + * @throws InterruptedException If disturbed during sleep + */ + private void moveConveyorBelts(List> conveyorBelts) throws InterruptedException { for (BoardElementContainer conveyorBelt : conveyorBelts) { if (!gameBoard.hasRobotOnPosition(conveyorBelt.getPosition())) { continue; } - Position newPosition = gameBoard.getNewPosition(conveyorBelt.getPosition(), - conveyorBelt.getObject().getDirection()); + Position conveyorBeltPosition = conveyorBelt.getPosition(); + Tile conveyorBeltTile = conveyorBelt.getObject(); + Position newPosition = gameBoard.getNewPosition(conveyorBeltPosition, conveyorBeltTile.getDirection()); Tile nextTile = gameBoard.getTileOnPosition(newPosition); - Direction currentDirection = conveyorBelt.getObject().getDirection(); + + Direction currentDirection = conveyorBeltTile.getDirection(); Direction nextDirection = nextTile.getDirection(); - RobotID robot = gameBoard.getRobotOnPosition(conveyorBelt.getPosition()); - if (listContainsTile(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); - } + RobotID robot = gameBoard.getRobotOnPosition(conveyorBeltPosition); + + //TODO: Check whether the robot is able to move before moving. Alternatively: Save position and direction + // of each robot and revert if a collision is found. + sleep(); + gameBoard.moveRobot(robot, currentDirection); + if (testPredicate(conveyorBelts, (container) -> container.getObject() == nextTile)) { + if (Direction.getRightRotatedDirection(nextDirection) == currentDirection) { + sleep(); + gameBoard.rotateRobotLeft(robot); + } else if (Direction.getLeftRotatedDirection(nextDirection) == currentDirection) { + sleep(); + gameBoard.rotateRobotRight(robot); } - } else { - sleep(); - gameBoard.moveRobot(gameBoard.getRobotOnPosition(conveyorBelt.getPosition()), - conveyorBelt.getObject().getDirection()); } } } diff --git a/src/main/resources/texture_sheet_tile_mapping.txt b/src/main/resources/texture_sheet_tile_mapping.txt index 0d190df..6f18cff 100644 --- a/src/main/resources/texture_sheet_tile_mapping.txt +++ b/src/main/resources/texture_sheet_tile_mapping.txt @@ -2,18 +2,18 @@ TILE 4 0 HOLE 5 0 COGWHEEL_RIGHT 5 6 COGWHEEL_LEFT 4 6 -TRANSPORT_BAND_SLOW 0 6 3 6 1 6 2 6 -TRANSPORT_BAND_SLOW_RIGHT 2 5 2 4 3 4 3 5 -TRANSPORT_BAND_SLOW_LEFT 1 5 0 5 0 4 1 4 -TRANSPORT_BAND_SLOW_SIDE_ENTRANCES 4 8 4 7 5 7 5 8 -TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_LEFT 0 7 1 7 2 7 3 7 -TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_RIGHT 0 8 1 8 2 8 3 8 -TRANSPORT_BAND_FAST 4 1 5 1 4 2 5 2 -TRANSPORT_BAND_FAST_RIGHT 2 3 2 2 3 2 3 3 -TRANSPORT_BAND_FAST_LEFT 1 3 0 3 0 2 1 2 -TRANSPORT_BAND_FAST_SIDE_ENTRANCES 3 10 0 10 1 10 2 10 -TRANSPORT_BAND_FAST_SIDE_ENTRANCE_RIGHT 4 9 5 9 5 10 4 10 -TRANSPORT_BAND_FAST_SIDE_ENTRANCE_LEFT 0 9 1 9 2 9 3 9 +CONVEYOR_BELT_SLOW 0 6 3 6 1 6 2 6 +CONVEYOR_BELT_SLOW_RIGHT 2 5 2 4 3 4 3 5 +CONVEYOR_BELT_SLOW_LEFT 1 5 0 5 0 4 1 4 +CONVEYOR_BELT_SLOW_SIDE_ENTRANCES 4 8 4 7 5 7 5 8 +CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_LEFT 0 7 1 7 2 7 3 7 +CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_RIGHT 0 8 1 8 2 8 3 8 +CONVEYOR_BELT_FAST 4 1 5 1 4 2 5 2 +CONVEYOR_BELT_FAST_RIGHT 2 3 2 2 3 2 3 3 +CONVEYOR_BELT_FAST_LEFT 1 3 0 3 0 2 1 2 +CONVEYOR_BELT_FAST_SIDE_ENTRANCES 3 10 0 10 1 10 2 10 +CONVEYOR_BELT_FAST_SIDE_ENTRANCE_RIGHT 4 9 5 9 5 10 4 10 +CONVEYOR_BELT_FAST_SIDE_ENTRANCE_LEFT 0 9 1 9 2 9 3 9 FLAG_1 6 6 FLAG_2 6 7 FLAG_3 6 8 diff --git a/src/test/java/inf112/fiasko/roborally/objects/TileTest.java b/src/test/java/inf112/fiasko/roborally/objects/TileTest.java index a6dd76c..1a89e75 100644 --- a/src/test/java/inf112/fiasko/roborally/objects/TileTest.java +++ b/src/test/java/inf112/fiasko/roborally/objects/TileTest.java @@ -40,6 +40,6 @@ public class TileTest { @Test (expected = IllegalArgumentException.class) public void invalidTileThrowsException() { - new Tile(TileType.TRANSPORT_BAND_FAST, Direction.NORTH_EAST); + new Tile(TileType.CONVEYOR_BELT_FAST, Direction.NORTH_EAST); } } \ No newline at end of file diff --git a/src/test/java/inf112/fiasko/roborally/utility/TextureConverterUtilTest.java b/src/test/java/inf112/fiasko/roborally/utility/TextureConverterUtilTest.java index b8fa6ac..3eed8bc 100644 --- a/src/test/java/inf112/fiasko/roborally/utility/TextureConverterUtilTest.java +++ b/src/test/java/inf112/fiasko/roborally/utility/TextureConverterUtilTest.java @@ -26,7 +26,7 @@ public class TextureConverterUtilTest { public void setUp() { tileNorth = new Tile(TileType.TILE, Direction.NORTH); holeNorth = new Tile(TileType.HOLE, Direction.NORTH); - transportBandSlowEast = new Tile(TileType.TRANSPORT_BAND_SLOW, Direction.EAST); + transportBandSlowEast = new Tile(TileType.CONVEYOR_BELT_SLOW, Direction.EAST); tileTextureRegion = TextureConverterUtil.convertElement(tileNorth); holeTextureRegion = TextureConverterUtil.convertElement(holeNorth); transportBandSlowEastTextureRegion = TextureConverterUtil.convertElement(transportBandSlowEast);