2020-03-02 18:46:45 +01:00
|
|
|
package inf112.fiasko.roborally.objects;
|
2020-02-04 17:52:17 +01:00
|
|
|
|
2020-03-10 17:29:36 +01:00
|
|
|
import inf112.fiasko.roborally.element_properties.Action;
|
2020-02-24 18:07:26 +01:00
|
|
|
import inf112.fiasko.roborally.element_properties.Position;
|
|
|
|
import inf112.fiasko.roborally.element_properties.RobotID;
|
2020-02-22 23:36:01 +01:00
|
|
|
import inf112.fiasko.roborally.utility.BoardLoaderUtil;
|
2020-01-31 13:53:08 +01:00
|
|
|
|
2020-02-22 23:36:01 +01:00
|
|
|
import java.io.IOException;
|
2020-02-03 14:10:52 +01:00
|
|
|
import java.util.ArrayList;
|
2020-01-31 13:53:08 +01:00
|
|
|
import java.util.List;
|
2020-02-24 18:07:26 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2020-01-31 13:53:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represent a game which is drawable using libgdx
|
|
|
|
*/
|
2020-03-02 18:46:45 +01:00
|
|
|
public class RoboRallyGame implements IDrawableGame {
|
2020-02-22 23:36:01 +01:00
|
|
|
private Board gameBoard;
|
|
|
|
|
2020-03-02 18:46:45 +01:00
|
|
|
public RoboRallyGame(boolean debug) {
|
2020-02-27 16:44:06 +01:00
|
|
|
if (debug) {
|
|
|
|
initializeDebugMode();
|
|
|
|
} else {
|
|
|
|
initializeGame();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 18:46:45 +01:00
|
|
|
public RoboRallyGame() {
|
2020-02-27 16:44:06 +01:00
|
|
|
initializeGame();
|
|
|
|
}
|
|
|
|
|
2020-02-28 19:46:40 +01:00
|
|
|
@Override
|
|
|
|
public int getWidth() {
|
|
|
|
return gameBoard.getBoardWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getHeight() {
|
|
|
|
return gameBoard.getBoardHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Tile> getTilesToDraw() {
|
|
|
|
return gameBoard.getTiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Wall> getWallsToDraw() {
|
|
|
|
return gameBoard.getWalls();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Robot> getRobotsToDraw() {
|
|
|
|
return gameBoard.getAliveRobots();
|
|
|
|
}
|
|
|
|
|
2020-03-10 17:29:36 +01:00
|
|
|
/**
|
|
|
|
* Makes the game thread wait a given time amount before continuing.
|
|
|
|
* @throws InterruptedException If interrupted while trying to sleep.
|
|
|
|
*/
|
|
|
|
private void sleep() throws InterruptedException {
|
|
|
|
long cycleDelay = 600;
|
|
|
|
TimeUnit.MILLISECONDS.sleep(cycleDelay);
|
|
|
|
}
|
2020-02-28 19:46:40 +01:00
|
|
|
/**
|
|
|
|
* Initializes the game with a debugging board
|
|
|
|
*/
|
2020-02-27 16:44:06 +01:00
|
|
|
private void initializeDebugMode() {
|
|
|
|
List<Robot> robots = new ArrayList<>();
|
2020-03-02 12:18:01 +01:00
|
|
|
robots.add(new Robot(RobotID.ROBOT_1, new Position(0, 16)));
|
|
|
|
robots.add(new Robot(RobotID.ROBOT_2, new Position(1, 16)));
|
|
|
|
robots.add(new Robot(RobotID.ROBOT_3, new Position(2, 16)));
|
2020-02-27 16:44:06 +01:00
|
|
|
try {
|
|
|
|
gameBoard = BoardLoaderUtil.loadBoard("boards/all_tiles_test_board.txt", robots);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 19:46:40 +01:00
|
|
|
/**
|
|
|
|
* Initializes the game with a playable board
|
|
|
|
*/
|
2020-02-27 16:44:06 +01:00
|
|
|
private void initializeGame() {
|
2020-02-22 23:36:01 +01:00
|
|
|
try {
|
|
|
|
List<Robot> robots = new ArrayList<>();
|
2020-02-24 18:07:26 +01:00
|
|
|
robots.add(new Robot(RobotID.ROBOT_1, new Position(1, 1)));
|
2020-02-24 23:35:58 +01:00
|
|
|
robots.add(new Robot(RobotID.ROBOT_2, new Position(1, 2)));
|
2020-02-26 19:45:39 +01:00
|
|
|
robots.add(new Robot(RobotID.ROBOT_3, new Position(1, 3)));
|
2020-02-22 23:36:01 +01:00
|
|
|
gameBoard = BoardLoaderUtil.loadBoard("boards/Checkmate.txt", robots);
|
2020-02-24 18:07:26 +01:00
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
runGameLoop();
|
|
|
|
} catch (InterruptedException e) {
|
2020-02-26 08:10:46 +01:00
|
|
|
Thread.currentThread().interrupt();
|
2020-02-24 18:07:26 +01:00
|
|
|
}
|
|
|
|
}).start();
|
2020-02-22 23:36:01 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 13:53:08 +01:00
|
|
|
|
2020-02-24 22:27:19 +01:00
|
|
|
/**
|
|
|
|
* Does whatever the game wants to do
|
|
|
|
* @throws InterruptedException If interrupted while trying to sleep
|
|
|
|
*/
|
2020-02-24 18:07:26 +01:00
|
|
|
private void runGameLoop() throws InterruptedException {
|
2020-02-24 22:27:19 +01:00
|
|
|
TimeUnit.SECONDS.sleep(3);
|
2020-03-10 17:29:36 +01:00
|
|
|
makeMove(RobotID.ROBOT_1, Action.MOVE_1);
|
|
|
|
makeMove(RobotID.ROBOT_1, Action.MOVE_2);
|
|
|
|
makeMove(RobotID.ROBOT_1, Action.BACK_UP);
|
|
|
|
makeMove(RobotID.ROBOT_1, Action.BACK_UP);
|
|
|
|
makeMove(RobotID.ROBOT_1, Action.MOVE_3);
|
|
|
|
makeMove(RobotID.ROBOT_1, Action.ROTATE_LEFT);
|
|
|
|
makeMove(RobotID.ROBOT_1, Action.U_TURN);
|
|
|
|
makeMove(RobotID.ROBOT_1, Action.ROTATE_RIGHT);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.MOVE_3);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.MOVE_3);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.U_TURN);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.MOVE_3);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT);
|
|
|
|
makeMove(RobotID.ROBOT_2, Action.U_TURN);
|
2020-03-10 18:18:32 +01:00
|
|
|
makeMove(RobotID.ROBOT_2, Action.MOVE_1);
|
2020-03-10 17:29:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the given robot move according to to the action input.
|
|
|
|
* @param robotID The ID of the robot to move.
|
|
|
|
* @param action The specific movement the robot is to take.
|
|
|
|
* @throws InterruptedException If interrupted wile trying to sleep.
|
|
|
|
*/
|
|
|
|
private void makeMove(RobotID robotID, Action action) throws InterruptedException {
|
|
|
|
if (!gameBoard.isRobotAlive(robotID)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sleep();
|
|
|
|
switch (action) {
|
|
|
|
case MOVE_1:
|
|
|
|
moveForward(robotID);
|
|
|
|
break;
|
|
|
|
case MOVE_2:
|
|
|
|
moveForward(robotID);
|
|
|
|
moveForward(robotID);
|
|
|
|
break;
|
|
|
|
case MOVE_3:
|
|
|
|
moveForward(robotID);
|
|
|
|
moveForward(robotID);
|
|
|
|
moveForward(robotID);
|
|
|
|
break;
|
|
|
|
case ROTATE_RIGHT:
|
|
|
|
gameBoard.rotateRobotRight(robotID);
|
|
|
|
break;
|
|
|
|
case ROTATE_LEFT:
|
|
|
|
gameBoard.rotateRobotLeft(robotID);
|
|
|
|
break;
|
|
|
|
case U_TURN:
|
|
|
|
gameBoard.rotateRobotLeft(robotID);
|
|
|
|
gameBoard.rotateRobotLeft(robotID);
|
|
|
|
break;
|
|
|
|
case BACK_UP:
|
|
|
|
gameBoard.reverseRobot(robotID);
|
|
|
|
break;
|
2020-03-10 18:26:16 +01:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Not a recognized action.");
|
2020-03-10 17:29:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for makeMove. Takes care of movement forward of given robot.
|
|
|
|
* @param robotID ID of the given robot.
|
|
|
|
* @throws InterruptedException If interrupted wile sleeping.
|
|
|
|
*/
|
|
|
|
private void moveForward(RobotID robotID) throws InterruptedException {
|
|
|
|
if (!gameBoard.isRobotAlive(robotID)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sleep();
|
|
|
|
gameBoard.moveRobotForward(robotID);
|
2020-02-24 18:07:26 +01:00
|
|
|
}
|
2020-01-31 13:53:08 +01:00
|
|
|
}
|