ippa / jaws

Jaws - HTML5 canvas javascript 2D Game Framework
https://jawsjs.ippa.se
GNU Lesser General Public License v3.0
363 stars 75 forks source link

Tests for TileMap.findPath() added, inverted feature added and a bit of tweaking #62

Closed MalphasWats closed 11 years ago

MalphasWats commented 11 years ago

I've added some tests for the pathfinding code, it's the first time I've written a test, so they might not be very good. Happy for any feedback so I can make them better!

This time I ran the build.rb script, so everything should be updated properly (although it didn't create the docs because I don't have jsdoc (or access to the server ;)

I added an 'inverted' feature that forces findPath() to only look at nodes that do have something in as it gives more options for use. I also re-factored the part that adds nodes to the open list, including checking to see if you're trying to look outside of the tile map (which was missing before and never caused a problem because my example had a wall all around the outside).

Hopefully this all makes sense and is useful. (these are my first 2 pull requests, so apologies if I've made things difficult, I'm still learning!).

(one other note, the tests worked fine in Safari, but wouldn't work at all on Chrome, both my new tests and a whole load of the existing ones, I'm not sure why, possibly an issue with running it locally?).

Thanks.

MalphasWats commented 11 years ago

I've tweaked this a little bit, changing the return value for the found path - it now returns a list of x,y co-ordinates, rather than col,row indexes in the TileMap. After I started using it in anger, I found I was having scoping problems moving sprites around and still needing to reference the TileMap. This way it's easier to pass a path into a 'move' function and just move between the points. I also fixed a bug in the edge case of start and end being the same, it now returns the single co-ords (and actually works) which I found was more useful in practice. I updated the tests to account for the changes.

I've also added a new lineOfSight([x0, y0], [x1, y1]) function that checks a TileMap for line of sight, added to the example to show it working too. Wrote a couple of tests for it too.

Again, hope this is useful and I'm not being too spammy! If anyone's interested, I'm building a rubbish game with all this over here: https://github.com/MalphasWats/Rouge