package com.gamingyourway.Dungeon { /** * Cell datatype * @version 2009 06 24 * @author nGFX */ public class Cell { public static const WALL:int = 0; public static const OPEN:int = 1; private var _aWall:Array; // array of values ... 0=empty ... private var _iValue:int; // stores a single value, ie. room num private var _bVisited:Boolean; // has cell been visited private var _bCorridor:Boolean; // is cell a corridor public function get aWall ():Array { return this._aWall; } public function set aWall (value:Array):void { this._aWall = value; } public function get iValue ():int { return this._iValue; } public function set iValue (value:int):void { this._iValue = value; } public function get bVisited ():Boolean { return this._bVisited; } public function set bVisited (value:Boolean):void { this._bVisited = value; } public function get bCorridor ():Boolean { return this._bCorridor; } public function set bCorridor (value:Boolean):void { this._bCorridor = value; } public function get wallCount ():int { var i:uint; var iCount:int = 0; for (i = 0; i < this._aWall.length; i++) { if (this._aWall[i] == 0) iCount++; } return iCount; } public function get isDeadEnd ():Boolean { return (this.wallCount == 3); } public function get isUnused ():Boolean { return (this.wallCount == 4); } /** * Creates a new empty (ie. all walls set) cells * @param iValue used to stor a single bit of info, ie. room num */ public function Cell (iValue:int = 0) { this._aWall = [0, 0, 0, 0]; this._iValue = iValue; this._bVisited = false; this._bCorridor = false; } /** * sets a single wall * @param iDir Direction of the wall * @param iWall value of the wall, 0 is a solid wall, any other value makes it "open" */ public function setWall (iDir:int, iWall:int = 0):void { this._aWall[iDir] = iWall; } /** * return the value if the wall in iDIr * @param iDir direction of the wall to get * @return value of the wall */ public function getWall (iDir:int):uint { return this._aWall[iDir]; } /** * shortcut for testing if there is a closed wall in iDir * @param iDir direction to test * @return true if there is a wall */ public function hasWall (iDir:int):Boolean { return (this._aWall[iDir] == 0); } /** * if the cell is a dead end, return the direction of the opening * @return the direction of the opening */ public function getDeadEndDir ():int { var iReturn:int = -1; // not a dead end if (isDeadEnd) { if (this._aWall[Dir.NORTH] != 0) iReturn = Dir.NORTH; if (this._aWall[Dir.EAST] != 0) iReturn = Dir.EAST; if (this._aWall[Dir.SOUTH] != 0) iReturn = Dir.SOUTH; if (this._aWall[Dir.WEST] != 0) iReturn = Dir.WEST; } return iReturn; } /** * returns a string representation of the cell * @return a string for the falls of this cell */ public function toString ():String { var i:uint; var strReturn:String = ""; for (i = 0; i < this._aWall.length; i++) { strReturn += this._aWall[i].toString(); } return strReturn; } } }
hasCellInDir (pPos:Point, iDir:uint):BooleangetNextPos (pPos:Point, iDir:uint):PointgetSurroundingCells (pPos:Point, bUsed:Boolean = false):Array
package com.gamingyourway.Dungeon { import de.drygoods.Random.MersenneTwister; import flash.geom.Point; /** * Dungeon generator * @version 2009 06 24 * @author nGFX */ public class DungeonGenerator { private var _Dungeon:Dungeon; private var _rnd:MersenneTwister; public function DungeonGenerator(iWidth:int, iHeight:int) { this._Dungeon = new Dungeon(iWidth, iHeight); this._rnd = MersenneTwister.getInstance(); } public function createMaze (iDirChange:int = 100):Dungeon { var aCellStack:Array = new Array(); var aCellNeighbors:Array; var iTotalCells:int = this._Dungeon.iWidth * this._Dungeon.iHeight; var iCellCount:int = 1; var iRndCell:int; var iRndDir:int; var pCurrentCell:Point = new Point(this._rnd.Range(0, this._Dungeon.iWidth -1), this._rnd.Range(0, this._Dungeon.iHeight -1)); var pPopCell:Point; while (iCellCount < iTotalCells) { // get neighbor cells ... aCellNeighbors = this._Dungeon.getSurroundingCells(pCurrentCell); // set the cell if (aCellNeighbors.length != 0) { iRndCell = this._rnd.Range(0, (aCellNeighbors.length - 1)); iRndDir = Dir.getDirFromPoint(pCurrentCell, aCellNeighbors[iRndCell]); // remove walls this._Dungeon.cell(pCurrentCell).setWall (iRndDir, 1); this._Dungeon.cell(aCellNeighbors[iRndCell]).setWall(Dir.getOppositeDir(iRndDir), 1); // store for later use ... aCellStack.push(new Point(pCurrentCell.x, pCurrentCell.y)); pCurrentCell = new Point(aCellNeighbors[iRndCell].x, aCellNeighbors[iRndCell].y); iCellCount++; } else { pPopCell = aCellStack.pop(); pCurrentCell = new Point(pPopCell.x, pPopCell.y); } } // while return this._Dungeon; } } }
Powered by: newtelligence dasBlog 1.9.6264.0
Disclaimer The opinions expressed herein are our personal opinions and do not represent our customer's view in anyway.
© Copyright 2010, gaming your way
E-mail