コード例 #1
0
        IEnumerator Pathfind()
        {
            frontier.Enqueue(new InternalPathNode(startPos, ActionType.WALK), 0);

            movementCosts.Set(startPos, 0);
            parents.Set(startPos, new InternalPathNode(startPos, ActionType.WALK));
            closestDistance = int.MaxValue;

            InternalPathNode current;

            var cycles = 0;

            while (frontier.Count > 0)
            {
                ++cycles;

                current = frontier.Dequeue();


                var adj = GetAdjacent(current.coord);
                foreach (var pos in adj)
                {
                    if (!IsClosed(pos.coord))
                    {
                        if (!frontier.Contains(pos))
                        {
                            movementCosts.Set(pos.coord, Mathf.Infinity);                             // so (new < orig) comparison works
                        }

                        UpdateNode(current, pos);

                        if (pos.coord == endPos)
                        {
                            ReconstructPath(pos);
                            yield break;
                        }

                        if (HexUtil.DistanceFlat(pos.coord, endPos) < closestDistance)
                        {
                            closestDistance = HexUtil.DistanceFlat(pos.coord, endPos);
                            closestPoint    = pos;
                        }
                    }
                }

                if (cycles >= cyclesPerFrame)
                {
                    cycles = 0;
                    yield return(null);
                }
            }


            // endpos was not on connected graph; return closest node
            ReconstructPath(closestPoint);
        }