Room Test
by cinderwell
HTML
<canvas id="myCanvas" width="1024" height="1024"></canvas>
JavaScript
/*
This was a simulation I made to see what procedural room generation would look like if we just kept adding a wall with a door, prependicular to existing walls.
Since we're adding a door to each new wall, all of the rooms will always be accessible, but it's kind of an architectural nightmare.
Down the road it'd probably help to have some predefined hallway layouts, and then use this to fill in the areas separated by hallways.
*/
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.lineWidth = 1;
var hTiles = 64;
var vTiles = 64;
var tileSize = 16;
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0,0,1040,1040);
var doorX = [];
var doorY = [];
var wallArray = new Array(65);
for (var i=0; i < 65; i++)
wallArray[i]=new Array(65);
/*
Wall nodes can only project a wall segment Down or Right:
o--
|
Wall node data is stored as such:
00 = no walls
10 = wall projected Down only
01 = wall projected Right only
11 = walls projected both Down and Right
This array is designed to wrap around a 64x64 grid of cells, so your room contents can exists in those cells.
*/
function drawLine(ctc,xs,ys,xe,ye) {
ctx.fillStyle = "#000000";
ctx.beginPath();
ctx.moveTo(xs,ys);
ctx.lineTo(xe,ye);
ctx.stroke();
}
function drawRedLine(ctc,xs,ys,xe,ye) {
ctx.fillStyle = "#FF0000";
ctx.beginPath();
ctx.moveTo(xs,ys);
ctx.lineTo(xe,ye);
ctx.stroke();
}
function getRandInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandInt2(max)
{
return Math.floor((Math.random() * max));
}
//find a suitable wall node
function findWallStart()
{
//fixed:
//It shouldn't start walls at the end of the array length... reducing 65 to 64
var optionsX = [];
var optionsY = [];
//we could pass paremeters to only wall up a subsection
for(var i = 0; i < 64; i++)
{
for(var j = 0; j < 64; j++)
{
var temp = wallArray[i][j];
if(temp == 10 || temp == 1)
{
optionsX.push(i);
...