A base class for pathfinding nodes. You can override it with your own g() and h() functions
new gamvas.AStarNode(x, y, id);
x, y | The position of the way point |
id | A unique id for this node within the pathfinding system (optional, default: autogenerated) |
gamvas. | A base class for pathfinding nodes. |
Variables | |
connected | A array holding the connected gamvas.AStarNode elements |
position | A gamvas.Vector2D holding the position of the node |
Functions | |
g | The path cost function, should return a value that represents how hard it is to reach the current node |
h | The heuristic estimate |
connect | Connect two nodes |
A array holding the connected gamvas.AStarNode elements
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
g: function( n )
The path cost function, should return a value that represents how hard it is to reach the current node
n | The node we are coming from |
Negative values | Current node can not be reached from node n |
Positive values | Representing the costs reaching the current node from node n |
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: function( n, t )
The heuristic estimate
n | The node we are coming from |
t | The target of the current path find |
The estimated cost to the path target
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(); } });
A gamvas.Vector2D holding the position of the node
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; }
The path cost function, should return a value that represents how hard it is to reach the current node
g: function( n )
The heuristic estimate
h: function( n, t )
Connect two nodes
connect: function( n, auto )