示例#1
0
        public string saveFileRooms   = "rooms.xml"; //the rooms file name
        #endregion

        /// <summary>
        /// Updates the SaveData with the latest information
        /// </summary>
        private void GenerateSaveData()
        {
            saveHeroData = new SaveHero() //prime SaveHero for saving the Hero
            {
                theHero = Game1.cHero,
            };

            saveRoomData = new SaveRoom() //prime SaveRoom
            {
                currentRoomNumber = Game1.cArea.currentRoom,
                roomTextureNames  = Game1.cArea.roomTex,
                theRooms          = Game1.cArea.Rooms,
            };
        }
示例#2
0
        /// <summary>
        /// Loads the game from the save file.
        /// </summary>
        public void LoadGame()
        {
            try
            {
                TextReader XMLReader = new StreamReader(saveFileHero);
                try
                {
                    XmlSerializer deserializer = new XmlSerializer(typeof(SaveHero));
                    saveHeroData = (SaveHero)deserializer.Deserialize(XMLReader);

                    //get the variables reassociated with the runtime data
                    Game1.cHero = saveHeroData.theHero;
                    Game1.cHero.LoadContent(Game1.cHero.heroAssetName);
                }
                catch
                {
                    Game1.cGameUI.Draw("Could not load your hero file, using default values.");
                }
                XMLReader.Close();
            }
            catch
            {
                if (Game1.currentState != Menu.GameState.ResetStats)
                {
                    Game1.cGameUI.Draw("Could not find your hero save file.");
                }
            }

            try
            {
                TextReader XMLReader = new StreamReader(saveFileRooms);
                try
                {
                    XmlSerializer deserializer = new XmlSerializer(typeof(SaveRoom));
                    saveRoomData = (SaveRoom)deserializer.Deserialize(XMLReader);

                    //get the variables reassociated with the runtime data
                    Game1.cArea.currentRoom =
                        (int)MathHelper.Clamp(saveRoomData.currentRoomNumber, 0, Game1.cArea.Rooms.Count - 1);

                    try { Game1.cArea.roomTex = saveRoomData.roomTextureNames; }
                    catch { Game1.cGameUI.Draw("You have an invalid string in loaded room texture names."); }

                    try { Game1.cArea.Rooms = saveRoomData.theRooms; }
                    catch { Game1.cGameUI.Draw("You have an invalid room in your loaded room list."); }
                    Game1.cArea.LoadContent();
                }
                catch
                {
                    if (Game1.currentState != Menu.GameState.ResetStats)
                    {
                        Game1.cGameUI.Draw("Could not load your saved rooms, using default rooms.");
                    }
                }
                XMLReader.Close();
            }
            catch
            {
                if (Game1.currentState != Menu.GameState.ResetStats)
                {
                    Game1.cGameUI.Draw("Could not find your rooms save file.");
                    throw new FileNotFoundException("Could not find your rooms save file.");
                }
            }
        }