Example #1
0
        /// <summary>
        /// loads a save file
        /// </summary>
        private void LoadSave()
        {
            Console.WriteLine(saveLoc);
            Stream       inStream = null;
            BinaryReader input    = null;

            try
            {
                //open the file for reading
                inStream = File.OpenRead(saveLoc);
                input    = new BinaryReader(inStream);

                //red the file
                string world = input.ReadString();
                Console.WriteLine("World: " + world);
                int pX = input.ReadInt32();
                int pY = input.ReadInt32();
                Console.WriteLine("X, Y: " + pX + ", " + pY);
                int pHealth = input.ReadInt32();
                Console.WriteLine("Health: " + pHealth);
                int pCash = input.ReadInt32();
                Console.WriteLine("Cash: " + pCash);
                int pQuestPoints = input.ReadInt32();
                Console.WriteLine("QuestPoints: " + pQuestPoints);

                //read the quests
                int numQuests = input.ReadInt32();
                Console.WriteLine("Num quests: " + numQuests);
                object[,] quests = new object[numQuests, 2];
                for (int i = 0; i < numQuests; i++)
                {
                    quests[i, 0] = input.ReadString();
                    Console.WriteLine("\tName: " + quests[i, 0]);
                    quests[i, 1] = input.ReadInt32();
                    Console.WriteLine("\tStatus: " + quests[i, 1]);
                }

                //read the inventory
                int numItems = input.ReadInt32();
                Console.WriteLine("Num Items: " + numItems);
                string[] items = new string[numItems];
                for (int i = 0; i < numItems; i++)
                {
                    items[i] = input.ReadString();
                    Console.WriteLine("\t" + items[i]);
                }

                int activeItem = input.ReadInt32();
                Console.WriteLine("Active Weapon: " + activeItem);

                //make the world
                if (MainGame.worldManager.worlds.ContainsKey(world))
                {
                    MainGame.worldManager.worlds[world] = LoadWorld(world);
                }
                else
                {
                    MainGame.worldManager.worlds.Add(world, LoadWorld(world));
                }

                //set the player in the world
                Player player = new Player(
                    new FloatRectangle(pX, pY, 32, 32),
                    Sprites.spritesDictionary["player"]
                    );
                player.Cash        = pCash;
                player.health      = pHealth;
                player.QuestPoints = pQuestPoints;

                MainGame.worldManager.CurrentWorld.manager.AddEntity(player);
                Game1.Instance.collisionManager.SwitchWorld();

                //load all of the quests in the quest file
                LoadQuests(MainGame.worldManager.worldQuests);

                //load the quest status
                string   quest;
                QuestLog log  = MainGame.worldManager.worldQuests;
                QuestLog pLog = player.log;
                for (int i = 0; i < numQuests; i++)
                {
                    quest = (string)quests[i, 0];
                    if (log.ContainsQuest(quest))
                    {
                        pLog[quest]        = log[quest];
                        pLog[quest].Status = (int)quests[i, 1];
                    }
                }

                //add new quests to quest log
                foreach (Quest newQuest in log)
                {
                    if (!pLog.ContainsQuest(newQuest))
                    {
                        pLog.Add(newQuest);
                    }
                }

                //load the items
                Item.Inventory inventory = player.inventory;
                Item.Item      newItem;
                for (int i = 0; i < numItems; i++)
                {
                    string name = items[i];
                    //newItem.image = Sprites.spritesDictionary[newItem.name].Texture;
                    //figure out the save path
                    int    startname = saveLoc.LastIndexOf('/') + 1;
                    int    endname   = saveLoc.LastIndexOf('.');
                    string filename  = saveLoc.Substring(startname, endname - startname);
                    string directory = saveLoc.Substring(0, startname) + "/" + filename;
                    newItem = LoadItem(directory + "/" + name + ".item");
                    inventory.Add(newItem);
                }
                inventory.ActiveWeapon = activeItem;

                //add the player to the world
                MainGame.worldManager.CurrentWorld.manager.AddEntity(player);

                //add the player to the quests
                foreach (Quest loopQuest in MainGame.worldManager.worldQuests)
                {
                    loopQuest.player = player;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error reading file: " + e.Message);
                Console.WriteLine("Stack: \n\t" + e.StackTrace);
            }
            finally
            {
                if (input != null)
                {
                    input.Close();
                }
            }
        }