コード例 #1
0
        /// <summary>
        /// This method reads the terrain from xml and forms the terrain using the methods in location class
        /// </summary>
        /// <param name="fileName"></param>
        public override void Construct(Terrain terrain, string fileName)
        {
            //empty the terrain
            ClearTerrain(terrain);

            using (XmlReader reader = XmlReader.Create(fileName))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        switch (reader.Name)
                        {
                            case "Location":
                                ILocation iLoc;
                                var name = reader["Name"];
                                var loc = new Location(name);
                                var markedLoc = new MarkedLocation(loc);
                                if (reader["ExitLocation"] == "true")
                                {
                                    iLoc = new ExitLocation(markedLoc);
                                }
                                else
                                {
                                    iLoc = markedLoc;
                                }
                                terrain.AddLocation(iLoc);
                                break;
                        }
                    }
                }
            }
            using (XmlReader reader = XmlReader.Create(fileName))
            {
                ILocation iLoc = null;
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        switch (reader.Name)
                        {
                            case "Location":
                                var name = reader["Name"];
                                iLoc = terrain.GetLocation(name);
                                break;
                            case "Neigbor":
                                var directionStr = reader["Direction"];
                                if (reader.Read())
                                {
                                    var neigborLocName = reader.Value.Trim();
                                    Directions direction;
                                    Enum.TryParse(directionStr, out direction);
                                    var neigborLocation = terrain.GetLocation(neigborLocName);
                                    if (neigborLocation == null)
                                        throw new Exception("There must be a location already defined by the name {0}" + neigborLocation);
                                    iLoc.AddNeighbor(terrain.GetLocation(neigborLocName), direction);
                                }
                                break;
                        }
                    }
                }
            }
        }
コード例 #2
0
 public void Setup()
 {
     location = new ExitLocation(new Location("exitlocation"));
 }