-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path.cs
59 lines (46 loc) · 1.54 KB
/
Path.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Path
{
Terrain terrain;
List<Vector2> ptsTraversed;
float sqrCost; // the total distance travelled, except each step is squared since
// it is cheaper to compute sqrMagnitude (and gives abs)
private Path(Terrain t, List<Vector2> pts, float sqrCost) {
terrain = t;
this.sqrCost = sqrCost;
ptsTraversed = new List<Vector2>(pts);
}
public Path(Terrain t, Vector2 initialPoint) {
terrain = t;
sqrCost = 0f;
ptsTraversed = new List<Vector2> {initialPoint};
}
public float AddVertex(Vector2 nextPt) {
Vector2 last = ptsTraversed[ptsTraversed.Count - 1];
Vector3 lastV = terrain.GetVertex((int) last.x, (int) last.y);
Vector3 nextV = terrain.GetVertex((int) nextPt.x, (int) nextPt.y);
ptsTraversed.Add(nextPt);
sqrCost += (nextV - lastV).sqrMagnitude;
return sqrCost;
}
public Vector2 GetCurrentPt() {
return ptsTraversed[ptsTraversed.Count - 1];
}
public bool ReachedBottom() {
return ptsTraversed[ptsTraversed.Count - 1].y == 0;
}
public float GetSqrCost() {
return sqrCost;
}
public float CompareTo(Path p) {
return this.sqrCost - p.GetSqrCost();
}
public Path Copy() {
return new Path(terrain, ptsTraversed, sqrCost);
}
public List<Vector2> GetPathPoints() {
return ptsTraversed;
}
}