Пример #1
0
    /// <summary>
    /// A*寻路
    /// </summary>
    private void AStarCount()
    {
        //将寻路起点放置到OpenList
        openList.Add(allCubes[(int)startPos.x,
                              (int)startPos.y]);

        //循环运算
        while (true)
        {
            //排序
            openList.Sort();
            //找到此次排序中,F值最小的格子
            CubeGrid currentGrid = openList[0];
            //如果当前格子是终点
            if (currentGrid.GetGridType() == GridType.End)
            {
                //生成路径
                PushCubePath(currentGrid);
                break;
            }

            //遍历周边的8个格子
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    //除去当前中心格子
                    if (i == 0 && j == 0)
                    {
                        continue;
                    }

                    //计算新格子的坐标
                    int x = currentGrid.x + i;
                    int z = currentGrid.z + j;

                    //排除特殊情况
                    if (x < 0 || z < 0 ||
                        x >= allCubes.GetLength(0) ||
                        z >= allCubes.GetLength(1))
                    {
                        continue;
                    }
                    //排除障碍
                    if (allCubes[x, z].GetGridType() == GridType.Obsticle)
                    {
                        continue;
                    }
                    //排除已经成为过发现者
                    if (closeList.Contains(allCubes[x, z]))
                    {
                        continue;
                    }

                    //声明一个G
                    int g = 0;

                    if (i == 0 || j == 0)
                    {
                        g = currentGrid.G + 10;
                    }
                    else
                    {
                        g = currentGrid.G + 14;
                    }

                    //如果该格子的G值从未计算过,则设置
                    //如果当前次的运算得到的结果G,比原本的G值要小,则更新
                    if (allCubes[x, z].G == 0 || g < allCubes[x, z].G)
                    {
                        allCubes[x, z].G = g;
                        //更新发现者
                        allCubes[x, z].finder = currentGrid;
                    }

                    //计算横向的x步数
                    int hx = (int)endPos.x - x;
                    //计算纵向的z步数
                    int hy = (int)endPos.y - z;

                    //计算H值
                    allCubes[x, z].H = hx > 0 ? hx : -hx + hy > 0 ? hy : -hy;
                    //计算F值
                    allCubes[x, z].F = allCubes[x, z].G + allCubes[x, z].H;

                    //如果OpenList中不包含当前格子
                    if (!openList.Contains(allCubes[x, z]))
                    {
                        //放进去
                        openList.Add(allCubes[x, z]);
                    }
                }
            }

            //将当前中心格子放置到CloseList
            closeList.Add(currentGrid);
            //从OpenList中移除
            openList.Remove(currentGrid);

            if (openList.Count == 0)
            {
                Debug.Log("Can Not Find Path!!!");
                break;
            }
        }
    }