Skip to content

Commit

Permalink
More Room Generation
Browse files Browse the repository at this point in the history
Finished up the room generation code. The integrated that into the
DungeonFactory. We have a working game!
  • Loading branch information
devNoiseConsulting committed Jun 12, 2017
1 parent 03db1d3 commit 2e1fe1f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 73 deletions.
91 changes: 39 additions & 52 deletions app/components/DungeonFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ const Wall = require('./Wall');
const Ladder = require('./Ladder');
const Monster = require('./Monster');
const Treasure = require('./Treasure');
const Room = require('./Room');

class DungeonFactory {
makeDungeon(currentState) {
let dungeon = this.initializeWalls(currentState);
let dungeon = this.initializeFloors(currentState);
dungeon = this.initializeRooms(dungeon, currentState);
dungeon = this.initializePlayer(dungeon, currentState);
dungeon = this.initializeTreasure(dungeon, currentState);
Expand Down Expand Up @@ -87,72 +88,58 @@ class DungeonFactory {
return dungeon;
}

initializeWalls(currentState) {
let dungeonSize = currentState.dungeonSize;
verticalWall(dungeon, x, y, height) {
for (let i = x; i < (x + height); i++) {
dungeon[i][y] = new Wall(i, y);
}

let dungeon = new Array(dungeonSize).fill(' ');
dungeon = dungeon.map((row) => {
return new Array(dungeonSize).fill(' ');
});
return dungeon;
}

for (let i = 0; i < dungeonSize; i++) {
for (let j = 0; j < dungeonSize; j++) {
dungeon[i][j] = new Wall(i, j);
}
horizontalWall(dungeon, x, y, width) {
for (let i = y; i < (y + width); i++) {
dungeon[x][i] = new Wall(i, y);
}

return dungeon;
}

initializeRooms(dungeon, currentState) {
initializeFloors(currentState) {
let dungeonSize = currentState.dungeonSize;
let dungeonCenter = Math.ceil(dungeonSize / 2);

let width = 20;
let height = 20;
let direction = 1;
let x = dungeonCenter - Math.floor(width / 2);
let y = dungeonCenter - Math.floor(height / 2);

dungeon = this._addRoom(dungeon, x, y, width, height, direction);
let dungeon = new Array(dungeonSize).fill(' ');
dungeon = dungeon.map((row) => {
return new Array(dungeonSize).fill(' ');
});

return dungeon;
}

_addRoom(dungeon, x, y, width, height, direction) {
let xDirection = 1;
let yDirection = 1;

switch (direction) {
case 3:
xDirection = -1;
yDirection = 1;
break;
case 4:
xDirection = 1;
yDirection = -1;
break;
}

let endX = x + (xDirection * width);
let endY = y + (yDirection * height);

if (x > endX) {
let oldX = x;
x = endX;
endX = oldX;
}
if (y > endY) {
let oldY = y;
y = endY;
endY = oldY;
}
initializeRooms(dungeon, currentState) {
// There are only 50 elements in the array so for creating rooms, the dungeon size is -1;
let dungeonSize = currentState.dungeonSize - 1;
dungeon[dungeonSize][dungeonSize] = new Wall(dungeonSize, dungeonSize);

let dungeonRoom = new Room(0, 0, dungeonSize, dungeonSize);
dungeonRoom.split();
let dungeonRooms = dungeonRoom.traverse();

// Make the Walls
dungeonRooms.forEach(room => {
//console.log(room);
//console.log(dungeon[room.x]);
dungeon = this.verticalWall(dungeon, room.x, room.y, room.width);
dungeon = this.verticalWall(dungeon, room.x, (room.y + room.height), room.width);
dungeon = this.horizontalWall(dungeon, room.x, room.y, room.height);
dungeon = this.horizontalWall(dungeon, (room.x + room.width), room.y, room.height);

for (let i = x; i < endX; i++) {
for (let j = y; j < endY; j++) {
dungeon[i][j] = ' ';
});
// Make the doors
dungeonRooms.forEach(room => {
if (room.door.x !== null) {
dungeon[room.door.x][room.door.y] = ' ';
}
}
});

return dungeon;
}
Expand Down
76 changes: 59 additions & 17 deletions app/components/Room.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
class Room {
constructor(x, y, width, height) {
constructor(x, y, width, height, doorX = null, doorY = null) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.door = {
x: doorX,
y: doorY
};
this.minSize = 6;
this.leftChild = null;
this.rightChild = null;
}

split() {
if (this.leftChild === null && this.rightChild === null) {
if ((this.width > (this.minSize * 2)) || (this.height > (this.minSize * 2))) {
let leftX, leftY;
let rightX, rightY;
let leftWidth, leftHeight;
let rightWidth, rightHeight;
if ((this.width > (this.minSize * 2)) && (this.height > (this.minSize * 2))) {
let leftX = this.x;
let leftY = this.y;
let rightX;
let rightY;
let doorX;
let doorY;
let leftWidth;
let leftHeight;
let rightWidth;
let rightHeight;
let splitSize;

// The split should always be bigger than the minSize.
// The split should be between 1/4 and 3/4 of the original size.
if (this.width > this.height) {
splitSize = Math.floor((this.width / 4) + (Math.random() * this.width / 2));
leftX = this.x;
leftY = this.y;

rightX = this.x + splitSize;
rightY = this.y;

doorX = rightX;
doorY = rightY + 3;

leftWidth = splitSize;
leftHeight = this.height;

rightWidth = this.width - splitSize;
rightHeight = this.height;
} else {
splitSize = Math.floor((this.height / 4) + (Math.random() * this.height / 2));
leftX = this.x;
leftY = this.y;

rightX = this.x;
rightY = this.y + splitSize;

doorX = rightX + 3;
doorY = rightY;

leftWidth = this.width;
leftHeight = splitSize;

Expand All @@ -49,18 +61,48 @@ class Room {
}

// create the rooms
this.leftChild = new Room(leftX, leftY, leftWidth, leftHeight);
this.rightChild = new Room(rightX, rightY, rightWidth, rightHeight);
this.leftChild = new Room(leftX, leftY, leftWidth, leftHeight, doorX, doorY);
this.rightChild = new Room(rightX, rightY, rightWidth, rightHeight, doorX, doorY);

this.leftChild.split();
this.rightChild.split();
}
}
}

traverse() {
let nodeArray = new Array();
nodeArray.push(this);
if (this.leftChild !== null) {
nodeArray = nodeArray.concat(this.leftChild.traverse());
}
if (this.rightChild !== null) {
nodeArray = nodeArray.concat(this.rightChild.traverse());
}
return nodeArray;
}

// Mostly for visual dubugging;
svg() {
let dungeonRooms = this.traverse();

dungeonRooms = dungeonRooms.map(room => `<rect x="${room.x}" y="${room.y}" width="${room.width}" height="${room.height}" stroke="black" stroke-width="2" fill="none"/>`);

dungeonRooms.unshift('<rect x="-10" y="-10" width="100" height="100" stroke="blue" stroke-width="2" fill="none"/>');
dungeonRooms.unshift('<svg width="250" height="250" viewBox="-10 -10 100 100" xmlns="http://www.w3.org/2000/svg">');
dungeonRooms.push('<rect x="0" y="0" width="51" height="51" stroke="blue" stroke-width="2" fill="none"/>');
dungeonRooms.push('</svg>');

return dungeonRooms.join('\n');
}
}

let dungeon = new Room(0, 0, 51, 51);
dungeon.split();
console.log(dungeon);
dungeon.split();
console.log(dungeon);
// Debug/testing code
/*
let dungeonRoom = new Room(0, 0, 50, 50);
dungeonRoom.split();
let svg = dungeonRoom.svg();
console.log(svg);
*/

module.exports = Room;
13 changes: 9 additions & 4 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
</head>

<body>
<div id='app'></div>
<div class="content">

<div class="row">
<div class="col-xs-12 col-md-12">
<a class="btn btn-default btn-block" href="https://github.com/devNoiseConsulting/roguelike-dungeon-crawler">GitHub Repo</a>
<div id='app'></div>

<div class="row">
<div class="col-xs-12 col-md-12">
<a class="btn btn-default btn-block" href="https://github.com/devNoiseConsulting/roguelike-dungeon-crawler">GitHub Repo</a>
</div>
</div>

</div>

</body>

</html>

0 comments on commit 2e1fe1f

Please sign in to comment.