-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridNode3d.cs
79 lines (62 loc) · 2.12 KB
/
GridNode3d.cs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public class GridNode3d {
public Vector3 gridCoordinates;
string gridName;
public Type type;
public GridNode3d parent;
public int F, G, H;
public enum Type {
BLANK,
START,
END,
BLOCK
}
/** INITIALISING */
void initialise() {
this.gridCoordinates = new Vector3();
this.gridName = "n/a";
this.type = Type.BLANK;
this.parent = null;
this.F = this.G = this.H = 0;
}
/** CREATING AND SETTING */
public GridNode(string gridName, Vector3 gridCoordinates) {
this.initialise();
this.setUp(gridName, gridCoordinates);
}
void setUp(string gridName, Vector3 gridCoordinates) {
this.gridName = gridName;
this.gridCoordinates.x = gridCoordinates.x;
this.gridCoordinates.y = gridCoordinates.y;
this.gridCoordinates.z = gridCoordinates.z;
}
/** PATHFINDING */
public void calculateValues(GridNode parentNode, GridNode endNode) {
this.parent = parentNode;
double xDistance = Mathf.Abs(this.gridCoordinates.x - this.parent.gridCoordinates.x);
double yDistance = Mathf.Abs(this.gridCoordinates.y - this.parent.gridCoordinates.y);
double zDistance = Mathf.Abs(this.gridCoordinates.z - this.parent.gridCoordinates.z);
double xEndDistance = Math.abs(this.gridCoordinates.x - endNode.gridCoordinates.x);
double yEndDistance = Math.abs(this.gridCoordinates.y - endNode.gridCoordinates.y);
double zEndDistance = Math.abs(this.gridCoordinates.z - endNode.gridCoordinates.z);
if (this.parent != null) {
if (xDistance != 0 && yDistance != 0 && zDistance != 0) {
this.G = this.parent.G + 17;
} else if (
(xDistance != 0 && yDistance != 0) ||
(xDistance != 0 && zDistance != 0) ||
(yDistance != 0 && zDistance != 0)
) {
this.G = this.parent.G + 14;
} else {
this.G = this.parent.G + 10;
}
}
this.H = (int)xEndDistance + (int)yEndDistance + (int)zEndDistance;
this.F = this.G = this.H;
}
/** DISPOSING / RESETTING */
public void reset() {
this.F = this.G = this.H = 0;
this.parent = null;
}
}