예제 #1
0
    IEnumerator findPath(Vector3 startPos, Vector3 endPos)
    {
        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(endPos);

        if (targetNode.walkable && startNode.walkable)
        {
            Heap <Node>    openSet   = new Heap <Node> (grid.MaxSize);
            HashSet <Node> closedSet = new HashSet <Node> ();

            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                Node currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);

                if (currentNode == targetNode)
                {
                    pathSuccess = true;
                    break;
                }

                foreach (Node neighbor in grid.GetNeighbors(currentNode))
                {
                    if (!neighbor.walkable || closedSet.Contains(neighbor))
                    {
                        continue;
                    }

                    int newMovementCostToNeighbor = currentNode.Gcost + getDistance(currentNode, neighbor);

                    if (newMovementCostToNeighbor < neighbor.Gcost || !openSet.Contains(neighbor))
                    {
                        neighbor.Gcost  = newMovementCostToNeighbor;
                        neighbor.Hcost  = getDistance(neighbor, targetNode);
                        neighbor.parent = currentNode;

                        if (!openSet.Contains(neighbor))
                        {
                            openSet.Add(neighbor);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessing(waypoints, pathSuccess);
    }
예제 #2
0
    IEnumerator FindPath(Vector3 _start, Vector3 _end)
    {
        //if they're on the same node, then return the nodes position

        Vector3[] path        = new Vector3[0];
        bool      pathSuccess = false;

        PathNode start  = tileGrid.GetTileAtPos(_start).GetComponent <PathNode>();
        PathNode target = tileGrid.GetTileAtPos(_end).GetComponent <PathNode>();

        if (!start || !target)
        {
            yield return(null);
        }

        if (start != target)
        {
            //list of open and closed nodes
            openNodes   = new Heap <PathNode>(tileGrid.xSize * tileGrid.ySize);
            closedNodes = new HashSet <PathNode>();

            openNodes.Add(start);

            int x = 0;
            while (openNodes.Count > 0)
            {
                currentNode = openNodes.RemoveFirst();
                closedNodes.Add(currentNode);

                if (currentNode == target)
                {
                    pathSuccess = true;
                    break;
                }

                foreach (PathNode n in tileGrid.GetNeighbours(currentNode))
                {
                    if (closedNodes.Contains(n))
                    {
                        continue;
                    }
                    if (!n.walkable)
                    {
                        continue;
                    }

                    int f = currentNode.g + GetDistance(currentNode, n) + n.weight;
                    if (f < n.g || !openNodes.Contains(n))
                    {
                        n.g          = f;
                        n.h          = GetDistance(n, target);
                        n.ParentNode = currentNode;

                        if (!openNodes.Contains(n))
                        {
                            openNodes.Add(n);
                        }
                    }
                    x++;
                }

                if (closedNodes.Count >= maxSize - 1)
                {
                    currentNode = null;
                    break;
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            path = RetracePath(start, target);
        }
        requestManager.FinishedProcessing(path, pathSuccess);
    }
예제 #3
0
    private IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        if (startNode.Walkable && targetNode.Walkable)
        {
            Heap <Node>    openSet  = new Heap <Node>(grid.MaxSize);
            HashSet <Node> closeSet = new HashSet <Node>();
            openSet.Add(startNode);

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

                if (currentNode == targetNode)
                {
                    sw.Stop();
                    print("path found: " + sw.ElapsedMilliseconds + "ms");
                    pathSuccess = true;
                    break;
                }

                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if (!neighbour.Walkable || closeSet.Contains(neighbour))
                    {
                        continue;
                    }

                    int newMovementCost = currentNode.GCost + GetDistance(currentNode, neighbour);
                    if (newMovementCost < neighbour.GCost || !openSet.Contains(neighbour))                     //if shorter or unvisited!
                    {
                        neighbour.GCost  = newMovementCost;
                        neighbour.HCost  = GetDistance(neighbour, targetNode);
                        neighbour.parent = currentNode;

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

        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }

        requestManager.FinishedProcessing(waypoints, pathSuccess);
    }