Advertisement
rooster5105

Untitled

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