Exemple #1
0
        public static TiledMap CreateMap(string tmxFile)
        {
            if (_tiledMaps.ContainsKey(tmxFile))
            {
                return(_tiledMaps[tmxFile]);
            }

            TiledMap      map    = null;
            XmlSerializer reader = new XmlSerializer(typeof(TiledMap));

            using (FileStream stream = new FileStream(string.Format("Content/{0}.tmx", tmxFile), FileMode.Open))
            {
                map = (TiledMap)reader.Deserialize(stream);
            }
            _tiledMaps.Add(tmxFile, map);
            return(map);
        }
Exemple #2
0
 public static void GenerateCollision(TiledMap map, string objectLayerName)
 {
     foreach (TiledObjectGroup group in map.ObjectGroups)
     {
         if (group.Name == objectLayerName)
         {
             foreach (TiledObject obj in group.Objects)
             {
                 BaseObject collisionObject = new BaseObject();
                 collisionObject.Position = new Vector2(obj.X, obj.Y) + new Vector2(obj.Width / 2, obj.Height / 2);
                 collisionObject.EnablePhysicsRectangle(
                     FarseerPhysics.Dynamics.BodyType.Static,
                     new Rectangle(0, 0, (int)obj.Width, (int)obj.Height));
                 collisionObject.Parent = map;
             }
         }
     }
 }
Exemple #3
0
        public static void GenerateTileLayers(TiledMap map)
        {
            for (int i = map.Layers.Count - 1; i >= 0; i--)
            {
                TiledTileLayer layer = map.Layers[i];
                if (layer.Data == null)
                {
                    continue;
                }

                long[,] tiles = GetTileDataFromBase64Zlib(layer.Data, map);
                for (int y = 0; y < tiles.GetLength(1); y++)
                {
                    for (int x = 0; x < tiles.GetLength(0); x++)
                    {
                        long tile = tiles[x, y];
                        if (tile == 0)
                        {
                            continue;
                        }

                        TiledTileSet set       = GetTileSet(tile, map);
                        int          realIndex = (int)(tile - set.FirstGID);
                        int          srcY      = (realIndex / set.Columns);
                        int          srcX      = (realIndex - (srcY * set.Columns));

                        string source = Path.ChangeExtension(set.Image.Source, null).Replace("../", "");
                        Sprite sprite = new Sprite(source);
                        sprite.SourceRect = new Rectangle(srcX * set.TileWidth, srcY * set.TileHeight, set.TileWidth, set.TileHeight);
                        sprite.Position   = new Vector2(x * set.TileWidth, y * set.TileHeight) + sprite.Origin;
                        sprite.Visible    = layer.Visible;
                        sprite.Opacity    = layer.Opacity;
                        sprite.Parent     = map;
                    }
                }
            }
        }
Exemple #4
0
        private static long[,] GetTileDataFromBase64Zlib(TiledLayerData data, TiledMap map)
        {
            if (data.Encoding == "base64" && data.Compression == "zlib")
            {
                byte[]       zlibBuffer = Convert.FromBase64String(data.Data);
                MemoryStream comp       = new MemoryStream(zlibBuffer);
                Stream       zlibStream = new ZlibStream(comp, CompressionMode.Decompress);

                long[,] tiles = new long[map.Width, map.Height];

                using (var bn = new BinaryReader(zlibStream))
                {
                    for (int j = 0; j < map.Height; j++)
                    {
                        for (int i = 0; i < map.Width; i++)
                        {
                            tiles[i, j] = bn.ReadInt32();
                        }
                    }
                }
                return(tiles);
            }
            return(null);
        }