void AddObjectToInventory(ScriptableObject purchasedObject, int purchasedQuantity, EncounterMerchantScriptable.coinTypes coinTypeUsed, int howManyCoins)
    {
        bool itemSlotFound = false;

        for (int i = 3; i < inventoryManager.GetComponent <MapSceneInventoryManager>().inventoryScriptables.Length; i++)
        {
            //Compare purchased object with items in inventory, until a match is found
            if (!itemSlotFound && inventoryManager.GetComponent <MapSceneInventoryManager>().inventoryScriptables[i] != null && purchasedObject == inventoryManager.GetComponent <MapSceneInventoryManager>().inventoryScriptables[i])
            {
                Debug.Log("Purchased item matches object in inventory");
                //If the items are stackable
                if (purchasedObject.GetType() == typeof(InventoryItemScriptable))
                {
                    InventoryItemScriptable purchasedItem = (InventoryItemScriptable)purchasedObject;
                    GameObject matchedItemHost            = inventoryManager.GetComponent <MapSceneInventoryManager>().inventoryParents[i].transform.GetChild(0).gameObject;
                    if (purchasedItem.isStackable)
                    {
                        Debug.Log("Purchased object stacks with object in inventory");

                        matchedItemHost.GetComponent <InventoryScriptableReader>().itemQuantity += purchasedQuantity;
                        print(matchedItemHost.GetComponent <InventoryScriptableReader>().itemQuantity);
                        matchedItemHost.GetComponent <Text>().text = (matchedItemHost.GetComponent <InventoryScriptableReader>().objectName + " x " +
                                                                      matchedItemHost.GetComponent <InventoryScriptableReader>().itemQuantity);

                        //inventoryManager.GetComponent<MapSceneInventoryManager>().inventoryScriptables[i] = matchedItem;
                        inventoryManager.GetComponent <MapSceneInventoryManager>().LogInventory();

                        SpendPlayerMoney(coinTypeUsed, howManyCoins);

                        itemSlotFound = true;
                        break;
                    }
                    //else {
                    //	Debug.Log("Objects do not stack. Finding empty slot");
                    //	FindEmptyInventorySlot(purchasedObject, coinTypeUsed, howManyCoins);
                    //}
                }

                //itemMatch = true;
            }
            //else {
            //	print("Purchased object didn't find a match");
            //	Debug.Log("itemSlotFound: " + itemSlotFound.ToString());
            //	if (inventoryManager.GetComponent<MapSceneInventoryManager>().inventoryScriptables[i] != null) {
            //		Debug.Log("There is an item here: " + inventoryManager.GetComponent<MapSceneInventoryManager>().inventoryScriptables[i]);
            //		if (purchasedObject == inventoryManager.GetComponent<MapSceneInventoryManager>().inventoryScriptables[i]) {
            //			Debug.Log("The items definitely match: " + purchasedObject.name + "::" + inventoryManager.GetComponent<MapSceneInventoryManager>().inventoryScriptables[i].name);
            //		}
            //	}
            //}
        }

        if (!itemSlotFound)
        {
            Debug.Log("Objects do not match/stack. Finding empty slot");
            FindEmptyInventorySlot(purchasedObject, purchasedQuantity, coinTypeUsed, howManyCoins);
        }
    }
    //WEAPON Variables


    //ARMOR Variables



    public void SetMyObjectType()
    {
        if (objectScript != null)
        {
            if (objectScript.GetType() == typeof(InventoryItemScriptable))
            {
                itemScript   = objectScript as InventoryItemScriptable;
                myObjectType = objectType.item;
                objectName   = itemScript.itemName;

                InitializeItem();
            }
            else if (objectScript.GetType() == typeof(InventoryWeaponScriptable))
            {
                weaponScript = objectScript as InventoryWeaponScriptable;
                myObjectType = objectType.weapon;
                objectName   = weaponScript.itemName;


                //itemQuantity = weaponScript.weaponQuantity;

                //if (weaponScript.MyWeaponType == InventoryWeaponScriptable.weaponTypes.Dagger) {

                //}

                InitializeWeapon();
            }
            else if (objectScript.GetType() == typeof(InventoryArmorScriptable))
            {
                armorScript  = objectScript as InventoryArmorScriptable;
                myObjectType = objectType.armor;
                objectName   = armorScript.itemName;

                //itemQuantity = armorScript.armorQuantity;

                InitializeArmor();
            }
        }
    }
Example #3
0
//Incorporated into EmptyInventory()
    //public void SaveInventory() {
    //	print("Saving inventory for recovery");
    //	savedInventoryScripts = new List<ScriptableObject>();
    //	savedInventoryQuantities = new List<int>();

    //	for (int i = 0; i < inventoryParents.Length; i++) {
    //		if (inventoryParents[i].transform.childCount != 0) {
    //			savedInventoryScripts.Add(inventoryParents[i].transform.GetChild(0).GetComponent<InventoryScriptableReader>().objectScript);
    //			savedInventoryQuantities.Add(inventoryParents[i].transform.GetChild(0).GetComponent<InventoryScriptableReader>().itemQuantity);
    //		}
    //	}

    //	savedSilver = InventoryManager.silverCarried;
    //	savedCopper = InventoryManager.copperCarried;
    //}


    public void RecoverSavedInventory(bool recoverAll, bool recoverPouch, bool recoverDagger, bool recoversRandomWeapon, InventoryItemScriptable confirmsItemInInventory, int quantityToConfirm)
    {
        List <ScriptableObject> recoveryScriptList = new List <ScriptableObject>();
        List <int> recoveryQuantitiesList          = new List <int>();

        if (recoverAll)
        {
//TODO Currently this only allows the player to have 12 items in their inventory before things (probably) get fucky (because of the cloak/armor/dagger slots).
//Is that a problem?
            for (int i = 0; i < savedInventoryScripts.Count; i++)
            {
                recoveryScriptList.Add(savedInventoryScripts[i]);
                recoveryQuantitiesList.Add(savedInventoryQuantities[i]);
            }
        }

        if (recoverPouch)
        {
            InventoryManager.silverCarried = savedSilver;
            InventoryManager.copperCarried = savedCopper;
        }


        if (recoverDagger)
        {
            bool foundSilverDagger = false;
            bool foundDagger       = false;

            //Search through the player's saved inventory for their SILVER DAGGER. If it can't be found, look for a REGULAR DAGGER. If not that, then give them nothing
            for (int i = 0; i < savedInventoryScripts.Count; i++)
            {
                if (savedInventoryScripts[i] == silverDagger)
                {
                    recoveryScriptList.Add(savedInventoryScripts[i]);
                    recoveryQuantitiesList.Add(savedInventoryQuantities[i]);

                    savedInventoryScripts.RemoveAt(i);
                    savedInventoryQuantities.RemoveAt(i);

                    foundSilverDagger = true;
                    break;
                }
            }

            if (!foundSilverDagger)
            {
                for (int i = 0; i < savedInventoryScripts.Count; i++)
                {
                    if (savedInventoryScripts[i] == dagger)
                    {
                        recoveryScriptList.Add(savedInventoryScripts[i]);
                        recoveryQuantitiesList.Add(1);

                        savedInventoryScripts.RemoveAt(i);
                        savedInventoryQuantities.RemoveAt(i);

                        foundDagger = true;
                        break;
                    }
                }
            }

            if (foundSilverDagger)
            {
                print("You found the SILVER DAGGER from your previous inventory");
            }
            else if (foundDagger)
            {
                print("You found a DAGGER from your previous inventory");
            }
            else
            {
                print("You had NO DAGGERS in your previous inventory");
            }
        }

        if (recoversRandomWeapon)
        {
            //Search through the player's saved inventory for any weapon OTHER THAN A DAGGER.
            print("You recovered a RANDOM WEAPON from your PREVIOUS INVENTORY");
            List <ScriptableObject> randomWeaponScriptList = new List <ScriptableObject>();

            for (int i = 0; i < savedInventoryScripts.Count; i++)
            {
                if (savedInventoryScripts[i].GetType() == typeof(InventoryWeaponScriptable))
                {
                    randomWeaponScriptList.Add(savedInventoryScripts[i]);
                }
            }

            if (randomWeaponScriptList.Count > 0)
            {
                int randomWeaponChosen = Random.Range(0, randomWeaponScriptList.Count);

                recoveryScriptList.Add(randomWeaponScriptList[randomWeaponChosen]);
                recoveryQuantitiesList.Add(1);
            }
            else
            {
                print("NO RANDOM WEAPONS saved in previous inventory");
            }
        }

        if (confirmsItemInInventory != null)
        {
            int objectsTotal = 0;

            for (int i = 0; i < savedInventoryScripts.Count; i++)
            {
                if (savedInventoryScripts[i] as InventoryItemScriptable == confirmsItemInInventory)
                {
                    InventoryItemScriptable itemConfirmed = savedInventoryScripts[i] as InventoryItemScriptable;
                    itemConfirmed.itemQuantity = savedInventoryQuantities[i];
                    objectsTotal = itemConfirmed.itemQuantity;

                    print("Object quantity: " + itemConfirmed.itemQuantity);
                    print("Object total: " + objectsTotal);
                }
            }

            if (objectsTotal == 0)
            {
                print("Didn't find object to recover in inventory");
            }
            else if (objectsTotal >= quantityToConfirm)
            {
                recoveryScriptList.Add(confirmsItemInInventory);
                recoveryQuantitiesList.Add(quantityToConfirm);

                print("You recovered " + quantityToConfirm + " " + confirmsItemInInventory);
            }
            else
            {
                recoveryScriptList.Add(confirmsItemInInventory);
                recoveryQuantitiesList.Add(objectsTotal);

                print("Couldn't recover " + quantityToConfirm + " " + confirmsItemInInventory + ". Can only recover " + objectsTotal);
            }
        }

        //Wait to respawn newItemHosts until the list has been fully compiled HERE
        for (int i = 0; i < recoveryScriptList.Count; i++)
        {
            GameObject newItemHost = Instantiate(itemHost, inventoryParents[3 + i].transform.position, Quaternion.identity, inventoryParents[3 + i].transform);

            newItemHost.GetComponent <InventoryScriptableReader>().objectScript = recoveryScriptList[i];
            newItemHost.GetComponent <InventoryScriptableReader>().itemQuantity = recoveryQuantitiesList[i];

            newItemHost.GetComponent <Text>().text = newItemHost.GetComponent <InventoryScriptableReader>().objectScript.name + " x " +
                                                     newItemHost.GetComponent <InventoryScriptableReader>().itemQuantity.ToString();

            newItemHost.GetComponent <InventoryScriptableReader>().SetMyObjectType();
        }

        LogInventory();
        itemListInventoryManager.GetComponent <MapSceneInventoryManager>().InitializeInventory();
        //print("Your inventory has been partially/fully recovered and logged");
        mapSceneManager.OpenCharacterSheet();
    }