public static TmxLayer LoadTmxLayer(this TmxLayer layer, TmxMap map, XElement xLayer, int width, int height) { layer.Map = map; layer.Name = (string)xLayer.Attribute("name"); layer.Opacity = (float?)xLayer.Attribute("opacity") ?? 1.0f; layer.Visible = (bool?)xLayer.Attribute("visible") ?? true; layer.OffsetX = (float?)xLayer.Attribute("offsetx") ?? 0.0f; layer.OffsetY = (float?)xLayer.Attribute("offsety") ?? 0.0f; layer.ParallaxFactorX = (float?)xLayer.Attribute("parallaxx") ?? 1.0f; layer.ParallaxFactorY = (float?)xLayer.Attribute("parallaxy") ?? 1.0f; // TODO: does the width/height passed in ever differ from the TMX layer XML? layer.Width = (int)xLayer.Attribute("width"); layer.Height = (int)xLayer.Attribute("height"); var xData = xLayer.Element("data"); var encoding = (string)xData.Attribute("encoding"); layer.Tiles = new TmxLayerTile[width * height]; //xData should just be a string, if not, then its chunked if (xData.HasElements) { foreach (var chunk in xData.Elements("chunk")) { var chunkWidth = (int)chunk.Attribute("width"); var chunkHeight = (int)chunk.Attribute("height"); var chunkX = (int)chunk.Attribute("x"); var chunkY = (int)chunk.Attribute("y"); LoadLayerData(layer, map, chunkWidth, chunkHeight, chunkX, chunkY, chunk, encoding); } } else { LoadLayerData(layer, map, width, height, 0, 0, xData, encoding); } layer.Properties = TiledMapLoader.ParsePropertyDict(xLayer.Element("properties")); return(layer);
public static TmxLayer LoadTmxLayer(this TmxLayer layer, TmxMap map, XElement xLayer, int width, int height) { layer.Map = map; layer.Name = (string)xLayer.Attribute("name"); layer.Opacity = (float?)xLayer.Attribute("opacity") ?? 1.0f; layer.Visible = (bool?)xLayer.Attribute("visible") ?? true; layer.OffsetX = (float?)xLayer.Attribute("offsetx") ?? 0.0f; layer.OffsetY = (float?)xLayer.Attribute("offsety") ?? 0.0f; layer.ParallaxFactorX = (float?)xLayer.Attribute("parallaxx") ?? 1.0f; layer.ParallaxFactorY = (float?)xLayer.Attribute("parallaxy") ?? 1.0f; // TODO: does the width/height passed in ever differ from the TMX layer XML? layer.Width = (int)xLayer.Attribute("width"); layer.Height = (int)xLayer.Attribute("height"); var xData = xLayer.Element("data"); var encoding = (string)xData.Attribute("encoding"); layer.Tiles = new TmxLayerTile[width * height]; if (encoding == "base64") { var decodedStream = new TmxBase64Data(xData); var stream = decodedStream.Data; var index = 0; using (var br = new BinaryReader(stream)) { for (var j = 0; j < height; j++) { for (var i = 0; i < width; i++) { var gid = br.ReadUInt32(); layer.Tiles[index++] = gid != 0 ? new TmxLayerTile(map, gid, i, j) : null; } } } } 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; layer.Tiles[k++] = gid != 0 ? new TmxLayerTile(map, gid, x, y) : null; } } 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; layer.Tiles[k++] = gid != 0 ? new TmxLayerTile(map, gid, x, y) : null; } } else { throw new Exception("TmxLayer: Unknown encoding."); } layer.Properties = TiledMapLoader.ParsePropertyDict(xLayer.Element("properties")); return(layer); }