private IEnumerable <TiledMapLayerModel> CreateImageLayerModels(TiledMapImageLayer imageLayer)
        {
            var modelBuilder = new TiledMapStaticLayerModelBuilder();

            modelBuilder.AddSprite(imageLayer.Image, imageLayer.Position, imageLayer.Image.Bounds, TiledMapTileFlipFlags.None);
            yield return(modelBuilder.Build(_graphicsDevice, imageLayer.Image));
        }
        private IEnumerable <TiledMapLayerModel> CreateTileLayerModels(TiledMap map, TiledMapTileLayer tileLayer)
        {
            var layerModels          = new List <TiledMapLayerModel>();
            var staticLayerBuilder   = new TiledMapStaticLayerModelBuilder();
            var animatedLayerBuilder = new TiledMapAnimatedLayerModelBuilder();

            foreach (var tileset in map.Tilesets)
            {
                var firstGlobalIdentifier = map.GetTilesetFirstGlobalIdentifier(tileset);
                var lastGlobalIdentifier  = tileset.TileCount + firstGlobalIdentifier - 1;
                var texture = tileset.Texture;

                foreach (var tile in tileLayer.Tiles.Where(t => firstGlobalIdentifier <= t.GlobalIdentifier && t.GlobalIdentifier <= lastGlobalIdentifier))
                {
                    var tileGid             = tile.GlobalIdentifier;
                    var localTileIdentifier = tileGid - firstGlobalIdentifier;
                    var position            = GetTilePosition(map, tile);
                    var tilesetColumns      = tileset.Columns == 0 ? 1 : tileset.Columns; // fixes a problem (what problem exactly?)
                    var sourceRectangle     = TiledMapHelper.GetTileSourceRectangle(localTileIdentifier, tileset.TileWidth, tileset.TileHeight, tilesetColumns, tileset.Margin, tileset.Spacing);
                    var flipFlags           = tile.Flags;

                    // animated tiles
                    var tilesetTile = tileset.Tiles.FirstOrDefault(x => x.LocalTileIdentifier == localTileIdentifier);

                    if (tilesetTile is TiledMapTilesetAnimatedTile animatedTilesetTile)
                    {
                        animatedLayerBuilder.AddSprite(texture, position, sourceRectangle, flipFlags);
                        animatedLayerBuilder.AnimatedTilesetTiles.Add(animatedTilesetTile);

                        if (animatedLayerBuilder.IsFull)
                        {
                            layerModels.Add(animatedLayerBuilder.Build(_graphicsDevice, texture));
                        }
                    }
                    else
                    {
                        staticLayerBuilder.AddSprite(texture, position, sourceRectangle, flipFlags);

                        if (staticLayerBuilder.IsFull)
                        {
                            layerModels.Add(staticLayerBuilder.Build(_graphicsDevice, texture));
                        }
                    }
                }

                if (staticLayerBuilder.IsBuildable)
                {
                    layerModels.Add(staticLayerBuilder.Build(_graphicsDevice, texture));
                }

                if (animatedLayerBuilder.IsBuildable)
                {
                    layerModels.Add(animatedLayerBuilder.Build(_graphicsDevice, texture));
                }
            }

            return(layerModels);
        }