Created
December 2, 2018 15:57
-
-
Save LucianBuzzo/1ba2c2a7e0c0bce4b688578207f04ac1 to your computer and use it in GitHub Desktop.
Tile walking algorithms for dungeoneer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const wait = () => { | |
| return new Promise((resolve) => { | |
| setTimeout(resolve, 5) | |
| }) | |
| } | |
| exports.fill = async (dungeon, ctx, cellSize) => { | |
| const width = dungeon.tiles.length | |
| const height = dungeon.tiles[0].length | |
| let startX = null | |
| let startY = null | |
| const colored = [] | |
| for (var x = 0; x < dungeon.tiles.length; x++) { | |
| if (!colored[x]) { | |
| colored[x] = [] | |
| } | |
| for (var y = 0; y < dungeon.tiles[x].length; y++) { | |
| colored[x].push(false) | |
| if (startX === null && startY === null && match(dungeon.tiles[x][y])) { | |
| startX = x; | |
| startY = y; | |
| } | |
| } | |
| } | |
| const stack = [[startX, startY]]; | |
| while(stack.length) { | |
| let reachLeft = false; | |
| let reachRight = false; | |
| let [x, y] = stack.pop(); | |
| while(y > 0 && match(dungeon.tiles[x][y - 1])) { | |
| y-- | |
| } | |
| colorTile(x, y); | |
| await wait() | |
| let seek = true | |
| while(seek) { | |
| colorTile(x, y); | |
| await wait() | |
| // If possible look left for possible seeds | |
| if(x >= 0 && !colored[x - 1][y]) { | |
| if (match(dungeon.tiles[x - 1][y])) { | |
| // If there is a match and reach left is false, push onto stack | |
| // This means you only push onto stack once for each range of | |
| // passable terrain | |
| if (!reachLeft) { | |
| stack.push([x - 1, y]); | |
| colorTile(x - 1, y, 'blue') | |
| reachLeft = true; | |
| } | |
| } else { | |
| reachLeft = false | |
| } | |
| } | |
| if(x < width - 1 && !colored[x + 1][y]) { | |
| if (match(dungeon.tiles[x + 1][y])) { | |
| if(!reachRight) { | |
| stack.push([x + 1, y]); | |
| colorTile(x + 1, y, 'blue') | |
| reachRight = true; | |
| } | |
| } else { | |
| reachRight = false | |
| } | |
| } | |
| if (match(dungeon.tiles[x][y + 1])) { | |
| y++ | |
| } else { | |
| seek = false | |
| } | |
| } | |
| } | |
| console.log('DONE!') | |
| function match(tile) { | |
| return tile.type === 'floor' || tile.type === 'door' | |
| } | |
| function colorTile(x, y, color = 'purple') { | |
| colored[x][y] = true | |
| ctx.fillStyle = color | |
| ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize) | |
| } | |
| } | |
| exports.fill2 = async (dungeon, ctx, cellSize) => { | |
| const width = dungeon.tiles.length | |
| const height = dungeon.tiles[0].length | |
| let startX = null | |
| let startY = null | |
| let start | |
| const colored = [] | |
| for (var x = 0; x < dungeon.tiles.length; x++) { | |
| if (!colored[x]) { | |
| colored[x] = [] | |
| } | |
| for (var y = 0; y < dungeon.tiles[x].length; y++) { | |
| colored[x].push(false) | |
| if (!start && match(dungeon.tiles[x][y])) { | |
| start = dungeon.tiles[x][y] | |
| } | |
| if (startX === null && startY === null && match(dungeon.tiles[x][y])) { | |
| startX = x; | |
| startY = y; | |
| } | |
| } | |
| } | |
| const stack = [start]; | |
| while(stack.length) { | |
| const tile = stack.shift(); | |
| colorTile(tile); | |
| await wait() | |
| for (const dir in tile.neighbours) { | |
| const neighbour = tile.neighbours[dir] | |
| if (!colored[neighbour.x][neighbour.y] && match(neighbour)) { | |
| stack.push(neighbour) | |
| colorTile(neighbour, 'blue') | |
| } | |
| } | |
| } | |
| console.log('DONE!') | |
| function match(tile) { | |
| return tile.type === 'floor' || tile.type === 'door' | |
| } | |
| function colorTile(tile, color = 'purple') { | |
| colored[tile.x][tile.y] = true | |
| ctx.fillStyle = color | |
| ctx.fillRect(tile.x * cellSize, tile.y * cellSize, cellSize, cellSize) | |
| } | |
| } | |
| exports.fill3 = async (dungeon, ctx, cellSize) => { | |
| const width = dungeon.tiles.length | |
| const height = dungeon.tiles[0].length | |
| let startX = null | |
| let startY = null | |
| let start | |
| const colored = [] | |
| for (var x = 0; x < dungeon.tiles.length; x++) { | |
| if (!colored[x]) { | |
| colored[x] = [] | |
| } | |
| for (var y = 0; y < dungeon.tiles[x].length; y++) { | |
| colored[x].push(false) | |
| if (!start && match(dungeon.tiles[x][y])) { | |
| start = dungeon.tiles[x][y] | |
| } | |
| if (startX === null && startY === null && match(dungeon.tiles[x][y])) { | |
| startX = x; | |
| startY = y; | |
| } | |
| } | |
| } | |
| const stack = [start]; | |
| while(stack.length) { | |
| const tile = stack.pop(); | |
| colorTile(tile); | |
| await wait() | |
| for (const dir in tile.neighbours) { | |
| const neighbour = tile.neighbours[dir] | |
| if (!colored[neighbour.x][neighbour.y] && match(neighbour)) { | |
| stack.push(neighbour) | |
| colorTile(neighbour, 'blue') | |
| } | |
| } | |
| } | |
| console.log('DONE!') | |
| function match(tile) { | |
| return tile.type === 'floor' || tile.type === 'door' | |
| } | |
| function colorTile(tile, color = 'purple') { | |
| colored[tile.x][tile.y] = true | |
| ctx.fillStyle = color | |
| ctx.fillRect(tile.x * cellSize, tile.y * cellSize, cellSize, cellSize) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment