-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.js
More file actions
53 lines (42 loc) · 851 Bytes
/
Node.js
File metadata and controls
53 lines (42 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Node
{
constructor(gridX, gridY, source, target)
{
this.gridX = gridX;
this.gridY = gridY;
// distance from starting node
this.gCost = 0;
// distance from end node
this.hCost = this.getCost(this.gridX,
this.gridY, target);
this.nodeParent;
this.neighbours = [];
}
get fCost()
{
return this.gCost + this.hCost;
}
set parent(p)
{
// calculating the cost
// based on parent's path
this.gCost = p.gCost + 1;
// setting parent
this.nodeParent = p;
}
get parent()
{
return this.nodeParent;
}
// get's g/h cost depending on passed argument
// x y - current position
// target - destination
// returns the distance between current position
// and the destination target
getCost(x, y, target)
{
var n1 = Math.abs(target.x - x);
var n2 = Math.abs(target.y - y);
return n1 + n2;
}
}