Esempio n. 1
0
        public void FromXml(string mapFilename)
        {
            XmlReaderSettings xmlSettings = new XmlReaderSettings();

            XmlReader r = XmlReader.Create(mapFilename);

            // read until we find the start of the world description
            while (r.Read())
            {
                // look for the start of the map description
                if (r.NodeType == XmlNodeType.Element)
                {
                    if (r.Name == "WorldMap")
                    {
                        break;
                    }
                }
            }

            // parse attributes
            for (int i = 0; i < r.AttributeCount; i++)
            {
                r.MoveToAttribute(i);

                // set the field in this object based on the element we just read
                switch (r.Name)
                {
                case "WorldName":
                    worldName = r.Value;
                    break;

                case "MinTileX":
                    minTile.x = int.Parse(r.Value);
                    break;

                case "MinTileZ":
                    minTile.z = int.Parse(r.Value);
                    break;

                case "MaxTileX":
                    maxTile.x = int.Parse(r.Value);
                    break;

                case "MaxTileZ":
                    maxTile.z = int.Parse(r.Value);
                    break;

                case "MinHeight":
                    minHeight = float.Parse(r.Value);
                    break;

                case "MaxHeight":
                    maxHeight = float.Parse(r.Value);
                    break;
                }
            }

            r.MoveToElement(); //Moves the reader back to the element node.

            // now parse the sub-elements
            while (r.Read())
            {
                // look for the start of an element
                if (r.NodeType == XmlNodeType.Element)
                {
                    // parse that element
                    // save the name of the element
                    string elementName = r.Name;
                    switch (elementName)
                    {
                    case "Zone":
                        MapZone zone = new MapZone(this, r);
                        zones.Add(zone.Name, zone);
                        break;

                    case "Layer":
                        MapLayer layer = MapLayer.MapLayerFactory(this, r);
                        layers.Add(layer.LayerName, layer);
                        layerAndWorldOWP.Add(layer);
                        break;

                    case "Property":
                        properties.ParseProperty(r);
                        break;
                    }
                }
                else if (r.NodeType == XmlNodeType.EndElement)
                {
                    break;
                }
            }
        }