Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game.world;
- import java.util.Random;
- import javax.swing.ImageIcon;
- /*Tile.Java
- @author Robert
- */
- public class Tile {
- //member variables
- private TileType type;
- private int numOfTypes = TileType.values().length;
- private static Random random = new Random();
- Tile(){
- //CTOR
- }
- /*****************************
- * Getters and setters. blah.
- *****************************/
- public String getSpriteString(){
- return type.tileImage;
- }
- public TileType getTypeByIndex(int i){
- //The Cleverest Function that could. When the Tile() constructor calls this from createTile(TileType.getTypeByIndex)
- //It simply returns the values we need. Genius! To Think, ten minutes ago I was about to give up on the enum type.
- return TileType.values()[i];
- }
- public int getNumOfTypes(){
- return numOfTypes;
- }
- public boolean isPassable() {
- /*
- * @Return passable
- */
- return type.passable;
- }
- public ImageIcon getImageIcon(){
- ImageIcon icon = new ImageIcon(type.tileImage);
- return icon;
- }
- public int getIndexOfType(){
- return this.getType().ordinal();
- }
- /**
- * @return the type
- */
- public TileType getType() {
- return type;
- }
- /**
- * @param type the type to set
- */
- public void setType(TileType type) {
- this.type = type;
- }
- public double getSpeedMod() {
- /**
- * @return the speedMod. This will tell units what percentage of their maximum speed they can move across this soil.
- */
- return type.speedmod;
- }
- public String getSType(){
- /**
- * @return the SType
- */
- return type.sType;
- }
- public char getTileChar() {
- /**
- * @return the tileChar
- */
- return type.tileChar;
- }
- public Tile setupTile(){
- //This function was made much neater by the use of an enum.
- //check out the cleverest little function that could.
- //This function basically just finalizes creating a tile, by grabbing the appropriate
- //information from TileType and returning a new tile.
- //thanks to everyone in ##Java on Freenode for their help.
- TileType typeSelector = getTypeByIndex(random.nextInt
- (TileType.values().length - 2) + 2);
- //here we'll create a tile, and then initialize all of it's variables based on our enum. *PRETTY CLEVER!*
- this.setType((typeSelector));
- return this;
- }
- /**
- * @deprecated
- * @overloaded function
- * @param typeSelector
- * @return
- */
- @Deprecated
- public Tile setupTile(int typeSelector){
- //This function over loads setupTile so that it can be used to modify a tile later on.
- TileType typeSelected = getTypeByIndex(typeSelector);
- //here we'll create a tile, and then initialize all of it's variables based on our enum. *PRETTY CLEVER!*
- this.setType((typeSelected));
- return this;
- }
- public Tile setupTile(TileType type){
- //This function over loads setupTile so that it can be used to modify a tile later on.
- //here we'll create a tile, and then initialize all of it's variables based on our enum. *PRETTY CLEVER!*
- this.setType((type));
- return this;
- }
- public void printTileInfo(){
- //Later to be deprecated with Tile::printTileToolTip
- System.out.println(this.getType() + "\nPassable: " + this.isPassable() + "\nSpeedMod: " + this.getSpeedMod());
- }
- }
- //###End Tile###
- package game.world;
- import game.units.Unit;
- public enum TileType {
- //Here we're enumerating out our tile types. This will make it easier to add tiles in the future.
- /*
- * Params:
- * char tileChar: Debug purposes, just to display the map in console.
- * boolean passable: Whether or not the tile is passable.
- * double speedMod: Any units moving through this tile multiply their speed by this.
- * String sType: The String version of the tile type
- * Unit[5] unitsInMe: Any units that are in the tile.
- * String sprite: The image file to load for this tile.
- */
- WATER('~', false, 0.0, "Water", null, "water.png"),
- SAND('§', true, 0.75, "Sand", null, "sand.png"),
- DIRT('#', true, 1.0, "Dirt", null, "dirt.png"),
- GRASS('"', true, 1.0, "Grass", null, "grass.png"),
- PEBBLES('*', true, 1.0, "Pebbles", null, "pebbles.png"),
- ROCK('^', false, 0.0, "Rock", null, "rocks.png"),
- TREE('†', true, 0.5, "Tree", null, "tree.png");
- //same member variables as our parent class.
- public String tileImage = "./resources/images/world/";
- char tileChar;
- boolean passable;
- double speedmod;
- String sType;
- Unit[] unitsInMe;
- String sprite;
- private TileType(char tileChar, boolean passable, double speedMod, String sType, Unit[] unitsInMe, String sprite){
- //CTOR
- this.tileChar = tileChar;
- this.passable = passable;
- this.speedmod = speedMod;
- this.sType = sType;
- this.tileImage = tileImage + sprite;
- this.unitsInMe = new Unit[5];
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement