Exemplo n.º 1
0
        private void FindPath()
        {
            TileNavGraph graph = TileNavGraph.Instance;

            OpenList.Add(origin);
            Node node       = GetTargetedNode();
            int  securityNb = 10000;

            while (node != null && securityNb != 0)
            {
                if (Path.IndexOf(node) != -1)
                {
                    break;
                }
                securityNb--;
                Path.Add(node);
                node = node.parent;
            }
            if (securityNb == 0)
            {
                Debug.LogError("No Path Found ...");
            }

            Path.Reverse();
            Reset();
            TileNavGraph.Instance.GuizmoPath = Path;
            TileNavGraph.Instance.OpenList   = OpenList;
            TileNavGraph.Instance.ClosedList = ClosedList;

            onPathFound();
        }
        public List <Connection> FindPath(Vector3 pos)
        {
            if (pos == transform.position)
            {
                return(null);
            }

            TileNavGraph graph = TileNavGraph.Instance;

            if (graph == null)
            {
                return(null);
            }

            targetNode = graph.GetNode(pos);

            if (targetNode == null || targetNode.Weight == graph.UnreachableCost)
            {
                return(null);
            }

            startNode = graph.GetNode(transform.position);

            ComputePathfinding(targetNode);
            return(path);
        }
        private void ComputePathfinding(Node target)
        {
            NodeRecord   goal  = new NodeRecord(target);
            TileNavGraph graph = TileNavGraph.Instance;

            NodeRecord start = new NodeRecord(startNode);

            openList.Add(start);

            NodeRecord currentNode = null;

            while (openList.Count > 0)
            {
                if (openList.Count > 1)
                {
                    openList = openList.OrderBy(o => o.estimatedTotalCost).ToList();
                }
                currentNode = openList[0];

                closedList.Add(currentNode);
                openList.Remove(currentNode);

                if (currentNode.node == goal.node)
                {
                    break;
                }

                foreach (Connection connection in graph.ConnectionsGraph[currentNode.node])
                {
                    Node       nextNode       = connection.ToNode;
                    NodeRecord nextNodeRecord = closedList.Find(o => o.node == nextNode);
                    if (nextNodeRecord == null)
                    {
                        float nextNodeCost = currentNode.costSoFar + connection.Cost;
                        nextNodeRecord = openList.Find(o => o.node == nextNode);

                        if (nextNodeRecord == null)
                        {
                            NodeRecord record = new NodeRecord(nextNode);
                            record.costSoFar          = nextNodeCost;
                            record.estimatedTotalCost = record.costSoFar + EstimateDistance(nextNode.Position, goal.node.Position);
                            record.connection         = connection;
                            openList.Add(record);
                        }
                        else if (nextNodeRecord.costSoFar > nextNodeCost)
                        {
                            nextNodeRecord.costSoFar          = nextNodeCost;
                            nextNodeRecord.estimatedTotalCost = nextNodeRecord.costSoFar + EstimateDistance(nextNode.Position, goal.node.Position);
                            nextNodeRecord.connection         = connection;
                        }
                    }
                }
            }

            openList.Clear();
            RetracePath(start, currentNode);
        }
Exemplo n.º 4
0
        public void SearchPath(Vector3 beginPos, Vector3 targetPos, Steering.OnPathFoundHandler onPathFoundEvent)
        {
            TileNavGraph graph = TileNavGraph.Instance;

            origin = graph.GetNode(beginPos);
            target = graph.GetNode(targetPos);

            if (origin == null || target == null)
            {
                return;
            }

            OpenList.Clear();
            ClosedList.Clear();
            Path.Clear();

            onPathFound = onPathFoundEvent;
            PathThread  = new Thread(new ThreadStart(FindPath));
            PathThread.Start();
        }