/// <summary>
        /// Sell item now to this vendor. The vendor doesn't sell the object, the user sells to this vendor.
        /// Note that this does not show any UI or warnings and immediately handles the action.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public virtual bool SellItemToVendorNow(InventoryItemBase item, uint amount)
        {
            if (CanSellItemToVendor(item, amount) == false)
            {
                return(false);
            }

            if (enableBuyBack)
            {
                var copy = GameObject.Instantiate <InventoryItemBase>(item);
                copy.currentStackSize = (uint)amount;
                copy.transform.SetParent(buyBackParent.transform);

                if (buyBackIsShared)
                {
                    if (buyBackDict.ContainsKey(vendorCategory) == false)
                    {
                        buyBackDict.Add(vendorCategory, new List <InventoryItemBase>());
                    }

                    buyBackDict[vendorCategory].Add(copy);
                    if (buyBackDict[vendorCategory].Count > maxBuyBackItemSlotsCount)
                    {
                        Destroy(buyBackDict[vendorCategory][0].gameObject);
                        buyBackDict[vendorCategory].RemoveAt(0);
                    }
                }
                else
                {
                    // Not shared drop in object array
                    buyBackList.Add(copy);
                    if (buyBackList.Count > maxBuyBackItemSlotsCount)
                    {
                        Destroy(buyBackList[0].gameObject);
                        buyBackList.RemoveAt(0);
                    }
                }
            }

            InventoryManager.instance.inventory.gold += GetSellPrice(item, (uint)amount);
            InventoryManager.RemoveItem(item.ID, amount, false);

            vendorUI.NotifyItemSoldToVendor(item, amount);
            return(true);
        }
        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;
            }
        }
Esempio n. 3
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;
            }
        }