Esempio n. 1
0
        private void ConnectCaves()
        {
            var floodFillAnalyzer         = new FloodFillAnalyzer(_map);
            List <MapSection> mapSections = floodFillAnalyzer.GetMapSections();
            var unionFind = new UnionFind(mapSections.Count);

            while (unionFind.Count > 1)
            {
                for (int i = 0; i < mapSections.Count; i++)
                {
                    int                 closestMapSectionIndex = FindNearestMapSection(mapSections, i, unionFind);
                    MapSection          closestMapSection      = mapSections[closestMapSectionIndex];
                    IEnumerable <ICell> tunnelCells            = _map.GetCellsAlongLine(mapSections[i].Bounds.Center.X, mapSections[i].Bounds.Center.Y,
                                                                                        closestMapSection.Bounds.Center.X, closestMapSection.Bounds.Center.Y);
                    ICell previousCell = null;
                    foreach (ICell cell in tunnelCells)
                    {
                        _map.SetCellProperties(cell.X, cell.Y, true, true);
                        if (previousCell != null)
                        {
                            if (cell.X != previousCell.X || cell.Y != previousCell.Y)
                            {
                                _map.SetCellProperties(cell.X + 1, cell.Y, true, true);
                            }
                        }
                        previousCell = cell;
                    }
                    unionFind.Union(i, closestMapSectionIndex);
                }
            }
        }
        private void ConnectCaves()
        {
            var floodFillAnalyzer         = new FloodFillAnalyzer(_map);
            List <MapSection> mapSections = floodFillAnalyzer.GetMapSections();
            UnionFind         unionFind   = new UnionFind(mapSections.Count);

            while (unionFind.Count > 1)
            {
                for (int i = 0; i < mapSections.Count; i++)
                {
                    int                    closestMapSectionIndex = FindNearestMapSection(mapSections, i, unionFind);
                    MapSection             closestMapSection      = mapSections[closestMapSectionIndex];
                    IEnumerable <TileData> tunnelTiles            = _map.GetCellsAlongLine((int)mapSections[i].Bounds.center.x, (int)mapSections[i].Bounds.center.y,
                                                                                           (int)closestMapSection.Bounds.center.x, (int)closestMapSection.Bounds.center.y);

                    TileData previousTile = null;
                    foreach (TileData tileData in tunnelTiles)
                    {
                        _map.SetTile(tileData.Position.x, tileData.Position.y, new Tile(Tile.Type.Empty));

                        if (previousTile != null)
                        {
                            if (tileData.Position.x != previousTile.Position.x || tileData.Position.y != previousTile.Position.y)
                            {
                                _map.SetTile(tileData.Position.x, tileData.Position.y, new Tile(Tile.Type.Empty));
                            }
                        }
                        previousTile = tileData;
                    }
                    unionFind.Union(i, closestMapSectionIndex);
                }
            }
        }