Пример #1
0
        public IEnumerable<TileBase> GetTiles(
            Rectangle bounds,
            [NotNull] IEnumerable<TileBase> existingTiles)
        {
            TileSet tiles = new TileSet();
            Queue<TileBase> openTiles = new Queue<TileBase>();
            List<TileBase> removeTiles = new List<TileBase>();

            Rectangle tileBounds;

            foreach (TileBase tile in existingTiles)
            {
                // if tile is in bounds
                // TODO The width and height must also be >= 1px
                tileBounds = tile.GetApproximateBounds();
                if (bounds.IntersectsWith(tileBounds))
                {
                    tiles.Add(tile);

                    if (tile.GetOpenEdgeParts().Any())
                        openTiles.Enqueue(tile);
                }
                else
                    removeTiles.Add(tile);
            }

            foreach (TileBase tile in removeTiles)
                tile.RemoveAdjacent();

            if (tiles.Count < 1)
            {
                TileBase tile = Tiles[0];
                Debug.Assert(tile != null, "tile != null");

                // TODO The width and height must also be >= 1px
                tileBounds = tile.GetApproximateBounds();
                if (!bounds.IntersectsWith(tileBounds))
                {
                    tile = new TileInstance(
                        (Tile) tile,
                        tile.Label,
                        tile.Transform * Matrix3x2.CreateTranslation(bounds.Center - tileBounds.Center));
                }

                // add initial tile to tiles
                tiles.Add(tile);

                // add initial tile to end of openTiles
                openTiles.Enqueue(tile);
            }

            // while there are tiles with no neighbour
            while (openTiles.Count > 0)
            {
                TileBase tile = openTiles.Dequeue();

                // for each edgePart with no neighbour
                foreach (EdgePart edgePart in tile.GetOpenEdgeParts())
                {
                    TileBase newTile = CreateNewTile(tile, edgePart);

                    tiles.Add(newTile);

                    // if newTile is in bounds
                    // TODO The width and height must also be >= 1px
                    tileBounds = newTile.GetApproximateBounds();
                    if (bounds.IntersectsWith(tileBounds))
                    {
                        // add newTile to end of openTiles
                        openTiles.Enqueue(newTile);
                    }
                }
            }

            // Set the style for the new cells
            foreach (TileBase tile in tiles.Where(t => t.Style == null))
            {
                // set cell style from styleManager
                tile.Style = StyleManager.GetStyle(tile);
            }

            return tiles.ToArray();
        }
Пример #2
0
        public IEnumerable <TileBase> GetTiles(
            Rectangle bounds,
            [NotNull] IEnumerable <TileBase> existingTiles)
        {
            TileSet          tiles       = new TileSet();
            Queue <TileBase> openTiles   = new Queue <TileBase>();
            List <TileBase>  removeTiles = new List <TileBase>();

            Rectangle tileBounds;

            foreach (TileBase tile in existingTiles)
            {
                // if tile is in bounds
                // TODO The width and height must also be >= 1px
                tileBounds = tile.GetApproximateBounds();
                if (bounds.IntersectsWith(tileBounds))
                {
                    tiles.Add(tile);

                    if (tile.GetOpenEdgeParts().Any())
                    {
                        openTiles.Enqueue(tile);
                    }
                }
                else
                {
                    removeTiles.Add(tile);
                }
            }

            foreach (TileBase tile in removeTiles)
            {
                tile.RemoveAdjacent();
            }

            if (tiles.Count < 1)
            {
                TileBase tile = Tiles[0];
                Debug.Assert(tile != null, "tile != null");

                // TODO The width and height must also be >= 1px
                tileBounds = tile.GetApproximateBounds();
                if (!bounds.IntersectsWith(tileBounds))
                {
                    tile = new TileInstance(
                        (Tile)tile,
                        tile.Label,
                        tile.Transform * Matrix3x2.CreateTranslation(bounds.Center - tileBounds.Center));
                }

                // add initial tile to tiles
                tiles.Add(tile);

                // add initial tile to end of openTiles
                openTiles.Enqueue(tile);
            }

            // while there are tiles with no neighbour
            while (openTiles.Count > 0)
            {
                TileBase tile = openTiles.Dequeue();

                // for each edgePart with no neighbour
                foreach (EdgePart edgePart in tile.GetOpenEdgeParts())
                {
                    TileBase newTile = CreateNewTile(tile, edgePart);

                    tiles.Add(newTile);

                    // if newTile is in bounds
                    // TODO The width and height must also be >= 1px
                    tileBounds = newTile.GetApproximateBounds();
                    if (bounds.IntersectsWith(tileBounds))
                    {
                        // add newTile to end of openTiles
                        openTiles.Enqueue(newTile);
                    }
                }
            }

            // Set the style for the new cells
            foreach (TileBase tile in tiles.Where(t => t.Style == null))
            {
                // set cell style from styleManager
                tile.Style = StyleManager.GetStyle(tile);
            }

            return(tiles.ToArray());
        }