コード例 #1
0
        public void FindPath(Vector2 startPosition, Vector2 endPosition)
        {
            AStarNode startNode = mapGenerator.nodes[(int)startPosition.x, (int)startPosition.y] as AStarNode;
            AStarNode endNode   = mapGenerator.nodes[(int)endPosition.x, (int)endPosition.y] as AStarNode;

            List <AStarNode>    unvisitedNodes = new List <AStarNode>();
            HashSet <AStarNode> visitedNodes   = new HashSet <AStarNode>();

            unvisitedNodes.Add(startNode);

            while (unvisitedNodes.Count > 0)
            {
                AStarNode currentNode = unvisitedNodes[0];
                for (int i = 1; i < unvisitedNodes.Count; i++)
                {
                    if (unvisitedNodes[i].FCost < currentNode.FCost || unvisitedNodes[i].FCost == currentNode.FCost && unvisitedNodes[i].HCost < currentNode.HCost)
                    {
                        currentNode = unvisitedNodes[i];
                    }
                }
                unvisitedNodes.Remove(currentNode);
                visitedNodes.Add(currentNode);

                if (currentNode == endNode)
                {
                    signalBus.Fire(new PathFoundSignal()
                    {
                        startingNode = startNode, endNode = endNode
                    });
                    return;
                }

                foreach (AStarNode neighbour in currentNode.Neighbours)
                {
                    if (neighbour.IsObstructed || visitedNodes.Contains(neighbour))
                    {
                        continue;
                    }
                    int MoveCost = (int)currentNode.GCost + NodeDistanceCalculator.GetDistance(currentNode.Position, neighbour.Position);

                    if (MoveCost < neighbour.GCost || !unvisitedNodes.Contains(neighbour))
                    {
                        neighbour.GCost  = MoveCost;
                        neighbour.HCost  = NodeDistanceCalculator.GetDistance(neighbour.Position, endNode.Position);
                        neighbour.Parent = currentNode;

                        if (!unvisitedNodes.Contains(neighbour))
                        {
                            unvisitedNodes.Add(neighbour);
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void FindPath(Vector2 startPosition, Vector2 endPosition)
        {
            startPointPosition = startPosition;
            GetDijkstraNodes();
            DijkstraNode startNode = mapGenerator.nodes[(int)startPosition.x, (int)startPosition.y] as DijkstraNode;
            DijkstraNode endNode   = mapGenerator.nodes[(int)endPosition.x, (int)endPosition.y] as DijkstraNode;

            while (unexploredNodes.Count > 0)
            {
                unexploredNodes.Sort((x, y) => x.DistanceFromStart.CompareTo(y.DistanceFromStart));

                DijkstraNode currentNode = unexploredNodes[0];
                if (currentNode == endNode)
                {
                    signalBus.Fire(new PathFoundSignal()
                    {
                        startingNode = startNode, endNode = endNode
                    });
                    return;
                }
                unexploredNodes.Remove(currentNode);

                List <DijkstraNode> neighbours = GetNeighbours(currentNode.Neighbours);
                foreach (DijkstraNode neighbour in neighbours)
                {
                    if (neighbour == null)
                    {
                        continue;
                    }
                    DijkstraNode node = neighbour.GetComponent <DijkstraNode>();

                    if (unexploredNodes.Contains(neighbour) && !node.IsObstructed)
                    {
                        int distance = NodeDistanceCalculator.GetDistance(neighbour.Position, currentNode.Position);
                        distance = currentNode.DistanceFromStart + distance;
                        if (distance < node.DistanceFromStart)
                        {
                            node.DistanceFromStart = distance;
                            node.Parent            = currentNode;
                        }
                    }
                }
            }
        }