Exemple #1
0
    public void InitializeInventory()
    {
        _rifle1  = new InventoryWeaponMount();
        _rifle2  = new InventoryWeaponMount();
        _handgun = new InventoryWeaponMount();
        _melee   = new InventoryWeaponMount();

        _ammo.Clear();
        _consumables.Clear();
        _messages.Clear();

        if (_rifle1Mount.item != null)
        {
            _rifle1.rounds = _rifle1Mount.rounds;
            _rifle1.item   = _rifle1Mount.item;
        }
        if (_rifle2Mount.item != null)
        {
            _rifle2.rounds = _rifle2Mount.rounds;
            _rifle2.item   = _rifle2Mount.item;
        }
        if (_handgunMount.item != null)
        {
            _handgun.rounds = _handgunMount.rounds;
            _handgun.item   = _handgunMount.item;
        }
        if (_meleeMount.item != null)
        {
            _melee.item = _meleeMount.item;
        }

        for (int i = 0; i < _ammoMounts.Count; i++)
        {
            if (_ammoMounts[i] == null)
            {
                continue;
            }

            InventoryAmmoMount ammoMount = new InventoryAmmoMount();
            ammoMount.rounds = _ammoMounts[i].rounds;
            ammoMount.item   = _ammoMounts[i].item;

            if (!_ammo.Contains(_ammoMounts[i]))
            {
                _ammo.Add(ammoMount);
            }
        }

        for (int i = 0; i < _consumableMounts.Count; i++)
        {
            if (_consumableMounts[i] == null)
            {
                continue;
            }

            InventoryConsumableMount consumableMount = new InventoryConsumableMount();
            consumableMount.item = _consumableMounts[i].item;

            if (!_consumables.Contains(_consumableMounts[i]))
            {
                _consumables.Add(consumableMount);
            }
        }

        for (int i = 0; i < _messagesMounts.Count; i++)
        {
            if (_messagesMounts[i] == null)
            {
                continue;
            }

            if (!_messages.Contains(_messagesMounts[i]))
            {
                _messages.Add(_messagesMounts[i]);
            }
        }
    }
    public void Repaint(bool firstPaint)
    {
        _itemInfos.Clear();
        _weaponInfos.Clear();

        for (int i = 0; i < _itemSlots.Count; i++)
        {
            ItemInfo itemInfo = new ItemInfo();
            itemInfo.transform = _itemSlots[i].transform;
            itemInfo.image     = _itemSlots[i].transform.Find("Frame").Find("Slot Image").GetComponent <Image>();
            itemInfo.image.gameObject.SetActive(false);
            itemInfo.isEmpty = true;

            _itemInfos.Add(itemInfo);
        }

        for (int i = 0; i < _weaponSlots.Count; i++)
        {
            Transform weaponInfoContainer = _weaponSlots[i].transform.Find("Weapon Info Container");

            WeaponInfo weaponInfo = new WeaponInfo();
            weaponInfo.transform  = weaponInfoContainer.transform;
            weaponInfo.isEmpty    = true;
            weaponInfo.nameText   = weaponInfoContainer.Find("Weapon Name").GetComponent <Text>();
            weaponInfo.roundsText = weaponInfoContainer.Find("Weapon Rounds").GetComponent <Text>();
            weaponInfo.image      = weaponInfoContainer.Find("Weapon Image").Find("Frame").Find("Weapon Slot Image").GetComponent <Image>();

            _weaponInfos.Add(weaponInfo);

            weaponInfoContainer.gameObject.SetActive(false);
        }

        if (_inventory != null)
        {
            // Setting inventory weapons
            List <InventoryWeaponMount> weaponMounts = new List <InventoryWeaponMount>();
            weaponMounts.Add(_inventory.rifle1);
            weaponMounts.Add(_inventory.rifle2);
            weaponMounts.Add(_inventory.handgun);
            weaponMounts.Add(_inventory.melee);

            for (int i = 0; i < weaponMounts.Count; i++)
            {
                if (weaponMounts[i].item != null)
                {
                    _weaponInfos[i].isEmpty         = false;
                    _weaponInfos[i].nameText.text   = weaponMounts[i].item.itemName;
                    _weaponInfos[i].roundsText.text = weaponMounts[i].rounds + "/" + weaponMounts[i].item.ammoCapacity;
                    _weaponInfos[i].description     = weaponMounts[i].item.itemDescription;
                    _weaponInfos[i].image.sprite    = weaponMounts[i].item.image;
                    _weaponInfos[i].weaponMount     = weaponMounts[i];

                    _weaponInfos[i].transform.gameObject.SetActive(true);

                    // After adding the weapon to the inventory, we are gonna add the weaponControl
                    // We only do this at the beginning
                    if (firstPaint)
                    {
                        // This shouldn't be called more than once because replace weapon looks for an empty space in backpack
                        // And adds another assault rifle even though we may have one in our inventory.
                        _weaponInfos[i].weaponMount.item.collectableWeapon.ReplaceWeapon(true, _weaponInfos[i].weaponMount, /* which rifle is either 1 or 2 */ i + 1);
                    }
                }
                else
                {
                    _weaponInfos[i].image.sprite    = null;
                    _weaponInfos[i].nameText.text   = "";
                    _weaponInfos[i].roundsText.text = "";
                }
            }

            // Setting inventory ammo
            for (int i = 0; i < _inventory.ammo.Count; i++)
            {
                if (_inventory.ammo[i].item != null)
                {
                    InventoryAmmoMount ammoMount = _inventory.ammo[i];
                    _itemInfos[i].isEmpty = false;
                    _itemInfos[i].image.gameObject.SetActive(true);
                    _itemInfos[i].image.sprite = _inventory.ammo[i].item.image;
                    _itemInfos[i].itemType     = ItemType.Ammo;
                    _itemInfos[i].ammoMount    = ammoMount;
                }
            }

            // Setting inventory consumables
            for (int i = 0; i < _inventory.consumables.Count; i++)
            {
                int consumableIndex = _inventory.ammo.Count + i;
                if (_inventory.consumables[i].item != null)
                {
                    InventoryConsumableMount consumableMount = _inventory.consumables[i];
                    _itemInfos[consumableIndex].isEmpty = false;
                    _itemInfos[consumableIndex].image.gameObject.SetActive(true);
                    _itemInfos[consumableIndex].image.sprite    = _inventory.consumables[i].item.image;
                    _itemInfos[consumableIndex].itemType        = ItemType.Consumable;
                    _itemInfos[consumableIndex].consumableMount = consumableMount;
                }
            }
        }

        if (_clickedItemIndex == -1)
        {
            ResetGeneralDescription();
        }
        else
        {
            ChangeGeneralDescription(_clickedItemIndex);
        }
    }
Exemple #3
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);
                }
            }
        }
    }