Tile.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package pathgame.tilemap;
  2. import java.util.function.Function;
  3. import pathgame.gameplay.Player;
  4. /** Base class for tiles. Tiles are registered on construction.
  5. *
  6. * @author kajetan
  7. */
  8. public class Tile
  9. {
  10. public static class TileBuilder
  11. {
  12. private int energyCost = 2;
  13. private float forestReplaceChance = 1.0f;
  14. private Function<Player, Integer> speedUp = (p) -> 0;
  15. private boolean canHostTown = true;
  16. private boolean blocksMovement = false;
  17. private TileType type = TileType.LAND;
  18. private boolean canHostPath = false;
  19. private boolean path = false;
  20. private TileRenderType renderType = TileRenderType.NORMAL;
  21. private TileBuilder()
  22. {
  23. }
  24. public static TileBuilder create()
  25. {
  26. return new TileBuilder();
  27. }
  28. public TileBuilder setEnergyCost(int energyCost)
  29. {
  30. this.energyCost = energyCost;
  31. return this;
  32. }
  33. public TileBuilder setForestReplaceChance(float forestReplaceChance)
  34. {
  35. this.forestReplaceChance = forestReplaceChance;
  36. return this;
  37. }
  38. public TileBuilder setSpeedUp(Function<Player, Integer> speedUp)
  39. {
  40. this.speedUp = speedUp;
  41. return this;
  42. }
  43. public TileBuilder noTown()
  44. {
  45. this.canHostTown = false;
  46. return this;
  47. }
  48. public TileBuilder noMovement()
  49. {
  50. this.blocksMovement = true;
  51. return this;
  52. }
  53. public TileBuilder setType(TileType type)
  54. {
  55. this.type = type;
  56. return this;
  57. }
  58. public TileBuilder pathHost()
  59. {
  60. this.canHostPath = true;
  61. return this;
  62. }
  63. public TileBuilder path()
  64. {
  65. this.path = true;
  66. return this;
  67. }
  68. public TileBuilder setRenderType(TileRenderType type)
  69. {
  70. this.renderType = type;
  71. return this;
  72. }
  73. public Tile build()
  74. {
  75. return new Tile(energyCost, forestReplaceChance, speedUp, canHostTown, blocksMovement, type, canHostPath, path, renderType);
  76. }
  77. }
  78. private static int idCounter = 0;
  79. private static Tile[] tiles = new Tile[8];
  80. private static int addTile(Tile t)
  81. {
  82. if(idCounter >= tiles.length)
  83. {
  84. Tile[] newTiles = new Tile[tiles.length * 2];
  85. System.arraycopy(tiles, 0, newTiles, 0, tiles.length);
  86. tiles = newTiles;
  87. }
  88. tiles[idCounter] = t;
  89. return idCounter++;
  90. }
  91. /** Returns a tile from the given id or null if the id is invalid.
  92. *
  93. * @param id the id of the tile
  94. * @return a tile from the given id or null if the id is invalid
  95. */
  96. public static Tile fromId(int id)
  97. {
  98. if(id < 0 || id >= idCounter)
  99. {
  100. return null;
  101. }
  102. return tiles[id];
  103. }
  104. private final int id;
  105. private final int energyCost;
  106. private final float forestReplaceChance;
  107. private final Function<Player, Integer> speedUp;
  108. private final boolean canHostTown;
  109. private final boolean blocksMovement;
  110. private final TileType type;
  111. private final boolean canHostPath;
  112. private final boolean path;
  113. private final TileRenderType renderType;
  114. protected Tile(int energyCost, float forestReplaceChance, Function<Player, Integer> speedUp,
  115. boolean canHostTown, boolean blocksMovement, TileType type, boolean canHostPath, boolean path,
  116. TileRenderType renderType)
  117. {
  118. id = addTile(this);
  119. this.energyCost = energyCost;
  120. this.forestReplaceChance = forestReplaceChance;
  121. this.speedUp = speedUp;
  122. this.canHostTown = canHostTown;
  123. this.blocksMovement = blocksMovement;
  124. this.type = type;
  125. this.canHostPath = canHostPath;
  126. this.path = path;
  127. this.renderType = renderType;
  128. }
  129. /** Returns the id of the tile.
  130. *
  131. * @return the id of the tile
  132. */
  133. public final int getId()
  134. {
  135. return id;
  136. }
  137. public int getEnergyCost(Player p)
  138. {
  139. return energyCost - speedUp.apply(p);
  140. }
  141. /** Returns the chance that this tile is replaced by forest.
  142. *
  143. * @return the chance that this tile is replaced by forest
  144. */
  145. public float getForestReplaceChance()
  146. {
  147. return forestReplaceChance;
  148. }
  149. /** Returns true if this tile can be replaced by a town.
  150. *
  151. * @return true if this tile can be replaced by a town
  152. */
  153. public boolean canHostTown()
  154. {
  155. return canHostTown;
  156. }
  157. /** Called when the player fully enters a tile.
  158. *
  159. * @param p the player
  160. * @param map the current tilemap
  161. * @param x the x coordinate of the tile
  162. * @param y the y coordinate of the tile
  163. */
  164. public void onEnter(Player p, TileMap map, int x, int y)
  165. {
  166. }
  167. /** Called when the player leaves a tile.
  168. *
  169. * @param p the player
  170. * @param map the current tilemap
  171. * @param x the x coordinate of the tile
  172. * @param y the y coordinate of the tile
  173. */
  174. public void onLeave(Player p, TileMap map, int x, int y)
  175. {
  176. }
  177. /** Returns true if this tile blocks movement.
  178. *
  179. * @param p the player
  180. * @return true if this tile blocks movement
  181. */
  182. public boolean isBlockingMovement(Player p)
  183. {
  184. return blocksMovement || (p.isSailing() && type == TileType.LAND) ||
  185. (!p.isSailing() && type == TileType.DEEP_WATER);
  186. }
  187. /** Returns the type of the tile.
  188. *
  189. * @return the type of the tile
  190. */
  191. public TileType getType()
  192. {
  193. return type;
  194. }
  195. /** Returns true if this tile can be replaced by a path.
  196. *
  197. * @return true if this tile can be replaced by a path
  198. */
  199. public boolean canHostPath()
  200. {
  201. return canHostPath;
  202. }
  203. /** Returns true if this tile is a path.
  204. *
  205. * @return true if this tile is a path
  206. */
  207. public boolean isPath()
  208. {
  209. return path;
  210. }
  211. /** Returns the rendering type for overlays.
  212. *
  213. * @return the rendering type for overlays
  214. */
  215. public TileRenderType getRenderType()
  216. {
  217. return renderType;
  218. }
  219. /** Called when the player stands on that tile.
  220. *
  221. * @param p the player
  222. * @param map the current tilemap
  223. * @param x the x coordinate of the tile
  224. * @param y the y coordinate of the tile
  225. */
  226. public void isStandingOn(Player p, TileMap map, int x, int y)
  227. {
  228. }
  229. }