Path follow mostly working - on to detecting adjacent nodes

Posted on February 23, 2012 | 4 minute read

This is my third blog post today. Well, technically today is the next day, but it still feels like yesterday. I promise they won’t be this frequent all the time. Today I’ve just had a lot to write about.

As far as I can tell in my tired state, path follow functionality is mostly done. The very core functionality, that is. That being:

  1. The player is able to select a base to draw a path from.

  2. The player is able to click on nodes to specify a path from the chosen base.

  3. The player is able to re-draw their path or clear it completely.

  4. The player is able to press ‘Go’ to spawn entities from the base and send them along the path to the destination.

  5. While the entities are moving, the player is able to start drawing another path from a different base.

There is still a long way to go with these paths. For example, I need to implement the following:

  • Error notifications if the player performs an illegal action (such as trying to select a base after another base is already active and before entities are spawned from the first selected base)

  • Making the base inactive after its set of entities is spawned (so you only spawn from each base once per level)

  • Doing something with the entities when they get to the destination (probably killing them after adding points etc)

  • Visually highlighting the chosen path for the player (right now nodes change appearance, but I want to actually have lines along the path get highlighted)

  • And probably fix a gazillion bugs that I have not accounted for yet.

What I ended up doing with the arrays

Jack suggested earlier that maybe I should copy the path array into the moving entity when the Go button is clicked. This ended up being a life saver. What’s happening now is:

  • There is one global array called It is stored in the controller class

  • When the global array is full (starting at the base and ending at the destination) and the player presses Go, it gets copied into the newly spawned moving entities and the global path_array is instantly emptied.

  • While the entities are moving, the player can start selecting a new base and a new path and get them going simultaneously

This has eliminated the need for the array of arrays and, while still using a global array and probably not being ideal, has made the code feel a lot less messy for me. Although I’m sure I’ll improve on it more later.

I have also added a deselectPath function to the controller class. This is called when one of the following happens:

  • The player clicks on a base after it is already selected

  • The player presses the Go button

It looks like this:

[crayon title=“playerController.js”] deselectPath: function() { for(var i =; i >= 0; i–){[i].active = false;[i].clickcount = 0; console.log (‘setting everything to inactive’); } = 0; }, [/crayon]

Every node-type entity (so the bases, the nodes, and the destination entities) have an “active” attribute which is always either true or false and a “clickcount” which is always either 1 or 0. So the for loop is able to iterate through all three types of entities and reset the same attributes for each. It then clears the path_array array.

Next up - adjacency

I now have to find a way to detect which nodes are adjacent to each other and only allow the user to build their path by using adjacent nodes. I was thinking of maybe somehow checking if there is a node between two nodes on either the X or Y axis and, if not, those two nodes are adjacent. Each instance of the node entity in a level might have its own array listing adjacent nodes in the controller class and then I can use this to see which nodes are selectable…

This is just a first thought and I’m not sure if it will work in practice, but I think it’s worth a shot. If it doesn’t work out I’ll scrap it and try something else.

Categories:game dev dev games
comments powered by Disqus