Esempio n. 1
0
    ///////////////////////////////
    ///
    ///     Main function to load savegame
    ///     This is the one you call publicly
    ///
    public void LoadSaveData(SaveData saveData, Action callback = null)
    {
        Logger.Debug("Loading save data");

        try {
            // Loading misc
            GameManager.instance.player.playerName         = saveData.playerName;
            GameManager.instance.cityManager.cityName      = saveData.cityName;
            GameManager.instance.cityManager.isTutorialRun = saveData.isTutorialRun;
            GameManager.instance.temporality.SetDate(saveData.cyclesPassed);
            GameManager.instance.temporality.SetTimeOfDay(saveData.timeOfDay);

            GridManagement gridMan = GameManager.instance.gridManagement;

            // Loading grid
            Logger.Debug("Loading " + saveData.blockGrid.Count + " objects into the grid");
            foreach (KeyValuePair <Vector3Int, BlockSaveData> blockData in saveData.blockGrid)
            {
                // Todo : Load states aswell
                Vector3Int coords = blockData.Key;
                Logger.Debug("Spawning block #" + blockData.Value.id + " at position " + blockData.Key.ToString());
                GameObject spawnedBlock = gridMan.SpawnBlock(blockData.Value.id, blockData.Key);
                spawnedBlock.GetComponent <Block>().container.closed = false;
            }

            // Converting bridges list
            foreach (KeyValuePair <Vector3Int, Vector3Int> bridge in saveData.bridges)
            {
                Block origin      = gridMan.grid[bridge.Key.x, bridge.Key.y, bridge.Key.z].GetComponent <Block>();
                Block destination = gridMan.grid[bridge.Value.x, bridge.Value.y, bridge.Value.z].GetComponent <Block>();
                if (gridMan.CreateBridge(origin, destination) == null)
                {
                    Logger.Warn("Could not replicate bridge from " + origin + ":(" + bridge.Key + ") to " + destination + ":(" + bridge.Value + ")");
                }
                ;
            }

            // Loading population
            PopulationManager popMan = GameManager.instance.populationManager;
            popMan.populations.Clear();
            foreach (PopulationSaveData data in saveData.popSaveData)
            {
                Population pop = popMan.GetPopulationByID(data.popId);
                if (pop == null)
                {
                    Logger.Error("Error while loading population from savegame : [" + data.popId + "] is not a valid POP id");
                }
                List <PopulationManager.Citizen> citizens = new List <PopulationManager.Citizen>();
                foreach (CitizenSaveData cSD in data.citizens)
                {
                    citizens.Add(new PopulationManager.Citizen()
                    {
                        name = cSD.name,
                        type = pop
                    });
                }

                popMan.populations[pop] = new PopulationManager.PopulationInformation()
                {
                    moodModifiers = data.moodModifiers,
                    foodModifiers = data.foodModifiers,
                    riotRisk      = data.riotRisk,
                    averageMood   = data.averageMood,
                    citizens      = citizens
                };
            }

            // Loading unlocks
            CityManager city = GameManager.instance.cityManager;
            city.ClearLocks();
            foreach (int id in saveData.lockedBuildings)
            {
                city.LockBuilding(id);
            }

            // Loading population order
            int prio = 0;
            foreach (int popId in saveData.populationTypeIds)
            {
                Population pop = popMan.GetPopulationByID(popId);
                if (pop == null)
                {
                    Logger.Error("Error while loading population types from savegame : [" + popId + "] is not a valid POP id");
                }
                popMan.ChangePopulationPriority(pop, prio);
                prio++;
            }
            MoodsDisplay display = FindObjectOfType <MoodsDisplay>();
            if (display != null)
            {
                // Refresh interface
                display.InitializeMoods(popMan.populationTypeList);
            }

            // Bulletins pool
            BulletinsManager bullMan = GameManager.instance.bulletinsManager;
            bullMan.SetBulletinPool(saveData.bulletinList);
            bullMan.SetBulletin(saveData.currentBulletin);

            // Events pool
            EventManager eventMan = GameManager.instance.eventManager;
            eventMan.eventsPool = saveData.eventsPool;

            // End of loading
            if (callback != null)
            {
                callback.Invoke();
            }
        }

        catch (Exception e) {
            Logger.Error("Could not load the savegame : \n" + e.ToString() + "\n Going back to the main menu instead.");
            DestroySave();
            GameManager.instance.ExitToMenu();
        }
    }