Пример #1
0
        public IList <Vector2Int> GetPath(ICellMap map, Vector2Int start, Vector2Int stop, NeighbourMode neighbourMode)
        {
            if (!_isInitialized)
            {
                Initialize(map);
            }

            CellCluster startContainer = HierarchicalMapGenerator.GetContainingCluster(HierarchicalGraph.ZeroLevelClusters, ClusterSizeZero, ClusterSizeZero, start);
            CellCluster stopContainer  = HierarchicalMapGenerator.GetContainingCluster(HierarchicalGraph.ZeroLevelClusters, ClusterSizeZero, ClusterSizeZero, stop);

            HierarchicalGraphNode startNode = new HierarchicalGraphNode();

            startNode.Position = start;
            startContainer.ConnectNode(startNode, neighbourMode);
            startNode.ParentCluster = startContainer;

            HierarchicalGraphNode stopNode = new HierarchicalGraphNode();

            stopNode.Position = stop;
            stopContainer.ConnectNode(stopNode, neighbourMode);
            stopNode.ParentCluster = stopContainer;

            AStarAlgorithm aStarAlgorithm = new AStarAlgorithm();

            _currentCellCluster = null;
            aStarAlgorithm.OnCellViewedEvent += OnAStarCellViewed;
            IList <IGraphNode> abstractPath = aStarAlgorithm.GetPath(null, startNode, stopNode);
            List <Vector2Int>  path         = null;

            if (abstractPath != null)
            {
                path = new List <Vector2Int>();
                for (var i = 0; i < abstractPath.Count - 1; i++)
                {
                    HierarchicalGraphNode nodeA = (HierarchicalGraphNode)abstractPath[i];
                    HierarchicalGraphNode nodeB = (HierarchicalGraphNode)abstractPath[i + 1];
                    if (nodeA.ParentCluster == nodeB.ParentCluster)
                    {
                        CoordinateTransformer transformer =
                            new CoordinateTransformer(nodeA.ParentCluster, nodeA.ParentCluster.LeftBottom);
                        _currentCellCluster = nodeA.ParentCluster;
                        Vector2Int         clusterStart = nodeA.Position - transformer.Transform;
                        Vector2Int         clusterStop  = nodeB.Position - transformer.Transform;
                        IList <Vector2Int> realPath     = aStarAlgorithm.GetPathSingleLayer(transformer, clusterStart, clusterStop, neighbourMode);

                        if (realPath == null)
                        {
                            var bitmap = CellMapToBitmap.GetBitmap(nodeA.ParentCluster, 16, clusterStart, clusterStop, null);
                            LogManager.Log($"Path in cluster not found. Start: {clusterStart}; Stop: {clusterStop}. Bitmap printed");
                            LogManager.Log(bitmap);
                            throw new InvalidOperationException();
                        }

                        TransformPath(realPath, transformer.Transform);
                        realPath = Utils.GetInvertedList(realPath);
                        path.AddRange(realPath);
                    }
                }
            }

            DestroyConnections(startNode);
            DestroyConnections(stopNode);
            DestroyData(HierarchicalGraph.ZeroLevelClusters);

            return(path);
        }