2018-02-28 23:57:45 +01:00
|
|
|
class Map {
|
|
|
|
private final Tile[][] tiles;
|
|
|
|
private int fullWidth;
|
|
|
|
private int fullHeight;
|
|
|
|
|
2017-11-29 15:50:43 +01:00
|
|
|
public Map(int width, int height, int wWidth, int wHeight, Tile empty, Tile wall) {
|
2018-02-28 23:57:45 +01:00
|
|
|
this.fullHeight = height + (2 * wHeight);
|
|
|
|
this.fullWidth = width + (2 * wWidth);
|
|
|
|
Tile[][] map = new Tile[this.fullWidth][this.fullHeight];
|
|
|
|
for (int i = 0; i < this.fullWidth; i++) {
|
|
|
|
for (int j = 0; j < this.fullHeight; j++) {
|
|
|
|
if (i < wWidth || i >= width + wWidth || j < wHeight || j >= height + wHeight) {
|
2017-11-29 15:50:43 +01:00
|
|
|
map[i][j] = wall;
|
|
|
|
} else {
|
|
|
|
map[i][j] = empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.tiles = map;
|
|
|
|
}
|
|
|
|
|
2018-02-28 23:57:45 +01:00
|
|
|
private Tile[][] getTiles() {
|
2017-11-29 15:50:43 +01:00
|
|
|
return this.tiles;
|
|
|
|
}
|
|
|
|
|
2018-02-28 23:57:45 +01:00
|
|
|
public void generateStructure(String name, int x, int y) throws IllegalArgumentException {
|
2017-11-29 15:50:43 +01:00
|
|
|
for (Structure structure : Structure.getStructures()) {
|
|
|
|
if (name.equals(structure.getName())) {
|
|
|
|
this.placeStructure(structure, x, y);
|
2018-02-28 23:57:45 +01:00
|
|
|
return;
|
2017-11-29 15:50:43 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 23:57:45 +01:00
|
|
|
throw new IllegalArgumentException("Invalid structure name.");
|
2017-11-29 15:50:43 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 23:57:45 +01:00
|
|
|
private void placeStructure(Structure structure, int x, int y) throws IllegalArgumentException {
|
|
|
|
if (x < 0 || y < 0 || this.fullWidth < x + structure.getWidth() || this.fullHeight < y + structure.getHeight()) {
|
|
|
|
throw new IllegalArgumentException("The structure is misplaced or does not fit.");
|
|
|
|
}
|
|
|
|
Tile[][] tiles = structure.getTiles();
|
|
|
|
for (int i = 0; i < structure.getWidth(); i++) {
|
|
|
|
for (int j = 0; j < structure.getHeight(); j++) {
|
|
|
|
if (!tiles[i][j].isSolid()) {
|
|
|
|
switch (structure.getDoorDirection()) {
|
|
|
|
case NORTH:
|
|
|
|
if (this.tiles[x+i][y+j-1].isSolid()) {
|
|
|
|
throw new IllegalArgumentException("A structure is blocked");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SOUTH:
|
|
|
|
if (this.tiles[x+i][y+j+1].isSolid()) {
|
|
|
|
throw new IllegalArgumentException("A structure is blocked");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WEST:
|
|
|
|
if (this.tiles[x+i-1][y+j].isSolid()) {
|
|
|
|
throw new IllegalArgumentException("A structure is blocked");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EAST:
|
|
|
|
if (this.tiles[x+i+1][y+j].isSolid()) {
|
|
|
|
throw new IllegalArgumentException("A structure is blocked");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.tiles[x+i][y+j] = tiles[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void placeTile(Tile tile, int x, int y) {
|
|
|
|
if (x < 0 || y < 0 || x >= this.tiles.length || y >= this.tiles[0].length) {
|
|
|
|
throw new IllegalArgumentException("Invalid tile position");
|
|
|
|
}
|
|
|
|
this.tiles[x][y] = tile;
|
2017-11-29 15:50:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
Tile[][] tiles = this.getTiles();
|
2018-02-28 23:57:45 +01:00
|
|
|
StringBuilder str = new StringBuilder();
|
2017-11-29 15:50:43 +01:00
|
|
|
for (int i = 0; i < tiles.length; i++) {
|
2018-02-28 23:57:45 +01:00
|
|
|
for (int j = 0; j < tiles.length; j++) {
|
|
|
|
str.append(tiles[j][i].toChar());
|
2017-11-29 15:50:43 +01:00
|
|
|
}
|
2018-02-28 23:57:45 +01:00
|
|
|
str.append("\n");
|
2017-11-29 15:50:43 +01:00
|
|
|
}
|
2018-02-28 23:57:45 +01:00
|
|
|
return str.toString();
|
2017-11-29 15:50:43 +01:00
|
|
|
}
|
|
|
|
}
|