示例#1
0
        public TmxObjectGroup(XElement xObjectGroup)
        {
            Name = (string)xObjectGroup.Attribute("name");

            var xColor = (string)xObjectGroup.Attribute("color");

            if (xColor != null)
            {
                xColor = xColor.TrimStart("#".ToCharArray());
                Color  = UInt32.Parse(xColor, NumberStyles.HexNumber);
            }

            var xOpacity = xObjectGroup.Attribute("opacity");

            Opacity = xOpacity == null ? 1.0 : (double)xOpacity;

            var xVisible = xObjectGroup.Attribute("visible");

            Visible = xVisible == null || (bool)xVisible;

            Objects = new TmxList <TmxObject>();
            foreach (var e in xObjectGroup.Elements("object"))
            {
                Objects.Add(new TmxObject(e));
            }

            Properties = new PropertyDict(xObjectGroup.Element("properties"));
        }
示例#2
0
 public TmxMap()
 {
     Tilesets     = new TmxList <TmxTileset>();
     Layers       = new TmxList <TmxLayer>();
     ObjectGroups = new TmxList <TmxObjectGroup>();
     ImageLayers  = new TmxList <TmxImageLayer>();
     Properties   = new PropertyDict();
 }
示例#3
0
        public TmxObject(XElement xObject)
        {
            var xName = xObject.Attribute("name");

            Name = xName == null ? "" : (string)xName;

            Type = (string)xObject.Attribute("type");
            X    = (int)xObject.Attribute("x");
            Y    = (int)xObject.Attribute("y");

            var xVisible = xObject.Attribute("visible");

            Visible = xVisible == null || (bool)xVisible;

            Width  = (int?)xObject.Attribute("width");
            Height = (int?)xObject.Attribute("height");

            // Assess object type and assign appropriate content
            var xGid      = xObject.Attribute("gid");
            var xEllipse  = xObject.Element("ellipse");
            var xPolygon  = xObject.Element("polygon");
            var xPolyline = xObject.Element("polyline");

            if (xGid != null)
            {
                Gid        = (int?)xGid;
                ObjectType = ETmxObjectType.Tile;
            }
            else if (xEllipse != null)
            {
                ObjectType = ETmxObjectType.Ellipse;
            }
            else if (xPolygon != null)
            {
                Points     = ParsePoints(xPolygon);
                ObjectType = ETmxObjectType.Polygon;
            }
            else if (xPolyline != null)
            {
                Points     = ParsePoints(xPolyline);
                ObjectType = ETmxObjectType.Polyline;
            }
            else
            {
                ObjectType = ETmxObjectType.Basic;
            }

            Properties = new PropertyDict(xObject.Element("properties"));
        }
示例#4
0
        public TmxImageLayer(TmxMap map, XElement xImageLayer, string tmxDir = "")
            : base(map)
        {
            Name   = (string)xImageLayer.Attribute("name");
            Width  = (int)xImageLayer.Attribute("width");
            Height = (int)xImageLayer.Attribute("height");

            var xVisible = xImageLayer.Attribute("visible");

            Visible = xVisible == null || (bool)xVisible;

            var xOpacity = xImageLayer.Attribute("opacity");

            Opacity = xOpacity == null ? 1.0 : (double)xOpacity;

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

            Properties = new PropertyDict(xImageLayer.Element("properties"));
        }
示例#5
0
        public TmxLayer(TmxMap map, XElement xLayer, int width, int height)
            : this(map) {
            Name   = (string)xLayer.Attribute("name");
            Width  = (int)xLayer.Attribute("width");
            Height = (int)xLayer.Attribute("height");

            var xOpacity = xLayer.Attribute("opacity");
            Opacity = xOpacity == null ? 1.0 : (double)xOpacity;

            var xVisible = xLayer.Attribute("visible");
            Visible = xVisible == null || (bool)xVisible;

            var xData    = xLayer.Element("data");
            var encoding = (string)xData.Attribute("encoding");

            Tiles = new List <TmxLayerTile>();
            if (encoding == "base64")
            {
                var    base64data = Convert.FromBase64String((string)xData.Value);
                Stream stream     = new MemoryStream(base64data, false);

                var compression = (string)xData.Attribute("compression");

                /*
                 * if (compression == "gzip")
                 *      stream = new GZipStream(stream, CompressionMode.Decompress,
                 *                                                      false);
                 * else if (compression == "zlib")
                 *      stream = new Ionic.Zlib.ZlibStream(stream,
                 *                              Ionic.Zlib.CompressionMode.Decompress, false);
                 * else
                 */
                if (compression != null)
                {
                    throw new Exception("Tiled: Unknown compression.");
                }

                using (stream)
                    using (var br = new BinaryReader(stream))
                        for (int j = 0; j < height; j++)
                        {
                            for (int i = 0; i < width; i++)
                            {
                                Tiles.Add(new TmxLayerTile(this, br.ReadUInt32(), i, j));
                            }
                        }
            }
            else if (encoding == "csv")
            {
                var csvData = (string)xData.Value;
                int k       = 0;
                foreach (var s in csvData.Split(','))
                {
                    var gid = uint.Parse(s.Trim());
                    var x   = k % width;
                    var y   = k / width;
                    Tiles.Add(new TmxLayerTile(this, gid, x, y));
                    k++;
                }
            }
            else if (encoding == null)
            {
                int k = 0;
                foreach (var e in xData.Elements("tile"))
                {
                    var gid = (uint)e.Attribute("gid");
                    var x   = k % width;
                    var y   = k / width;
                    Tiles.Add(new TmxLayerTile(this, gid, x, y));
                    k++;
                }
            }
            else
            {
                throw new Exception("Tiled: Unknown encoding.");
            }

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