Esempio n. 1
0
    public List <Vector3> PathFind(aStarPoint NodeStart, aStarPoint NodeFinish)
    {
        Heap <aStarPoint> openList   = new Heap <aStarPoint>(mc.mapStarMap.GetLength(0) * mc.mapStarMap.GetLength(1));
        List <aStarPoint> closedList = new List <aStarPoint> ();

        openList.Add(NodeStart);
        int t = 0;

        while (openList.Count > 0)
        {
            t++;
            aStarPoint currentNode = openList.RemoveFirst();
            closedList.Add(currentNode);
            if (currentNode == NodeFinish)
            {
                List <Vector3> Path = new List <Vector3> ();
                aStarPoint     Node = NodeFinish;
                while (Node != NodeStart)
                {
                    Path.Add(Node.Location);
                    Node = Node.parent;
                }
                foreach (aStarPoint a in mc.mapStarMap)
                {
                    a.reset();
                }
                Path.Reverse();
                return(Path);
            }

            List <aStarPoint> neighbours = findValidNeighbours(currentNode);
            foreach (aStarPoint node in neighbours)
            {
                if (closedList.Contains(node))
                {
                    continue;
                }
                int g = Mathf.RoundToInt(Vector2.Distance(node.gridPosition(), currentNode.gridPosition()) * 10);
                int h = GetDistance(node, NodeFinish);
                int f = g + h;
                if (f < node.f || !openList.Contains(node))
                {
                    node.f      = f;
                    node.h      = h;
                    node.g      = g;
                    node.parent = currentNode;
                    if (!openList.Contains(node))
                    {
                        openList.Add(node);
                        openList.UpdateItem(node);
                    }
                }
            }
        }
        return(null);
    }
Esempio n. 2
0
    protected override void ThreadFunction()
    {
        Heap <aStarPoint> openList   = new Heap <aStarPoint>(mc.mapStarMap.GetLength(0) * mc.mapStarMap.GetLength(1));
        List <aStarPoint> closedList = new List <aStarPoint> ();

        openList.Add(Location);
        int t = 0;

        while (openList.Count > 0)
        {
            t++;
            aStarPoint currentNode = openList.RemoveFirst();
            closedList.Add(currentNode);

            /*
             * if (currentNode == Destination) {
             *      List <Vector3> Path = new List<Vector3> ();
             *      aStarPoint Node = Destination;
             *      while(Node != Location){
             *              Path.Add (Node.Location);
             *              Node = Node.parent;
             *      }
             *      foreach(aStarPoint a in mc.mapStarMap){
             *              a.reset ();
             *      }
             *      Path = SimplifyPath (Path);
             *      Path.Reverse();
             *      if(!Path.Contains(Destination.Location)){
             *              Path.Add (Destination.Location);
             *      }
             *
             *      //sw.Stop ();
             *      //UnityEngine.Debug.Log (sw.ElapsedMilliseconds+"ms");
             *      //sw.Reset ();
             *      Pathfound = Path;
             * }
             */
            if (currentNode == Destination)
            {
                List <Vector3> Path = new List <Vector3> ();
                aStarPoint     Node = Destination;
                while (Node != Location)
                {
                    Path.Add(Node.Location);
                    Node = Node.parent;
                }
                foreach (aStarPoint a in mc.mapStarMap)
                {
                    a.reset();
                }
                Path = CleanRoute(Path);
                Path.Reverse();
                if (!Path.Contains(Destination.Location))
                {
                    Path.Add(Destination.Location);
                }
                Path.Insert(0, Location.Location);
                //SimplifyPath (Path);
                //sw.Stop ();
                //UnityEngine.Debug.Log (sw.ElapsedMilliseconds+"ms");
                //sw.Reset ();
                Pathfound = Path;
            }

            List <aStarPoint> neighbours = findValidNeighbours(currentNode);
            foreach (aStarPoint node in neighbours)
            {
                if (closedList.Contains(node))
                {
                    continue;
                }
                int g = Mathf.RoundToInt(Vector2.Distance(node.gridPosition(), currentNode.gridPosition()) * 10);
                int h = GetDistance(node, Destination);
                int f = g + h;
                if (f < node.f || !openList.Contains(node))
                {
                    node.f      = f;
                    node.h      = h;
                    node.g      = g;
                    node.parent = currentNode;
                    if (!openList.Contains(node))
                    {
                        //CalculatePosition (node,openList);
                        openList.Add(node);
                        openList.UpdateItem(node);
                    }
                }
            }
        }
    }