Exemplo n.º 1
0
        public static TmxTilesetTile LoadTmxTilesetTile(this TmxTilesetTile tile, TmxTileset tileset, XElement xTile, TmxList <TmxTerrain> Terrains, string tmxDir = "")
        {
            tile.Tileset = tileset;
            tile.Id      = (int)xTile.Attribute("id");

            var strTerrain = (string)xTile.Attribute("terrain");

            if (strTerrain != null)
            {
                tile.TerrainEdges = new TmxTerrain[4];
                var index = 0;
                foreach (var v in strTerrain.Split(','))
                {
                    var success = int.TryParse(v, out int result);

                    TmxTerrain edge;
                    if (success)
                    {
                        edge = Terrains[result];
                    }
                    else
                    {
                        edge = null;
                    }
                    tile.TerrainEdges[index++] = edge;
                }
            }

            tile.Probability = (double?)xTile.Attribute("probability") ?? 1.0;
            tile.Type        = (string)xTile.Attribute("type");
            var xImage = xTile.Element("image");

            if (xImage != null)
            {
                tile.Image = new TmxImage().LoadTmxImage(xImage, tmxDir);
            }

            tile.ObjectGroups = new TmxList <TmxObjectGroup>();
            foreach (var e in xTile.Elements("objectgroup"))
            {
                tile.ObjectGroups.Add(new TmxObjectGroup().LoadTmxObjectGroup(tileset.Map, e));
            }

            tile.AnimationFrames = new List <TmxAnimationFrame>();
            if (xTile.Element("animation") != null)
            {
                foreach (var e in xTile.Element("animation").Elements("frame"))
                {
                    tile.AnimationFrames.Add(new TmxAnimationFrame().LoadTmxAnimationFrame(e));
                }
            }

            tile.Properties = ParsePropertyDict(xTile.Element("properties"));

            if (tile.Properties != null)
            {
                tile.ProcessProperties();
            }

            return(tile);
        }
Exemplo n.º 2
0
        public static TmxTileset LoadTmxTileset(this TmxTileset tileset, TmxMap map, XElement xTileset, int firstGid, string tsxDir)
        {
            tileset.Map      = map;
            tileset.FirstGid = firstGid;

            tileset.Name       = (string)xTileset.Attribute("name");
            tileset.TileWidth  = (int)xTileset.Attribute("tilewidth");
            tileset.TileHeight = (int)xTileset.Attribute("tileheight");
            tileset.Spacing    = (int?)xTileset.Attribute("spacing") ?? 0;
            tileset.Margin     = (int?)xTileset.Attribute("margin") ?? 0;
            tileset.Columns    = (int?)xTileset.Attribute("columns");
            tileset.TileCount  = (int?)xTileset.Attribute("tilecount");
            tileset.TileOffset = ParseTmxTileOffset(xTileset.Element("tileoffset"));

            var xImage = xTileset.Element("image");

            if (xImage != null)
            {
                tileset.Image = new TmxImage().LoadTmxImage(xImage, tsxDir);
            }

            var xTerrainType = xTileset.Element("terraintypes");

            if (xTerrainType != null)
            {
                tileset.Terrains = new TmxList <TmxTerrain>();
                foreach (var e in xTerrainType.Elements("terrain"))
                {
                    tileset.Terrains.Add(ParseTmxTerrain(e));
                }
            }

            tileset.Tiles = new Dictionary <int, TmxTilesetTile>();
            foreach (var xTile in xTileset.Elements("tile"))
            {
                var tile = new TmxTilesetTile().LoadTmxTilesetTile(tileset, xTile, tileset.Terrains, tsxDir);
                tileset.Tiles[tile.Id] = tile;
            }

            tileset.Properties = ParsePropertyDict(xTileset.Element("properties"));

            // cache our source rects for each tile so we dont have to calculate them every time we render. If we have
            // an image this is a normal tileset, else its an image tileset
            tileset.TileRegions = new Dictionary <int, RectangleF>();
            if (tileset.Image != null)
            {
                var id = firstGid;
                for (var y = tileset.Margin; y < tileset.Image.Height - tileset.Margin; y += tileset.TileHeight + tileset.Spacing)
                {
                    var column = 0;
                    for (var x = tileset.Margin; x < tileset.Image.Width - tileset.Margin; x += tileset.TileWidth + tileset.Spacing)
                    {
                        tileset.TileRegions.Add(id++, new RectangleF(x, y, tileset.TileWidth, tileset.TileHeight));

                        if (++column >= tileset.Columns)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                foreach (var tile in tileset.Tiles.Values)
                {
                    tileset.TileRegions.Add(firstGid + tile.Id, new RectangleF(0, 0, tile.Image.Width, tile.Image.Height));
                }
            }

            return(tileset);
        }