internal Tile LoadTile(Layer layer, int gid)
        {
            if (gid == 0)
            {
                return(null);
            }
            TileSheet selectedTileSheet = null;
            int       tileIndex         = -1;

            foreach (TileSheet tileSheet in layer.Map.TileSheets)
            {
                int property1 = tileSheet.Properties["@FirstGid"];
                int property2 = tileSheet.Properties["@LastGid"];
                if (gid >= property1 && gid <= property2)
                {
                    selectedTileSheet = tileSheet;
                    tileIndex         = gid - property1;
                    break;
                }
            }
            if (selectedTileSheet == null)
            {
                throw new Exception(string.Format("Invalid tile gid: {0}", gid));
            }
            TiledTileSet tiledTileSet;

            if ((tiledTileSet = TiledMap.TileSets.FirstOrDefault(tileSheet => tileSheet.SheetName == selectedTileSheet.Id)) == null)
            {
                return(new StaticTile(layer, selectedTileSheet, BlendMode.Alpha, tileIndex));
            }
            TiledTile tiledTile1 = tiledTileSet.Tiles.FirstOrDefault(tiledTile =>
            {
                if (tiledTile.TileId == tileIndex)
                {
                    return(tiledTile.Animation != null);
                }
                return(false);
            });

            if (tiledTile1 == null || tiledTile1.Animation.Count <= 0)
            {
                return(new StaticTile(layer, selectedTileSheet, BlendMode.Alpha, tileIndex));
            }
            StaticTile[] array = tiledTile1.Animation.Select(frame => new StaticTile(layer, selectedTileSheet, BlendMode.Alpha, frame.TileId)).ToArray();
            return(new AnimatedTile(layer, array, tiledTile1.Animation[0].Duration));
        }
 internal void StoreLayers(Map map, TiledMap tiledMap)
 {
     foreach (Layer layer in map.Layers)
     {
         TiledLayer tiledLayer1 = new TiledLayer();
         tiledLayer1.Name   = layer.Id;
         tiledLayer1.Width  = layer.LayerWidth;
         tiledLayer1.Height = layer.LayerHeight;
         int num1 = !layer.Visible ? 1 : 0;
         tiledLayer1.Hidden = num1 != 0;
         tiledLayer1.Data   = new TiledLayerData()
         {
             EncodingType = "csv"
         };
         List <TiledProperty> tiledPropertyList = new List <TiledProperty>();
         tiledLayer1.Properties = tiledPropertyList;
         TiledLayer tiledLayer2 = tiledLayer1;
         List <int> intList     = new List <int>();
         for (int index1 = 0; index1 < layer.LayerHeight; ++index1)
         {
             for (int index2 = 0; index2 < layer.LayerWidth; ++index2)
             {
                 Tile tile = layer.Tiles[index2, index1];
                 if (tile is AnimatedTile animatedTile)
                 {
                     foreach (TiledTileSet tileSet in tiledMap.TileSets.Where(t => t.SheetName == animatedTile.TileSheet.Id))
                     {
                         TiledTile tiledTile1 = tileSet.Tiles.FirstOrDefault(tiledTile => tiledTile.TileId == tile.TileIndex);
                         if (tiledTile1 == null)
                         {
                             tileSet.Tiles.Add(new TiledTile()
                             {
                                 TileId    = tile.TileIndex,
                                 Animation = ((IEnumerable <StaticTile>)animatedTile.TileFrames).Select(frame => new TiledAnimationFrame()
                                 {
                                     TileId   = frame.TileIndex,
                                     Duration = (int)animatedTile.FrameInterval
                                 }).ToList()
                             });
                         }
                         else if (tiledTile1.Animation == null)
                         {
                             tiledTile1.Animation = ((IEnumerable <StaticTile>)animatedTile.TileFrames).Select(frame => new TiledAnimationFrame()
                             {
                                 TileId   = frame.TileIndex,
                                 Duration = (int)animatedTile.FrameInterval
                             }).ToList();
                         }
                     }
                 }
                 int num2 = 0;
                 if (tile != null)
                 {
                     int          tileIndex    = tile.TileIndex;
                     TiledTileSet tiledTileSet = tiledMap.TileSets.FirstOrDefault(tileSet => tileSet.SheetName == tile.TileSheet.Id);
                     int          num3         = tiledTileSet != null ? tiledTileSet.FirstGid : 1;
                     num2 = tileIndex + num3;
                 }
                 intList.Add(num2);
             }
         }
         tiledLayer2.Data.Data = string.Join(",", intList);
         if (layer.Description.Length > 0)
         {
             tiledLayer2.Properties.Add(new TiledProperty("@Description", layer.Description));
         }
         tiledMap.Layers.Add(tiledLayer2);
     }
 }