Advertisement
KaeruCT

mapgenerator

Feb 16th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package slzy
  2. {
  3.     import org.rndm.Rndm;
  4.     import org.flixel.FlxTilemap;
  5.     public class MapGenerator
  6.     {
  7.         private var width:int;
  8.         private var height:int;
  9.         private var map:Array;
  10.         private var random:Rndm;
  11.         [Embed(source = "../../asset/img/blocks.png")]
  12.         private var blockTiles:Class;
  13.         private const FILLED:int = 1;
  14.         private const EMPTY:int = 0;
  15.        
  16.         public function MapGenerator(width:int, height:int, seed:int=20){
  17.             this.width = width;
  18.             this.height = height;
  19.             this.map = [];
  20.             // base seed on player name or something, to make it interesting
  21.             this.random = new Rndm(seed);
  22.             var i:int;
  23.             var k:int;
  24.            
  25.             // initialize map
  26.             for(i = 0; i < this.height; i++){
  27.                 this.map[i] = [];
  28.                
  29.                 for (k = 0; k < this.width; k++) {
  30.                     // cells start filled, we dig through them later
  31.                     this.map[i][k] = this.FILLED;
  32.                 }
  33.             }
  34.         }
  35.        
  36.         private function getNeighboringWalls(curCell:Array):Array {
  37.            
  38.             var col:int = curCell[0];
  39.             var row:int = curCell[1];
  40.            
  41.             var neighbors:Array = [];
  42.             var walls:Array = [];
  43.            
  44.             if (row > 0) {
  45.                 neighbors.push([col, row-1]);
  46.             }
  47.            
  48.             if(col > 0){
  49.                 neighbors.push([col-1, row]);
  50.             }
  51.            
  52.             if(row < this.height-1){
  53.                 neighbors.push([col, row+1]);
  54.             }
  55.            
  56.             if(col < this.width-1){
  57.                 neighbors.push([col+1, row]);
  58.             }
  59.  
  60.             for each(var neighbor:Array in neighbors) {
  61.                
  62.                 // check if this neighbor is a wall
  63.  
  64.                 if(this.map[neighbor[1]][neighbor[0]] == this.FILLED) {
  65.                     var opCol:int = 0;
  66.                     var opRow:int = 0;
  67.                    
  68.                     // save opposite cell's data
  69.                     if (col == neighbor[0]) {
  70.                         opCol = col;
  71.                         if(neighbor[1] > row){
  72.                             opRow = row + 2;
  73.                         }else{
  74.                             opRow = row - 2;
  75.                         }  
  76.                     } else {
  77.                         opRow = row;
  78.                         if(neighbor[0] > col) {
  79.                             opCol = col + 2;
  80.                         }else{
  81.                             opCol = col - 2;
  82.                         }
  83.                     }
  84.  
  85.                     // checking if the opposite cell is not out of bounds
  86.                     if(opRow < height && opRow >= 0 && opCol < width && opCol >= 0){
  87.                         // it must be a wall too
  88.  
  89.                         if (this.map[opRow][opCol] == this.FILLED) {
  90.                             neighbor.push(opCol, opRow);
  91.                             walls.push(neighbor);
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.        
  97.             return walls;
  98.         }
  99.        
  100.         public function generate():FlxTilemap {
  101.            
  102.             var curCell:Array = [this.random.integer(0, this.width), this.random.integer(0, this.height)]
  103.             var carvList:Array = [];
  104.             var i:int = 0;
  105.            
  106.             this.map[curCell[1]][curCell[0]] = this.EMPTY; // empty current cell
  107.             carvList.push(curCell);
  108.  
  109.             while(carvList.length > 0)
  110.             {
  111.                 var neighbors:Array = this.getNeighboringWalls(curCell);
  112.                 var cell:Array;
  113.  
  114.                 if(neighbors.length > 0){
  115.                     random.shuffle(neighbors);
  116.                     cell = neighbors[0];
  117.                     this.map[cell[1]][cell[0]] = this.EMPTY; // empty neighbor
  118.                     this.map[cell[3]][cell[2]] = this.EMPTY; // empty opposite cell from neighbor
  119.                     carvList.push([cell[2], cell[3]]); // add opposite cell to carve list
  120.                 }else {
  121.                     carvList.pop();
  122.                 }
  123.                
  124.                 random.shuffle(carvList);
  125.                 curCell = carvList[carvList.length-1];
  126.                 trace(carvList.length);
  127.                
  128.                 //if (i == 20) break;
  129.                 i++;
  130.             }
  131.            
  132.             trace("end: "+i);
  133.            
  134.             var tileMap:FlxTilemap = new FlxTilemap();
  135.             var settings:GameSettings = GameSettings.getInstance();
  136.            
  137.             tileMap.loadMap(this.buildString(), this.blockTiles, settings.TILE_SIZE, settings.TILE_SIZE);
  138.             return tileMap;
  139.         }
  140.        
  141.         private function buildString():String {
  142.            
  143.             var mapString:String = "";
  144.            
  145.             for each(var row:Array in this.map) {
  146.  
  147.                 mapString += row.join(",")+"\n";
  148.             }
  149.            
  150.             return mapString;
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement