예제 #1
0
        private void ProcessTileset(XElement xTileset, string tmxDir = "")
        {
            Name       = (string)xTileset.Attribute("name");
            TileWidth  = (int)xTileset.Attribute("tilewidth");
            TileHeight = (int)xTileset.Attribute("tileheight");
            Spacing    = (int?)xTileset.Attribute("spacing") ?? 0;
            Margin     = (int?)xTileset.Attribute("margin") ?? 0;
            Columns    = (int?)xTileset.Attribute("columns");
            TileCount  = (int?)xTileset.Attribute("tilecount");
            TileOffset = new TmxTileOffset(xTileset.Element("tileoffset"));
            Image      = new TmxImage(xTileset.Element("image"), tmxDir);

            Terrains = new TmxList <TmxTerrain>();
            var xTerrainType = xTileset.Element("terraintypes");

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

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

            Properties = new PropertyDict(xTileset.Element("properties"));
        }
예제 #2
0
        public TmxTilesetTile(XElement xTile, TmxList <TmxTerrain> Terrains,
                              string tmxDir = "")
        {
            Id = (int)xTile.Attribute("id");

            TerrainEdges = new List <TmxTerrain>(4);

            int        result;
            TmxTerrain edge;

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

            foreach (var v in strTerrain.Split(','))
            {
                var success = int.TryParse(v, out result);
                if (success)
                {
                    edge = Terrains[result];
                }
                else
                {
                    edge = null;
                }
                TerrainEdges.Add(edge);
            }

            Probability = (double?)xTile.Attribute("probability") ?? 1.0;
            Image       = new TmxImage(xTile.Element("image"), tmxDir);
            Properties  = new PropertyDict(xTile.Element("properties"));
        }
예제 #3
0
        public TmxImageLayer(XElement xImageLayer, string tmxDir = "")
        {
            Name    = (string)xImageLayer.Attribute("name");
            Width   = (int)xImageLayer.Attribute("width");
            Height  = (int)xImageLayer.Attribute("height");
            Visible = (bool?)xImageLayer.Attribute("visible") ?? true;
            Opacity = (double?)xImageLayer.Attribute("opacity") ?? 1.0;

            Image = new TmxImage(xImageLayer.Element("image"), tmxDir);

            Properties = new PropertyDict(xImageLayer.Element("properties"));
        }
예제 #4
0
        public TmxTilesetTile(XElement xTile, TmxList <TmxTerrain> Terrains,
                              string tmxDir = "")
        {
            Id = (int)xTile.Attribute("id");

            TerrainEdges = new Collection <TmxTerrain>();

            int        result;
            TmxTerrain edge;

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

            foreach (var v in strTerrain.Split(','))
            {
                var success = int.TryParse(v, out result);
                if (success)
                {
                    edge = Terrains[result];
                }
                else
                {
                    edge = null;
                }
                TerrainEdges.Add(edge);

                // TODO: Assert that TerrainEdges length is 4
            }

            Probability = (double?)xTile.Attribute("probability") ?? 1.0;
            Type        = (string)xTile.Attribute("type");
            Image       = new TmxImage(xTile.Element("image"), tmxDir);

            ObjectGroups = new TmxList <TmxObjectGroup>();
            int objectGroupIndex = 0;

            foreach (var e in xTile.Elements("objectgroup"))
            {
                ObjectGroups.Add(new TmxObjectGroup(e, objectGroupIndex++));
            }

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

            Properties = new PropertyDict(xTile.Element("properties"));
        }
예제 #5
0
        // TMX tileset element constructor
        public TmxTileset(XElement xTileset, string tmxDir = "", Func <string, Stream> readerFunc = null)
        {
            var xFirstGid = xTileset.Attribute("firstgid");
            var source    = (string)xTileset.Attribute("source");

            if (source != null)
            {
                // source is always preceded by firstgid
                FirstGid = (int)xFirstGid;

                // Everything else is in the TSX file
                // Prepend the parent TMX directory if necessary

                XDocument xDocTileset;

                if (readerFunc == null)
                {
                    source      = Path.Combine(tmxDir, source);
                    xDocTileset = ReadXml(source);
                }
                else
                {
                    using (Stream inputStream = readerFunc(source))
                    {
                        xDocTileset = XDocument.Load(inputStream);
                    }
                }

                var ts = new TmxTileset(xDocTileset, TmxDirectory);

                Name       = ts.Name;
                TileWidth  = ts.TileWidth;
                TileHeight = ts.TileHeight;
                Spacing    = ts.Spacing;
                Margin     = ts.Margin;
                Columns    = ts.Columns;
                TileCount  = ts.TileCount;
                TileOffset = ts.TileOffset;
                Image      = ts.Image;
                Terrains   = ts.Terrains;
                Tiles      = ts.Tiles;
                Properties = ts.Properties;
            }
            else
            {
                // firstgid is always in TMX, but not TSX
                if (xFirstGid != null)
                {
                    FirstGid = (int)xFirstGid;
                }

                Name       = (string)xTileset.Attribute("name");
                TileWidth  = (int)xTileset.Attribute("tilewidth");
                TileHeight = (int)xTileset.Attribute("tileheight");
                Spacing    = (int?)xTileset.Attribute("spacing") ?? 0;
                Margin     = (int?)xTileset.Attribute("margin") ?? 0;
                Columns    = (int?)xTileset.Attribute("columns");
                TileCount  = (int?)xTileset.Attribute("tilecount");
                TileOffset = new TmxTileOffset(xTileset.Element("tileoffset"));
                Image      = new TmxImage(xTileset.Element("image"), tmxDir);

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

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

                Properties = new PropertyDict(xTileset.Element("properties"));
            }
        }
예제 #6
0
        // TMX tileset element constructor
        public TmxTileset(XElement xTileset)
        {
            var xFirstGid = xTileset.Attribute("firstgid");
            var source    = (string)xTileset.Attribute("source");

            if (source != null)
            {
                // source is always preceded by firstgid
                FirstGid = (uint)xFirstGid;
                source   = @"Content\Levels\" + source;
                // Everything else is in the TSX file
                var xDocTileset = ReadXml(source);
                var ts          = new TmxTileset(xDocTileset);
                Name       = ts.Name;
                TileWidth  = ts.TileWidth;
                TileHeight = ts.TileHeight;
                Spacing    = ts.Spacing;
                Margin     = ts.Margin;
                Image      = ts.Image;
                Tiles      = ts.Tiles;
            }
            else
            {
                // firstgid is always in TMX, but not TSX
                if (xFirstGid != null)
                {
                    FirstGid = (uint)xFirstGid;
                }

                Name       = (string)xTileset.Attribute("name");
                Image      = new TmxImage(xTileset.Element("image"));
                TileWidth  = (int)xTileset.Attribute("tilewidth");
                TileHeight = (int)xTileset.Attribute("tileheight");

                var xSpacing = xTileset.Attribute("spacing");
                if (xSpacing == null)
                {
                    Spacing = 0;
                }
                else
                {
                    Spacing = (int)xSpacing;
                }

                var xMargin = xTileset.Attribute("margin");
                if (xMargin == null)
                {
                    Margin = 0;
                }
                else
                {
                    Margin = (int)xMargin;
                }

                Tiles = new Dictionary <int, PropertyDict>();
                foreach (var xml_tile in xTileset.Elements("tile"))
                {
                    var id    = (int)xml_tile.Attribute("id");
                    var xProp = xml_tile.Element("properties");
                    Tiles.Add(id, new PropertyDict(xProp));
                }
            }
        }
예제 #7
0
        // TMX tileset element constructor
        public TmxTileset(XElement xTileset, string tmxDir = "")
        {
            var xFirstGid = xTileset.Attribute("firstgid");
            var source    = (string)xTileset.Attribute("source");

            if (source != null)
            {
                // Prepend the parent TMX directory if necessary
                source = Path.Combine(tmxDir, source);

                // source is always preceded by firstgid
                FirstGid = (int)xFirstGid;

                // Everything else is in the TSX file
                var xDocTileset = ReadXml(source);
                var ts          = new TmxTileset(xDocTileset, TmxDirectory);

                Name       = ts.Name;
                TileWidth  = ts.TileWidth;
                TileHeight = ts.TileHeight;
                Spacing    = ts.Spacing;
                Margin     = ts.Margin;
                TileOffset = ts.TileOffset;
                Image      = ts.Image;
                Terrains   = ts.Terrains;
                Tiles      = ts.Tiles;
                Properties = ts.Properties;
            }
            else
            {
                // firstgid is always in TMX, but not TSX
                if (xFirstGid != null)
                {
                    FirstGid = (int)xFirstGid;
                }

                Name       = (string)xTileset.Attribute("name");
                TileWidth  = (int)xTileset.Attribute("tilewidth");
                TileHeight = (int)xTileset.Attribute("tileheight");
                Spacing    = (int?)xTileset.Attribute("spacing") ?? 0;
                Margin     = (int?)xTileset.Attribute("margin") ?? 0;

                TileOffset = new TmxTileOffset(xTileset.Element("tileoffset"));
                Image      = new TmxImage(xTileset.Element("image"), tmxDir);

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

                Tiles = new List <TmxTilesetTile>();
                foreach (var xTile in xTileset.Elements("tile"))
                {
                    var tile = new TmxTilesetTile(xTile, Terrains, tmxDir);
                    Tiles.Add(tile);
                }

                Properties = new PropertyDict(xTileset.Element("properties"));
            }
        }