-
Notifications
You must be signed in to change notification settings - Fork 1
/
Node2D.cs
59 lines (47 loc) · 1.54 KB
/
Node2D.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 UnityEngine;
namespace hutian.AI.PathFinding
{
public class Node2D : IHeapItem<Node2D>
{
public int gCost; //从起点到当前节点的代价
public int hCost; //从当前格子到节点的代价(预估代价)
public bool obstacle; //当前节点是否是障碍物
public Vector3 worldPosition; //当前节点所在的世界坐标
public int GridX, GridY; //节点在二维网格中的坐标
public Node2D parent; //父节点,根据父节点回溯到起点从而找到路径
private int heapIndex;
public Node2D(bool _obstacle, Vector3 _worldPos, int _gridX, int _gridY)
{
obstacle = _obstacle;
worldPosition = _worldPos;
GridX = _gridX;
GridY = _gridY;
}
public int FCost //节点的总代价
{
get
{
return gCost + hCost;
}
}
public int HeapIndex
{
get => heapIndex;
set => heapIndex = value;
}
public int CompareTo(Node2D nodeToCompare)
{
int compare = FCost.CompareTo(nodeToCompare.FCost);
//总代价相同,比较预估代价(选择离终点更近的节点)
if (compare == 0)
{
compare = hCost.CompareTo(nodeToCompare.hCost);
}
return -compare;
}
public void SetObstacle(bool isObstacle)
{
obstacle = isObstacle;
}
}
}