Beispiel #1
0
        /// <summary>
        /// Pickups the item and stores it in the Inventory.
        /// </summary>
        /// <returns>Returns 0 if item was stored, -1 if not, -2 for some other unknown reason.</returns>
        public virtual bool PickupItem(bool addToInventory = true)
        {
            if (addToInventory)
            {
                return(InventoryManager.AddItem(this));
            }

            return(true);
        }
        /// <summary>
        /// Buy an item from this vendor, this does not display a dialog, but moves the item directly to the inventory.
        /// Note that this does not show any UI or warnings and immediately handles the action.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="amount"></param>
        public virtual bool BuyItemFromVendorNow(InventoryItemBase item, uint amount, bool isBuyBack = false)
        {
            if (CanBuyItemFromVendor(item, amount, isBuyBack) == false)
            {
                return(false);
            }


            var c1 = GameObject.Instantiate <InventoryItemBase>(item);

            c1.currentStackSize = amount;
            InventoryManager.AddItem(c1); // Will handle unstacking if the stack size goes out of bounds.


            if (isBuyBack)
            {
                InventoryManager.instance.inventory.gold -= GetBuyBackPrice(item, amount);

                if (buyBackIsShared)
                {
                    buyBackDict[vendorCategory][(int)item.index].currentStackSize -= amount;
                    if (buyBackDict[vendorCategory][(int)item.index].currentStackSize < 1)
                    {
                        Destroy(buyBackDict[vendorCategory][(int)item.index].gameObject);
                        buyBackDict[vendorCategory].RemoveAt((int)item.index);
                    }
                }
                else
                {
                    buyBackList[(int)item.index].currentStackSize -= amount;
                    if (buyBackList[(int)item.index].currentStackSize < 1)
                    {
                        Destroy(buyBackList[(int)item.index].gameObject);
                        buyBackList.RemoveAt((int)item.index);
                    }
                }

                vendorUI.NotifyItemBoughtBackFromVendor(item, amount);
            }
            else
            {
                InventoryManager.instance.inventory.gold -= GetBuyPrice(item, amount);

                if (removeItemAfterPurchase)
                {
                    item.itemCollection.SetItem(item.index, null);
                    item.itemCollection.NotifyItemRemoved(item.ID, item.index, item.currentStackSize);
                    item.itemCollection[item.index].Repaint();
                    //Destroy(item.gameObject);
                }
            }

            vendorUI.NotifyItemBoughtFromVendor(item, amount);

            return(true);
        }
        public override IList <InventoryItemUsability> GetExtraItemUsabilities(IList <InventoryItemUsability> basic)
        {
            var l = base.GetExtraItemUsabilities(basic);

            l.Add(new InventoryItemUsability("To inventory", (item) =>
            {
                var oldCollection = item.itemCollection;
                uint oldIndex     = item.index;

                bool added = InventoryManager.AddItem(item);
                if (added)
                {
                    oldCollection.SetItem(oldIndex, null);
                    oldCollection[oldIndex].Repaint();

                    oldCollection.NotifyItemRemoved(item.ID, oldIndex, item.currentStackSize);
                }
            }));

            return(l);
        }
        private IEnumerator _CraftItem(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, int amount, float currentCraftTime)
        {
            bool canCraft = CanCraftBlueprint(blueprint, category.alsoScanBankForRequiredItems, amount);

            if (canCraft)
            {
                float counter = currentCraftTime;
                while (true)
                {
                    yield return(new WaitForSeconds(Time.deltaTime)); // Update loop

                    counter -= Time.deltaTime;
                    NotifyCraftProgress(category, blueprint, 1.0f - Mathf.Clamp01(counter / currentCraftTime));

                    if (counter <= 0.0f)
                    {
                        break;
                    }
                }


                // Remove items from inventory
                foreach (var item in blueprint.requiredItems)
                {
                    InventoryManager.RemoveItem(item.item.ID, (uint)item.amount, category.alsoScanBankForRequiredItems); //  * GetCraftInputFieldAmount()
                }

                // Remove gold
                InventoryManager.instance.inventory.gold -= blueprint.craftCostPrice;

                if (blueprint.successChanceFactor >= Random.value)
                {
                    // Store crafted item
                    var c = GameObject.Instantiate <InventoryItemBase>(blueprint.itemResult);
                    c.currentStackSize = (uint)(blueprint.itemResultCount); //  * GetCraftInputFieldAmount()
                    if (category.forceSaveInCollection != null)
                    {
                        category.forceSaveInCollection.AddItem(c);
                    }
                    else
                    {
                        InventoryManager.AddItem(c);
                    }

                    InventoryManager.instance.lang.craftedItem.Show(blueprint.itemResult.name, blueprint.itemResult.description);

                    if (OnCraftSuccess != null)
                    {
                        OnCraftSuccess(category, blueprint, c);
                    }
                }
                else
                {
                    InventoryManager.instance.lang.craftingFailed.Show(blueprint.itemResult.name, blueprint.itemResult.description);

                    if (OnCraftFailed != null)
                    {
                        OnCraftFailed(category, blueprint);
                    }
                }

                amount--;

                if (amount > 0)
                {
                    if (blueprintCraftAmountInput != null)
                    {
                        blueprintCraftAmountInput.text = amount.ToString();
                    }

                    activeCraft = _CraftItem(category, blueprint, amount, Mathf.Clamp(currentCraftTime / blueprint.craftingTimeSpeedupFactor, 0.0f, blueprint.craftingTimeDuration));
                    StartCoroutine(activeCraft);
                }
                else
                {
                    activeCraft = null; // All done
                }
            }
            else
            {
                //StopCoroutine(activeCraft);
                activeCraft = null;
            }
        }
Beispiel #5
0
        private IEnumerator _CraftItem(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, int amount, float currentCraftTime)
        {
            bool canCraft = CanCraftBlueprint(blueprint, category.alsoScanBankForRequiredItems, amount);

            if (canCraft)
            {
                float counter = currentCraftTime;
                while (true)
                {
                    yield return(new WaitForSeconds(Time.deltaTime)); // Update loop

                    counter -= Time.deltaTime;
                    NotifyCraftProgress(category, blueprint, 1.0f - Mathf.Clamp01(counter / currentCraftTime));

                    if (counter <= 0.0f)
                    {
                        break;
                    }
                }


                // Remove items from inventory
                uint[] keys = currentBlueprintItemsDict.Keys.ToArray();
                uint[] vals = currentBlueprintItemsDict.Values.ToArray();
                for (int i = 0; i < keys.Length; i++)
                {
                    InventoryManager.RemoveItem(keys[i], vals[i], category.alsoScanBankForRequiredItems); //  * GetCraftInputFieldAmount()
                }

                for (int i = 0; i < keys.Length; i++)
                {
                    uint c = InventoryManager.GetItemCount(keys.ElementAt(i), category.alsoScanBankForRequiredItems);
                    foreach (var item in items)
                    {
                        if (item.item != null && c == 0)
                        {
                            item.item = null;
                            item.Repaint();
                        }
                    }
                }


                // Remove gold
                InventoryManager.instance.inventory.gold -= blueprint.craftCostPrice;

                if (blueprint.successChanceFactor >= Random.value)
                {
                    // Store crafted item
                    var c = GameObject.Instantiate <InventoryItemBase>(blueprint.itemResult);
                    c.currentStackSize = (uint)(blueprint.itemResultCount); //  * GetCraftInputFieldAmount()
                    if (category.forceSaveInCollection != null)
                    {
                        category.forceSaveInCollection.AddItem(c);
                    }
                    else
                    {
                        InventoryManager.AddItem(c);
                    }

                    InventoryManager.instance.lang.craftedItem.Show(blueprint.itemResult.name, blueprint.itemResult.description);

                    if (OnCraftSuccess != null)
                    {
                        OnCraftSuccess(category, blueprint, c);
                    }
                }
                else
                {
                    InventoryManager.instance.lang.craftingFailed.Show(blueprint.itemResult.name, blueprint.itemResult.description);

                    if (OnCraftFailed != null)
                    {
                        OnCraftFailed(category, blueprint);
                    }
                }

                amount--;
                currentBlueprintItemsDict.Clear();

                if (amount > 0)
                {
                    activeCraft = _CraftItem(category, blueprint, amount, Mathf.Clamp(currentCraftTime / blueprint.craftingTimeSpeedupFactor, 0.0f, blueprint.craftingTimeDuration));
                    StartCoroutine(activeCraft);
                }
                else
                {
                    activeCraft = null; // All done
                }
            }
            else
            {
                //StopCoroutine(activeCraft);
                activeCraft = null;
            }
        }