Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package slzy
- {
- import org.rndm.Rndm;
- import org.flixel.FlxTilemap;
- public class MapGenerator
- {
- private var width:int;
- private var height:int;
- private var map:Array;
- private var random:Rndm;
- [Embed(source = "../../asset/img/blocks.png")]
- private var blockTiles:Class;
- private const FILLED:int = 1;
- private const EMPTY:int = 0;
- public function MapGenerator(width:int, height:int, seed:int=20){
- this.width = width;
- this.height = height;
- this.map = [];
- // base seed on player name or something, to make it interesting
- this.random = new Rndm(seed);
- var i:int;
- var k:int;
- // initialize map
- for(i = 0; i < this.height; i++){
- this.map[i] = [];
- for (k = 0; k < this.width; k++) {
- // cells start filled, we dig through them later
- this.map[i][k] = this.FILLED;
- }
- }
- }
- private function getNeighboringWalls(curCell:Array):Array {
- var col:int = curCell[0];
- var row:int = curCell[1];
- var neighbors:Array = [];
- var walls:Array = [];
- if (row > 0) {
- neighbors.push([col, row-1]);
- }
- if(col > 0){
- neighbors.push([col-1, row]);
- }
- if(row < this.height-1){
- neighbors.push([col, row+1]);
- }
- if(col < this.width-1){
- neighbors.push([col+1, row]);
- }
- for each(var neighbor:Array in neighbors) {
- // check if this neighbor is a wall
- if(this.map[neighbor[1]][neighbor[0]] == this.FILLED) {
- var opCol:int = 0;
- var opRow:int = 0;
- // save opposite cell's data
- if (col == neighbor[0]) {
- opCol = col;
- if(neighbor[1] > row){
- opRow = row + 2;
- }else{
- opRow = row - 2;
- }
- } else {
- opRow = row;
- if(neighbor[0] > col) {
- opCol = col + 2;
- }else{
- opCol = col - 2;
- }
- }
- // checking if the opposite cell is not out of bounds
- if(opRow < height && opRow >= 0 && opCol < width && opCol >= 0){
- // it must be a wall too
- if (this.map[opRow][opCol] == this.FILLED) {
- neighbor.push(opCol, opRow);
- walls.push(neighbor);
- }
- }
- }
- }
- return walls;
- }
- public function generate():FlxTilemap {
- var curCell:Array = [this.random.integer(0, this.width), this.random.integer(0, this.height)]
- var carvList:Array = [];
- var i:int = 0;
- this.map[curCell[1]][curCell[0]] = this.EMPTY; // empty current cell
- carvList.push(curCell);
- while(carvList.length > 0)
- {
- var neighbors:Array = this.getNeighboringWalls(curCell);
- var cell:Array;
- if(neighbors.length > 0){
- random.shuffle(neighbors);
- cell = neighbors[0];
- this.map[cell[1]][cell[0]] = this.EMPTY; // empty neighbor
- this.map[cell[3]][cell[2]] = this.EMPTY; // empty opposite cell from neighbor
- carvList.push([cell[2], cell[3]]); // add opposite cell to carve list
- }else {
- carvList.pop();
- }
- random.shuffle(carvList);
- curCell = carvList[carvList.length-1];
- trace(carvList.length);
- //if (i == 20) break;
- i++;
- }
- trace("end: "+i);
- var tileMap:FlxTilemap = new FlxTilemap();
- var settings:GameSettings = GameSettings.getInstance();
- tileMap.loadMap(this.buildString(), this.blockTiles, settings.TILE_SIZE, settings.TILE_SIZE);
- return tileMap;
- }
- private function buildString():String {
- var mapString:String = "";
- for each(var row:Array in this.map) {
- mapString += row.join(",")+"\n";
- }
- return mapString;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement