diff options
Diffstat (limited to 'html')
-rw-r--r-- | html/broughlike/index.html | 105 |
1 files changed, 35 insertions, 70 deletions
diff --git a/html/broughlike/index.html b/html/broughlike/index.html index 2a768f5..e0028a0 100644 --- a/html/broughlike/index.html +++ b/html/broughlike/index.html @@ -285,86 +285,51 @@ walls = []; const MIN_ROOM_SIZE = 2; - class Node { - constructor(x, y, width, height) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; - this.left = null; - this.right = null; - this.room = null; - } - - split() { - if (this.left || this.right) return false; - - const splitHorizontally = Math.random() > 0.5; - const max = (splitHorizontally ? this.height : this.width) - MIN_ROOM_SIZE; - - if (max <= MIN_ROOM_SIZE) return false; - - const split = Math.floor(Math.random() * (max - MIN_ROOM_SIZE)) + MIN_ROOM_SIZE; + function splitNode(x, y, width, height) { + const splitHorizontally = Math.random() > 0.5; + const max = (splitHorizontally ? height : width) - MIN_ROOM_SIZE; - if (splitHorizontally) { - this.left = new Node(this.x, this.y, this.width, split); - this.right = new Node(this.x, this.y + split, this.width, this.height - split); - } else { - this.left = new Node(this.x, this.y, split, this.height); - this.right = new Node(this.x + split, this.y, this.width - split, this.height); - } + if (max <= MIN_ROOM_SIZE) return [{ x, y, width, height }]; - return true; - } + const split = Math.floor(Math.random() * (max - MIN_ROOM_SIZE)) + MIN_ROOM_SIZE; - createRooms() { - if (this.left || this.right) { - // If the node has children, recursively create rooms in the children - if (this.left) this.left.createRooms(); - if (this.right) this.right.createRooms(); - } else { - // If the node has no children, create a room within the node - const roomWidth = Math.floor(Math.random() * (this.width - 1)) + 1; // Random room width - const roomHeight = Math.floor(Math.random() * (this.height - 1)) + 1; // Random room height - const roomX = this.x + Math.floor(Math.random() * (this.width - roomWidth)); // Random x position inside of node - const roomY = this.y + Math.floor(Math.random() * (this.height - roomHeight)); // Random y position inside of node - this.room = { x: roomX, y: roomY, width: roomWidth, height: roomHeight }; // Assign the room to the node - } + if (splitHorizontally) { + return [ + ...splitNode(x, y, width, split), + ...splitNode(x, y + split, width, height - split) + ]; + } else { + return [ + ...splitNode(x, y, split, height), + ...splitNode(x + split, y, width - split, height) + ]; } } - const root = new Node(0, 0, GRID_SIZE, GRID_SIZE); - const nodes = [root]; - - while (nodes.length) { - const node = nodes.pop(); - if (node.split()) { - nodes.push(node.left, node.right); - } + function createRoom(node) { + const roomWidth = Math.floor(Math.random() * (node.width - 1)) + 1; + const roomHeight = Math.floor(Math.random() * (node.height - 1)) + 1; + const roomX = node.x + Math.floor(Math.random() * (node.width - roomWidth)); + const roomY = node.y + Math.floor(Math.random() * (node.height - roomHeight)); + return { x: roomX, y: roomY, width: roomWidth, height: roomHeight }; } - root.createRooms(); - - function traverse(node) { - if (node.room) { - for (let x = node.room.x; x < node.room.x + node.room.width; x++) { - for (let y = node.room.y; y < node.room.y + node.room.height; y++) { - if ( - (x !== player.x || y !== player.y) && - (x !== exit.x || y !== exit.y) && - !enemies.some(enemy => enemy.x === x && enemy.y === y) && - !items.some(item => item.x === x && item.y === y) - ) { - walls.push({ x, y }); - } - } + const nodes = splitNode(0, 0, GRID_SIZE, GRID_SIZE); + const rooms = nodes.map(createRoom); + + rooms.forEach(room => { + for (let x = room.x; x < room.x + room.width; x++) { + for (let y = room.y; y < room.y + room.height; y++) { + if ( + (x !== player.x || y !== player.y) && + (x !== exit.x || y !== exit.y) && + !enemies.some(enemy => enemy.x === x && enemy.y === y) && + !items.some(item => item.x === x && item.y === y) + ) { + walls.push({ x, y }); } } - if (node.left) traverse(node.left); - if (node.right) traverse(node.right); - } - - traverse(root); + }}); if (!isPassable()) { generateWallsRSP(); |