xxxxxxxxxx
251
//https://www.reddit.com/r/roguelikedev/comments/3n63hw/faq_friday_22_map_generation/
//door-based dungeon
const doors = {
top: 'top',
left: 'left',
right: 'right',
bot: 'bot'
}
let T = 33;
let f = true;
let rooms;
function setup() {
createCanvas(394, 394);
noStroke();
rooms = [];
for (i = 0; i < T; i++) {
rooms[i] = new Array();
}
for (x = 0; x < T; x++) {
for (y = 0; y < T; y++) {
rooms[x][y] = new Cell(x * 12, y * 12, true);
if(x==0||x==T-1||y==0||y==T-1)
rooms[x][y].end = true;
}
}
var I
do {
I = Math.floor(Math.random() * T - 2);
} while (I < 10 || I > T - 10);
print(I);
rooms[I][I].empty = false;
rooms[I][I].start = true;
rooms[I][I].topD = true;
rooms[I][I].rightD = true;
rooms[I][I].leftD = true;
rooms[I][I].botD = true;
}
function draw(){
CreateDungeon();
background(10);
for (x = 0; x < T; x++) {
for (y = 0; y < T; y++) {
rooms[x][y].displayCell();
}
}
}
function CreateDungeon() {
for (i = 0; i < 10; i++) {
for (x = 0; x < T; x++) {
for (y = 0; y < T; y++) { //loop every room
if (!rooms[x][y].empty) {
//entra aqui quan una casella no esta buida
CreateRooms(x, y);
}
}
}
}
for (x = 0; x < T; x++) {
for (y = 0; y < T; y++) {
if (!rooms[x][y].empty) {
//tenca les habitacions
CreateFinalRoom(x, y);
}
}
}
}
function CreateRooms(x, y) {
//intent de prevenir l'error que surgeix quan sintenta crear una habitacio al limit del grid
/*
if (rooms[x + 1][y].end) {
rooms[x][y].leftD = false;
return;
}
if (rooms[x - 1][y].end) {
rooms[x][y].rightD = false;
return;
}
if (rooms[x][y + 1].end) {
rooms[x][y].botD = false;
return;
}
if (rooms[x][y - 1].end) {
rooms[x][y].topD = false;
return;
}*/
if(rooms[x][y].end){
CreateEndRoom(x,y);
return;
}
//si la room te una porta, es crea una habitacio en aquella direccio. Aquesta room triara aleatoriament si tindra portes o no
if (rooms[x][y].topD && rooms[x][y - 1].empty) {
rooms[x][y - 1].empty = false;
rooms[x][y - 1].botD = true;
rooms[x][y - 1].topD = RandomBool();
rooms[x][y - 1].leftD = RandomBool();
rooms[x][y - 1].rightD = RandomBool();
return;
}
if (rooms[x][y].leftD && rooms[x - 1][y].empty) {
rooms[x - 1][y].empty = false;
rooms[x - 1][y].rightD = true;
rooms[x - 1][y].topD = RandomBool();
rooms[x - 1][y].leftD = RandomBool();
rooms[x - 1][y].botD = RandomBool();
return;
}
if (rooms[x][y].rightD && rooms[x + 1][y].empty) {
rooms[x + 1][y].empty = false;
rooms[x + 1][y].leftD = true;
rooms[x + 1][y].topD = RandomBool();
rooms[x + 1][y].rightD = RandomBool();
rooms[x + 1][y].botD = RandomBool();
return;
}
if (rooms[x][y].botD && rooms[x][y + 1].empty) {
rooms[x][y + 1].empty = false;
rooms[x][y + 1].topD = true;
rooms[x][y + 1].leftD = RandomBool();
rooms[x][y + 1].rightD = RandomBool();
rooms[x][y + 1].botD = RandomBool();
return;
}
}
function CreateFinalRoom(x, y) {
//if aquesta porta te una door without another room create a room
if (rooms[x][y].topD && rooms[x][y - 1].empty) {
CreateOneDoorRoom(x, y - 1, 'bot');
}
if (rooms[x][y].leftD && rooms[x - 1][y].empty) {
CreateOneDoorRoom(x - 1, y, 'right');
}
if (rooms[x][y].rightD && rooms[x + 1][y].empty) {
CreateOneDoorRoom(x + 1, y, 'left');
}
if (rooms[x][y].botD && rooms[x][y + 1].empty) {
CreateOneDoorRoom(x, y + 1, 'top');
}
}
function CreateEndRoom(x,y){
if (rooms[x][y].topD) {
CreateOneDoorRoom(x, y - 1, 'bot');
}
if (rooms[x][y].leftD) {
CreateOneDoorRoom(x - 1, y, 'right');
}
if (rooms[x][y].rightD) {
CreateOneDoorRoom(x + 1, y, 'left');
}
if (rooms[x][y].botD) {
CreateOneDoorRoom(x, y + 1, 'top');
}
}
function CreateOneDoorRoom(x, y, doors) {
//crea una habitacio amb una sola porta, la de entrada. Aquesta habitacio pot existis sense voler
switch (doors) {
case 'top':
//room with a bot door
rooms[x][y].empty = false;
rooms[x][y].topD = true;
if (f)
rooms[x][y].finish = true;
break;
case 'bot':
//room with a top door
rooms[x][y].empty = false;
rooms[x][y].botD = true;
if (f)
rooms[x][y].finish = true;
break;
case 'left':
//room with right door
rooms[x][y].empty = false;
rooms[x][y].leftD = true;
if (f)
rooms[x][y].finish = true;
break;
case 'right':
//room with left door
rooms[x][y].empty = false;
rooms[x][y].rightD = true;
if (f)
rooms[x][y].finish = true;
break;
}
}
//funcio que tria aleatoriament entre true i false
function RandomBool() {
if (Math.floor(Math.random() * 10) > 5)
return true;
else
return false;
}