Ejemplo n.º 1
0
    public void Start(Vector3 curPos, Vector3 endPos)
    {
        OpenList.Clear();
        ClosedList.Clear();
        Path.Clear();
        started = true;

        float startDist = 100000.0f;
        float endDist = 100000.0f;

        foreach (ListedNode n in this.nodeList)
        {
            float dist = Vector3.Distance(curPos, n.Position);
            if (dist < startDist)
            {
                startDist = dist;
                startNode = (ListedNode)n;
            }
            dist = Vector3.Distance(endPos, n.Position);
            if (dist < endDist)
            {
                endDist = dist;
                endNode = (ListedNode)n;
            }
        }

        startNode.Scores(startNode.Position, endNode.Position);
        endNode.Scores(startNode.Position, endNode.Position);

        currentNode = startNode;
        OpenList.Add(startNode);
    }
Ejemplo n.º 2
0
    // We need to convert the grid we have, into a referenced grid to nodes without links.
    void Start()
    {
        int i = 0;

        // Set the nodes in a neat array
        myWaypoints = new ListedNode[WaypointList.Count];
        foreach (WaypointNode n in WaypointList)
        {
            myWaypoints[i] = (ListedNode)n;
            i++;
        }

        // Set the neighbors in the neat array nodes
        for (i = 0; i < WaypointList.Count; i++)
        {
            // ITerate through the neighbornodes
            for (int x = 0; x < WaypointList[i].NeighborNodes.Length; x++)
            {
                ListedNode listedNode = getListedNode(WaypointList[i].NeighborNodes[x].from);
                if (myWaypoints[i].Position != listedNode.Position)
                {
                    myWaypoints[i].NeighborNodes[x] = listedNode;
                    continue;
                }
                listedNode = getListedNode(WaypointList[i].NeighborNodes[x].to);
                if (myWaypoints[i].Position != listedNode.Position)
                {
                    myWaypoints[i].NeighborNodes[x] = listedNode;
                    continue;
                }
                Debug.LogWarning("ERROR, NO MIRRION DORRARS FOR U!! F**K U!!");
            }
        }
    }
Ejemplo n.º 3
0
    public void Start(Vector3 curPos, Vector3 endPos)
    {
        OpenList.Clear();
        ClosedList.Clear();
        Path.Clear();
        started = true;

        float startDist = 100000.0f;
        float endDist   = 100000.0f;

        foreach (ListedNode n in this.nodeList)
        {
            float dist = Vector3.Distance(curPos, n.Position);
            if (dist < startDist)
            {
                startDist = dist;
                startNode = (ListedNode)n;
            }
            dist = Vector3.Distance(endPos, n.Position);
            if (dist < endDist)
            {
                endDist = dist;
                endNode = (ListedNode)n;
            }
        }

        startNode.Scores(startNode.Position, endNode.Position);
        endNode.Scores(startNode.Position, endNode.Position);

        currentNode = startNode;
        OpenList.Add(startNode);
    }
Ejemplo n.º 4
0
 void AddNeighboursToOpenList(ListedNode n)
 {
     foreach (ListedNode ln in n.NeighborNodes)
     {
         if (!OpenList.Contains(ln) && !ClosedList.Contains(ln))
         {
             ln.Parent = currentNode;
             ln.Scores(startNode.Position, endNode.Position);
             OpenList.Add(ln);
         }
     }
 }
Ejemplo n.º 5
0
    public bool Update()
    {
        if (started)
        {
            ListedNode bestNode = null;
            float      BestF    = 10000.0f;

            if (currentNode == endNode)
            {
                started = false;

                while (currentNode != startNode)
                {
                    Path.Add(currentNode);
                    currentNode = currentNode.Parent;
                    ClosedList.Remove(currentNode);
                }
            }

            foreach (ListedNode n in OpenList)
            {
                if (n.F < BestF)
                {
                    BestF    = n.F;
                    bestNode = n;
                }
            }

            currentNode = bestNode;

            if (bestNode != null)
            {
                OpenList.Remove(currentNode);
                ClosedList.Add(currentNode);
                AddNeighboursToOpenList(currentNode);
            }
            else
            {
                Debug.LogError("bestNode should not be null!!!!");
            }


            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 6
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.F6) && Application.isEditor)
        {
            showNodes = !showNodes;
        }

        if (findPath)
        {
            if (instant)
            {
                while (myFinder.Update())
                {
                }
            }
            else
            {
                myFinder.Update();
            }

            if (!myFinder.Update())
            {
                myPath      = myFinder.GetPath();
                findPath    = false;
                startMoving = true;
                pointInPath = myPath.Count - 1;
            }
        }
        if (startMoving)
        {
            ListedNode point = null;
            if (pointInPath >= 0)
            {
                point = (ListedNode)myPath[pointInPath];
            }

            if (point != null)
            {
                transform.Translate(((point.Position - transform.position).normalized * Time.deltaTime * 15.0f));
                // transform.LookAt(point.Position);
                if (Vector3.Distance(transform.position, point.Position) < 0.2f)
                {
                    pointInPath--;
                }
            }
        }
    }
Ejemplo n.º 7
0
    public bool Update()
    {
        if (started)
        {
            ListedNode bestNode = null;
            float BestF = 10000.0f;

            if (currentNode == endNode)
            {
                started = false;

                while (currentNode != startNode)
                {
                    Path.Add(currentNode);
                    currentNode = currentNode.Parent;
                    ClosedList.Remove(currentNode);
                }
            }

            foreach (ListedNode n in OpenList)
            {
                if (n.F < BestF)
                {
                    BestF = n.F;
                    bestNode = n;
                }
            }

            currentNode = bestNode;

            if (bestNode != null)
            {
                OpenList.Remove(currentNode);
                ClosedList.Add(currentNode);
                AddNeighboursToOpenList(currentNode);
            }
            else Debug.LogError("bestNode should not be null!!!!");


            return true;
        }
        else
        {
            return false;
        }
    }
Ejemplo n.º 8
0
 // Constructor for pathfinder.
 public PathFinder(ListedNode[] nodeList)
 {
     this.nodeList = nodeList;
 }
Ejemplo n.º 9
0
 void AddNeighboursToOpenList(ListedNode n)
 {
     foreach (ListedNode ln in n.NeighborNodes)
     {
         if (!OpenList.Contains(ln) && !ClosedList.Contains(ln))
         {
             ln.Parent = currentNode;
             ln.Scores(startNode.Position, endNode.Position);
             OpenList.Add(ln);
         }
     }
 }