//-------------------マップ関連----------------------
public var minRoomSize:int; //最小部屋サイズ
public var maxRoomSize:int; //最大部屋サイズ
public var mapWidth:int; //アップの幅
public var mapHeight:int; //アップの高さ
public var mapData:Array; //マップデータ
public var tileRectList:Array; //区画情報
private var m_upStepX:int;
private var m_upStepY:int;
private var m_downStepX:int;
private var m_downStepY:int;
function CreateDungeon():Array{
tileRectList = new Array();
CreateRect(0, 0, mapWidth-1, mapHeight-1);
var i = Random.Range(0,2);
Debug.Log("分割数" + tileRectList.length);
function GetTileData(x:int, y:int):DungeonTileData{
return mapData[x + y * mapWidth];
// 指定矩形を壁か道にする(right,bottom自体のマスは塗らない)
private function FillRect(left:int, top:int, right:int, bottom:int, isWall:boolean, isRoom:boolean):void{
for( y=top; y<bottom; y++ ){
for( x=left; x<right; x++ ){
var tile:DungeonTileData = mapData[y * mapWidth + x];
private function CreateRoom():void{
var upStepRect:int = Random.Range(0, tileRectList.length - 1);
for( i=0; i<tileRectList.length; i++ ){
//Debug.Log("区画の位置:左" + rect.rect_left + " 右" + rect.rect_right + " 上" + rect.rect_top + " 下" + rect.rect_bottom);
// 矩形の大きさを計算 壁1ます 空き1ます 分割線1ますあける
// Debug.Log("部屋の大きさの余裕は 横:" + cw + "縦:" + ch);
sw = Random.Range(0, cw) + minRoomSize;
sh = Random.Range(0, ch) + minRoomSize;
if(sw > maxRoomSize) sw = maxRoomSize;
if(sh > maxRoomSize) sh = maxRoomSize;
Debug.Log("部屋の大きさは 横:" + sw + "縦:" + sh);
// 部屋の位置を決定する 分割線1ますを除いた2ます分の補正
rx = Random.Range(0, rw) + 2;
ry = Random.Range(0, rh) + 2;
var left:int = rect.rect_left + rx;
var right:int = left + sw;
var top:int =rect.rect_top + ry;
var bottom:int = top + sh;
rect.setRoom(left, top, right, bottom);
FillRect( rect.room_left, rect.room_top, rect.room_right, rect.room_bottom, false ,true);
// のぼり階段設置する部屋なら適当な位置に上階段を配置
m_upStepX = rect.room_left + x;
m_upStepY = rect.room_top + y;
var tile:DungeonTileData = GetTileData(m_upStepX, m_upStepY);
if( i ==tileRectList.length-1 ){
m_downStepX = rect.room_left + x;
m_downStepY = rect.room_top + y;
tile = GetTileData(m_downStepX, m_downStepY);
private function CreateRoad(rectANo:int, rectBNo:int):void{
var rectA:DungeonRect = tileRectList[rectANo];
var rectB:DungeonRect = tileRectList[rectBNo];
// 区画は上下か左右のどちらで繋がっているかで処理をわける
if(rectA.rect_bottom == rectB.rect_top || rectA.rect_top == rectB.rect_bottom){
//道の開始位置 // TODO or left right
x1 = Random.Range(0, rectA.getRoomW()) + rectA.room_left;
x2 = Random.Range(0, rectB.getRoomW()) + rectB.room_left;
if( rectA.rect_top > rectB.rect_top ){
FillRect( x1, y+1, x1+1, rectA.room_top, false, false); // Aと横道をつなぐ道を作る
FillRect( x2, rectB.room_bottom, x2+1, y, false, false); // Bと横道をつなぐ道を作る
FillRect( x1, rectA.room_bottom, x1+1, y, false, false);
FillRect( x2, y, x2+1, rectB.room_top, false, false);
FillHLine( x1, x2, y, false ); // 横道を作る
}else if(rectA.rect_left == rectB.rect_right || rectA.rect_right == rectB.rect_left){
y1 = Random.Range(0, rectA.getRoomH()) + rectA.room_top;
y2 = Random.Range(0, rectB.getRoomH()) + rectB.room_top;
if( rectA.rect_left > rectB.rect_left ){
FillRect( rectB.room_right, y2, x, y2+1, false ,false);
FillRect( x+1, y1, rectA.room_left, y1+1, false ,false);
FillRect( rectA.room_right, y1, x, y1+1, false ,false);
FillRect( x, y2, rectB.room_left, y2+1, false ,false);
FillVLine( y1, y2, x, false); // 横道を作る
private function FillHLine( left:int, right:int, y:int, isWall:boolean):void{
for( x=left; x<=right; x++ ){
var tile:DungeonTileData = mapData[y * mapWidth + x];
private function FillVLine( top:int, bottom:int, x:int, isWall:boolean):void{
for( y=top; y<=bottom; y++ ){
var tile:DungeonTileData = mapData[y * mapWidth + x];
private function ConnectRoom():void{
for( var i:int =0; i<tileRectList.length -1; i++ ){
private function SplitRect(isVertical:boolean):void{
parent = tileRectList[tileRectList.length -1];
if( parent.getRectW() < (minRoomSize+3)*2+1 ){
// 左端のA点を求める 最小の部屋のサイズと通路の分は取っておく
a = parent.rect_left + minRoomSize + 2;
b = parent.rect_right - minRoomSize - 2;
if(maxRoomSize < ab) ab = maxRoomSize;
p = a + Random.Range(0, ab) + 1;
child = new DungeonRect();
child.setRect( p, parent.rect_top, parent.rect_right, parent.rect_bottom);
tileRectList.push(child);
// 元の区画の右を p 地点に移動させて、左側の区画とする
parent.rect_right = child.rect_left;
if( ( parent.getRectH()) < (minRoomSize+3)*2+1 ){
// 上端のA点を求める 最小の部屋のサイズと通路の分は取っておく
a = parent.rect_top + minRoomSize + 2;
b = parent.rect_bottom - minRoomSize - 2;
if(maxRoomSize < ab) ab = maxRoomSize;
p = a + Random.Range(0, ab) + 1;
child= new DungeonRect();
child.setRect(parent.rect_left, p, parent.rect_right, parent.rect_bottom);
tileRectList.push(child);
// 元の区画の右を p 地点に移動させて、左側の区画とする
parent.rect_bottom = child.rect_top;
// 次の分割をランダムで決定するために入れ替える
var rect:DungeonRect = tileRectList[tileRectList.length - 1];
tileRectList[tileRectList.length - 1] = tileRectList[tileRectList.length - 2];
tileRectList[tileRectList.length - 2] = rect;
var rect1:DungeonRect = tileRectList[tileRectList.length-1];
var rect2:DungeonRect = tileRectList[tileRectList.length-2];
Debug.Log("できた区画1:左" + rect1.rect_left + " 右" + rect1.rect_right + " 上" + rect1.rect_top + " 下" + rect1.rect_bottom);
Debug.Log("できた区画2:左" + rect2.rect_left + " 右" + rect2.rect_right + " 上" + rect2.rect_top + " 下" + rect2.rect_bottom);
SplitRect( !isVertical );
private function CreateRect(left:int, top:int, right:int, bottom:int):void{
var dunRect:DungeonRect = new DungeonRect();
dunRect.rect_right = mapWidth - 1;
dunRect.rect_bottom = mapHeight - 1;
tileRectList[0] = dunRect;
for(var y:int = 0; y < mapHeight; y++){
for(var x:int = 0; x < mapWidth; x++){
var tileData:DungeonTileData = new DungeonTileData();
mapData[y * mapWidth + x] = tileData;