Esempio n. 1
0
 private void Awake()
 {
     instance = this;
     gameObject.SetActive(false);
 }
Esempio n. 2
0
    private void Awake()
    {
        if (SaveGame.instance != null)
        {
            return;
        }

        instance = this;

        if (_deleteSaveData)
        {
            DeleteSaveGame();
            return;
        }

        SavedData savedData = JsonUtility.FromJson <SavedData>(PlayerPrefs.GetString("data"));

        _savedData = savedData;

        List <InventoryAmmoMount>       ammos       = new List <InventoryAmmoMount>();
        List <InventoryConsumableMount> consumables = new List <InventoryConsumableMount>();
        List <Messages> messages = new List <Messages>();

        if (_playerInventory != null && savedData != null)
        {
            if (savedData.rifle1.index != -1)
            {
                _playerInventory.rifle1.item   = _inventoryWeapons[savedData.rifle1.index];
                _playerInventory.rifle1.rounds = savedData.rifle1.rounds;
            }

            if (savedData.rifle2.index != -1 && _inventoryWeapons.Count > savedData.rifle2.index)
            {
                _playerInventory.rifle2.item   = _inventoryWeapons[savedData.rifle2.index];
                _playerInventory.rifle2.rounds = savedData.rifle2.rounds;
            }

            if (savedData.handgun.index != -1 && _inventoryWeapons.Count > savedData.handgun.index)
            {
                _playerInventory.handgun.item   = _inventoryWeapons[savedData.handgun.index];
                _playerInventory.handgun.rounds = savedData.handgun.rounds;
            }

            if (savedData.melee.index != -1 && _inventoryWeapons.Count > savedData.melee.index)
            {
                _playerInventory.melee.item = _inventoryWeapons[savedData.melee.index];
            }

            foreach (SavedSingleData savedSingleData in savedData.inventoryList)
            {
                switch (savedSingleData.type)
                {
                case DataType.Ammo:
                    if (_inventoryAmmos.Count > savedSingleData.index)
                    {
                        InventoryAmmoMount ammoMount = new InventoryAmmoMount();
                        ammoMount.rounds = savedSingleData.rounds;
                        ammoMount.item   = _inventoryAmmos[savedSingleData.index];
                        ammos.Add(ammoMount);
                    }
                    break;

                case DataType.Consumable:
                    if (_inventoryConsumables.Count > savedSingleData.index &&
                        _inventoryConsumables[savedSingleData.index] != null &&
                        _inventoryConsumables[savedSingleData.index].collectableConsumable.canSave)
                    {
                        InventoryConsumableMount consumableMount = new InventoryConsumableMount();
                        consumableMount.item = _inventoryConsumables[savedSingleData.index];

                        // Now we alter the game progress
                        // Before adding each consumable, we need to make sure that the progress states associated with the mare registered
                        List <GameProgress> progressStates  = consumableMount.item.collectableConsumable.progressStates;
                        ProgressManager     progressManager = FindObjectOfType <ProgressManager>();
                        if (progressStates.Count > 0 && progressManager != null)
                        {
                            foreach (GameProgress gameProgress in progressStates)
                            {
                                progressManager.SetProgress(gameProgress.key, gameProgress.value);
                            }
                        }

                        consumables.Add(consumableMount);
                    }
                    break;

                case DataType.Message:
                    if (_messages.Count > savedSingleData.index)
                    {
                        Messages message = _messages[savedSingleData.index];
                        messages.Add(message);
                    }
                    break;
                }

                _playerInventory.ammo        = ammos;
                _playerInventory.consumables = consumables;
                _playerInventory.messages    = messages;
            }

            if (_healthSharedFloat != null)
            {
                _healthSharedFloat.value = savedData.health;
            }

            if (_staminaSharedFloat != null)
            {
                _staminaSharedFloat.value = savedData.stamina;
            }

            if (_infectionSharedFloat != null)
            {
                _infectionSharedFloat.value = savedData.infection;
            }

            // We only load the player position if the last saved scene is the same as the current scene
            // So that we dont load the player in a random position when he just entered the map from another map
            // We can only load the player's positon when we are in wasteland
            GoneWrong.Player player = FindObjectOfType <GoneWrong.Player>();
            if (player != null && _savedData.currentScene == SceneManager.GetActiveScene().buildIndex &&
                SceneManager.GetActiveScene().name == "Wasteland")
            {
                CharacterController playerCharacterController = player.GetComponent <CharacterController>();
                playerCharacterController.enabled = false;

                if (savedData.playerPosition != Vector3.zero)
                {
                    FlashlightLight flashLight = FindObjectOfType <FlashlightLight>();
                    // We should also put the flash light in place
                    Vector3 differenceWithFlashLight = player.transform.position - flashLight.transform.position;
                    player.transform.position     = savedData.playerPosition;
                    flashLight.transform.position = player.transform.position - differenceWithFlashLight;
                }

                if (savedData.playerRotation != Quaternion.identity)
                {
                    player.transform.rotation = savedData.playerRotation;
                }

                playerCharacterController.enabled = true;
            }

            // Now for the collectable items
            // We check if we already saved the data for the current scene:
            SceneData sceneData = null;
            switch (SceneManager.GetActiveScene().name)
            {
            case "Hospital":
                sceneData = savedData.hospitalSceneData;
                break;

            case "Wasteland":
                sceneData = savedData.wasteLandData;
                break;

            case "Tunnels":
                sceneData = savedData.tunnelsSceneData;
                break;

            case "Underground":
                sceneData = savedData.undergroundSceneData;
                break;
            }

            if (sceneData != null && sceneData.savedBefore)
            {
                // We change the skybox and the fog depending on what is stored in the sceneData
                switch (sceneData.dayTime)
                {
                case DayTime.Night:
                    RenderSettings.fogDensity = 0f;
                    RenderSettings.skybox     = null;
                    break;

                case DayTime.Mist:
                    if (_fogSkyBox != null)
                    {
                        RenderSettings.fogDensity = 0.35f;
                        RenderSettings.skybox     = _fogSkyBox;
                    }
                    break;
                }

                // We already saved the scene before. So we destroy all the collectableItems in the scene
                CollectableConsumable[] collectableConsumables = FindObjectsOfType <CollectableConsumable>();
                foreach (CollectableConsumable collectableConsumable in collectableConsumables)
                {
                    if (collectableConsumable.canSave)
                    {
                        Destroy(collectableConsumable.gameObject);
                    }
                }

                CollectableAmmo[] colectableAmmos = FindObjectsOfType <CollectableAmmo>();
                foreach (CollectableAmmo collectableAmmo in colectableAmmos)
                {
                    if (collectableAmmo.canSave)
                    {
                        Destroy(collectableAmmo.gameObject);
                    }
                }

                // Then we instantiate all the remaining collectable Items that were present when saving the data
                foreach (SceneCollectableItem sceneCollectableItem in sceneData.collectableItems)
                {
                    // If we have the collectable item in list of collectable items
                    if (sceneCollectableItem.index != -1)
                    {
                        // Then we instantiate the collectable item from the inventory scriptable object in our list
                        if (sceneCollectableItem.type == DataType.Consumable)
                        {
                            if (_inventoryConsumables[sceneCollectableItem.index].collectableConsumable != null
                                // check if we can instantiate it in the first place
                                && _inventoryConsumables[sceneCollectableItem.index].collectableConsumable.canSave)
                            {
                                CollectableConsumable tmp = Instantiate(_inventoryConsumables[sceneCollectableItem.index].collectableConsumable);
                                if (sceneCollectableItem.parentName != "")
                                {
                                    Transform parent = GameObject.Find(sceneCollectableItem.parentName).transform;
                                    if (parent != null)
                                    {
                                        tmp.transform.parent = parent;
                                    }

                                    tmp.transform.localPosition = sceneCollectableItem.localPosition;
                                    tmp.transform.localRotation = sceneCollectableItem.localRotation;
                                }
                            }
                        }
                        else if (sceneCollectableItem.type == DataType.Ammo)
                        {
                            if (_inventoryConsumables[sceneCollectableItem.index].collectableConsumable != null)
                            {
                                CollectableAmmo tmp = Instantiate(_inventoryAmmos[sceneCollectableItem.index].collectableAmmo);
                                if (sceneCollectableItem.parentName != "")
                                {
                                    Transform parent = GameObject.Find(sceneCollectableItem.parentName).transform;
                                    if (parent != null)
                                    {
                                        tmp.transform.parent = parent;
                                    }

                                    tmp.transform.localPosition = sceneCollectableItem.localPosition;
                                    tmp.transform.localRotation = sceneCollectableItem.localRotation;
                                }
                            }
                        }
                    }
                }

                // Now we check the remaining zombies
                // Each zombie whose name doesn't belong to the list of the remaining zombies, will be deactivated
                if (_saveZombies)
                {
                    AIStateMachine[] stateMachines = FindObjectsOfType <AIStateMachine>();
                    foreach (AIStateMachine stateMachine in stateMachines)
                    {
                        int  index       = 0;
                        bool foundZombie = false;
                        do
                        {
                            if (sceneData.remainingZombies.Count > index && stateMachine.transform.name == sceneData.remainingZombies[index])
                            {
                                foundZombie = true;
                            }
                            index++;
                        } while (index < sceneData.remainingZombies.Count && !foundZombie);

                        // If we didn't find the zombie in our list, we destroy him
                        if (!foundZombie)
                        {
                            Destroy(stateMachine.gameObject);
                            //stateMachine.gameObject.SetActive(false);
                        }
                    }
                }

                // Handling progress objects (activating and deactivating them, then setting their position)
                foreach (ProgressObject progressObject in sceneData.progressObjects)
                {
                    if (progressObject.index != -1 && _progressObjects.Count > progressObject.index)
                    {
                        Transform sceneProgressObject = _progressObjects[progressObject.index];
                        if (sceneProgressObject != null)
                        {
                            sceneProgressObject.gameObject.SetActive(progressObject.active);
                            sceneProgressObject.transform.position = progressObject.position;
                            sceneProgressObject.transform.rotation = progressObject.rotation;
                        }
                    }
                }

                PlayerHUD playerHud = PlayerHUD.instance;
                if (playerHud == null)
                {
                    playerHud = FindObjectOfType <PlayerHUD>();
                }

                if (playerHud != null)
                {
                    playerHud.ChangeLevelObjectiveText(sceneData.nextObjective);
                }

                CarHUD carHUD = CarHUD.instance;
                if (carHUD == null)
                {
                    carHUD = FindObjectOfType <CarHUD>();
                }

                if (carHUD != null)
                {
                    carHUD.ChangeLevelObjectiveText(sceneData.nextObjective);
                }
            }
        }
    }