From 301073110b726290425c464cda9b6a7925e2bc7d Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Sat, 28 Jun 2014 09:08:22 -0700 Subject: [PATCH] Snap sprites into the nearest edge of the grid when finding the nearest walkable tile --- moonlight/src/Util.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/moonlight/src/Util.js b/moonlight/src/Util.js index 3100552..766edb0 100644 --- a/moonlight/src/Util.js +++ b/moonlight/src/Util.js @@ -81,6 +81,14 @@ function nearestWalkableTile(spr) var sprgridx = parseInt(spr.x / 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) { var startx = parseInt(Math.max(sprgridx - (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 )