TileMap.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package pathgame.tilemap;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. /** Fixed size container for tile maps. Changes are stored in a dirty flag,
  5. * which can be used for rendering.
  6. *
  7. * @author kajetan
  8. */
  9. public class TileMap
  10. {
  11. private static class TownConverter
  12. {
  13. private int x;
  14. private int y;
  15. private int ticks = 0;
  16. private int index = -1;
  17. }
  18. private int towns = 0;
  19. private final int width;
  20. private final int height;
  21. private final int[][] tiles;
  22. private boolean dirty = true;
  23. private final LinkedList<TownConverter> townConverters = new LinkedList<>();
  24. private int homeX;
  25. private int homeY;
  26. /** Creates a new tile map of the given size.
  27. *
  28. * @param width the width of the map
  29. * @param height the height of the map
  30. */
  31. public TileMap(int width, int height)
  32. {
  33. this.width = width;
  34. this.height = height;
  35. tiles = new int[width][height];
  36. }
  37. /** Returns the width of the map.
  38. *
  39. * @return the width of the map
  40. */
  41. public int getWidth()
  42. {
  43. return width;
  44. }
  45. /** Returns the height of the map.
  46. *
  47. * @return the height of the map
  48. */
  49. public int getHeight()
  50. {
  51. return height;
  52. }
  53. /** Sets the tile at the given map position. The dirty flag is set.
  54. *
  55. * @param x the x coordinate of the tile
  56. * @param y the y coordinate of the tile
  57. * @param tile a tile
  58. */
  59. public void setTile(int x, int y, Tile tile)
  60. {
  61. dirty = true;
  62. if(tiles[x][y] == Tiles.TOWN.getId())
  63. {
  64. towns--;
  65. }
  66. if(tile == Tiles.TOWN)
  67. {
  68. towns++;
  69. }
  70. tiles[x][y] = tile.getId();
  71. }
  72. /** Returns the tile at the given map position or null if the tile is invalid.
  73. *
  74. * @param x the x coordinate of the tile
  75. * @param y the y coordinate of the tile
  76. * @return the tile at the given map position or null if the tile is invalid
  77. */
  78. public Tile getTile(int x, int y)
  79. {
  80. return Tile.fromId(tiles[x][y]);
  81. }
  82. /** Returns true if the map was modified. This is used for rendering.
  83. *
  84. * @return true if the map was modified
  85. */
  86. public boolean isDirty()
  87. {
  88. return dirty;
  89. }
  90. /** Clears the dirty flag of the map.
  91. *
  92. */
  93. public void clean()
  94. {
  95. dirty = false;
  96. }
  97. /** Ticks the tilemap.
  98. *
  99. */
  100. public void tick()
  101. {
  102. Iterator<TownConverter> iter = townConverters.iterator();
  103. while(iter.hasNext())
  104. {
  105. TownConverter tc = iter.next();
  106. tc.ticks++;
  107. if(tc.ticks % 10 == 0)
  108. {
  109. tc.index++;
  110. if(tc.index >= Tiles.TOWN_BLOCKED.length)
  111. {
  112. iter.remove();
  113. continue;
  114. }
  115. setTile(tc.x, tc.y, Tiles.TOWN_BLOCKED[tc.index]);
  116. }
  117. }
  118. }
  119. /** Slowly converts a tile into blocked town.
  120. *
  121. * @param x the x coordinate of the tile
  122. * @param y the y coordinate of the tile
  123. */
  124. public void convertTown(int x, int y)
  125. {
  126. TownConverter tc = new TownConverter();
  127. tc.x = x;
  128. tc.y = y;
  129. townConverters.add(tc);
  130. }
  131. /** Returns the number of active towns.
  132. *
  133. * @return the number of active towns
  134. */
  135. public int getNumberOfTowns()
  136. {
  137. return towns;
  138. }
  139. /** Sets the position of the home town.
  140. *
  141. * @param x the x coordinate of the home town
  142. * @param y the y coordinate of the home town
  143. */
  144. public void setHomeTown(int x, int y)
  145. {
  146. homeX = x;
  147. homeY = y;
  148. }
  149. /** Returns the x coordinate of the home town.
  150. *
  151. * @return the x coordinate of the home town
  152. */
  153. public int getHomeX()
  154. {
  155. return homeX;
  156. }
  157. /** Returns the y coordinate of the home town.
  158. *
  159. * @return the y coordinate of the home town
  160. */
  161. public int getHomeY()
  162. {
  163. return homeY;
  164. }
  165. }