This commit is contained in:
Kristian Knarvik 2020-03-12 11:21:14 +01:00
commit d5d87d11dd
2 changed files with 100 additions and 5 deletions

View File

@ -1,10 +1,9 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Direction;
import inf112.fiasko.roborally.element_properties.Position;
import inf112.fiasko.roborally.element_properties.RobotID;
import inf112.fiasko.roborally.element_properties.TileType;
import inf112.fiasko.roborally.element_properties.*;
import inf112.fiasko.roborally.utility.TextureConverterUtil;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -327,4 +326,62 @@ public class Board {
}
return elements;
}
}
/**
* Gets a list of BoardElementContainers, containing all tiles and positions of given tile types
* @param tiles The tiles you want all positions for
* @return A list of BoardElementContainers
*/
public List<BoardElementContainer<Tile>> getPositionsOfTileOnBoard(TileType ... tiles) {
List<BoardElementContainer<Tile>> combinedList = new ArrayList<>();
for (TileType tile : tiles) {
combinedList.addAll(makeTileList(tile, this.tiles));
}
return combinedList;
}
/**
* Gets a list of BoardElementContainers, containing all tiles and positions of given wall types
* @param walls The walls you want all positions for
* @return A list of BoardElementContainers
*/
public List<BoardElementContainer<Wall>> getPositionsOfTileOnBoard(WallType ... walls) {
List<BoardElementContainer<Wall>> combinedList = new ArrayList<>();
for (WallType wall : walls) {
combinedList.addAll(makeTileList(wall, this.walls));
}
return combinedList;
}
/**
* Finds all position of an obj and makes a list of BoardElementContainers
* @param type Type of obj
* @param grid Grid to search
* @param <K> Type of type
* @param <T> Type of grid
* @return List of BoardElementContainers
*/
private <K,T> List<BoardElementContainer<T>> makeTileList(K type, IGrid<T> grid) {
List<BoardElementContainer<T>> 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.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("Grid has unknown type.");
}
}
}
return objList;
}
}

View File

@ -0,0 +1,38 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Position;
/**
* This class represents a type of object and its position
* @param <K> The type of object
*/
public class BoardElementContainer <K>{
K obj;
Position pos;
/**
* Initializes the BoardElementContainer
* @param obj The object
* @param pos The position
*/
BoardElementContainer(K obj, Position pos) {
this.obj = obj;
this.pos = pos;
}
/**
* Gets the object
* @return object
*/
public K getObject() {
return obj;
}
/**
* Gets the position
* @return position
*/
public Position getPosition() {
return pos;
}
}