Snap sprites into the nearest edge of the grid when finding the nearest walkable tile

This commit is contained in:
2014-06-28 09:08:22 -07:00
parent e166d580fc
commit 301073110b

View File

@@ -81,6 +81,14 @@ function nearestWalkableTile(spr)
var sprgridx = parseInt(spr.x / 32); var sprgridx = parseInt(spr.x / 32);
var sprgridy = parseInt(spr.y / 32); var sprgridy = parseInt(spr.y / 32);
// Snap sprites outside the grid to the grid edge
if ( sprgridy > grid.nodes.length ) {
sprgridy = grid.nodes.length - 1;
}
if ( sprgridx > grid.nodes[sprgridy].length ) {
sprgridx = grid.nodes[sprgridy].length - 1;
}
function _walkable_inner(multiplier) { function _walkable_inner(multiplier) {
var startx = parseInt(Math.max(sprgridx - (1 * multiplier), 0)); var startx = parseInt(Math.max(sprgridx - (1 * multiplier), 0));
var starty = parseInt(Math.max(sprgridy - (1 * multiplier), 0)); var starty = parseInt(Math.max(sprgridy - (1 * multiplier), 0));
@@ -99,7 +107,7 @@ function nearestWalkableTile(spr)
} }
} }
} }
return null; throw "No walkable tile found near (" + spr.x + "," + spr.y + ")";
} }
if ( grid.nodes[sprgridy][sprgridx].walkable == true ) if ( grid.nodes[sprgridy][sprgridx].walkable == true )