gamvas.AStarNode

Description

A base class for pathfinding nodes.  You can override it with your own g() and h() functions

Constructor

new gamvas.AStarNode(x, y, id);

Parameters

x, yThe position of the way point
idA unique id for this node within the pathfinding system (optional, default: autogenerated)
Summary
gamvas.AStarNodeA base class for pathfinding nodes.
Variables
connectedA array holding the connected gamvas.AStarNode elements
positionA gamvas.Vector2D holding the position of the node
Functions
gThe path cost function, should return a value that represents how hard it is to reach the current node
hThe heuristic estimate
connectConnect two nodes

Variables

connected

A array holding the connected gamvas.AStarNode elements

position

create: function(
   x,
   y,
   id
) { this.connected = new Array(); this.position = new gamvas.Vector2D(x, y); (id) ? this.id = id : this.id = this.position.x+'_'+this.position.y; }

A gamvas.Vector2D holding the position of the node

Functions

g

g: function(n)

The path cost function, should return a value that represents how hard it is to reach the current node

Parameters

nThe node we are coming from

Returns

Negative valuesCurrent node can not be reached from node n
Positive valuesRepresenting the costs reaching the current node from node n

Example

gamvas.AStarGridNode = gamvas.AStarNode.extend({
   create: function(x, y, id) {
      this._super(x, y, id);
   },
   g: function(n) {
      // get height difference
      var diff = Math.abs(this.position.y-n.position.y);
      if (diff > 10) {
         return -1; // we can not step over 10 in height
      }
      // give it some weight so the higher the differnce, the more unlikely it is, the path will walk over it
      return diff * diff;
   }
});

h

h: function(n,
t)

The heuristic estimate

Parameters

nThe node we are coming from
tThe target of the current path find

Returns

The estimated cost to the path target

Example

This is the standard implementation, which returns the distance to the target.

If you have more information, like for example number of opponents between target and current position, you could return a higher value if too many opponants are between target and current position, so the algorithm will try to avoid running directly through hordes of monsters.

gamvas.AStarGridNode = gamvas.AStarNode.extend({
   create: function(x, y, id) {
      this._super(x, y, id);
   },
   h: function(n, t) {
      var dif = this.position.difference(t.position);
      return dif.length();
   }
});

connect

connect: function(n,
auto)

Connect two nodes

Parameters

nThe node to connect to
autotrue/false if automatically set up a bidirectional connection (optional, default: true)
A base class for pathfinding nodes.
create: function(
   x,
   y,
   id
) { this.connected = new Array(); this.position = new gamvas.Vector2D(x, y); (id) ? this.id = id : this.id = this.position.x+'_'+this.position.y; }
A gamvas.Vector2D holding the position of the node
A 2D vector class
g: function(n)
The path cost function, should return a value that represents how hard it is to reach the current node
h: function(n,
t)
The heuristic estimate
connect: function(n,
auto)
Connect two nodes
Close