Beispiel #1
0
        public void SerializeTo(StreamWriter stream)
        {
            TileSet  tileSet = this.tileSet;
            MapState state   = this.state;

            Dictionary <TerrainKind, string> terrainIds = new Dictionary <TerrainKind, string>();

            for (int i = 0; i < tileSet.TerrainByIndex.Length; i++)
            {
                terrainIds[tileSet.TerrainByIndex[i]] = i.ToString();
            }

            Dictionary <Tile, string> tileIds = new Dictionary <Tile, string>();

            for (int i = 0; i < tileSet.TileList.Count; i++)
            {
                tileIds[tileSet.TileList[i]] = i.ToString();
            }

            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = true;
            XmlWriter xml = XmlWriter.Create(stream, settings);

            xml.WriteStartElement("Map");

            xml.WriteElementString("Tileset", "Linedraw");
            xml.WriteElementString("Width", Width.ToString());
            xml.WriteElementString("Height", Height.ToString());

            xml.WriteStartElement("Terrain");
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    xml.WriteElementString("Square", terrainIds[this.state.GetSquareKind(x, y)]);
                }
            }
            xml.WriteEndElement(); /* Terrain */

            xml.WriteStartElement("Tiles");
            for (int layer = 0; layer < 4; layer++)
            {
                for (int y = 0; y < Height + 1; y++)
                {
                    for (int x = 0; x < Width + 1; x++)
                    {
                        Tile tile = this.state.GetTile(layer, x, y);
                        if (tile.IsLarge && (tile.XPosition > 0 || tile.YPosition > 0))
                        {
                            continue;
                        }
                        if (tile.IsLarge)
                        {
                            tile = tile.SuperTile;
                        }

                        xml.WriteStartElement("Tile");
                        xml.WriteElementString("Layer", layer.ToString());
                        xml.WriteElementString("X", x.ToString());
                        xml.WriteElementString("Y", y.ToString());
                        xml.WriteElementString("Value", tileIds[tile]);
                        xml.WriteEndElement(); /* Tile */
                    }
                }
            }
            xml.WriteEndElement(); /* Tiles */

            xml.WriteEndElement(); /* Map */

            xml.Flush();
        }
Beispiel #2
0
        public static Map DeserializeFrom(StreamReader stream)
        {
            XmlReader xml = XmlReader.Create(stream);

            xml.ReadStartElement("Map");

            String tilesetName = xml.ReadElementString("Tileset");

            if (tilesetName != "Linedraw")
            {
                throw new NotImplementedException();
            }

            TileSet tileSet = TileSet.Load(@"Data\Simple.mdts");

            int width  = int.Parse(xml.ReadElementString("Width"));
            int height = int.Parse(xml.ReadElementString("Height"));

            Map      map   = new Map(tileSet, width, height);
            MapState state = map.state;

            xml.ReadStartElement("Terrain");
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int tileId = int.Parse(xml.ReadElementString("Square"));
                    state.SetSquareKind(x, y, tileSet.TerrainByIndex[tileId]);
                }
            }
            xml.ReadEndElement(); /* Terrain */

            xml.ReadStartElement("Tiles");
            while (xml.IsStartElement("Tile"))
            {
                xml.ReadStartElement("Tile");

                int layer = int.Parse(xml.ReadElementString("Layer"));
                int x     = int.Parse(xml.ReadElementString("X"));
                int y     = int.Parse(xml.ReadElementString("Y"));
                int value = int.Parse(xml.ReadElementString("Value"));

                Tile tile = tileSet.TileList[value];
                if (tile.IsLarge)
                {
                    for (int subX = 0; subX < tile.Width; subX++)
                    {
                        for (int subY = 0; subY < tile.Height; subY++)
                        {
                            state.SetTile(layer, x + subX, y + subY, tile.GetSubtile(subX, subY));
                        }
                    }
                }
                else
                {
                    state.SetTile(layer, x, y, tile);
                }

                xml.ReadEndElement(); /* Tile */
            }

            xml.ReadEndElement(); /* Tiles */

            xml.ReadEndElement(); /* Map */

            // TODO: Verify that map is correct
            map.ClearHoverTerrain();
            return(map);
        }