Esempio n. 1
0
        protected bool __FindPath__(PathNode StartNode, ref PathNode EndNode)
        {
            StartNode.Build(0, EndNode);
            OpenList.Insert(StartNode);

            while (OpenList.Count > 0)
            {
                BinaryHeapNode MinNode = OpenList.Pop();

                if (MinNode.Data.Equals(EndNode))
                {
                    EndNode = MinNode.Data;
                    return true;
                }

                CloseList.Add(MinNode.Data);

                // 获取所有的相邻节点
                LinkedList<PathNode> LinkNodes = __GetLinkNode__(MinNode.Data, EndNode);

                // 处理所有相邻节点
                foreach (PathNode Node in LinkNodes)
                {
                    // 如果在closelist中,不处理
                    if (CloseList.Contains(Node))
                    {
                        continue;
                    }

                    int index = OpenList.GetIndexOf(Node);
                    if (index >= 0)
                    {
                        BinaryHeapNode NodeInOpenList = OpenList[index];
                        if (NodeInOpenList.Data.F > Node.F)
                        {
                            OpenList.Modify(index, Node);
                        }
                        continue;
                    }

                    OpenList.Insert(Node);
                }
            }

            return false;
        }
Esempio n. 2
0
        protected LinkedList<PathNode> __GetLinkNode__(PathNode Node, PathNode EndNode)
        {
            LinkedList<PathNode> LinkNodes = new LinkedList<PathNode>();

            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    for (int k = -1; k < 2; k++)
                    {
                        if (i == 0 && j == 0 && k == 0)
                            continue;

                        int GAddValue = 0;

                        if (Mathf.Abs(i + j + k) % 2 == 0)
                        {
                            GAddValue = 10;
                        }
                        else
                        {
                            GAddValue = 14;
                        }

                        // 此处指定了坐标和父节点
                        PathNode __Node = new PathNode(Node.X + i, Node.Y + j, Node.Z + k, Node);
                        __Node.Build(Node.G + GAddValue, EndNode);
                        LinkNodes.AddLast(__Node);
                    }
                }
            }

            return LinkNodes;
        }