Fikser retningsproblemer på brettet Closes #19

Fikser retning når en leser fra Grid
Fikser retning når en laster inn tiles og vegger via BoardLoaderUtil til et grid
Fikser posisjonering av tegnbare objekter
This commit is contained in:
Kristian Knarvik 2020-02-24 22:27:19 +01:00
parent f731ebe5cf
commit 97c9059901
7 changed files with 74 additions and 52 deletions

View File

@ -55,9 +55,6 @@ public class GameLauncher extends ApplicationAdapter {
List<IDrawableObject> elementsToDraw = IOUtil.getDrawableObjectsFromGame(game, tileDimensions, tileDimensions);
for (IDrawableObject object : elementsToDraw) {
TextureRegion objectTextureRegion = object.getTexture();
/*System.out.println(object.getTexture() + " " + object.getXPosition() + " " + object.getYPosition() + " " + object.getWidth() + " " +
object.getHeight() + " " + object.getRotation() + " " + objectTextureRegion.getRegionX() + " " +
objectTextureRegion.getRegionY() + " " + objectTextureRegion.getRegionWidth() + " " + objectTextureRegion.getRegionHeight());*/
batch.draw(objectTextureRegion.getTexture(), object.getXPosition(), object.getYPosition(),
(float)object.getWidth()/2, (float)object.getHeight()/2,
object.getWidth(), object.getHeight(), 1, 1, object.getRotation(),

View File

@ -37,17 +37,26 @@ public class Game implements IDrawableGame {
}
}
/**
* Does whatever the game wants to do
* @throws InterruptedException If interrupted while trying to sleep
*/
private void runGameLoop() throws InterruptedException {
TimeUnit.SECONDS.sleep(10);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.NORTH);
TimeUnit.SECONDS.sleep(3);
gameBoard.rotateRobotRight(RobotID.ROBOT_1);
gameBoard.rotateRobotRight(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.SOUTH);
TimeUnit.SECONDS.sleep(1);
gameBoard.rotateRobotLeft(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.EAST);
TimeUnit.SECONDS.sleep(1);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.NORTH);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.EAST);
TimeUnit.SECONDS.sleep(1);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.WEST);
gameBoard.rotateRobotRight(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.WEST);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.SOUTH);
}
@Override

View File

@ -267,12 +267,11 @@ public class Board {
* @return The new position of the element
*/
private Position getNewPosition(Position oldPosition, Direction direction) {
//TODO: Make sure we're accounting for the flipped y axis in libgdx
switch (direction) {
case NORTH:
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() + 1);
case SOUTH:
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() - 1);
case SOUTH:
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() + 1);
case EAST:
return new Position(oldPosition.getXCoordinate() + 1, oldPosition.getYCoordinate());
case WEST:
@ -290,8 +289,8 @@ public class Board {
*/
private <K> List<K> getAllElementsFromGrid(IGrid<K> grid) {
List<K> elements = new ArrayList<>();
for (int x = grid.getWidth() - 1; x >= 0; x--) {
for (int y = 0; y < grid.getHeight(); y++) {
for (int y = grid.getHeight() - 1; y >= 0; y--) {
for (int x = 0; x < grid.getWidth(); x++) {
elements.add(grid.getElement(x, y));
}
}

View File

@ -49,17 +49,17 @@ public final class BoardLoaderUtil {
*/
private static IGrid<Tile> loadTileGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException {
IGrid<Tile> tileGrid = new Grid<>(gridWidth, gridHeight);
for (int i = 0; i < gridHeight; i++) {
for (int y = 0; y < gridHeight; y++) {
String gridLine = reader.readLine();
String[] tilesOnLine = gridLine.split(" ");
for (int j = 0; j < gridWidth; j++) {
String[] tileData = tilesOnLine[j].split(";");
for (int x = 0; x < gridWidth; x++) {
String[] tileData = tilesOnLine[x].split(";");
TileType tileType = TileType.getTileTypeFromID(Integer.parseInt(tileData[0]));
Direction direction = Direction.getDirectionFromID(Integer.parseInt(tileData[1]));
if (direction == null) {
throw new IllegalArgumentException("Invalid direction for tile encountered when loading board file.");
}
tileGrid.setElement(i, j, new Tile(tileType, direction));
tileGrid.setElement(x, y, new Tile(tileType, direction));
}
}
return tileGrid;
@ -75,20 +75,20 @@ public final class BoardLoaderUtil {
*/
private static IGrid<Wall> loadWallGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException {
IGrid<Wall> wallGrid = new Grid<>(gridWidth, gridHeight);
for (int i = 0; i < gridHeight; i++) {
for (int y = 0; y < gridHeight; y++) {
String gridLine = reader.readLine();
String[] wallsOnLine = gridLine.split(" ");
for (int j = 0; j < gridWidth; j++) {
if (wallsOnLine[j].equals("0")) {
for (int x = 0; x < gridWidth; x++) {
if (wallsOnLine[x].equals("0")) {
continue;
}
String[] wallData = wallsOnLine[j].split(";");
String[] wallData = wallsOnLine[x].split(";");
WallType wallType = WallType.getWallTypeFromID(Integer.parseInt(wallData[0]));
Direction direction = Direction.getDirectionFromID(Integer.parseInt(wallData[1]));
if (direction == null) {
throw new IllegalArgumentException("Invalid direction for tile encountered when loading board file.");
}
wallGrid.setElement(i, j, new Wall(wallType, direction));
wallGrid.setElement(x, y, new Wall(wallType, direction));
}
}
return wallGrid;

View File

@ -30,13 +30,21 @@ public class IOUtil {
List<Robot> robotsToDraw = game.getRobotsToDraw();
int gameWidth = game.getWidth();
int gameHeight = game.getHeight();
drawableObjects.addAll(getDrawableObjectsFromElementList(tilesToDraw, gameWidth, gameHeight, tileWidth, tileHeight));
drawableObjects.addAll(getDrawableObjectsFromElementList(wallsToDraw, gameWidth, gameHeight, tileWidth, tileHeight));
drawableObjects.addAll(getDrawableRobots(robotsToDraw, gameWidth, gameHeight, tileWidth, tileHeight));
drawableObjects.addAll(getDrawableObjectsFromElementList(tilesToDraw, gameWidth, tileWidth, tileHeight));
drawableObjects.addAll(getDrawableObjectsFromElementList(wallsToDraw, gameWidth, tileWidth, tileHeight));
drawableObjects.addAll(getDrawableRobots(robotsToDraw, gameHeight, tileWidth, tileHeight));
return drawableObjects;
}
private static List<IDrawableObject> getDrawableRobots(List<Robot> robots, int gameWidth, int gameHeight, int tileWidth, int tileHeight) {
/**
* Gets a list of all drawable robots on the board
* @param robots A list of robots to draw
* @param gameHeight The height of the game
* @param tileWidth The width of a tile
* @param tileHeight The height of a tile
* @return A list of drawable robots
*/
private static List<IDrawableObject> getDrawableRobots(List<Robot> robots, int gameHeight, int tileWidth, int tileHeight) {
List<IDrawableObject> drawableObjects = new ArrayList<>();
for (Robot robot : robots) {
TextureRegion region = TextureConverterUtil.convertElement(robot);
@ -53,41 +61,48 @@ public class IOUtil {
* Gets a list of drawable objects with correct positions from a list of elements
* @param elementsToDraw A list of elements to draw
* @param gameWidth The width of the game board in tiles
* @param gameHeight The height of the game board in tiles
* @param tileWidth The width of a tile
* @param tileHeight The height of a tile
* @param <K> Should be type Robot, Tile or Wall
* @return A list of drawable objects
*/
private static <K> List<IDrawableObject> getDrawableObjectsFromElementList(List<K> elementsToDraw, int gameWidth,
int gameHeight, int tileWidth,
private static <K> List<IDrawableObject> getDrawableObjectsFromElementList(List<K> elementsToDraw, int gameWidth, int tileWidth,
int tileHeight) {
List<IDrawableObject> drawableObjects = new ArrayList<>();
for (int j = 0; j < gameHeight; j++) {
for (int i = j * gameWidth; i < (j + 1) * gameWidth; i++) {
K currentElement = elementsToDraw.get(i);
if (currentElement == null) {
continue;
}
TextureRegion region;
if (currentElement.getClass().isAssignableFrom(Tile.class)) {
Tile tile = (Tile) currentElement;
region = TextureConverterUtil.convertElement(tile);
} else if (currentElement.getClass().isAssignableFrom(Wall.class)) {
Wall wall = (Wall) currentElement;
region = TextureConverterUtil.convertElement(wall);
} else {
throw new IllegalArgumentException("Unknown element type passed to function.");
}
int rotation = getElementRotation(currentElement);
IDrawableObject drawableObject = new DrawableObject(region,
(i % gameWidth) * tileWidth, j * tileHeight, tileWidth, tileHeight, rotation);
drawableObjects.add(drawableObject);
int y = 0;
for (int i = 0; i < elementsToDraw.size(); i++) {
K currentElement = elementsToDraw.get(i);
int x = i % gameWidth;
if (i > 0 && i % gameWidth == 0) {
y++;
}
if (currentElement == null) {
continue;
}
TextureRegion region;
if (currentElement.getClass().isAssignableFrom(Tile.class)) {
Tile tile = (Tile) currentElement;
region = TextureConverterUtil.convertElement(tile);
} else if (currentElement.getClass().isAssignableFrom(Wall.class)) {
Wall wall = (Wall) currentElement;
region = TextureConverterUtil.convertElement(wall);
} else {
throw new IllegalArgumentException("Unknown element type passed to function.");
}
int rotation = getElementRotation(currentElement);
IDrawableObject drawableObject = new DrawableObject(region,
x * tileWidth, y * tileHeight, tileWidth, tileHeight, rotation);
drawableObjects.add(drawableObject);
}
return drawableObjects;
}
/**
* Gets the amount of degrees to rotate an element
* @param element The element to rotate
* @param <K> Should be of type Tile, Robot or Wall
* @return The amount of degrees the tile should be rotated to be properly displayed
*/
private static <K> int getElementRotation(K element) {
boolean hasRotatedTexture;
Direction direction;

View File

@ -1,4 +1,4 @@
12 12
12 13
01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 21;1
01;1 12;3 11;3 11;3 11;3 11;3 11;3 11;3 11;3 11;3 12;5 01;1
01;1 11;1 05;3 01;1 05;3 01;1 05;7 17;1 05;7 01;1 11;5 01;1
@ -11,6 +11,7 @@
01;1 11;1 01;1 05;3 01;1 05;3 01;1 05;7 01;1 05;7 11;5 01;1
01;1 12;1 11;7 11;7 11;7 11;7 11;7 11;7 11;7 11;7 12;7 01;1
21;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1
01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1
0 0 1;1 0 1;1 0 0 1;1 0 1;1 0 0
0 0 0 1;5 0 1;5 1;5 0 1;5 0 0 0
1;7 0 0 0 0 0 0 0 0 0 0 1;3
@ -22,4 +23,5 @@
0 1;3 0 0 0 0 0 0 0 0 1;7 0
1;7 0 0 0 0 0 0 0 0 0 0 1;3
0 0 0 1;1 0 1;1 1;1 0 1;1 0 0 0
0 0 1;5 0 1;5 0 0 1;5 0 1;5 0 0
0 0 1;5 0 1;5 0 0 1;5 0 1;5 0 0
0 0 0 0 0 0 0 0 0 0 0 0

View File

@ -30,7 +30,7 @@ public class BoardTest {
robotList = new ArrayList<>();
robotList.add(new Robot(RobotID.ROBOT_1, someValidPosition));
robotList.add(new Robot(RobotID.ROBOT_2, someValidPosition));
wallGrid.setElement(2, 3, new Wall(WallType.WALL_NORMAL, Direction.SOUTH));
wallGrid.setElement(2, 1, new Wall(WallType.WALL_NORMAL, Direction.SOUTH));
wallGrid.setElement(2, 2, new Wall(WallType.WALL_NORMAL, Direction.EAST));
wallGrid.setElement(1, 2, new Wall(WallType.WALL_CORNER, Direction.NORTH_EAST));
board = new Board(tileGrid, wallGrid, robotList);