Created
January 16, 2013 04:57
-
-
Save McFunkypants/4544744 to your computer and use it in GitHub Desktop.
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
// Neighbours functions, used by findNeighbours function | |
// to locate adjacent available cells that aren't blocked | |
// Returns every available North, South, East or West | |
// cell that is empty. No diagonals, | |
// unless distanceFunction function is not Manhattan | |
function Neighbours(x, y) | |
{ | |
var N = y - 1, | |
S = y + 1, | |
E = x + 1, | |
W = x - 1, | |
myN = N > -1 && canWalkHere(x, N), | |
myS = S < worldHeight && canWalkHere(x, S), | |
myE = E < worldWidth && canWalkHere(E, y), | |
myW = W > -1 && canWalkHere(W, y), | |
result = []; | |
if(myN) | |
result.push({x:x, y:N}); | |
if(myE) | |
result.push({x:E, y:y}); | |
if(myS) | |
result.push({x:x, y:S}); | |
if(myW) | |
result.push({x:W, y:y}); | |
findNeighbours(myN, myS, myE, myW, N, S, E, W, result); | |
return result; | |
} | |
// returns every available North East, South East, | |
// South West or North West cell - no squeezing through | |
// "cracks" between two diagonals | |
function DiagonalNeighbours(myN, myS, myE, myW, N, S, E, W, result) | |
{ | |
if(myN) | |
{ | |
if(myE && canWalkHere(E, N)) | |
result.push({x:E, y:N}); | |
if(myW && canWalkHere(W, N)) | |
result.push({x:W, y:N}); | |
} | |
if(myS) | |
{ | |
if(myE && canWalkHere(E, S)) | |
result.push({x:E, y:S}); | |
if(myW && canWalkHere(W, S)) | |
result.push({x:W, y:S}); | |
} | |
} | |
// returns every available North East, South East, | |
// South West or North West cell including the times that | |
// you would be squeezing through a "crack" | |
function DiagonalNeighboursFree(myN, myS, myE, myW, N, S, E, W, result) | |
{ | |
myN = N > -1; | |
myS = S < worldHeight; | |
myE = E < worldWidth; | |
myW = W > -1; | |
if(myE) | |
{ | |
if(myN && canWalkHere(E, N)) | |
result.push({x:E, y:N}); | |
if(myS && canWalkHere(E, S)) | |
result.push({x:E, y:S}); | |
} | |
if(myW) | |
{ | |
if(myN && canWalkHere(W, N)) | |
result.push({x:W, y:N}); | |
if(myS && canWalkHere(W, S)) | |
result.push({x:W, y:S}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment