Esempio n. 1
0
    void drawEdges()
    {
        foreach (Transform first in waypoints.GetComponentInChildren <Transform>())
        {
            NodeAway tmp = new NodeAway(first.position.x, first.position.y);
            tmp.wayp = first.gameObject;
            allNodeAways.Add(tmp);

            foreach (Transform second in waypoints.GetComponentInChildren <Transform>())
            {
                if (first.localPosition != second.localPosition)
                {
                    if (clearPath(first, second))
                    {
                        engeline = new GameObject();
                        engeline.transform.position = new Vector3(first.localPosition.x, first.localPosition.y, -2);
                        engeline.AddComponent <LineRenderer>();
                        engeline.transform.parent = first;

                        LineRenderer lr = engeline.GetComponent <LineRenderer>();
                        lr.material   = line_mat;
                        lr.startColor = Color.green;
                        lr.endColor   = Color.green;

                        lr.startWidth    = .3f;
                        lr.endWidth      = .3f;
                        lr.positionCount = 2;
                        lr.SetPosition(0, first.localPosition);
                        lr.SetPosition(1, second.localPosition);
                    }
                }
            }
        }
    }
Esempio n. 2
0
    List <NodeAway> getWayNei(NodeAway a)
    {
        List <NodeAway> neighs = new List <NodeAway>();

        foreach (Transform line in a.wayp.GetComponentInChildren <Transform>())
        {
            Vector3 pos = new Vector3(line.GetComponent <LineRenderer>().GetPosition(1).x, line.GetComponent <LineRenderer>().GetPosition(1).y, -2);
            foreach (NodeAway nd in allNodeAways)
            {
                if (nd.gridX == pos.x && nd.gridY == pos.y)
                {
                    neighs.Add(nd);
                }
            }
        }
        return(neighs);
    }
Esempio n. 3
0
    List <NodeAway> RetracePathW(NodeAway startNode, NodeAway endNode)
    {
        // print("Start: " + startNode.gridX + " "+startNode.gridY);
        //print("End: " + endNode.gridX + " "+endNode.gridY);
        List <NodeAway> path        = new List <NodeAway>();
        NodeAway        currentNode = endNode;

        while (currentNode.gridX != startNode.gridX || currentNode.gridY != startNode.gridY)
        {
            //print(lol);
            path.Add(currentNode);
            //by taking the parentNodes we assigned
            currentNode = currentNode.parentNode;
            // print(currentNode.gridX + " " + currentNode.gridY);
        }
        path.Add(startNode);

        //then we simply reverse the list
        path.Reverse();

        return(path);
    }
Esempio n. 4
0
    private List <NodeAway> FindPathWayPoint()
    {
        Vector3    startloc     = GameObject.FindGameObjectWithTag("start").transform.position;
        Vector3    closestS     = new Vector3(-999, -999, 0);
        Vector3    endloc       = GameObject.FindGameObjectWithTag("end").transform.position;
        Vector3    closestE     = new Vector3(-999, -999, 0);
        GameObject startW       = new GameObject();
        GameObject endW         = new GameObject();
        float      closestDistS = Vector3.Distance(startloc, closestS);
        float      closestDistE = Vector3.Distance(startloc, closestE);

        //get the nearest waypoint
        foreach (Transform x in waypoints.GetComponentInChildren <Transform>())
        {
            if (Vector3.Distance(startloc, x.localPosition) < closestDistS)
            {
                closestDistS = Vector3.Distance(startloc, x.localPosition);
                closestS     = x.localPosition;
                startW       = x.gameObject;
            }
            if (Vector3.Distance(endloc, x.localPosition) < closestDistE)
            {
                closestDistE = Vector3.Distance(endloc, x.localPosition);
                closestE     = x.localPosition;
                endW         = x.gameObject;
            }
        }
        //closest is the locaitons of the closest  object in word space

        NodeAway startN = new NodeAway(closestS.x, closestS.y);

        startN.wayp = startW;
        openSetW.Add(startN);
        StartCoroutine(ChangedSearched2());
        while (openSetW.Count > 0)
        {
            NodeAway currentNode = openSetW[0];
            for (int i = 0; i < openSetW.Count; i++)
            {
                //We check the costs for the current node
                //You can have more opt. here but that's not important now
                if (openSetW[i].fCost < currentNode.fCost ||
                    (openSetW[i].fCost == currentNode.fCost && openSetW[i].hCost < currentNode.hCost))
                {
                    //and then we assign a new current node
                    currentNode = openSetW[i];
                }
            }


            //we remove the current node from the open set and add to the closed set
            openSetW.Remove(currentNode);
            closedSetW.Add(currentNode);
            // print("curr: " + currentNode.gridX + " " + currentNode.gridY);
            toChange2.Add(currentNode);

            //if the current node is the target node
            if (currentNode.gridX == closestE.x && currentNode.gridY == closestE.y)
            {
                //that means we reached our destination, so we are ready to retrace our path
                // print("GOAL");
                // found = true;
                return(RetracePathW(startN, currentNode));
            }
            foreach (NodeAway neighbour in getWayNei(currentNode))
            {
                if (!closedSetW.Contains(neighbour))
                {
                    float newMovementCostToNeighbour = Vector2.Distance(new Vector2(neighbour.gridX, neighbour.gridY), new Vector2(closestS.x, closestS.y));

                    if (newMovementCostToNeighbour < neighbour.gCost || !openSetW.Contains(neighbour))
                    {
                        neighbour.gCost = (int)newMovementCostToNeighbour;
                        if (manhattan)
                        {
                            neighbour.hCost = (int)(Vector2.Distance(new Vector2(neighbour.gridX, neighbour.gridY), closestE));
                        }
                        else
                        {
                            neighbour.hCost = (int)(Mathf.Sqrt(Mathf.Pow(Mathf.Abs(neighbour.gridX - endloc.x), 2) + Mathf.Pow(Mathf.Abs(neighbour.gridY - endloc.y), 2)));
                        }

                        neighbour.hCost = (int)(neighbour.hCost * weightH);

                        neighbour.parentNode = currentNode;
                        bool contains = false;
                        foreach (NodeAway n in openSetW)
                        {
                            if (n.gridX == neighbour.gridX && n.gridY == neighbour.gridY)
                            {
                                contains = true;
                            }
                        }

                        if (!contains)
                        {
                            openSetW.Add(neighbour);
                        }
                    }
                }
            }
        }
        return(new List <NodeAway>());
    }