Ejemplo n.º 1
0
    public bool JPS(Vector2 startPoint, Vector2 destination) // Pathfinding using Jump Point Search algorithm
    {
        List <Node>    OPEN   = new List <Node>();           // Set of nodes to be evaluated
        HashSet <Node> CLOSED = new HashSet <Node>();        // Set of evaluated nodes

        Node startNode  = grid.GetPathableNode(startPoint);
        Node targetNode = grid.GetPathableNode(destination);    // Find the start and destination nodes from world points

        if (startNode == null || targetNode.isBlocker)
        {
            return(false);
        }

        startNode.isOpen = true;
        OPEN.Add(startNode);                    // Start by adding the starting node to the open set

        while (OPEN.Count > 0)                  // Loop until there are no more possibilities
        {
            Node currentNode = OPEN[0];

            foreach (Node n in OPEN)
            {
                if (n.fCost < currentNode.fCost || n.fCost == currentNode.hCost && n.hCost < currentNode.hCost)
                {
                    currentNode = n;            // Find the node in OPEN with the lowest f Cost
                }
            }

            OPEN.Remove(currentNode);
            currentNode.isOpen   = false;
            currentNode.isClosed = true;
            CLOSED.Add(currentNode);            // Remove the current node from OPEN and add it to CLOSED

            if (currentNode == targetNode)
            {
                foreach (Node n in CLOSED)
                {
                    n.isClosed = false;
                }

                foreach (Node n in OPEN)
                {
                    n.isOpen = false;
                }

                return(true);                    // A path has been found
            }

            List <Node> neighbouringNodes = grid.GetNeighbouringNodes(currentNode);

            foreach (Node n in neighbouringNodes)
            {
                Node jumpPoint = Jump(n, currentNode, targetNode);      // Find our jump points

                if (jumpPoint != null && !jumpPoint.isClosed)
                {
                    int gCost = currentNode.gCost + GetDistance(currentNode, jumpPoint);

                    if (!jumpPoint.isOpen || gCost < jumpPoint.gCost)
                    {
                        jumpPoint.gCost      = gCost;                                               // Set or update the g cost of the jump point
                        jumpPoint.hCost      = GetDistance(jumpPoint, targetNode);                  // Set the hCost of the jump point
                        jumpPoint.parentNode = currentNode;                                         // Set the parent of the jump point

                        if (!jumpPoint.isOpen)
                        {
                            jumpPoint.isOpen = true;
                            OPEN.Add(jumpPoint);                                              // If the jump point isn't open, add it
                        }
                    }
                }
            }
        }

        foreach (Node n in CLOSED)
        {
            n.isClosed = false;
        }

        Debug.Log("NO PATH");
        return(false);                           // A path has not been found
    }