xxxxxxxxxx
190
/*
HOW TO CREATE A TBOI like dungeon?
1.Create 2D Array (the grid)
2.Select a point in the array and set it to be the start (this can be random)
3.Select a random cell from the grid.
4.If the selected cell have a grid next to it, transform the cell to a room, else repet 3.*
5.Repet 3-4 until have all the rooms you have
6.**
*in this point you could make and extra step and see if the cell have more than x neighbours and dont create a room if so, that make the dungeon more large avoiding making a dungeon to much like a square
** to make special rooms like bosses its simple. just chek for a room with no more neighbours than one and that room it will be the end of a corridor!
making this step without the 4* step could return some errors
for more info heres a link
http://adampajor.blogspot.com/2012/07/simple-dungeon-generator-algorithm.html
*/
let ws = 10; //world Size
let cellsS = 25;
var g = [];
let numberOfRooms = 15;
let reservedX = [],
reservedY = [];
let p = [];
function setup() {
createCanvas(500, 500);
rectMode(CENTER);
noStroke();
if (numberOfRooms > ws * ws) {
noLoop();
return
}
for (i = 0; i < ws; i++) {
g[i] = [];
}
//1. Create and emty grid
for (x = 0; x < ws; x++) {
for (y = 0; y < ws; y++) {
g[x][y] = new Grid(x, y, 0);
}
}
//2.start point
let startPos = GetRandomVec();
g[startPos.x][startPos.y].type = 2;
reservedX.push(startPos.x);
reservedY.push(startPos.y);
for (let iii = 0; iii < numberOfRooms - 1;) {
//3.select a random cell
let rp = GetRandomVec();
let cc = g[rp.x][rp.y];
//4. chek neighbours
let maybe = chekN(cc) == 2&&random()<0.5;
if(chekN(cc) == 1||maybe){//changin == for >= is 4**
//5. repete 3-4
iii++;
if (cc.type != 1) {
cc.type = 1;
} else {
iii--;
}
}
}
let fb = false;
//6**
for (x = 0; x < ws; x++) {
for (y = 0; y < ws; y++) {
if (g[x][y].type == 1 && chekN(g[x][y]) == 1) {
if (!fb) {
g[x][y].type = 4;
fb = true;
} else {
g[x][y].type = 3;
}
}
}
}
g[startPos.x][startPos.y].type = 2;
}
function draw() {
background(128);
for (x = 0; x < ws; x++) {
for (y = 0; y < ws; y++) {
g[x][y].display();
}
}
}
function chekN(cell) { //cheking all neighbours
let nn = 0; // number of neighbours
if (cell.pos.x + 1 !== ws) {
if (g[cell.pos.x + 1][cell.pos.y].type >= 1) {
nn++;
}
}
if (cell.pos.x - 1 !== -1) {
if (g[cell.pos.x - 1][cell.pos.y].type >= 1) {
nn++;
}
}
if (cell.pos.y + 1 !== ws) {
if (g[cell.pos.x][cell.pos.y + 1].type >= 1) {
nn++;
}
}
if (cell.pos.y - 1 !== -1) {
if (g[cell.pos.x][cell.pos.y - 1].type >= 1) {
nn++;
}
}
return nn;
}
function GetRandomVec() {
let rX = int(random(ws))
reservedX.push(rX);
let rY = int(random(ws))
reservedY.push(rY);
if (xIsInArray(rX, reservedX) || xIsInArray(rX, reservedX)) {
return GetRandomVec();
}
return createVector(rX, rY);
}
function xIsInArray(x, a) {
for (j = 0; j < a.length; j++) {
if (x == a) {
return true;
}
}
return false;
}
// CLASSES
class Grid {
constructor(x, y, t) {
this.pos = createVector(x, y);
this.type = t;
}
display() { // 0 = no room, 1 = normal room, 2 = start room, 3 = special room
switch (this.type) {
default:
fill(128);
break;
case 0:
fill(100);
break;
case 1:
fill(255);
break;
case 2:
fill(0, 255, 0);
break;
case 3:
fill(255, 255, 0);
break;
case 4:
fill(255, 0, 0);
break;
}
rect(this.pos.x * cellsS * 2 + cellsS, this.pos.y * cellsS * 2 + cellsS, cellsS, cellsS);
}
}