Exemplo n.º 1
0
        /// <summary>
        /// 开始寻路;
        /// </summary>
        /// <param name="originNode"></param>
        /// <param name="targetNode"></param>
        /// <param name="passNodes"></param>
        /// <returns></returns>
        public static bool FindPath(AStarNode originNode, AStarNode targetNode, ref List <AStarNode> passNodes)
        {
            passNodes.Clear();

            if (originNode.EqualOther(targetNode))
            {
                passNodes.Add(originNode);
                return(true);
            }

            // 清楚打开和关闭列表;
            OpenNodes.Clear();
            CloseNodes.Clear();

            originNode.G          = 0;
            originNode.H          = originNode.GetH(targetNode);
            originNode.ParentNode = null;

            OpenNodes.Add(originNode);

            AStarNode currenNode = null;

            while (OpenNodes.Count > 0)
            {
                // 找出open表里面最小的F;
                currenNode = GetMinF(OpenNodes);
                if (currenNode.EqualOther(null))
                {
                    return(false);
                }

                // 判断当前的节点是不是目标点,是的话就找到了路径;
                if (currenNode.EqualOther(targetNode))
                {
                    while (!currenNode.EqualOther(originNode))
                    {
                        passNodes.Add(currenNode);
                        currenNode = currenNode.ParentNode;
                    }
                    passNodes.Add(originNode);
                    passNodes.Reverse();
                    OpenNodes.Clear();
                    CloseNodes.Clear();
                    return(true);
                }

                // 没有找到就移除Open列表里面;
                OpenNodes.Remove(currenNode);
                CloseNodes.Add(currenNode);

                // 判断邻居节点;
                Check8(currenNode, targetNode);

                //PrintLog();
            }

            passNodes.Clear();
            OpenNodes.Clear();
            CloseNodes.Clear();
            return(false);
        }
Exemplo n.º 2
0
 internal AStarLink(AStarNode node, int cost)
 {
     Node = node;
     Cost = cost;
 }
Exemplo n.º 3
0
 private static bool InNodeList(AStarNode node, List <AStarNode> Nodes)
 {
     return(Nodes.FirstOrDefault(c => c.Point == node.Point) != null);
 }
Exemplo n.º 4
0
 internal void setEndNode(int x, int y)
 {
     _endNode = _grid[x][y];
 }
Exemplo n.º 5
0
 internal void setStartNode(int x, int y)
 {
     _startNode = _grid[x][y];
 }
Exemplo n.º 6
0
 public abstract AStarNode GetNeighbourNode(AStarNode node, int index);
Exemplo n.º 7
0
 public abstract int GetNeighbourNodeCount(AStarNode node);
Exemplo n.º 8
0
 private int compare(AStarNode a, AStarNode b)
 {
     return(b._f - a._f);
 }