Wrote a quick A* plugin while I was learning melonJS / Hi There

122 views
Skip to first unread message

Sean Muron

unread,
Jul 14, 2013, 6:22:24 PM7/14/13
to mel...@googlegroups.com

Hi,

I've created a rough version of a plugin integrating Brian Grinstead's JS A* implementation. (see: https://github.com/bgrins/javascript-astar)

Repo is here: https://github.com/swmuron/melonjs-astar

Spaghetti code warning. I hacked this together one day while experimenting with Melon. I have a list of improvements in mind for it: you can see some of them on the project's README.

Right now it's creating its own set of nodes for searching. Ideally I'd like to attach its information to tiles directly instead of generating N x M new objects for each level. It's not a huge concern unless you're using huge levels or memory is a concern (e.g. mobile).

It also currently requires the 4 cartesian coordinates to perform the search with. Future improvements might be allowing a Rect, a Vector, or an Entity to be passed to it instead.

Aside from the above issues, I'm also a bit unsure of what the intended layout of a plugin should be like. The plugin code lies in a closure, but all the plugin code on the base class is doing is initializing my own AStarInstance class from the file's closure into `me.astar`. This is because the subscribe callback uses window as its scope and I can't access the plugin object directly. I won't necessarily know what name the user is going to give it, right? Maybe I'm just being a dumb JS newbie but is there a cleaner way of doing it besides the way I did it? (If you dig through the code manually, everything not-A*-related is line 321 and below.)

Relatedly, I think there would be value in a plugin tutorial. I really like the engine, especially how well it works with Tiled and that it has plugin functionality. I might be up to write one myself once I find out if I'm doing things "the proper way." Will also poke around the MelonJS repo itself and see if there's any small tasks I can do.

Cheers,

Sean

melonJS

unread,
Jul 15, 2013, 1:33:46 AM7/15/13
to mel...@googlegroups.com
Hi !

This is great, and I was about to reply that this answers the corresponding ticket on github,  but then realized that you also commented there :)

I did not go through your source code yet, but to reply your question (on how to access your plugin), and taking your example me.plugin.register(aStarPlugin, "astar"); , you can then access your plugin using me.plugin.astar
the line responsible for that is visible here :

note that we also have some kind of unoffical repo for plugins, it's kind of desolated for now, but if you wish i can give the required  access on it and you could host your plugin there ? and the plugin would become an official one ?

Cheers :P
Oliver.
Message has been deleted

bdrea...@gmail.com

unread,
Jul 18, 2013, 8:59:16 AM7/18/13
to mel...@googlegroups.com
Hello Sean,

I started using your plugin but I have a problem. I set everything properly but the objects seems to ignore the solid objects and pass through everything. The code is pretty simple. One object reaches another:

grid = me.astar.search(srcObject.pos.x,srcObject.pos.y,targetObj.pos.x,targetObj.pos.y)


Any clue?

Sean Muron

unread,
Jul 18, 2013, 10:29:29 AM7/18/13
to mel...@googlegroups.com
If you have a collision layer set up with solid tiles, it should work. There currently isn't any checking for entities at this point; only solid collision tiles.

I can't really think of any other useful advice without additional code or information, sorry. :(
Reply all
Reply to author
Forward
0 new messages