Exemple #1
0
        public TmxImage(XElement xImage, string tmxDir = "")
        {
            if (xImage == null)
            {
                return;
            }

            var xSource = xImage.Attribute("source");

            if (xSource != null)
            {
                // Append directory if present
                Source = Path.Combine(tmxDir, (string)xSource);
            }
            else
            {
                Format = (string)xImage.Attribute("format");
                var xData         = xImage.Element("data");
                var decodedStream = new TmxBase64Data(xData);
                Data = decodedStream.Data;
            }

            Trans  = new TmxColor(xImage.Attribute("trans"));
            Width  = (int?)xImage.Attribute("width");
            Height = (int?)xImage.Attribute("height");
        }
Exemple #2
0
        public TmxLayer(XElement xLayer, int width, int height)
        {
            Name    = (string)xLayer.Attribute("name");
            Opacity = (double?)xLayer.Attribute("opacity") ?? 1.0;
            Visible = (bool?)xLayer.Attribute("visible") ?? true;
            OffsetX = (double?)xLayer.Attribute("offsetx") ?? 0.0;
            OffsetY = (double?)xLayer.Attribute("offsety") ?? 0.0;

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

            Tiles = new Collection <TmxLayerTile>();
            if (encoding == "base64")
            {
                var decodedStream = new TmxBase64Data(xData);
                var stream        = decodedStream.Data;

                using (var br = new BinaryReader(stream))
                {
                    for (int j = 0; j < height; j++)
                    {
                        for (int i = 0; i < width; i++)
                        {
                            Tiles.Add(new TmxLayerTile(br.ReadUInt32(), i, j));
                        }
                    }
                }
            }
            else if (encoding == "csv")
            {
                var csvData = 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(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(gid, x, y));
                    k++;
                }
            }
            else
            {
                throw new Exception("TmxLayer: Unknown encoding.");
            }

            Properties = new PropertyDict(xLayer.Element("properties"));
        }
        private void ReadChunk(int width, int height, int startX, int startY, string encoding, XElement xData)
        {
            if (encoding == "base64")
            {
                var decodedStream = new TmxBase64Data(xData);
                var stream        = decodedStream.Data;

                using (var br = new BinaryReader(stream))
                    for (int j = 0; j < height; j++)
                    {
                        for (int i = 0; i < width; i++)
                        {
                            Tiles.Add(new TmxLayerTile(br.ReadUInt32(), i + startX, j + startY));
                        }
                    }
            }
            else if (encoding == "csv")
            {
                var csvData = (string)xData.Value;
                int k       = 0;
                foreach (var s in csvData.Split(new[] { ',', '\n' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var gid = uint.Parse(s.Trim());
                    var x   = k % width;
                    var y   = k / width;
                    Tiles.Add(new TmxLayerTile(gid, x + startX, y + startY));
                    k++;
                }
            }
            else if (encoding == null)
            {
                int k = 0;
                foreach (var e in xData.Elements("tile"))
                {
                    var gid = (uint?)e.Attribute("gid") ?? 0;

                    var x = k % width;
                    var y = k / width;

                    Tiles.Add(new TmxLayerTile(gid, x + startX, y + startY));
                    k++;
                }
            }
            else
            {
                throw new Exception("TmxLayer: Unknown encoding.");
            }
        }