package pathgame.tilemap; import java.util.function.Function; import pathgame.gameplay.Player; import pathgame.gameplay.PlayerAbilities; /** Base class for tiles. Tiles are registered on construction. * * @author kajetan */ public class Tile { public static class TileBuilder { private int energyCost = 1; private float forestReplaceChance = 1.0f; private Function speedUp = (pa) -> 0; private boolean canHostTown = true; private boolean blocksMovement = false; private TileBuilder() { } public static TileBuilder create() { return new TileBuilder(); } public TileBuilder setEnergyCost(int energyCost) { this.energyCost = energyCost; return this; } public TileBuilder setForestReplaceChance(float forestReplaceChance) { this.forestReplaceChance = forestReplaceChance; return this; } public TileBuilder setSpeedUp(Function speedUp) { this.speedUp = speedUp; return this; } public TileBuilder noTown() { this.canHostTown = false; return this; } public TileBuilder noMovement() { this.blocksMovement = true; return this; } public Tile build() { return new Tile(energyCost, forestReplaceChance, speedUp, canHostTown, blocksMovement); } } private static int idCounter = 0; private static Tile[] tiles = new Tile[8]; private static int addTile(Tile t) { if(idCounter >= tiles.length) { Tile[] newTiles = new Tile[tiles.length * 2]; System.arraycopy(tiles, 0, newTiles, 0, tiles.length); tiles = newTiles; } tiles[idCounter] = t; return idCounter++; } /** Returns a tile from the given id or null if the id is invalid. * * @param id the id of the tile * @return a tile from the given id or null if the id is invalid */ public static Tile fromId(int id) { if(id < 0 || id >= idCounter) { return null; } return tiles[id]; } private final int id; private final int energyCost; private final float forestReplaceChance; private final Function speedUp; private final boolean canHostTown; private final boolean blocksMovement; protected Tile(int energyCost, float forestReplaceChance, Function speedUp, boolean canHostTown, boolean blocksMovement) { id = addTile(this); this.energyCost = energyCost; this.forestReplaceChance = forestReplaceChance; this.speedUp = speedUp; this.canHostTown = canHostTown; this.blocksMovement = blocksMovement; } /** Returns the id of the tile. * * @return the id of the tile */ public final int getId() { return id; } public int getEnergyCost(PlayerAbilities pa) { return energyCost - speedUp.apply(pa); } /** Returns the chance that this tile is replaced by forest. * * @return the chance that this tile is replaced by forest */ public float getForestReplaceChance() { return forestReplaceChance; } /** Returns true if this tile can be replaced by a town. * * @return true if this tile can be replaced by a town */ public boolean canHostTown() { return canHostTown; } /** Called when the player fully enters a tile. * * @param p the player * @param map the current tilemap * @param x the x coordinate of the tile * @param y the y coordinate of the tile */ public void onEnter(Player p, TileMap map, int x, int y) { } /** Called when the player leaves a tile. * * @param p the player * @param map the current tilemap * @param x the x coordinate of the tile * @param y the y coordinate of the tile */ public void onLeave(Player p, TileMap map, int x, int y) { } /** Returns true if this tile blocks movement. * * @return true if this tile blocks movement */ public boolean isBlockingMovement() { return blocksMovement; } }