This repository has been archived by the owner on Oct 18, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Path generation algorithm. Playing audio on click.
- Loading branch information
Showing
28 changed files
with
495 additions
and
247 deletions.
There are no files selected for viewing
This file contains 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
Binary file not shown.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ body { | |
|
||
#game { | ||
background-color: #eee; | ||
cursor: pointer; | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import createSwitchesSprite from './sprites/switches' | ||
import tilesets from './tilesets' | ||
|
||
const render = function () { | ||
const tiles = tilesets() | ||
tiles.render() | ||
|
||
const switches = createSwitchesSprite(32 * 8, 32 * 9) | ||
switches.render() | ||
} | ||
|
||
export default render |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const constraints = { | ||
edges: [{ | ||
canConnectToTop: false, | ||
canConnectToRight: false, | ||
canConnectToBottom: false, | ||
canConnectToLeft: false | ||
}] | ||
} | ||
|
||
export default constraints |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import sizes from '../sizes' | ||
import modem from './modem' | ||
import path from './path' | ||
import pc from './pc' | ||
import servers from './servers' | ||
import switches from './switches' | ||
import xyToIndex from './xyToIndex' | ||
|
||
const getFixedTiles = function () { | ||
const withModem = modem() | ||
const withPc = pc() | ||
const withSwitch = switches() | ||
const withServer = servers() | ||
const prefilledMaze = [ | ||
...withModem, | ||
...withPc, | ||
...withSwitch, | ||
...withServer | ||
] | ||
return prefilledMaze | ||
} | ||
|
||
const generateMaze = function () { | ||
const { rows, cols } = sizes | ||
const emptyMaze = new Array(rows * cols) | ||
const generatedMaze = emptyMaze.fill(0) | ||
const maze = getFixedTiles() | ||
|
||
maze.forEach((entry) => { | ||
const { x, y, value } = entry | ||
const index = xyToIndex(x, y) | ||
generatedMaze[ index ] = value | ||
}) | ||
|
||
path(generatedMaze) | ||
return generatedMaze | ||
} | ||
|
||
|
||
const maze = function () { | ||
const generatedMaze = generateMaze() | ||
console.log('Generated maze', generatedMaze) | ||
return generatedMaze | ||
} | ||
|
||
export default maze |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import sizes from '../sizes' | ||
|
||
// index start at 0 | ||
// x and y start at 1 | ||
const indexToXY = function (index) { | ||
const { cols } = sizes | ||
const x = 1 + (index % cols) | ||
const y = 1 + (index - (x - 1)) / cols | ||
return { x, y } | ||
} | ||
|
||
export default indexToXY |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import sizes from '../sizes' | ||
|
||
const isAllowedUnsetIndex = function (maze, index) { | ||
const { rows, cols } = sizes | ||
const lowerLimit = 0 | ||
const upperLimit = rows * cols | ||
|
||
const unsetTiles = [ 0, 9, 10, 11 ] | ||
const id = maze[ index ] | ||
|
||
const isWithinLimits = index >= lowerLimit && index < upperLimit | ||
const isNotSetYet = unsetTiles.includes(id) | ||
return isWithinLimits && isNotSetYet | ||
} | ||
|
||
export default isAllowedUnsetIndex |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import sizes from '../sizes' | ||
|
||
const modem = function () { | ||
const { rows, cols } = sizes | ||
|
||
const modemValue = 16 | ||
const modemTile = { | ||
x: cols, | ||
y: rows - 1, | ||
value: modemValue | ||
} | ||
return [ modemTile ] | ||
} | ||
|
||
export default modem |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import neighbors from '../neighbors' | ||
import randomPick from '../randomPick' | ||
import indexToXY from './indexToXY' | ||
import isAllowedUnsetIndex from './isAllowedUnsetIndex' | ||
import xyToIndex from './xyToIndex' | ||
|
||
const pickNextDirection = function (possibleDirections, options) { | ||
const numberOfDirections = possibleDirections.length | ||
const moreData = options || {} | ||
let index | ||
|
||
if (numberOfDirections === 0) { | ||
throw new Error('No directions to choose from!') | ||
} | ||
|
||
if (numberOfDirections === 1) { | ||
index = possibleDirections[ 0 ] | ||
} else { | ||
// TODO: Pick the closest direction towards end | ||
// index = randomPick(possibleDirections) | ||
index = possibleDirections[ 0 ] | ||
} | ||
|
||
return { | ||
index, | ||
...moreData | ||
} | ||
} | ||
|
||
const nextDirection = function (maze, current) { | ||
const currentIndex = current.index | ||
const currentId = current.id | ||
const { x, y } = indexToXY(currentIndex) | ||
|
||
const connections = neighbors.find((neighbor) => neighbor.id === currentId) | ||
const { toTop, toRight, toBottom, toLeft } = connections | ||
|
||
const allDirections = [] | ||
if (toTop) { allDirections.push(xyToIndex(x, y - 1)) } | ||
if (toRight) { allDirections.push(xyToIndex(x + 1, y )) } | ||
if (toBottom) { allDirections.push(xyToIndex(x, y + 1)) } | ||
if (toLeft) { allDirections.push(xyToIndex(x - 1, y )) } | ||
|
||
const possibleDirections = allDirections | ||
.filter((index) => isAllowedUnsetIndex(maze, index)) | ||
|
||
return pickNextDirection(possibleDirections, { | ||
toTop: toTop && toBottom ? true : !toTop, | ||
toRight: toLeft && toRight ? true : !toRight, | ||
toBottom: toTop && toBottom ? true : !toBottom, | ||
toLeft: toLeft && toRight ? true : !toLeft | ||
}) | ||
} | ||
|
||
export default nextDirection |
Oops, something went wrong.