コード例 #1
0
    public List <Node> FindPath(Vector3 startPosition, Vector3 targetPosition)
    {
        Node startNode  = customGrid.NodeFromWorldPoint(startPosition);
        Node targetNode = customGrid.NodeFromWorldPoint(targetPosition);

        List <Node>    openSet   = new List <Node>();
        HashSet <Node> closedSet = new HashSet <Node>();

        openSet.Add(startNode);//adiciona node inicial a lista Open

        while (openSet.Count > 0)
        {
            Node currentNode = openSet[0]; // primeiro valor a lista open é o valor do atual node
            for (int i = 1; i < openSet.Count; i++)
            {
                // considere todos os valores dentro da lista open
                //se o valor F de algum nodo for menor que o atual
                //ou se for igual e o valor H for menor do que o do atual
                // o novo nodo se tornará o nodo atual
                if (openSet[i].fCost < currentNode.fCost || (openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost))
                {
                    currentNode = openSet[i];
                }
            }
            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            if (currentNode == targetNode)
            {
                return(Caminho_Reverso(startNode, targetNode));//mudar aqui
            }

            foreach (Node neighbours in customGrid.GetNeighbours(currentNode))
            {
                //se nao da pra passar ou ja ta na lista dos fechados
                if (!neighbours.walkable || closedSet.Contains(neighbours))
                {
                    continue;
                }



                int new_MovCost = currentNode.gCost + GetDistanceA_B(currentNode, neighbours);

                if (new_MovCost < neighbours.gCost || !openSet.Contains(neighbours))
                {
                    neighbours.gCost  = new_MovCost;
                    neighbours.hCost  = GetDistanceA_B(neighbours, targetNode);
                    neighbours.parent = currentNode;

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

        return(null);
    }
コード例 #2
0
    public List <Node> FindPath(Vector2 startPositiong, Vector2 targetPosition, GameObject walkableException = null)
    {
        Node startNode  = grid.NodeFromWorldPoint(startPositiong);
        Node targetNode = grid.NodeFromWorldPoint(targetPosition);

        List <Node>    openSet   = new List <Node>();
        HashSet <Node> closedSet = new HashSet <Node>();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            Node currentNode = openSet[0];
            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].FCost < currentNode.FCost || openSet[i].FCost == currentNode.FCost && openSet[i].hCost < currentNode.hCost)
                {
                    currentNode = openSet[i];
                }
            }
            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            if (currentNode == targetNode)
            {
                return(RetracePath(startNode, targetNode));
            }

            foreach (Node neighbour in grid.GetNeighbours(currentNode))
            {
                var isException = IsWalkableException(walkableException, neighbour);
                if ((!neighbour.walkable && !isException) || closedSet.Contains(neighbour))
                {
                    continue;
                }

                int newMovementCost = currentNode.gCost + GetNodeDistance(currentNode, neighbour);
                if (newMovementCost < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newMovementCost;
                    neighbour.hCost  = GetNodeDistance(neighbour, targetNode);
                    neighbour.parent = currentNode;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
        return(null);
    }
コード例 #3
0
    Vector2[] FindPath(Vector2 from, Vector2 to)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

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

        Node startNode  = grid.NodeFromWorldPoint(from);
        Node targetNode = grid.NodeFromWorldPoint(to);

        startNode.parent = startNode;

        if (!startNode.walkable)
        {
            startNode = grid.ClosestWalkableNode(startNode);
        }
        if (!targetNode.walkable)
        {
            targetNode = grid.ClosestWalkableNode(targetNode);
        }

        if (startNode.walkable && targetNode.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)
                {
                    sw.Stop();
                    // print ("Path found: " + sw.ElapsedMilliseconds + " ms");
                    pathSuccess = true;
                    break;
                }

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

                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + TurningCost(currentNode, neighbour);
                    if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = newMovementCostToNeighbour;
                        neighbour.hCost  = GetDistance(neighbour, targetNode);
                        neighbour.parent = currentNode;

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

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

        return(waypoints);
    }