Tile.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package pathgame.tilemap;
  2. import java.util.function.Function;
  3. import pathgame.gameplay.Player;
  4. import pathgame.gameplay.PlayerAbilities;
  5. /** Base class for tiles. Tiles are registered on construction.
  6. *
  7. * @author kajetan
  8. */
  9. public class Tile
  10. {
  11. public static class TileBuilder
  12. {
  13. private int energyCost = 1;
  14. private float forestReplaceChance = 1.0f;
  15. private Function<PlayerAbilities, Integer> speedUp = (pa) -> 0;
  16. private boolean canHostTown = true;
  17. private boolean blocksMovement = false;
  18. private TileBuilder()
  19. {
  20. }
  21. public static TileBuilder create()
  22. {
  23. return new TileBuilder();
  24. }
  25. public TileBuilder setEnergyCost(int energyCost)
  26. {
  27. this.energyCost = energyCost;
  28. return this;
  29. }
  30. public TileBuilder setForestReplaceChance(float forestReplaceChance)
  31. {
  32. this.forestReplaceChance = forestReplaceChance;
  33. return this;
  34. }
  35. public TileBuilder setSpeedUp(Function<PlayerAbilities, Integer> speedUp)
  36. {
  37. this.speedUp = speedUp;
  38. return this;
  39. }
  40. public TileBuilder noTown()
  41. {
  42. this.canHostTown = false;
  43. return this;
  44. }
  45. public TileBuilder noMovement()
  46. {
  47. this.blocksMovement = true;
  48. return this;
  49. }
  50. public Tile build()
  51. {
  52. return new Tile(energyCost, forestReplaceChance, speedUp, canHostTown, blocksMovement);
  53. }
  54. }
  55. private static int idCounter = 0;
  56. private static Tile[] tiles = new Tile[8];
  57. private static int addTile(Tile t)
  58. {
  59. if(idCounter >= tiles.length)
  60. {
  61. Tile[] newTiles = new Tile[tiles.length * 2];
  62. System.arraycopy(tiles, 0, newTiles, 0, tiles.length);
  63. tiles = newTiles;
  64. }
  65. tiles[idCounter] = t;
  66. return idCounter++;
  67. }
  68. /** Returns a tile from the given id or null if the id is invalid.
  69. *
  70. * @param id the id of the tile
  71. * @return a tile from the given id or null if the id is invalid
  72. */
  73. public static Tile fromId(int id)
  74. {
  75. if(id < 0 || id >= idCounter)
  76. {
  77. return null;
  78. }
  79. return tiles[id];
  80. }
  81. private final int id;
  82. private final int energyCost;
  83. private final float forestReplaceChance;
  84. private final Function<PlayerAbilities, Integer> speedUp;
  85. private final boolean canHostTown;
  86. private final boolean blocksMovement;
  87. protected Tile(int energyCost, float forestReplaceChance, Function<PlayerAbilities, Integer> speedUp, boolean canHostTown, boolean blocksMovement)
  88. {
  89. id = addTile(this);
  90. this.energyCost = energyCost;
  91. this.forestReplaceChance = forestReplaceChance;
  92. this.speedUp = speedUp;
  93. this.canHostTown = canHostTown;
  94. this.blocksMovement = blocksMovement;
  95. }
  96. /** Returns the id of the tile.
  97. *
  98. * @return the id of the tile
  99. */
  100. public final int getId()
  101. {
  102. return id;
  103. }
  104. public int getEnergyCost(PlayerAbilities pa)
  105. {
  106. return energyCost - speedUp.apply(pa);
  107. }
  108. /** Returns the chance that this tile is replaced by forest.
  109. *
  110. * @return the chance that this tile is replaced by forest
  111. */
  112. public float getForestReplaceChance()
  113. {
  114. return forestReplaceChance;
  115. }
  116. /** Returns true if this tile can be replaced by a town.
  117. *
  118. * @return true if this tile can be replaced by a town
  119. */
  120. public boolean canHostTown()
  121. {
  122. return canHostTown;
  123. }
  124. /** Called when the player fully enters a tile.
  125. *
  126. * @param p the player
  127. * @param map the current tilemap
  128. * @param x the x coordinate of the tile
  129. * @param y the y coordinate of the tile
  130. */
  131. public void onEnter(Player p, TileMap map, int x, int y)
  132. {
  133. }
  134. /** Called when the player leaves a tile.
  135. *
  136. * @param p the player
  137. * @param map the current tilemap
  138. * @param x the x coordinate of the tile
  139. * @param y the y coordinate of the tile
  140. */
  141. public void onLeave(Player p, TileMap map, int x, int y)
  142. {
  143. }
  144. /** Returns true if this tile blocks movement.
  145. *
  146. * @return true if this tile blocks movement
  147. */
  148. public boolean isBlockingMovement()
  149. {
  150. return blocksMovement;
  151. }
  152. }