Esempio n. 1
0
    /// <summary>
    /// 길찾기 코루틴
    /// </summary>
    /// <param name="start"></param>
    /// <param name="goal"></param>
    /// <returns></returns>
    private IEnumerator FindPath(CellClass start, CellClass goal)
    {
        CellClass[] wayPoints = new CellClass[0];
        mPathSuccess = false;
        if (start.GetWalkable() && goal.GetWalkable())
        {
            Heap <CellClass>    openSet  = new Heap <CellClass>(mMap.GetMaxSize());
            HashSet <CellClass> closeSet = new HashSet <CellClass>();

            openSet.Add(start);

            while (openSet.Count > 0)
            {
                CellClass currentNode = openSet.RemoveFirst();
                closeSet.Add(currentNode);

                if (currentNode == goal)
                {
                    mPathSuccess = true;
                    break;
                }

                foreach (CellClass neighbour in mMap.GetNeighbours(currentNode))
                {
                    if (!neighbour.GetWalkable() || closeSet.Contains(neighbour))
                    {
                        continue;
                    }

                    int newMovementCostToNeighbour = currentNode.GetGCost() + GetDistance(currentNode, neighbour);
                    if (newMovementCostToNeighbour < neighbour.GetGCost() || !openSet.Contains(neighbour))
                    {
                        neighbour.SetGCost(newMovementCostToNeighbour);
                        neighbour.SetHCost(GetDistance(neighbour, goal));
                        neighbour.SetParent(currentNode);

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                    }
                }
            }
        }

        yield return(null);

        if (mPathSuccess)
        {
            wayPoints = RetracePath(start, goal);
            OnPathFound(wayPoints, true);
        }
    }