From 4aae3663ad2c2d27dddcca5cf9888a444bd1757f Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 28 Apr 2020 22:54:23 +0200 Subject: [PATCH] Refaktorerer og forenkler Board litt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lager en hjelpeklasse for Grid Lager en hjelpeklasser for lasere Bytter navn på noen metoder i Board Legger til en interface for å lettere bruke Tile, Wall og Particle om hverandre --- .../fiasko/roborally/objects/Board.java | 261 ++++-------------- .../roborally/objects/BoardElement.java | 26 ++ .../objects/BoardElementContainer.java | 8 +- .../fiasko/roborally/objects/Particle.java | 16 +- .../fiasko/roborally/objects/Phase.java | 16 +- .../roborally/objects/RoboRallyGame.java | 4 +- .../inf112/fiasko/roborally/objects/Tile.java | 16 +- .../inf112/fiasko/roborally/objects/Wall.java | 16 +- .../fiasko/roborally/utility/GridUtil.java | 55 ++++ .../fiasko/roborally/utility/LaserHelper.java | 127 +++++++++ .../utility/TextureConverterUtil.java | 12 +- .../fiasko/roborally/objects/BoardTest.java | 28 +- .../roborally/objects/ParticleTest.java | 4 +- .../fiasko/roborally/objects/TileTest.java | 4 +- .../fiasko/roborally/objects/WallTest.java | 6 +- 15 files changed, 316 insertions(+), 283 deletions(-) create mode 100644 src/main/java/inf112/fiasko/roborally/objects/BoardElement.java create mode 100644 src/main/java/inf112/fiasko/roborally/utility/GridUtil.java create mode 100644 src/main/java/inf112/fiasko/roborally/utility/LaserHelper.java diff --git a/src/main/java/inf112/fiasko/roborally/objects/Board.java b/src/main/java/inf112/fiasko/roborally/objects/Board.java index 5f776dc..59009dd 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Board.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Board.java @@ -6,6 +6,8 @@ import inf112.fiasko.roborally.elementproperties.Position; import inf112.fiasko.roborally.elementproperties.RobotID; import inf112.fiasko.roborally.elementproperties.TileType; import inf112.fiasko.roborally.elementproperties.WallType; +import inf112.fiasko.roborally.utility.GridUtil; +import inf112.fiasko.roborally.utility.LaserHelper; import java.util.ArrayList; import java.util.HashMap; @@ -25,6 +27,7 @@ public class Board { private List deadRobots; private List realDeadRobots; private List dangerousTiles; + private List> wallLasers; /** * Initializes the board @@ -53,6 +56,8 @@ public class Board { this.realDeadRobots = new ArrayList<>(); this.dangerousTiles = new ArrayList<>(); loadDangerousTileTypes(); + wallLasers = getPositionsOfWallsOnBoard(WallType.WALL_LASER_SINGLE, + WallType.WALL_LASER_DOUBLE, WallType.WALL_LASER_TRIPLE); } /** @@ -68,9 +73,9 @@ public class Board { } /** - * All the Real dead player's robots. + * Gets a list of robots no longer part of the game * - * @return A list of Robots. + * @return Robots no longer part of the game */ public List getRealDeadRobots() { return realDeadRobots; @@ -111,9 +116,9 @@ public class Board { * @return A list of robots */ public List getAllRobots() { - List robotsCopy = new ArrayList<>(robots.values()); - robotsCopy.addAll(deadRobots); + List robotsCopy = new ArrayList<>(deadRobots); robotsCopy.replaceAll(Robot::copy); + robotsCopy.addAll(getAliveRobots()); return robotsCopy; } @@ -123,7 +128,7 @@ public class Board { * @return A list of all tiles on the board */ public List getTiles() { - return getAllElementsFromGrid(tiles); + return GridUtil.getAllElementsFromGrid(tiles); } /** @@ -132,7 +137,7 @@ public class Board { * @return A list of all the walls on the board */ public List getWalls() { - return getAllElementsFromGrid(walls); + return GridUtil.getAllElementsFromGrid(walls); } /** @@ -141,7 +146,7 @@ public class Board { * @return A list of all the particles on the board */ public List getParticles() { - return getAllElementsFromGrid(particles); + return GridUtil.getAllElementsFromGrid(particles); } /** @@ -191,22 +196,20 @@ public class Board { * @param powerDown The status of the power down */ public void setPowerDown(RobotID robotID, Boolean powerDown) { - Robot alternateRobot = getRobotFromDeadRobots(robotID); - if (robots.containsKey(robotID)) { - robots.get(robotID).setPowerDown(powerDown); - } else if (alternateRobot != null) { - alternateRobot.setPowerDown(powerDown); + Robot robot = getRobot(robotID); + if (robot != null) { + robot.setPowerDown(powerDown); } } /** * Sets the backup position of a given robot to a given position * - * @param robotID The robot to change backup position for - * @param pos The robot's new backup position + * @param robotID The robot to change backup position for + * @param position The robot's new backup position */ - public void setBackupPositionOfRobot(RobotID robotID, Position pos) { - robots.get(robotID).setBackupPosition(pos); + public void setBackupPositionOfRobot(RobotID robotID, Position position) { + robots.get(robotID).setBackupPosition(position); } /** @@ -216,13 +219,8 @@ public class Board { * @return The power down status of the robot */ public boolean getPowerDown(RobotID robotID) { - Robot alternateRobot = getRobotFromDeadRobots(robotID); - if (robots.containsKey(robotID)) { - return robots.get(robotID).isInPowerDown(); - } else if (alternateRobot != null) { - return alternateRobot.isInPowerDown(); - } - return false; + Robot robot = getRobot(robotID); + return robot != null && robot.isInPowerDown(); } /** @@ -275,7 +273,7 @@ public class Board { /** * Moves a robot one unit in a specified direction * - * @param robotID ID of the robot to move + * @param robotID Id of the robot to move * @param direction The direction to move the robot * @return True if the robot moved away from its old position */ @@ -315,7 +313,7 @@ public class Board { if (tile == null) { return false; } - int tileTypeId = tile.getTileType().getTileTypeID(); + int tileTypeId = tile.getType().getTileTypeID(); return tileTypeId >= 5 && tileTypeId <= 16; } @@ -493,11 +491,11 @@ public class Board { * @param robotID The RobotID of a robot * @param flagID TileType of the flag we check */ - public void updateFlagOnRobot(RobotID robotID, TileType flagID) { + public void updateRobotFlag(RobotID robotID, TileType flagID) { Robot robot = robots.get(robotID); - int flagNr = flagID.getTileTypeID() % 16; - if (flagNr - 1 == robot.getLastFlagVisited()) { - robot.setLastFlagVisited(flagNr); + int flagNumber = flagID.getTileTypeID() % 16; + if (flagNumber - 1 == robot.getLastFlagVisited()) { + robot.setLastFlagVisited(flagNumber); setHasTouchedFlagThisTurn(robotID, true); } } @@ -528,12 +526,10 @@ public class Board { * Fires all lasers on the board and kills any robot that has taken to much damage after all lasers have fired. */ public void fireAllLasers() { - List> listOfWallLasers = getPositionsOfWallOnBoard(WallType.WALL_LASER_SINGLE, - WallType.WALL_LASER_DOUBLE, WallType.WALL_LASER_TRIPLE); for (Robot robot : robots.values()) { fireRobotLaser(robot.getPosition(), robot.getFacingDirection()); } - for (BoardElementContainer laser : listOfWallLasers) { + for (BoardElementContainer laser : wallLasers) { fireWallLaser(laser); } } @@ -565,10 +561,10 @@ public class Board { * @param tiles The tiles you want all positions for * @return A list of BoardElementContainers */ - public List> getPositionsOfTileOnBoard(TileType... tiles) { + public List> getPositionsOfTilesOnBoard(TileType... tiles) { List> combinedList = new ArrayList<>(); for (TileType tile : tiles) { - combinedList.addAll(makeTileList(tile, this.tiles)); + combinedList.addAll(GridUtil.getMatchingElements(tile, this.tiles)); } return combinedList; } @@ -579,10 +575,10 @@ public class Board { * @param walls The walls you want all positions for * @return A list of BoardElementContainers */ - public List> getPositionsOfWallOnBoard(WallType... walls) { + public List> getPositionsOfWallsOnBoard(WallType... walls) { List> combinedList = new ArrayList<>(); for (WallType wall : walls) { - combinedList.addAll(makeTileList(wall, this.walls)); + combinedList.addAll(GridUtil.getMatchingElements(wall, this.walls)); } return combinedList; } @@ -649,7 +645,7 @@ public class Board { if (tileRobotStepsOn == null) { throw new IllegalArgumentException("The game board is missing a tile. This should not happen."); } - TileType tileTypeRobotStepsOn = tileRobotStepsOn.getTileType(); + TileType tileTypeRobotStepsOn = tileRobotStepsOn.getType(); if (dangerousTiles.contains(tileTypeRobotStepsOn)) { killRobot(robot); } @@ -690,58 +686,6 @@ public class Board { } } - /** - * Gets all elements on a grid - * - * @param grid The grid to get elements from - * @param The type of the elements int the grid - * @return A list containing all the elements in the grid - */ - private List getAllElementsFromGrid(Grid grid) { - List elements = new ArrayList<>(); - for (int y = grid.getHeight() - 1; y >= 0; y--) { - for (int x = 0; x < grid.getWidth(); x++) { - elements.add(grid.getElement(x, y)); - } - } - return elements; - } - - /** - * Finds all tiles/walls with a certain type - * - * @param type The type of tile/wall to look for - * @param grid The grid to look through - * @param Type of the type to look for - * @param Type of the grid - * @return List of BoardElementContainers - */ - private List> makeTileList(K type, Grid grid) { - List> objList = new ArrayList<>(); - - for (int y = grid.getHeight() - 1; y >= 0; y--) { - for (int x = 0; x < grid.getWidth(); x++) { - T gridElement = grid.getElement(x, y); - if (gridElement != null) { - if (gridElement.getClass().isAssignableFrom(Tile.class)) { - Tile tile = (Tile) gridElement; - if (tile.getTileType() == type) { - objList.add(new BoardElementContainer<>(gridElement, new Position(x, y))); - } - } else if (gridElement.getClass().isAssignableFrom(Wall.class)) { - Wall wall = (Wall) gridElement; - if (wall.getWallType() == type) { - objList.add(new BoardElementContainer<>(gridElement, new Position(x, y))); - } - } else { - throw new IllegalArgumentException("ListGrid has unknown type."); - } - } - } - } - return objList; - } - /** * Kills all robots that have taken too much damage */ @@ -764,7 +708,7 @@ public class Board { List laserTargets = new ArrayList<>(); getLaserTarget(laserDirection, wallLaser.getPosition(), laserTargets); Position hitPosition = laserTargets.get(laserTargets.size() - 1); - WallType laserType = wallLaser.getElement().getWallType(); + WallType laserType = wallLaser.getElement().getType(); updateLaserDisplay(laserTargets, laserDirection, laserType); if (getRobotOnPosition(hitPosition) != null) { applyLaserDamage(laserType, robots.get(getRobotOnPosition(hitPosition))); @@ -844,20 +788,7 @@ public class Board { * @param laserType The type of the laser shooting */ private void updateLaserBeamOnParticleGrid(Position addPosition, Direction laserDirection, WallType laserType) { - ParticleType laserParticleType; - switch (laserType) { - case WALL_LASER_SINGLE: - laserParticleType = ParticleType.LASER_BEAM_SINGLE; - break; - case WALL_LASER_DOUBLE: - laserParticleType = ParticleType.LASER_BEAM_DOUBLE; - break; - case WALL_LASER_TRIPLE: - laserParticleType = ParticleType.LASER_BEAM_TRIPLE; - break; - default: - throw new IllegalArgumentException("Invalid laser type encountered."); - } + ParticleType laserParticleType = LaserHelper.getParticleFromLaserType(laserType); Particle laserParticle = new Particle(laserParticleType, laserDirection); int positionX = addPosition.getXCoordinate(); int positionY = addPosition.getYCoordinate(); @@ -865,105 +796,11 @@ public class Board { if (particleAtPosition == null) { particles.setElement(positionX, positionY, laserParticle); } else { - particles.setElement(positionX, positionY, getNewLaserBeamParticle(laserParticle, particleAtPosition)); + particles.setElement(positionX, positionY, + LaserHelper.getNewLaserBeamParticle(laserParticle, particleAtPosition)); } } - /** - * Gets the new particle to use given the laser firing and the existing beam particle - * - * @param laserBeam The laser beam which is fired - * @param existingBeam The laser beam which already exists at a tile - * @return The particle which is a combination of the two - */ - private Particle getNewLaserBeamParticle(Particle laserBeam, Particle existingBeam) { - ParticleType laserBeamType = laserBeam.getParticleType(); - ParticleType existingBeamType = existingBeam.getParticleType(); - Direction laserDirection = laserBeam.getDirection(); - Direction existingDirection = existingBeam.getDirection(); - - int forwardBeamsLaser = getNumberOfForwardBeams(laserBeamType); - int crossingBeamsLaser = getNumberOfPerpendicularBeams(laserBeamType); - int forwardBeamsExisting = getNumberOfForwardBeams(existingBeamType); - int crossingBeamsExisting = getNumberOfPerpendicularBeams(existingBeamType); - - //Flip number of beams if beams are perpendicular - if (Direction.arePerpendicular(laserDirection, existingDirection)) { - int temp = forwardBeamsExisting; - forwardBeamsExisting = crossingBeamsExisting; - crossingBeamsExisting = temp; - } - //Calculates the new number of forward beams - int forwardBeams = getNumberOfBeams(forwardBeamsLaser, forwardBeamsExisting); - //Calculates the new number of crossing beams - int crossingBeams = getNumberOfBeams(crossingBeamsLaser, crossingBeamsExisting); - //The direction should be the direction with the least amount of beams - Direction newDirection; - if (forwardBeams > crossingBeams) { - newDirection = existingDirection; - } else { - newDirection = laserDirection; - } - //If using the existing direction and the beams are perpendicular, the direction needs to be rotated - if (newDirection.equals(existingDirection) && - Direction.arePerpendicular(laserDirection, existingDirection)) { - newDirection = Direction.getLeftRotatedDirection(newDirection); - } - //If the particle does not exist, we have to rotate the beam to get the correct one - ParticleType newParticleType = getNewParticleType(forwardBeams, crossingBeams); - if (newParticleType == null) { - newParticleType = getNewParticleType(crossingBeams, forwardBeams); - newDirection = Direction.getLeftRotatedDirection(newDirection); - } - return new Particle(newParticleType, newDirection); - } - - /** - * Gets the correct number of beams given existing beams and the beams to add - * - * @param newBeams The beam count of the new beam to add - * @param existingBeams The beam count of the existing beam - * @return The new number/thickness of beams/the beam - */ - private int getNumberOfBeams(int newBeams, int existingBeams) { - if ((newBeams + existingBeams) != 0 && (newBeams + existingBeams) % 3 == 0) { - return 3; - } else { - return Math.max(newBeams, existingBeams); - } - } - - /** - * Gets a new particle type with the given number of beams - * - * @param forwardBeams The number of beams in the direction of the laser - * @param perpendicularBeams The number of beams in the perpendicular direction of the laser - * @return The correct particle type to be displayed - */ - private ParticleType getNewParticleType(int forwardBeams, int perpendicularBeams) { - return ParticleType.getParticleTypeFromID(10 * forwardBeams + perpendicularBeams); - } - - /** - * Gets the number of beams in the forward direction given a particle type - * - * @param particleType The type of particle to check - * @return The number of beams in the forward direction of the laser beam - */ - private int getNumberOfForwardBeams(ParticleType particleType) { - return particleType.getParticleTypeID() / 10; - } - - /** - * Gets the number of beams in the perpendicular direction given a particle type - * - * @param particleType The type of particle to check - * @return The number of beams in the perpendicular direction of the laser beam - */ - private int getNumberOfPerpendicularBeams(ParticleType particleType) { - return particleType.getParticleTypeID() % 10; - } - /** * Gets the int corresponding to the flag a robot has last visited * @@ -981,12 +818,9 @@ public class Board { * @param hasTouched If the robot has touched a flag this turn */ public void setHasTouchedFlagThisTurn(RobotID robotID, boolean hasTouched) { - Robot aliveRobot = robots.get(robotID); - Robot deadRobot = getRobotFromDeadRobots(robotID); - if (aliveRobot != null) { - aliveRobot.setHasTouchedFlagThisTurn(hasTouched); - } else if (deadRobot != null) { - deadRobot.setHasTouchedFlagThisTurn(hasTouched); + Robot robot = getRobot(robotID); + if (robot != null) { + robot.setHasTouchedFlagThisTurn(hasTouched); } } @@ -1000,4 +834,19 @@ public class Board { return robots.get(robotID).hasTouchedFlagThisTurn(); } + /** + * Gets the robot with the given robot id + * + * @param robotID The id of the robot to get + * @return The robot with the id or null if it's not in the game + */ + private Robot getRobot(RobotID robotID) { + Robot aliveRobot = robots.get(robotID); + if (aliveRobot == null) { + return getRobotFromDeadRobots(robotID); + } else { + return aliveRobot; + } + } + } \ No newline at end of file diff --git a/src/main/java/inf112/fiasko/roborally/objects/BoardElement.java b/src/main/java/inf112/fiasko/roborally/objects/BoardElement.java new file mode 100644 index 0000000..773bc08 --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/objects/BoardElement.java @@ -0,0 +1,26 @@ +package inf112.fiasko.roborally.objects; + +import inf112.fiasko.roborally.elementproperties.Direction; + +/** + * Represents an element on the board + * + * @param The type of the element + */ +public interface BoardElement { + + /** + * Gets the type of the element + * + * @return An enum value of type K + */ + K getType(); + + /** + * Gets the direction of the element + * + * @return The element's direction + */ + Direction getDirection(); + +} diff --git a/src/main/java/inf112/fiasko/roborally/objects/BoardElementContainer.java b/src/main/java/inf112/fiasko/roborally/objects/BoardElementContainer.java index f84a75d..13b4b86 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/BoardElementContainer.java +++ b/src/main/java/inf112/fiasko/roborally/objects/BoardElementContainer.java @@ -7,7 +7,7 @@ import inf112.fiasko.roborally.elementproperties.Position; * * @param The type of element */ -public class BoardElementContainer { +public class BoardElementContainer { private final K element; private final Position position; @@ -17,7 +17,7 @@ public class BoardElementContainer { * @param element The element * @param position The position */ - BoardElementContainer(K element, Position position) { + public BoardElementContainer(K element, Position position) { this.element = element; this.position = position; } @@ -32,9 +32,9 @@ public class BoardElementContainer { } /** - * Gets the position + * Gets the position of the element * - * @return The position + * @return The position of the element */ public Position getPosition() { return position; diff --git a/src/main/java/inf112/fiasko/roborally/objects/Particle.java b/src/main/java/inf112/fiasko/roborally/objects/Particle.java index a34737a..5e5dd12 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Particle.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Particle.java @@ -6,7 +6,7 @@ import inf112.fiasko.roborally.elementproperties.ParticleType; /** * This class represents a particle */ -public class Particle { +public class Particle implements BoardElement { private ParticleType particleType; private Direction direction; @@ -25,20 +25,12 @@ public class Particle { this.direction = direction; } - /** - * Gets the particle type of the particle - * - * @return The particle's particle type - */ - public ParticleType getParticleType() { + @Override + public ParticleType getType() { return particleType; } - /** - * Gets the direction of the particle - * - * @return The particle's direction - */ + @Override public Direction getDirection() { return direction; } diff --git a/src/main/java/inf112/fiasko/roborally/objects/Phase.java b/src/main/java/inf112/fiasko/roborally/objects/Phase.java index 2e5a4fc..78d0881 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Phase.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Phase.java @@ -109,7 +109,7 @@ public class Phase { if (gameBoard.hasTouchedFlagThisTurn(robotID)) { continue; } - gameBoard.updateFlagOnRobot(robotID, flag.getElement().getTileType()); + gameBoard.updateRobotFlag(robotID, flag.getElement().getType()); checkIfPlayerWon(robotID, flags.size()); } } @@ -165,7 +165,7 @@ public class Phase { continue; } RobotID robotAtCogwheel = gameBoard.getRobotOnPosition(cogwheel.getPosition()); - if (cogwheel.getElement().getTileType() == TileType.COGWHEEL_RIGHT) { + if (cogwheel.getElement().getType() == TileType.COGWHEEL_RIGHT) { gameBoard.rotateRobotRight(robotAtCogwheel); } else { gameBoard.rotateRobotLeft(robotAtCogwheel); @@ -355,23 +355,23 @@ public class Phase { * Generates lists containing board element containers with all tiles of certain types */ private void generateTileLists() { - cogwheels = gameBoard.getPositionsOfTileOnBoard(TileType.COGWHEEL_RIGHT, + cogwheels = gameBoard.getPositionsOfTilesOnBoard(TileType.COGWHEEL_RIGHT, TileType.COGWHEEL_LEFT); - fastConveyorBelts = gameBoard.getPositionsOfTileOnBoard(TileType.CONVEYOR_BELT_FAST, + fastConveyorBelts = gameBoard.getPositionsOfTilesOnBoard(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, + conveyorBelts.addAll(gameBoard.getPositionsOfTilesOnBoard(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)); - flags = gameBoard.getPositionsOfTileOnBoard(TileType.FLAG_1, + flags = gameBoard.getPositionsOfTilesOnBoard(TileType.FLAG_1, TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4); - oddPushers = gameBoard.getPositionsOfWallOnBoard(WallType.WALL_PUSHER_ODD); - evenPushers = gameBoard.getPositionsOfWallOnBoard(WallType.WALL_PUSHER_EVEN); + oddPushers = gameBoard.getPositionsOfWallsOnBoard(WallType.WALL_PUSHER_ODD); + evenPushers = gameBoard.getPositionsOfWallsOnBoard(WallType.WALL_PUSHER_EVEN); } } diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index 7eb2ea5..1eb95d5 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -228,7 +228,7 @@ public class RoboRallyGame implements DrawableGame, InteractableGame { gameBoard = BoardLoaderUtil.loadBoard("boards/" + boardName, robots); moveRobotsToSpawn(); - repairTiles = gameBoard.getPositionsOfTileOnBoard(TileType.FLAG_1, TileType.FLAG_2, TileType.FLAG_3, + repairTiles = gameBoard.getPositionsOfTilesOnBoard(TileType.FLAG_1, TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4, TileType.WRENCH, TileType.WRENCH_AND_HAMMER); if (host) { @@ -248,7 +248,7 @@ public class RoboRallyGame implements DrawableGame, InteractableGame { for (Player player : playerList) { RobotID robotID = player.getRobotID(); TileType robotSpawn = TileType.getTileTypeFromID(robotID.getRobotIDID() + 22); - List> spawnTileContainerList = gameBoard.getPositionsOfTileOnBoard(robotSpawn); + List> spawnTileContainerList = gameBoard.getPositionsOfTilesOnBoard(robotSpawn); if (spawnTileContainerList.size() < 1) { throw new IllegalArgumentException("The chosen board seems to be missing a robot spawn"); } diff --git a/src/main/java/inf112/fiasko/roborally/objects/Tile.java b/src/main/java/inf112/fiasko/roborally/objects/Tile.java index d8e9e77..3e6a166 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Tile.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Tile.java @@ -6,7 +6,7 @@ import inf112.fiasko.roborally.elementproperties.TileType; /** * This class represents a simple tile */ -public class Tile { +public class Tile implements BoardElement { private TileType tileType; private Direction direction; @@ -25,20 +25,12 @@ public class Tile { this.direction = direction; } - /** - * Gets the tile type of the tile - * - * @return The tile's tile type - */ - public TileType getTileType() { + @Override + public TileType getType() { return tileType; } - /** - * Gets the direction of the tile - * - * @return The tile's direction - */ + @Override public Direction getDirection() { return direction; } diff --git a/src/main/java/inf112/fiasko/roborally/objects/Wall.java b/src/main/java/inf112/fiasko/roborally/objects/Wall.java index 99f675c..9db667f 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Wall.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Wall.java @@ -6,7 +6,7 @@ import inf112.fiasko.roborally.elementproperties.WallType; /** * This class represents a wall */ -public class Wall { +public class Wall implements BoardElement { private final WallType wallType; private final Direction direction; @@ -24,20 +24,12 @@ public class Wall { this.direction = direction; } - /** - * Gets the type of the wall - * - * @return The wall type - */ - public WallType getWallType() { + @Override + public WallType getType() { return wallType; } - /** - * Gets the direction of the wall - * - * @return The direction of the wall - */ + @Override public Direction getDirection() { return direction; } diff --git a/src/main/java/inf112/fiasko/roborally/utility/GridUtil.java b/src/main/java/inf112/fiasko/roborally/utility/GridUtil.java new file mode 100644 index 0000000..e14f42f --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/utility/GridUtil.java @@ -0,0 +1,55 @@ +package inf112.fiasko.roborally.utility; + +import inf112.fiasko.roborally.elementproperties.Position; +import inf112.fiasko.roborally.objects.BoardElement; +import inf112.fiasko.roborally.objects.BoardElementContainer; +import inf112.fiasko.roborally.objects.Grid; + +import java.util.ArrayList; +import java.util.List; + +/** + * A helper class containing helper methods fro a grid + */ +public class GridUtil { + + /** + * Gets all elements in a grid + * + * @param grid The grid to get elements from + * @param The type of the elements int the grid + * @return A list containing all the elements in the grid + */ + public static List getAllElementsFromGrid(Grid grid) { + List elements = new ArrayList<>(); + for (int y = grid.getHeight() - 1; y >= 0; y--) { + for (int x = 0; x < grid.getWidth(); x++) { + elements.add(grid.getElement(x, y)); + } + } + return elements; + } + + /** + * Finds all tiles/walls with a certain type + * + * @param type The type of tile/wall to look for + * @param grid The grid to look through + * @param Type of the type to look for + * @param Type of the grid + * @return List of BoardElementContainers + */ + public static List> getMatchingElements(K type, Grid grid) { + List> containerList = new ArrayList<>(); + for (int y = grid.getHeight() - 1; y >= 0; y--) { + for (int x = 0; x < grid.getWidth(); x++) { + T gridElement = grid.getElement(x, y); + if (gridElement != null && gridElement.getType() == type) { + containerList.add(new BoardElementContainer<>(gridElement, new Position(x, y))); + } + } + } + return containerList; + } + +} diff --git a/src/main/java/inf112/fiasko/roborally/utility/LaserHelper.java b/src/main/java/inf112/fiasko/roborally/utility/LaserHelper.java new file mode 100644 index 0000000..31ec828 --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/utility/LaserHelper.java @@ -0,0 +1,127 @@ +package inf112.fiasko.roborally.utility; + +import inf112.fiasko.roborally.elementproperties.Direction; +import inf112.fiasko.roborally.elementproperties.ParticleType; +import inf112.fiasko.roborally.elementproperties.WallType; +import inf112.fiasko.roborally.objects.Particle; + +/** + * Helps with displaying laser beams + */ +public class LaserHelper { + + /** + * Gets the correct particle type from a laser type + * + * @param laserType The type of laser firing + * @return The particle representing the laser's beam + */ + public static ParticleType getParticleFromLaserType(WallType laserType) { + switch (laserType) { + case WALL_LASER_SINGLE: + return ParticleType.LASER_BEAM_SINGLE; + case WALL_LASER_DOUBLE: + return ParticleType.LASER_BEAM_DOUBLE; + case WALL_LASER_TRIPLE: + return ParticleType.LASER_BEAM_TRIPLE; + default: + throw new IllegalArgumentException("Invalid laser type encountered."); + } + } + + /** + * Gets the new particle to use given the laser firing and the existing beam particle + * + * @param laserBeam The laser beam which is fired + * @param existingBeam The laser beam which already exists at a tile + * @return The particle which is a combination of the two + */ + public static Particle getNewLaserBeamParticle(Particle laserBeam, Particle existingBeam) { + ParticleType laserBeamType = laserBeam.getType(); + ParticleType existingBeamType = existingBeam.getType(); + Direction laserDirection = laserBeam.getDirection(); + Direction existingDirection = existingBeam.getDirection(); + + int forwardBeamsLaser = getNumberOfForwardBeams(laserBeamType); + int crossingBeamsLaser = getNumberOfPerpendicularBeams(laserBeamType); + int forwardBeamsExisting = getNumberOfForwardBeams(existingBeamType); + int crossingBeamsExisting = getNumberOfPerpendicularBeams(existingBeamType); + + //Flip number of beams if beams are perpendicular + if (Direction.arePerpendicular(laserDirection, existingDirection)) { + int temp = forwardBeamsExisting; + forwardBeamsExisting = crossingBeamsExisting; + crossingBeamsExisting = temp; + } + //Calculates the new number of forward beams + int forwardBeams = getNumberOfBeams(forwardBeamsLaser, forwardBeamsExisting); + //Calculates the new number of crossing beams + int crossingBeams = getNumberOfBeams(crossingBeamsLaser, crossingBeamsExisting); + //The direction should be the direction with the least amount of beams + Direction newDirection; + if (forwardBeams > crossingBeams) { + newDirection = existingDirection; + } else { + newDirection = laserDirection; + } + //If using the existing direction and the beams are perpendicular, the direction needs to be rotated + if (newDirection.equals(existingDirection) && + Direction.arePerpendicular(laserDirection, existingDirection)) { + newDirection = Direction.getLeftRotatedDirection(newDirection); + } + //If the particle does not exist, we have to rotate the beam to get the correct one + ParticleType newParticleType = getNewParticleType(forwardBeams, crossingBeams); + if (newParticleType == null) { + newParticleType = getNewParticleType(crossingBeams, forwardBeams); + newDirection = Direction.getLeftRotatedDirection(newDirection); + } + return new Particle(newParticleType, newDirection); + } + + /** + * Gets the correct number of beams given existing beams and the beams to add + * + * @param newBeams The beam count of the new beam to add + * @param existingBeams The beam count of the existing beam + * @return The new number/thickness of beams/the beam + */ + private static int getNumberOfBeams(int newBeams, int existingBeams) { + if ((newBeams + existingBeams) != 0 && (newBeams + existingBeams) % 3 == 0) { + return 3; + } else { + return Math.max(newBeams, existingBeams); + } + } + + /** + * Gets a new particle type with the given number of beams + * + * @param forwardBeams The number of beams in the direction of the laser + * @param perpendicularBeams The number of beams in the perpendicular direction of the laser + * @return The correct particle type to be displayed + */ + private static ParticleType getNewParticleType(int forwardBeams, int perpendicularBeams) { + return ParticleType.getParticleTypeFromID(10 * forwardBeams + perpendicularBeams); + } + + /** + * Gets the number of beams in the forward direction given a particle type + * + * @param particleType The type of particle to check + * @return The number of beams in the forward direction of the laser beam + */ + private static int getNumberOfForwardBeams(ParticleType particleType) { + return particleType.getParticleTypeID() / 10; + } + + /** + * Gets the number of beams in the perpendicular direction given a particle type + * + * @param particleType The type of particle to check + * @return The number of beams in the perpendicular direction of the laser beam + */ + private static int getNumberOfPerpendicularBeams(ParticleType particleType) { + return particleType.getParticleTypeID() % 10; + } + +} diff --git a/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java b/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java index 876eba0..fd0b269 100644 --- a/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java +++ b/src/main/java/inf112/fiasko/roborally/utility/TextureConverterUtil.java @@ -124,7 +124,7 @@ public final class TextureConverterUtil { } } Direction direction = tile.getDirection(); - TextureConverterContainer converterContainer = tileSheetTileTextureMappings.get(tile.getTileType()); + TextureConverterContainer converterContainer = tileSheetTileTextureMappings.get(tile.getType()); if (converterContainer != null) { return getDirectionalTextureRegion(direction, converterContainer.getXNorth(), converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(), @@ -149,7 +149,7 @@ public final class TextureConverterUtil { } } Direction direction = particle.getDirection(); - TextureConverterContainer converterContainer = tileSheetParticleTextureMappings.get(particle.getParticleType()); + TextureConverterContainer converterContainer = tileSheetParticleTextureMappings.get(particle.getType()); if (converterContainer != null) { return getDirectionalTextureRegion(direction, converterContainer.getXNorth(), converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(), @@ -177,7 +177,7 @@ public final class TextureConverterUtil { return null; } Direction direction = wall.getDirection(); - TextureConverterContainer converterContainer = tileSheetWallTextureMappings.get(wall.getWallType()); + TextureConverterContainer converterContainer = tileSheetWallTextureMappings.get(wall.getType()); if (converterContainer != null) { return getDirectionalTextureRegion(direction, converterContainer.getXNorth(), converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(), @@ -242,7 +242,7 @@ public final class TextureConverterUtil { e.printStackTrace(); } } - return tileSheetTileHasRotatedTextureMappings.get(tile.getTileType()); + return tileSheetTileHasRotatedTextureMappings.get(tile.getType()); } /** @@ -261,7 +261,7 @@ public final class TextureConverterUtil { e.printStackTrace(); } } - return tileSheetWallHasRotatedTextureMappings.get(wall.getWallType()); + return tileSheetWallHasRotatedTextureMappings.get(wall.getType()); } /** @@ -280,7 +280,7 @@ public final class TextureConverterUtil { e.printStackTrace(); } } - return tileSheetParticleHasRotatedTextureMappings.get(particle.getParticleType()); + return tileSheetParticleHasRotatedTextureMappings.get(particle.getType()); } /** diff --git a/src/test/java/inf112/fiasko/roborally/objects/BoardTest.java b/src/test/java/inf112/fiasko/roborally/objects/BoardTest.java index 1b1f0f3..0fd305e 100644 --- a/src/test/java/inf112/fiasko/roborally/objects/BoardTest.java +++ b/src/test/java/inf112/fiasko/roborally/objects/BoardTest.java @@ -234,7 +234,7 @@ public class BoardTest { public void flagGetsUpdatedOnRobotWithCorrectLastVisitedFlag() { Robot testRobot = robotList.get(6); assertEquals(0, testRobot.getLastFlagVisited()); - board.updateFlagOnRobot(RobotID.ROBOT_7, TileType.FLAG_1); + board.updateRobotFlag(RobotID.ROBOT_7, TileType.FLAG_1); assertEquals(1, testRobot.getLastFlagVisited()); } @@ -242,7 +242,7 @@ public class BoardTest { public void flagDoesNotUpdatedOnRobotWithWrongLastVisitedFlag() { Robot testRobot = robotList.get(6); assertEquals(0, testRobot.getLastFlagVisited()); - board.updateFlagOnRobot(RobotID.ROBOT_7, TileType.FLAG_2); + board.updateRobotFlag(RobotID.ROBOT_7, TileType.FLAG_2); assertEquals(0, testRobot.getLastFlagVisited()); } @@ -347,74 +347,74 @@ public class BoardTest { @Test public void getPositionsOfTileOnBoardGivesCorrectAmountOfCogwheelLeftTiles() { assertEquals((int) tileTypeNumberMap.get(TileType.COGWHEEL_LEFT), - boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT).size()); + boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.COGWHEEL_LEFT).size()); } @Test public void getPositionsOfTileOnBoardHasTypeCogwheelLeft() { - List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT); + List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.COGWHEEL_LEFT); assertTrue(checkIfAllElementsAreOfSpecificTileType(boardElemList, TileType.COGWHEEL_LEFT)); } @Test public void getPositionsOfTileOnBoardGivesCorrectAmountOfTileTiles() { assertEquals((int) tileTypeNumberMap.get(TileType.TILE), - boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.TILE).size()); + boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.TILE).size()); } @Test public void getPositionsOfTileOnBoardHasTypeTile() { - List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.TILE); + List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.TILE); assertTrue(checkIfAllElementsAreOfSpecificTileType(boardElemList, TileType.TILE)); } @Test public void getPositionsOfWallOnBoardGivesCorrectAmountOfWallNormalWalls() { assertEquals((int) wallTypeNumberMap.get(WallType.WALL_NORMAL), - boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_NORMAL).size()); + boardWithDifferentAmountOfAllTypes.getPositionsOfWallsOnBoard(WallType.WALL_NORMAL).size()); } @Test public void getPositionsOfWallOnBoardHasTypeWallNormal() { - List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_NORMAL); + List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallsOnBoard(WallType.WALL_NORMAL); assertTrue(checkIfAllElementsAreOfSpecificWallType(boardElemList, WallType.WALL_NORMAL)); } @Test public void getPositionsOfWallOnBoardGivesCorrectAmountOfWallCornerWalls() { assertEquals((int) wallTypeNumberMap.get(WallType.WALL_CORNER), - boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER).size()); + boardWithDifferentAmountOfAllTypes.getPositionsOfWallsOnBoard(WallType.WALL_CORNER).size()); } @Test public void getPositionsOfWallOnBoardHasTypeWallCorner() { - List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER); + List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallsOnBoard(WallType.WALL_CORNER); assertTrue(checkIfAllElementsAreOfSpecificWallType(boardElemList, WallType.WALL_CORNER)); } @Test public void getPositionsOfWallOnBoardHasCorrectTypesWithMultipleParameters() { - List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT, TileType.COGWHEEL_RIGHT); + List> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.COGWHEEL_LEFT, TileType.COGWHEEL_RIGHT); List tileTypeList = new ArrayList<>(); List tileTypeListResult = new ArrayList<>(); tileTypeList.add(TileType.COGWHEEL_LEFT); tileTypeList.add(TileType.COGWHEEL_RIGHT); for (BoardElementContainer elem : boardElemList) { - tileTypeListResult.add(elem.getElement().getTileType()); + tileTypeListResult.add(elem.getElement().getType()); } assertTrue(tileTypeList.containsAll(tileTypeListResult) && tileTypeListResult.containsAll(tileTypeList)); } private boolean checkIfAllElementsAreOfSpecificWallType(List> elemList, K WallType) { - Predicate> pred = (element) -> element.getElement().getWallType() == WallType; + Predicate> pred = (element) -> element.getElement().getType() == WallType; elemList.removeIf(pred); return 0 == elemList.size(); } private boolean checkIfAllElementsAreOfSpecificTileType(List> elemList, K TileType) { - Predicate> pred = (element) -> element.getElement().getTileType() == TileType; + Predicate> pred = (element) -> element.getElement().getType() == TileType; elemList.removeIf(pred); return 0 == elemList.size(); } diff --git a/src/test/java/inf112/fiasko/roborally/objects/ParticleTest.java b/src/test/java/inf112/fiasko/roborally/objects/ParticleTest.java index a690ca2..745f180 100644 --- a/src/test/java/inf112/fiasko/roborally/objects/ParticleTest.java +++ b/src/test/java/inf112/fiasko/roborally/objects/ParticleTest.java @@ -20,12 +20,12 @@ public class ParticleTest { @Test public void getParticleTypeFromParticle() { - assertEquals(ParticleType.LASER_BEAM_SINGLE, particle.getParticleType()); + assertEquals(ParticleType.LASER_BEAM_SINGLE, particle.getType()); } @Test public void getParticleTypeFromParticle2() { - assertEquals(ParticleType.LASER_BEAM_DOUBLE_CROSS, particle2.getParticleType()); + assertEquals(ParticleType.LASER_BEAM_DOUBLE_CROSS, particle2.getType()); } diff --git a/src/test/java/inf112/fiasko/roborally/objects/TileTest.java b/src/test/java/inf112/fiasko/roborally/objects/TileTest.java index 990e7bd..cbff92c 100644 --- a/src/test/java/inf112/fiasko/roborally/objects/TileTest.java +++ b/src/test/java/inf112/fiasko/roborally/objects/TileTest.java @@ -20,12 +20,12 @@ public class TileTest { @Test public void getTileTypeFromTile() { - assertEquals(TileType.HOLE, tile.getTileType()); + assertEquals(TileType.HOLE, tile.getType()); } @Test public void getTileTypeFromTile2() { - assertEquals(TileType.COGWHEEL_RIGHT, tile2.getTileType()); + assertEquals(TileType.COGWHEEL_RIGHT, tile2.getType()); } diff --git a/src/test/java/inf112/fiasko/roborally/objects/WallTest.java b/src/test/java/inf112/fiasko/roborally/objects/WallTest.java index ae367e9..093332f 100644 --- a/src/test/java/inf112/fiasko/roborally/objects/WallTest.java +++ b/src/test/java/inf112/fiasko/roborally/objects/WallTest.java @@ -10,19 +10,19 @@ public class WallTest { @Test public void testWallGetWallTypeNormal() { Wall testGetWall = new Wall(WallType.WALL_NORMAL, Direction.NORTH); - assertEquals(WallType.WALL_NORMAL, testGetWall.getWallType()); + assertEquals(WallType.WALL_NORMAL, testGetWall.getType()); } @Test public void testWallGetWallTypeCorner() { Wall testGetWall = new Wall(WallType.WALL_CORNER, Direction.NORTH); - assertEquals(WallType.WALL_CORNER, testGetWall.getWallType()); + assertEquals(WallType.WALL_CORNER, testGetWall.getType()); } @Test public void testWallGetWallTypeLaserSingle() { Wall testGetWall = new Wall(WallType.WALL_LASER_SINGLE, Direction.NORTH); - assertEquals(WallType.WALL_LASER_SINGLE, testGetWall.getWallType()); + assertEquals(WallType.WALL_LASER_SINGLE, testGetWall.getType()); } @Test