internal TmxLayer(XElement layer, int width, int height) { this.Name = (string)layer.Attribute("name"); this.Opacity = (double?)layer.Attribute("opacity") ?? 1.0; this.Visible = (bool?)layer.Attribute("visible") ?? true; var data = layer.Element("data"); var encoding = (string)data.Attribute("encoding"); this.Tiles = new List<TmxLayerTile>(); if (encoding == "base64") { var decodedStream = new TmxBase64Data(data); var stream = decodedStream.Data; using (var br = new BinaryReader(stream)) for (int j = 0; j < height; j++) for (int i = 0; i < width; i++) this.Tiles.Add(new TmxLayerTile(br.ReadUInt32(), i, j)); } else if (encoding == "csv") { var csvData = data.Value; int k = 0; foreach (var s in csvData.Split(',')) { var gid = uint.Parse(s.Trim()); var x = k % width; var y = k / width; this.Tiles.Add(new TmxLayerTile(gid, x, y)); k++; } } else if (encoding == null) { int k = 0; foreach (var e in data.Elements("tile")) { var gid = (uint)e.Attribute("gid"); var x = k % width; var y = k / width; this.Tiles.Add(new TmxLayerTile(gid, x, y)); k++; } } else throw new Exception("TmxLayer: Unknown encoding."); this.Properties = new PropertyDict(layer.Element("properties")); }
internal TmxImage(XElement image, string tmxDir = "") { if (image == null) return; var source = image.Attribute("source"); if (source != null) // Append directory if present this.Source = Path.Combine(tmxDir, (string)source); else { this.Format = (string)image.Attribute("format"); var data = image.Element("data"); var decodedStream = new TmxBase64Data(data); this.Data = decodedStream.Data; } this.Trans = new TmxColor(image.Attribute("trans")); this.Width = (int?)image.Attribute("width"); this.Height = (int?)image.Attribute("height"); }