Advertisement
rooster5105

IslandBuilder.java

Oct 29th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. package com.kfgeeks.java.Mapper;
  2.  
  3. import java.util.Random;
  4.  
  5. public class IslandBuilder {
  6.    
  7.     public static Map run(Map passWorld_Map){
  8.         Map World_Map = passWorld_Map;
  9.         buildIslands(World_Map);
  10.         populateShores(World_Map);
  11.         return World_Map;
  12.     }
  13. private static Map buildIslands(Map passWorld_Map){
  14.        
  15.         Map worldMap = passWorld_Map;
  16.         Island newIsland = new Island();
  17.         Random generator = new Random();
  18.         int numOfIslands = 0;
  19.        
  20.         while (numOfIslands < 20){
  21.            
  22.             newIsland.setMaxHeight(24);
  23.             newIsland.setMinHeight(5);
  24.             newIsland.setMaxWidth(24);
  25.             newIsland.setMinWidth(5);
  26.                        
  27.             newIsland.setHeight(generator.nextInt(newIsland.getMax_height() - newIsland.getMin_height()) + newIsland.getMin_height());
  28.             newIsland.setWidth(generator.nextInt(newIsland.getMax_width() - newIsland.getMin_width()) + newIsland.getMin_width());
  29.            
  30.             System.out.print(newIsland.getHeight() + " -- ");
  31.             System.out.print(newIsland.getWidth() + "\n");
  32.            
  33.             newIsland.islandCells = new Cell[newIsland.getHeight()][];
  34.            
  35.                 for (int i = 0; i < newIsland.islandCells.length; i++){
  36.                 newIsland.islandCells[i]=new Cell[generator.nextInt(newIsland.getMax_width())];
  37.                 for (int j = 0; j < newIsland.islandCells[i].length; j++){
  38.                         newIsland.islandCells[i][j] = new Cell();
  39.                         newIsland.islandCells[i][j].setTerrain(3);
  40.                 }
  41.             }
  42.            
  43.             int startCell_X = generator.nextInt(worldMap.getHeight());
  44.             int startCell_Y = generator.nextInt(worldMap.getWidth());
  45.                
  46.             if ((startCell_X + newIsland.getHeight() < worldMap.getHeight() -1 ) && (startCell_Y + newIsland.getWidth() < worldMap.getWidth()-1)){
  47.                 for (int i = 0; i < newIsland.islandCells.length; i++){
  48.                     for (int j = 0; j < newIsland.islandCells[i].length; j++){
  49.                         worldMap.mapCells[startCell_X + i ][startCell_Y + j].setTerrain(newIsland.islandCells[i][j].getTerrain());
  50.                     }
  51.                 }
  52.             }  
  53.             else{
  54.                 continue;
  55.             }
  56.         numOfIslands++;
  57.         }
  58.         return worldMap;
  59.        
  60.     }
  61.  
  62.    
  63.    
  64.     private static Map populateShores(Map passWorld_Map) {
  65.        
  66.         Map WorldMap = passWorld_Map;
  67.                
  68.         for (int i = 1; i < WorldMap.mapCells.length - 1; i++) {
  69.             for (int j = 1; j < WorldMap.mapCells[i].length -1; j++) {
  70.                 if (WorldMap.mapCells[i][j].getTerrain() == 3){
  71.                     //check surrounding cells for water
  72.                     for (int ii = i - 1; ii <= i + 1; ii++){
  73.                         for (int jj = j -1; jj <= j + 1; jj++){
  74.                             if ((ii == i)&&(jj == j)){
  75.                                 continue;
  76.                             }
  77.                             else if (WorldMap.mapCells[ii][jj].getTerrain() == 0){
  78.                                 WorldMap.mapCells[i][j].setTerrain(1);
  79.                             }
  80.                         }
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.         return WorldMap;   
  86.     }
  87. }
  88.  
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement