static void fillGrid(WorldDescriptor world, TileDescriptor tile) { for (int x = 0; x < world.Width; x++) { for (int y = 0; y < world.Height; y++) { world.Tiles[x, y] = tile; } } }
public static WorldDescriptor Load(string text) { WorldDescriptor world = new WorldDescriptor(); StringReader str = new StringReader(text); using (XmlReader xml = XmlReader.Create(str)) { while (xml.Read()) { switch (xml.NodeType) { case XmlNodeType.Element: { if (xml.Name == "BaseTile") { string tileName = xml.GetAttribute("value"); TileDescriptor tile = TileDescriptor.Create(tileName); fillGrid(world, tile); } else if (xml.Name == "World") { string sw = xml.GetAttribute("w"); string sh = xml.GetAttribute("h"); int w, h; int.TryParse(sw, out w); int.TryParse(sh, out h); world.Width = w; world.Height = h; world.Tiles = new TileDescriptor[w, h]; } else if (xml.Name == "Tile") { string sx = xml.GetAttribute("x"); string sy = xml.GetAttribute("y"); string type = xml.GetAttribute("type"); int x, y; int.TryParse(sx, out x); int.TryParse(sy, out y); TileDescriptor tile = TileDescriptor.Create(type); world.Tiles[x, y] = tile; } } break; } } } return(world); }
public static string SaveToString(WorldDescriptor world) { StringBuilder sb = new StringBuilder(); XmlWriterSettings setting = new XmlWriterSettings() { Indent = true, IndentChars = " ", NewLineChars = "\n" }; using (XmlWriter xml = XmlWriter.Create(sb, setting)) { xml.WriteStartElement("World"); xml.WriteAttributeString("w", "" + world.Width); xml.WriteAttributeString("h", "" + world.Height); string baseTile = "FlatTile"; xml.WriteStartElement("BaseTile"); xml.WriteAttributeString("value", baseTile); xml.WriteEndElement(); for (int y = 0; y < world.Height; y++) { for (int x = 0; x < world.Width; x++) { string tile = world.Tiles[x, y].TypeName; if (tile != baseTile) { xml.WriteStartElement("Tile"); xml.WriteAttributeString("x", "" + x); xml.WriteAttributeString("y", "" + y); xml.WriteAttributeString("type", tile); xml.WriteEndElement(); } } } xml.WriteEndElement(); } return(sb.ToString()); }