Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kfgeeks.java.Mapper;
- import java.util.Random;
- public class IslandBuilder {
- public static Map run(Map passWorld_Map){
- Map World_Map = passWorld_Map;
- buildIslands(World_Map);
- populateShores(World_Map);
- return World_Map;
- }
- private static Map buildIslands(Map passWorld_Map){
- Map worldMap = passWorld_Map;
- Island newIsland = new Island();
- Random generator = new Random();
- int numOfIslands = 0;
- while (numOfIslands < 20){
- newIsland.setMaxHeight(24);
- newIsland.setMinHeight(5);
- newIsland.setMaxWidth(24);
- newIsland.setMinWidth(5);
- newIsland.setHeight(generator.nextInt(newIsland.getMax_height() - newIsland.getMin_height()) + newIsland.getMin_height());
- newIsland.setWidth(generator.nextInt(newIsland.getMax_width() - newIsland.getMin_width()) + newIsland.getMin_width());
- System.out.print(newIsland.getHeight() + " -- ");
- System.out.print(newIsland.getWidth() + "\n");
- newIsland.islandCells = new Cell[newIsland.getHeight()][];
- for (int i = 0; i < newIsland.islandCells.length; i++){
- newIsland.islandCells[i]=new Cell[generator.nextInt(newIsland.getMax_width())];
- for (int j = 0; j < newIsland.islandCells[i].length; j++){
- newIsland.islandCells[i][j] = new Cell();
- newIsland.islandCells[i][j].setTerrain(3);
- }
- }
- int startCell_X = generator.nextInt(worldMap.getHeight());
- int startCell_Y = generator.nextInt(worldMap.getWidth());
- if ((startCell_X + newIsland.getHeight() < worldMap.getHeight() -1 ) && (startCell_Y + newIsland.getWidth() < worldMap.getWidth()-1)){
- for (int i = 0; i < newIsland.islandCells.length; i++){
- for (int j = 0; j < newIsland.islandCells[i].length; j++){
- worldMap.mapCells[startCell_X + i ][startCell_Y + j].setTerrain(newIsland.islandCells[i][j].getTerrain());
- }
- }
- }
- else{
- continue;
- }
- numOfIslands++;
- }
- return worldMap;
- }
- private static Map populateShores(Map passWorld_Map) {
- Map WorldMap = passWorld_Map;
- for (int i = 1; i < WorldMap.mapCells.length - 1; i++) {
- for (int j = 1; j < WorldMap.mapCells[i].length -1; j++) {
- if (WorldMap.mapCells[i][j].getTerrain() == 3){
- //check surrounding cells for water
- for (int ii = i - 1; ii <= i + 1; ii++){
- for (int jj = j -1; jj <= j + 1; jj++){
- if ((ii == i)&&(jj == j)){
- continue;
- }
- else if (WorldMap.mapCells[ii][jj].getTerrain() == 0){
- WorldMap.mapCells[i][j].setTerrain(1);
- }
- }
- }
- }
- }
- }
- return WorldMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement