Added more object types.

Added Map and Structure. Created a working map int Test.java, but Structures are still not implemented.
This commit is contained in:
Kristian Knarvik 2017-11-29 15:50:43 +01:00
parent ad09ee4b1d
commit fc74f55c85
4 changed files with 102 additions and 4 deletions

47
java/Map.java Normal file

@ -0,0 +1,47 @@
public class Map {
private Tile[][] tiles;
public Map(int width, int height, int wWidth, int wHeight, Tile empty, Tile wall) {
int fullHeight = height + (2 * wHeight);
int fullWidth = width + (2 * wWidth);
Tile[][] map = new Tile[fullHeight][fullWidth];
for (int i = 0; i < fullHeight; i++) {
for (int j = 0; j < fullWidth; j++) {
if (i < wHeight || i >= height + wHeight || j < wWidth || j >= width + wWidth) {
map[i][j] = wall;
} else {
map[i][j] = empty;
}
}
}
this.tiles = map;
}
public Tile[][] getTiles() {
return this.tiles;
}
public void generateStructure(String name, int x, int y) {
for (Structure structure : Structure.getStructures()) {
if (name.equals(structure.getName())) {
this.placeStructure(structure, x, y);
}
}
}
private void placeStructure(Structure structure, int x, int y) {
}
public String toString() {
Tile[][] tiles = this.getTiles();
String str = "";
for (int i = 0; i < tiles.length; i++) {
for (int j = 0; j < tiles[i].length; j++) {
str += tiles[i][j].toChar();
}
str += "\n";
}
return str;
}
}

39
java/Structure.java Normal file

@ -0,0 +1,39 @@
import java.util.ArrayList;
public class Structure {
private static ArrayList<Structure> structures;
private Tile[][] tiles;
private String name;
public Structure(String name, Tile[][] tiles) {
this.name = name;
this.tiles = tiles;
structures.add(this);
}
public String getName() {
return this.name;
}
public Tile[][] getTiles() {
return this.tiles;
}
public static ArrayList<Structure> getStructures() {
return structures;
}
public int getWidth() {
return this.tiles.length;
}
public int getHeight() {
int max = 0;
for (Tile[] tile : this.tiles) {
if (tile.length > max) {
max = tile.length;
}
}
return max;
}
}

8
java/Test.java Normal file

@ -0,0 +1,8 @@
public class Test {
public static void main(String[] args) {
Tile wall = new Tile('#', true, "NONE");
Tile empty = new Tile('+', false, "NONE");
Map map = new Map(50, 5, 5, 5, empty, wall);
System.out.println(map);
}
}

@ -5,7 +5,7 @@ public class Tile {
private Action action;
private int[] teleportTarget;
public Tile (char representation, boolean solid, String action) {
public Tile (char representation, boolean solid, String action) throws IllegalArgumentException {
this.representation = representation;
this.solid = solid;
this.action = Action.valueOf(action.toUpperCase());
@ -16,12 +16,16 @@ public class Tile {
}
}
public Tile (char representation, boolean solid, String action, int x, int y) {
public Tile (char representation, boolean solid, String action, int x, int y) throws IllegalArgumentException {
this.representation = representation;
this.solid = solid;
this.action = Action.valueOf(action.toUpperCase());
int[] intArray = {x, y};
this.teleportTarget = intArray;
if (this.action == Action.TELEPORT) {
int[] intArray = {x, y};
this.teleportTarget = intArray;
} else {
throw new IllegalArgumentException("A non-teleport tile can't have a teleport target.");
}
}
public char toChar() {