寻路基本算法选择A*启发式搜索,但由于山地路不平,无法直接用平面网格,用unity自带的网格导航固然可以,但是还有一个选择,就是自己布点。将寻路网格当成双向有向图,采用有向图邻接表的数据结构存储路网。A*启发函数选择马哈顿距离(d = dx + dy)。代码如下:
//Use Graph to describe availue path for players or non-players
//Graph Edge
//Use index in array as id ,so there is no need to store start point
public class AStarPathEdge {
public AStarPoint2f endPoint; // End point
public float distance; // Distance between start point end end point,
//which means this value stands for edge distance
public override string ToString ()
{
return "endPointIndex: " + endPoint.index + " distane: " + distance + " ";
}
}
public class AStarPoint2f {
public float x;
public float y;
private IList edges = new ArrayList(); //All edges that start from this point
public int index = 0; //Index in array
public float h = float.MaxValue; //Distance between this point and end point
public float g = float.MaxValue; //Distance between this point and start point
public float f = float.MaxValue;
public AStarPoint2f father;//Record A Star path
//Caculate Manhattan Distance
public void caculateDestDistance(AStarPoint2f destPoint) {
if(destPoint == this)
h = 0;
else
h = ManhattanDistance(