Пример #1
0
    public IEnumerator Sit()
    {
        GoneWrong.Player player = GoneWrong.Player.instance;

        // Reset blood screen
        if (player != null)
        {
            player.ResetBloodScreen();
        }

        if (player == null || _playerSit == null || _carController == null || _playerOut == null)
        {
            _sitCoroutine = null;
            yield break;
        }

        _playerIn = !_playerIn;

        player.canMove = !_playerIn;

        player.transform.parent = _playerIn ? _playerSit : _playerOut;

        Vector3    playerInitialPosition = player.transform.localPosition;
        Quaternion playerInitialRotation = player.transform.localRotation;

        float duration = _playerIn ? _getInCarDuration : _getOutOfCarDuration;

        if (!_playerIn)
        {
            // We open the door before getting outside the car
            if (_carDoor != null)
            {
                _carDoor.Interact(transform);
            }

            // We deactivate the car controller if we are getting out
            _carController.enabled = false;
        }
        else
        {
            // If we are getting inside the car, we deactivate the player controls
            player.GetComponent <CharacterController>().enabled = !_playerIn;
            // We also tell the player that we are inside a car
            player.insideACar = true;
        }

        float time = 0;

        while (time < duration)
        {
            time += Time.deltaTime;
            float normalizedTime = time / duration;

            player.transform.localPosition = Vector3.Lerp(playerInitialPosition, Vector3.zero, normalizedTime);
            player.transform.localRotation = Quaternion.Slerp(playerInitialRotation, Quaternion.Euler(Vector3.zero), normalizedTime);

            yield return(null);
        }

        // Make sure we got the right values
        player.transform.localPosition = Vector3.zero;
        player.transform.localRotation = Quaternion.Euler(Vector3.zero);

        // Make sure the main camera gets set to rotation zero after we get out
        if (!_playerIn)
        {
            Camera.main.transform.localRotation = Quaternion.Euler(Vector3.zero);
        }

        // We close the door after getting inside the car
        if (_carDoor != null && _playerIn)
        {
            _carDoor.Interact(transform);
        }

        if (_playerIn)
        {
            // We activate the char controller after it is enabled
            _carController.enabled = true;
        }
        else
        {
            player.transform.parent = null;
            player.GetComponent <CharacterController>().enabled = true;
            player.insideACar = false;
            // Then we close the door after we are out.
            if (_carDoor != null)
            {
                _carDoor.Interact(transform);
            }
        }

        // Now we change the text
        _text = _playerIn ? _getOutText : _interactiveText;

        // Force the player to go back to his initial local scale after we get him out of the car
        if (!_playerIn)
        {
            player.transform.localScale = _playerLocalScale;
        }

        // Now we activate or deactive playerHUD or carHUD
        if (CarHUD.instance != null)
        {
            CarHUD.instance.gameObject.SetActive(_playerIn);
        }
        if (PlayerHUD.instance != null)
        {
            PlayerHUD.instance.gameObject.SetActive(!_playerIn);
        }

        // Set the player drived vehicle
        player.drivedVehicle = _playerIn ? GetComponentInParent <Vehicle>() : null;

        // Activate the flashlight the moment the player gets out of the car
        if (!_playerIn && Flashlight.instance != null)
        {
            Flashlight.instance.Look(!Flashlight.instance.looking);
        }

        // Play the vehicle's startup sound:
        if (_playerIn && player.drivedVehicle.startUpSound != null && GoneWrong.AudioManager.instance != null)
        {
            GoneWrong.AudioManager.instance.PlayOneShotSound(player.drivedVehicle.startUpSound, 1, 0, 0, transform.position);
        }

        _sitCoroutine = null;
    }
Пример #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);
                }
            }
        }
    }