Tile rendering.
/**
* Checks every tile -> Gets tile id -> Draws correct texture.
* If Tile is empty, ignore it.
* @param batch
*/
public void drawTiles(SpriteBatch batch) {
for(int x = 0; x < WORLD_X; x++) {
for(int y = 0; y < WORLD_Y; y++) {
if(getTileId(x, y) != 0) {
batch.draw(Tiles.getTile(getTileId(x, y)).getTileImg(), 16 * x, 16 * y);
}
}
}
}
Generating world
//Contains map information
private Tile[][] map = new Tile[WORLD_X][WORLD_Y];
/**
* Generates new map
*/
public TerrainGenerator() {
for(int x = 0; x < WORLD_X; x++) {
for(int y = 0; y < WORLD_Y; y++) {
map[x][y] = new Tile(Misc.random(0, 2));
}
}
}
Tile class
public class Tile {
private int id;
private int durability;
public Tile(int tileId) {
this.id = tileId;
this.durability = Tiles.getTile(tileId).getDurability();
}
public int getId() {
return this.id;
}
public int setId(int tileId) {
return this.id = tileId;
}
public int getDurability() {
return this.durability;
}
}
Contains tile information.
public enum Tiles {
EMPTY(0, null, -1),
DIRT(1, new Texture("tiles/sand.png"), 5),
ROCK(2, new Texture("tiles/rock.png"), 2);
//Tile id
private int id;
//Tile image
private Texture tileImg;
//Tile durability
private int durability;
Tiles(int id, Texture tileImg, int durability) {
this.id = id;
this.tileImg = tileImg;
this.durability = durability;
}
/**
* Returns Specified Tile data.
* @param tileId
* @return
*/
public static Tiles getTile(int tileId) {
for(Tiles t : Tiles.values()) {
if(t.getId() == tileId) {
return t;
}
}
return null;
}
/**
* Returns Tile id
* @return
*/
public int getId() {
return id;
}
/**
* Returns Tiles image
* @return
*/
public Texture getTileImg() {
return tileImg;
}
public int getDurability() {
return durability;
}
}