public class Edge
{
public Node StartNode; //起始点
public Node EndNode; //终点
public int Weight = int.MaxValue; //边的权重
public Edge(Node _StartNode, Node _EndNode, int _Weight)
{
StartNode = _StartNode;
EndNode = _EndNode;
Weight = _Weight;
}
public Edge()
{
Weight = int.MaxValue;
}
}
2、节点:
public class Node
{
public int nIndex; //点的索引
public Vector2 vPos; //位置
public List<Node> ltLinkNode = new List<Node>(); //连接的节点
public string strName; //节点名称,这个用于调试和编辑使用
public List<Edge> ltLinkEdge = new List<Edge>(); //连接的边
public Edge AddLink(Node _Node, int nWeight)
{
ltLinkNode.Add(_Node);
Edge _Edge = new Edge(this, _Node, nWeight);
ltLinkEdge.Add(_Edge);
_Node.ReversAddLink(this, nWeight);
return _Edge;
}
private void ReversAddLink(Node _Node, int nWeight)
{
Edge _Edge = new Edge(this, _Node, nWeight);
ltLinkEdge.Add(_Edge);
}
}
3、寻路节点
public class Path
{
public int nW = int.MaxValue; //当前的最短路径
public Path ParentNode = null; //当前路径下的父节点
public int nIndex; //节点索引
}
4、寻路算法
public IEnumerator DijkstraFun(int nStartIndex, int nEndIndex)
{
Stack<Node> ltClose = new Stack<Node>();
List<Node> ltOpen = new List<Node>(m_ltAllNode);
Path[] dis = new Path[ltOpen.Count];
bool[] used = new bool[ltOpen.Count];
for (int i = 0; i < ltOpen.Count; ++i)
{
Node aNode = ltOpen;
dis[aNode.nIndex] = new Path();
dis[aNode.nIndex].nIndex = i;
used[aNode.nIndex] = false;
}
List<Edge> _ltLink = curNode.ltLinkEdge;
Edge aEdge = new Edge();
for (int i = 0; i < _ltLink.Count; ++i)
{
if (used[_ltLink.EndNode.nIndex])
{
continue;
}