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); List<IDrawableObject> elementsToDraw = IOUtil.getDrawableObjectsFromGame(game, tileDimensions, tileDimensions);
for (IDrawableObject object : elementsToDraw) { for (IDrawableObject object : elementsToDraw) {
TextureRegion objectTextureRegion = object.getTexture(); 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(), batch.draw(objectTextureRegion.getTexture(), object.getXPosition(), object.getYPosition(),
(float)object.getWidth()/2, (float)object.getHeight()/2, (float)object.getWidth()/2, (float)object.getHeight()/2,
object.getWidth(), object.getHeight(), 1, 1, object.getRotation(), 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 { private void runGameLoop() throws InterruptedException {
TimeUnit.SECONDS.sleep(10); TimeUnit.SECONDS.sleep(3);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.NORTH); 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); TimeUnit.SECONDS.sleep(1);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.EAST); gameBoard.moveRobot(RobotID.ROBOT_1, Direction.EAST);
TimeUnit.SECONDS.sleep(1); TimeUnit.SECONDS.sleep(1);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.NORTH); gameBoard.moveRobot(RobotID.ROBOT_1, Direction.EAST);
TimeUnit.SECONDS.sleep(1); TimeUnit.SECONDS.sleep(1);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.WEST); gameBoard.rotateRobotRight(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1); TimeUnit.SECONDS.sleep(1);
gameBoard.moveRobot(RobotID.ROBOT_1, Direction.WEST); gameBoard.moveRobot(RobotID.ROBOT_1, Direction.SOUTH);
} }
@Override @Override

View File

@ -267,12 +267,11 @@ public class Board {
* @return The new position of the element * @return The new position of the element
*/ */
private Position getNewPosition(Position oldPosition, Direction direction) { private Position getNewPosition(Position oldPosition, Direction direction) {
//TODO: Make sure we're accounting for the flipped y axis in libgdx
switch (direction) { switch (direction) {
case NORTH: case NORTH:
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() + 1);
case SOUTH:
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() - 1); return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() - 1);
case SOUTH:
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() + 1);
case EAST: case EAST:
return new Position(oldPosition.getXCoordinate() + 1, oldPosition.getYCoordinate()); return new Position(oldPosition.getXCoordinate() + 1, oldPosition.getYCoordinate());
case WEST: case WEST:
@ -290,8 +289,8 @@ public class Board {
*/ */
private <K> List<K> getAllElementsFromGrid(IGrid<K> grid) { private <K> List<K> getAllElementsFromGrid(IGrid<K> grid) {
List<K> elements = new ArrayList<>(); List<K> elements = new ArrayList<>();
for (int x = grid.getWidth() - 1; x >= 0; x--) { for (int y = grid.getHeight() - 1; y >= 0; y--) {
for (int y = 0; y < grid.getHeight(); y++) { for (int x = 0; x < grid.getWidth(); x++) {
elements.add(grid.getElement(x, y)); 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 { private static IGrid<Tile> loadTileGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException {
IGrid<Tile> tileGrid = new Grid<>(gridWidth, gridHeight); 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 gridLine = reader.readLine();
String[] tilesOnLine = gridLine.split(" "); String[] tilesOnLine = gridLine.split(" ");
for (int j = 0; j < gridWidth; j++) { for (int x = 0; x < gridWidth; x++) {
String[] tileData = tilesOnLine[j].split(";"); String[] tileData = tilesOnLine[x].split(";");
TileType tileType = TileType.getTileTypeFromID(Integer.parseInt(tileData[0])); TileType tileType = TileType.getTileTypeFromID(Integer.parseInt(tileData[0]));
Direction direction = Direction.getDirectionFromID(Integer.parseInt(tileData[1])); Direction direction = Direction.getDirectionFromID(Integer.parseInt(tileData[1]));
if (direction == null) { if (direction == null) {
throw new IllegalArgumentException("Invalid direction for tile encountered when loading board file."); 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; return tileGrid;
@ -75,20 +75,20 @@ public final class BoardLoaderUtil {
*/ */
private static IGrid<Wall> loadWallGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException { private static IGrid<Wall> loadWallGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException {
IGrid<Wall> wallGrid = new Grid<>(gridWidth, gridHeight); 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 gridLine = reader.readLine();
String[] wallsOnLine = gridLine.split(" "); String[] wallsOnLine = gridLine.split(" ");
for (int j = 0; j < gridWidth; j++) { for (int x = 0; x < gridWidth; x++) {
if (wallsOnLine[j].equals("0")) { if (wallsOnLine[x].equals("0")) {
continue; continue;
} }
String[] wallData = wallsOnLine[j].split(";"); String[] wallData = wallsOnLine[x].split(";");
WallType wallType = WallType.getWallTypeFromID(Integer.parseInt(wallData[0])); WallType wallType = WallType.getWallTypeFromID(Integer.parseInt(wallData[0]));
Direction direction = Direction.getDirectionFromID(Integer.parseInt(wallData[1])); Direction direction = Direction.getDirectionFromID(Integer.parseInt(wallData[1]));
if (direction == null) { if (direction == null) {
throw new IllegalArgumentException("Invalid direction for tile encountered when loading board file."); 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; return wallGrid;

View File

@ -30,13 +30,21 @@ public class IOUtil {
List<Robot> robotsToDraw = game.getRobotsToDraw(); List<Robot> robotsToDraw = game.getRobotsToDraw();
int gameWidth = game.getWidth(); int gameWidth = game.getWidth();
int gameHeight = game.getHeight(); int gameHeight = game.getHeight();
drawableObjects.addAll(getDrawableObjectsFromElementList(tilesToDraw, gameWidth, gameHeight, tileWidth, tileHeight)); drawableObjects.addAll(getDrawableObjectsFromElementList(tilesToDraw, gameWidth, tileWidth, tileHeight));
drawableObjects.addAll(getDrawableObjectsFromElementList(wallsToDraw, gameWidth, gameHeight, tileWidth, tileHeight)); drawableObjects.addAll(getDrawableObjectsFromElementList(wallsToDraw, gameWidth, tileWidth, tileHeight));
drawableObjects.addAll(getDrawableRobots(robotsToDraw, gameWidth, gameHeight, tileWidth, tileHeight)); drawableObjects.addAll(getDrawableRobots(robotsToDraw, gameHeight, tileWidth, tileHeight));
return drawableObjects; 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<>(); List<IDrawableObject> drawableObjects = new ArrayList<>();
for (Robot robot : robots) { for (Robot robot : robots) {
TextureRegion region = TextureConverterUtil.convertElement(robot); 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 * Gets a list of drawable objects with correct positions from a list of elements
* @param elementsToDraw A list of elements to draw * @param elementsToDraw A list of elements to draw
* @param gameWidth The width of the game board in tiles * @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 tileWidth The width of a tile
* @param tileHeight The height of a tile * @param tileHeight The height of a tile
* @param <K> Should be type Robot, Tile or Wall * @param <K> Should be type Robot, Tile or Wall
* @return A list of drawable objects * @return A list of drawable objects
*/ */
private static <K> List<IDrawableObject> getDrawableObjectsFromElementList(List<K> elementsToDraw, int gameWidth, private static <K> List<IDrawableObject> getDrawableObjectsFromElementList(List<K> elementsToDraw, int gameWidth, int tileWidth,
int gameHeight, int tileWidth,
int tileHeight) { int tileHeight) {
List<IDrawableObject> drawableObjects = new ArrayList<>(); List<IDrawableObject> drawableObjects = new ArrayList<>();
for (int j = 0; j < gameHeight; j++) { int y = 0;
for (int i = j * gameWidth; i < (j + 1) * gameWidth; i++) { for (int i = 0; i < elementsToDraw.size(); i++) {
K currentElement = elementsToDraw.get(i); K currentElement = elementsToDraw.get(i);
if (currentElement == null) { int x = i % gameWidth;
continue; if (i > 0 && i % gameWidth == 0) {
} y++;
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);
} }
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; 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) { private static <K> int getElementRotation(K element) {
boolean hasRotatedTexture; boolean hasRotatedTexture;
Direction direction; 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 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 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 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 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 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 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 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 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 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 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 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 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 = new ArrayList<>();
robotList.add(new Robot(RobotID.ROBOT_1, someValidPosition)); robotList.add(new Robot(RobotID.ROBOT_1, someValidPosition));
robotList.add(new Robot(RobotID.ROBOT_2, 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(2, 2, new Wall(WallType.WALL_NORMAL, Direction.EAST));
wallGrid.setElement(1, 2, new Wall(WallType.WALL_CORNER, Direction.NORTH_EAST)); wallGrid.setElement(1, 2, new Wall(WallType.WALL_CORNER, Direction.NORTH_EAST));
board = new Board(tileGrid, wallGrid, robotList); board = new Board(tileGrid, wallGrid, robotList);