/// <summary> /// Sets the tile grid for this layer. /// </summary> /// <param name="tileGrid">The tile grid this Layer manages.</param> /// <param name="parentMap">The reference to the parent map of this Layer.</param> public void SetTileGrid(TileGrid tileGrid) { Tiles = new TileGrid(tileGrid.Width, tileGrid.Height); // data is left-to-right, top-to-bottom for (int x = 0; x < tileGrid.Width; x++) { for (int y = 0; y < tileGrid.Height; y++) { /* It is extremely important to clone instead of simply passing in tileGrid[x, y], as Clone() ensures the stored entity is an separate object, not a reference that dissapears when the parameter tileGrid is destroyed. */ Tiles[x, y] = tileGrid[x, y].Clone(); } } }
private void LoadLayer(XmlReader reader) { // We are now at the <layer> node. // Read the attribute(s) 'name'. // TODO: Should we make the name a #? Like the layer name is '1', and that indicates the draw order. string layerName = reader["name"]; XmlReader layerDataReader = reader.ReadSubtree(); layerDataReader.ReadToFollowing("data"); // We are now at the <data> node. #region TODO: Handle XML, Base64, GZIP, and ZLib Compression /* var encoding = layerDataReader.GetAttribute("encoding"); var compressor = reader.GetAttribute("compression"); switch (encoding) { case "base64": { int dataSize = (TileDimensions * TileDimensions * 4) + 1024; var buffer = new byte[dataSize]; reader.ReadElementContentAsBase64(buffer, 0, dataSize); Stream stream = new MemoryStream(buffer, false); if (compressor == "gzip") stream = new GZipStream(stream, CompressionMode.Decompress, false); using (stream) using (var br = new BinaryReader(stream)) { for (int i = 0; i < tileArray.Length; i++) tileArray[i] = br.ReadInt32(); } continue; }; default: throw new Exception("Your map layer, called '" + name + "' is encoded with an unrecognized compression algorithm. The accepted values are XML, Base64, and GZIP. Don't use ZLib!"); }*/ #endregion XmlReader layerDataTileReader = layerDataReader.ReadSubtree(); // We are now at the <tile> node. There are a lot of these... TileGrid layerTiles = new TileGrid(Width, Height, true); Layer layer = new Layer(); layer.Name = layerName; int lineReadIndex = 0; // the # of lines read by the parser while (layerDataTileReader.ReadToFollowing("tile")) { int tilegid = int.Parse(layerDataTileReader["gid"]); layerTiles[lineReadIndex % Width, lineReadIndex / Width].Id = tilegid; layerTiles[lineReadIndex % Width, lineReadIndex / Width].ParentLayer = layer; lineReadIndex++; } layer.Tiles = layerTiles; layer.ParentMap = (Map)this; Layers.Add(layer); }