package pathgame.tilemap; import java.util.Iterator; import java.util.LinkedList; public class TileMap { private static class TownConverter { private int x; private int y; private int ticks = 0; private int index = -1; } private int towns = 0; private final int width; private final int height; private final int[][] tiles; private boolean dirty = true; private final LinkedList townConverters = new LinkedList<>(); private int homeX; private int homeY; public TileMap(int width, int height) { this.width = width; this.height = height; tiles = new int[width][height]; } public int getWidth() { return width; } public int getHeight() { return height; } public void setTile(int x, int y, Tile tile) { dirty = true; if(tiles[x][y] == Tiles.TOWN.getId()) { towns--; } if(tile == Tiles.TOWN) { towns++; } tiles[x][y] = tile.getId(); } public Tile getTile(int x, int y) { return Tile.fromId(tiles[x][y]); } public boolean isDirty() { return dirty; } public void clean() { dirty = false; } public void tick() { Iterator iter = townConverters.iterator(); while(iter.hasNext()) { TownConverter tc = iter.next(); tc.ticks++; if(tc.ticks % 10 == 0) { tc.index++; if(tc.index >= Tiles.TOWN_BLOCKED.length) { iter.remove(); continue; } setTile(tc.x, tc.y, Tiles.TOWN_BLOCKED[tc.index]); } } } public void convertTown(int x, int y) { TownConverter tc = new TownConverter(); tc.x = x; tc.y = y; townConverters.add(tc); } public int getNumberOfTowns() { return towns; } public void setHomeTown(int x, int y) { homeX = x; homeY = y; } public int getHomeX() { return homeX; } public int getHomeY() { return homeY; } }