【发布时间】:2017-09-04 19:25:18
【问题描述】:
https://www.gamasutra.com/blogs/AAdonaac/20150903/252889/Procedural_Dungeon_Generation_Algorithm.php
我正在尝试做类似 jquery 中的链接之类的事情,但我在实现它时遇到了一些麻烦。
1) 我创建了 10(x) 个不同的矩形并将它们放在画布的中间,它们相互混合在一起 - 没问题
2) 分开房间,让每个房间在画布上找个空的地方——大问题
计划 1:我比较两个房间,如果它们相互接触,则将其中一个以最短的方式移动到另一个的边缘。但是,如果那里已经有房间怎么办?然后我刚刚得到了两个相互接触的新房间......
计划 2:制作一系列空/占用点,并将每个房间移动到一个空的地方。但是我怎么知道要移动的方向,所以所有房间都围绕中心放置?
请帮忙!
试用码:
<canvas id="cnvs" width="500" height="500" style="background-color: #333;"></canvas>
var rooms = [],
gw = 25, //grid width
gh = 25; //grid height
//Room
function Room(x,y,w,h){
this.x = x; this.y = y;
this.w = w; this.h = h;
};
//Setup
for(var i=0; i<10; i++){
var w = Math.floor(2+Math.random()*3),
h = Math.floor(2+Math.random()*3),
x = Math.floor(gw/2)-Math.floor(w/2),
y = Math.floor(gh/2)-Math.floor(h/2),
room = new Room(x,y,w,h);
rooms.push(room);
};
//Draw in canvas
var ctx = $("#cnvs")[0].getContext("2d"),
sz = 20;
ctx.lineWidth = "2";
ctx.strokeStyle = 'white';
for(var i=0; i<rooms.length; i++){
ctx.rect(rooms[i].x*sz,rooms[i].y*sz,rooms[i].w*sz,rooms[i].h*sz);
ctx.stroke();
};
【问题讨论】:
-
你能解释一下什么不起作用吗?问题出在哪里?
-
第 1 步有效,但我对如何执行第 2 步(单独的房间)感到非常困惑。如何像链接一样分隔房间,使没有房间覆盖另一个房间?