| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 | 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<PlayerAbilities, Integer> 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<PlayerAbilities, Integer> 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<PlayerAbilities, Integer> speedUp;    private final boolean canHostTown;    private final boolean blocksMovement;        protected Tile(int energyCost, float forestReplaceChance, Function<PlayerAbilities, Integer> 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;    }}
 |