Exemplo n.º 1
0
    public List <GameObject> GetPathDjikstraAsGameObject(GameObject a_start, GameObject a_end)
    {
        Djikstras.Node start = a_start.GetComponent <Djikstras.Node>();
        Djikstras.Node end   = a_end.GetComponent <Djikstras.Node>();

        return(GetPathDjikstraAsGameObject(start, end));
    }
Exemplo n.º 2
0
    public List <Djikstras.Node> GetPathDjikstra(GameObject a_start, GameObject a_end)
    {
        // Gets GameObject from Node components
        Djikstras.Node startNode = a_start.GetComponent <Djikstras.Node>();
        Djikstras.Node endNode   = a_end.GetComponent <Djikstras.Node>();

        // Return path
        return(GetPathDjikstra(startNode, endNode));
    }
Exemplo n.º 3
0
    public List <GameObject> GetPathDjikstraAsGameObject(Djikstras.Node a_start, Djikstras.Node a_end)
    {
        List <Djikstras.Node> pathAsNodes = GetPathDjikstra(a_start, a_end);
        // Adds the gameobject component of every node in the path to another list
        List <GameObject> path = new List <GameObject>();

        for (int i = 0; i < pathAsNodes.Count; i++)
        {
            path.Add(pathAsNodes[i].gameObject);
        }

        // New path of game objects
        return(path);
    }
Exemplo n.º 4
0
    public List <Djikstras.Node> GetPathDjikstra(Djikstras.Node a_start, Djikstras.Node a_end)
    {
        List <Djikstras.Node> path = new List <Djikstras.Node>();

        // Resets everything to default values
        for (int x = 0; x < gridDimensions.x; x++)
        {
            for (int y = 0; y < gridDimensions.y; y++)
            {
                nodes[x, y].ResetNode();
            }
        }

        path.Clear();

        if (!a_start || !a_end)
        {
            Debug.LogWarning("Start or End node is equal to NULL");
            return(path);
        }

        if (a_start == a_end)
        {
            path.Add(a_start);
            return(path);
        }

        Queue <Djikstras.Node> openList   = new Queue <Djikstras.Node>();
        List <Djikstras.Node>  closedList = new List <Djikstras.Node>();

        openList.Enqueue(a_start);

        Djikstras.Node currentNode;
        while (openList.Count > 0)
        {
            currentNode = openList.Dequeue();
            closedList.Add(currentNode);

            List <Djikstras.Edge> edges = currentNode.GetEdges();
            for (int i = 0; i < edges.Count; i++)
            {
                // If edge is not valid, move along
                if (!edges[i].IsValid())
                {
                    continue;
                }

                //
                Djikstras.Node otherNode = null;
                if (edges[i].GetNodes()[0] == currentNode)
                {
                    otherNode = edges[i].GetNodes()[1];
                }
                else
                {
                    otherNode = edges[i].GetNodes()[0];
                }

                // If the other node is not in the closed list
                if (!closedList.Contains(otherNode))
                {
                    float currentGScore = currentNode.GetGScore() + edges[i].GetCost();

                    // If the other node is not in the open list
                    if (!openList.Contains(otherNode))
                    {
                        // Set the GScore to the current node's score + the cost of the edge
                        otherNode.SetGScore(currentNode.GetGScore() + edges[i].GetCost());

                        otherNode.SetPreviousNode(currentNode);

                        // Add the other node to the open list
                        openList.Enqueue(otherNode);
                    }
                    else if (currentGScore < otherNode.GetGScore())
                    {
                        // Override the current gScore
                        otherNode.SetGScore(currentGScore);
                        // and override the previous node
                        otherNode.SetPreviousNode(currentNode);
                    }
                }
            }
        }

        Djikstras.Node endNode = a_end;
        path.Add(endNode);

        while (endNode != a_start)
        {
            if (!endNode)
            {
                path.Clear();
                return(path);
            }

            endNode = endNode.GetPreviousNode();
            path.Add(endNode);
        }

        // Return the final path
        return(path);
    }
Exemplo n.º 5
0
 void CreateEdge(Djikstras.Node a_nodeA, Djikstras.Node a_nodeB, float a_cost = 1.0f)
 {
     Djikstras.Edge newEdge = new Djikstras.Edge(a_nodeA, a_nodeB, a_cost);
 }