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;
var xData = xLayer.Element("data");
var encoding = (string)xData.Attribute("encoding");
Tiles = new List<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 = (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(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"));
}