예제 #1
0
    /// <summary>
    /// 方法:寻路算法
    /// </summary>
    ///<param name="sg">起点</param>
    ///<param name="eg">终点</param>
    ///<param name="map">Map类的实例</param>
    ///<returns>PacGrid:返回路径起点</returns>
    internal PacGrid Find(PacGrid sg, PacGrid eg, PacMap map)
    {
        openList.Clear();
        closeList.Clear();
        openList.Add(sg);
        PacGrid g;
        int     i = 0;

        while (!IsInList(eg.X, eg.Y, openList) || openList.Count == 0)
        {
            g = GetMinFFromList(openList);
            if (g != null)
            {
                openList.Remove(g);
                closeList.Add(g);
                CheckAround(g, eg, map);
            }
            else
            {
                return(g);
            }
            i++;
        }
        PacGrid tmpg = GetGridFromList(eg, openList);

        return(Save(tmpg));
    }
예제 #2
0
    /// <summary>
    /// 方法:检查当前节点周边的节点
    /// </summary>
    ///<param name="sg">当前节点</param>
    ///<param name="eg">终点</param>
    ///<param name="map">Map类的实例</param>
    protected void CheckAround(PacGrid sg, PacGrid eg, PacMap map) //
    {
        int gridmapRow = map.LenY;                                 //获取地图的行数
        int gridmapCol = map.LenX;                                 //获取地图的列数

        PacGrid[,] gridmap = map.simpleMap;
        for (int i = sg.X - 1; i < sg.X + 2; i++)
        {
            for (int j = sg.Y - 1; j < sg.Y + 2; j++)                           //筛选出相邻点
            {
                if (i < 0 || i > gridmapCol - 1 || j < 0 || j > gridmapRow - 1) //若超出地图,pass
                {
                    continue;
                }
                if (gridmap[i, j].LandAttribute == 0 || IsInList(i, j, closeList) || (i == sg.X && j == sg.Y))//若为障碍点/已考虑节点/当前点,pass
                {
                    continue;
                }
                gridmap[i, j].HCostAttribute = GetGridCostH(i, j, eg);              //计算H值(此节点到目标节点的移动损耗)
                if (!IsInList(i, j, openList))                                      //不在open列表,则加入
                {
                    openList.Add(gridmap[i, j]);                                    //加入open列表
                    gridmap[i, j].fatherGrid     = sg;                              //将此节点的父节点设为当前节点
                    gridmap[i, j].GCostAttribute = GetGridCostG(gridmap[i, j], sg); //计算G值(从起点到此节点的移动损耗)
                }
                else
                {
                    int k = GetGridCostG(gridmap[i, j], sg);
                    if (gridmap[i, j].GCostAttribute > k)  //如果此节点经由当前节点离起点更近,则指向当前节点
                    {
                        gridmap[i, j].GCostAttribute = k;  //更新G值
                        gridmap[i, j].fatherGrid     = sg; //更新父节点
                    }
                }
            }
        }
    }