Ejemplo n.º 1
0
    public bool LoadGame()
    {
        //Debug.Log ("Loading Game");

        controller.Reset();

        //Load file
        if (File.Exists(Application.persistentDataPath + "/baseInfo.dat"))
        {
            BinaryFormatter formatter  = new BinaryFormatter();
            FileStream      fileStream = File.Open(Application.persistentDataPath + "/baseInfo.dat", FileMode.Open);
            GameData        gameData   = (GameData)formatter.Deserialize(fileStream);
            fileStream.Close();

            //Set supply values
            supplyStone  = gameData.supplyStone;
            supplyWood   = gameData.supplyWood;
            supplyFood   = gameData.supplyFood;
            maxVillagers = gameData.maxVillagers;

            //Remove villagers from world
            foreach (BaseVillager villager in villagerList)
            {
                Destroy(villager.gameObject);
            }

            //Clear villager array
            villagerList.Clear();

            //Clear Character Menu
            characterScroll.GetComponent <CharacterDisplay>().RemoveAllButtons();

            //Load Villagers
            foreach (VillagerData villager in gameData.villagerList)
            {
                GameObject newVillager = SpawnVillager();

                BaseVillager toAdd = newVillager.GetComponent <BaseVillager> ();
                toAdd.Load(villager);
            }

            //Load Quests
            GetComponent <QuestManager>().Load(gameData.questList);

            //Destroy all buildings in world ready to load
            foreach (BaseBuilding building in buildingList)
            {
                Destroy(building.gameObject);
            }
            buildingList.Clear();
            foreach (BaseBuilding building in toBeBuilt)
            {
                Destroy(building.gameObject);
            }
            toBeBuilt.Clear();
            foreach (BaseBuilding building in toBeUpgraded)
            {
                Destroy(building.gameObject);
            }
            toBeUpgraded.Clear();

            //Load Buildings
            foreach (BuildingData building in gameData.toBeBuilt)
            {
                GameObject newBuilding = Instantiate(Resources.Load(building.loadPath)) as GameObject;

                BaseBuilding toAdd = newBuilding.GetComponent <BaseBuilding>();
                toAdd.Load(building, this);
                toBeBuilt.Add(toAdd);
            }
            foreach (BuildingData building in gameData.buildingList)
            {
                GameObject newBuilding = Instantiate(Resources.Load(building.loadPath)) as GameObject;

                BaseBuilding toAdd = newBuilding.GetComponent <BaseBuilding>();
                toAdd.Load(building, this);
                buildingList.Add(toAdd);
            }

            foreach (BuildingData building in gameData.toBeUpgraded)
            {
                GameObject newBuilding = Instantiate(Resources.Load(building.loadPath)) as GameObject;

                BaseBuilding toAdd = newBuilding.GetComponent <BaseBuilding> ();
                toAdd.Load(building, this);
                toBeUpgraded.Add(toAdd);
            }


            inventoryReference.ClearInventory();

            //Load Inventory
            inventoryReference.Load(gameData.inventoryData);

            attackTimer    = gameData.attackTimer;
            attackTimerSet = true;

            Debug.Log("Game Loaded");

            return(true);
        }
        Debug.Log("Game Failed to Load");


        return(false);
    }