Skip to content

Instantly share code, notes, and snippets.

@McFunkypants
Created January 12, 2013 04:07
Show Gist options
  • Save McFunkypants/4516009 to your computer and use it in GitHub Desktop.
Save McFunkypants/4516009 to your computer and use it in GitHub Desktop.
// the spritesheet is ready
function loaded()
{
console.log('Spritesheet loaded.');
spritesheetLoaded = true;
createWorld();
}
// fill the world with walls
function createWorld()
{
console.log('Creating world...');
// create emptiness
for (var x=0; x < worldWidth; x++)
{
world[x] = [];
for (var y=0; y < worldHeight; y++)
{
world[x][y] = 0;
}
}
// scatter some walls
for (var x=0; x < worldWidth; x++)
{
for (var y=0; y < worldHeight; y++)
{
if (Math.random() > 0.75)
world[x][y] = 1;
}
}
// calculate initial possible path
// note: unlikely but possible to never find one...
currentPath = [];
while (currentPath.length == 0)
{
pathStart = [Math.floor(Math.random()*worldWidth),Math.floor(Math.random()*worldHeight)];
pathEnd = [Math.floor(Math.random()*worldWidth),Math.floor(Math.random()*worldHeight)];
if (world[pathStart[0]][pathStart[1]] == 0)
currentPath = findPath(world,pathStart,pathEnd,'Manhattan');
}
redraw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment