Advertisement
rooster5105

Untitled

Jul 7th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. public class Tile {
  2.    
  3.     //member variables
  4.     private TileType type;
  5.     private int numOfTypes = TileType.values().length;
  6.     private static Random random = new Random();
  7.    
  8.     Tile(){
  9.         //CTOR
  10.     }
  11.    
  12.    
  13.        
  14.    
  15.     /*****************************
  16.      * Getters and setters.  blah.  
  17.      *****************************/
  18.     public String getSpriteString(){
  19.         return type.tileImage;
  20.     }
  21.     public TileType getTypeByIndex(int i){
  22.         //The Cleverest Function that could.   When the Tile() constructor calls this from createTile(TileType.getTypeByIndex)
  23.         //It simply returns the values we need.  Genius!  To Think, ten minutes ago I was about to give up on the enum type.  
  24.             return TileType.values()[i];
  25.         }
  26.    
  27.     public int getNumOfTypes(){
  28.         return numOfTypes;
  29.     }
  30.    
  31.     public boolean isPassable() {
  32.         /*
  33.          * @Return passable
  34.          */
  35.         return type.passable;
  36.     }
  37.  
  38.    
  39.    
  40.     public ImageIcon getImageIcon(){
  41.         ImageIcon icon = new ImageIcon(type.tileImage);    
  42.         return icon;
  43.        
  44.     }
  45.    
  46.     public int getIndexOfType(){
  47.         return this.getType().ordinal();
  48.     }
  49.    
  50.     /**
  51.      * @return the type
  52.      */
  53.     public TileType getType() {
  54.         return type;
  55.     }
  56.  
  57.     /**
  58.      * @param type the type to set
  59.      */
  60.     public void setType(TileType type) {
  61.         this.type = type;
  62.     }
  63.  
  64.     public double getSpeedMod() {
  65.         /**
  66.          * @return the speedMod.  This will tell units what percentage of their maximum speed they can move across this soil.
  67.          */
  68.         return type.speedmod;
  69.     }
  70.  
  71.     public String getSType(){
  72.         /**
  73.          * @return the SType
  74.          */
  75.         return type.sType;
  76.     }
  77.  
  78.     public char getTileChar() {
  79.         /**
  80.          * @return the tileChar
  81.          */
  82.         return type.tileChar;
  83.     }
  84.    
  85.    
  86.     public Tile setupTile(){
  87.         //This function was made much neater by the use of an enum.  
  88.         //check out the cleverest little function that could.
  89.         //This function basically just finalizes creating a tile, by grabbing the appropriate
  90.         //information from TileType and returning a new tile.  
  91.         //thanks to everyone in ##Java on Freenode for their help.
  92.         TileType typeSelector = getTypeByIndex(random.nextInt
  93.                                                     (TileType.values().length - 2) + 2);
  94.         //here we'll create a tile, and then initialize all of it's variables based on our enum.  *PRETTY CLEVER!*
  95.         this.setType((typeSelector));
  96.         return this;
  97.     }
  98.    
  99.     /**
  100.      * @deprecated
  101.      * @overloaded function
  102.      * @param typeSelector
  103.      * @return
  104.      */
  105.     @Deprecated
  106.     public Tile setupTile(int typeSelector){
  107.         //This function over loads setupTile so that it can be used to modify a tile later on.  
  108.         TileType typeSelected = getTypeByIndex(typeSelector);
  109.         //here we'll create a tile, and then initialize all of it's variables based on our enum.  *PRETTY CLEVER!*
  110.         this.setType((typeSelected));
  111.         return this;
  112.     }
  113.    
  114.     public Tile setupTile(TileType type){
  115.         //This function over loads setupTile so that it can be used to modify a tile later on.  
  116.         //here we'll create a tile, and then initialize all of it's variables based on our enum.  *PRETTY CLEVER!*
  117.         this.setType((type));
  118.         return this;
  119.     }
  120.     public void printTileInfo(){
  121.         //Later to be deprecated with Tile::printTileToolTip
  122.         System.out.println(this.getType() + "\nPassable: " + this.isPassable() + "\nSpeedMod: " + this.getSpeedMod());
  123.     }
  124. }
  125. //###End Tile###
  126.  
  127. package game.world;
  128.  
  129. import game.sprites.Unit;
  130.  
  131.  
  132. public enum TileType {
  133.             //Here we're enumerating out our tile types.  This will make it easier to add tiles in the future.  
  134.             /*
  135.              * Params:
  136.              * char tileChar: Debug purposes, just to display the map in console.
  137.              * boolean passable: Whether or not the tile is passable.
  138.              * double speedMod: Any units moving through this tile multiply their speed by this.
  139.              * String sType: The String version of the tile type
  140.              * Unit[5] unitsInMe: Any units that are in the tile.
  141.              * String sprite: The image file to load for this tile.
  142.              */
  143.             WATER('~', false, 0.0, "Water", null, "water.png"),
  144.             SAND('§', true, 0.75, "Sand", null, "sand.png"),
  145.             DIRT('#', true, 1.0, "Dirt", null, "dirt.png"),
  146.             GRASS('"', true, 1.0, "Grass", null, "grass.png"),
  147.             PEBBLES('*', true, 1.0, "Pebbles", null, "pebbles.png"),
  148.             ROCK('^', false, 0.0, "Rock", null, "rocks.png"),
  149.             TREE('†', true, 0.5, "Tree", null, "tree.png");
  150.            
  151.             //same member variables as our parent class.
  152.             public String tileImage = "./resources/images/world/";
  153.             char tileChar;
  154.             boolean passable;
  155.             double speedmod;
  156.             String sType;      
  157.             Unit[] unitsInMe;
  158.             String sprite;
  159.            
  160.             private TileType(char tileChar, boolean passable, double speedMod, String sType, Unit[] unitsInMe, String sprite){
  161.                 //CTOR
  162.                 this.tileChar = tileChar;
  163.                 this.passable = passable;
  164.                 this.speedmod = speedMod;
  165.                 this.sType = sType;
  166.                 this.tileImage = tileImage + sprite;
  167.                 this.unitsInMe = new Unit[5];
  168.            
  169.             }
  170.            
  171.            
  172.            
  173. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement