/// <summary> /// Sell an item to this vendor. /// </summary> /// <param name="item"></param> /// <param name="amount"></param> public virtual void SellItemToVendor(InventoryItemBase item) { uint max = InventoryManager.GetItemCount(item.ID, false); if (CanSellItemToVendor(item, max) == false) { InventoryManager.instance.lang.vendorCannotSellItem.Show(item.name, item.description, max); return; } InventorySettingsManager.instance.buySellDialog.ShowDialog("Sell " + name, "Are you sure you want to sell " + name, "Sell", "Cancel", 1, (int)max, item, ItemBuySellDialogAction.Selling, this, (amount) => { // Sell items SellItemToVendorNow(item, (uint)amount); }, (amount) => { // Canceled }); }
/// <summary> /// Does the inventory contain the required items? /// </summary> /// <param name="blueprint"></param> /// <param name="alsoScanBank"></param> /// <param name="craftCount"></param> /// <returns></returns> public virtual bool CanCraftBlueprint(InventoryCraftingBlueprint blueprint, bool alsoScanBank, int craftCount) { foreach (var item in blueprint.requiredItems) { uint count = InventoryManager.GetItemCount(item.item.ID, alsoScanBank); if (count < item.amount * craftCount) { InventoryManager.instance.lang.craftingDontHaveRequiredItems.Show(item.item.name, item.item.description, blueprint.name); return(false); } } if (InventoryManager.instance.inventory.gold < blueprint.craftCostPrice * craftCount) { InventoryManager.instance.lang.userNotEnoughGold.Show(blueprint.itemResult.name, blueprint.itemResult.description, craftCount, blueprint.craftCostPrice * craftCount, InventoryManager.instance.inventory.gold); return(false); } // Can the items be stored in the inventory / designated spot? if (currentCategory.forceSaveInCollection != null) { bool added = currentCategory.forceSaveInCollection.CanAddItem(blueprint.itemResult); if (added == false) { return(false); } } else { bool added = InventoryManager.CanAddItem(blueprint.itemResult); if (added == false) { return(false); } } return(true); }
public override void Repaint() { base.Repaint(); if (item != null) { uint count = InventoryManager.GetItemCount(item.ID, false); amountText.text = count.ToString(); if (count == 0) { icon.material = InventoryManager.instance.skillbar.grayMaterial; } else { icon.material = InventoryManager.instance.skillbar.defaultMaterial; } } else { amountText.text = string.Empty; icon.material = InventoryManager.instance.skillbar.defaultMaterial; } }
protected virtual void SetBlueprint(InventoryCraftingBlueprint blueprint) { if (window.isVisible == false) { return; } // Set all the details for the blueprint. if (blueprintTitle != null) { blueprintTitle.text = blueprint.name; } if (blueprintDescription != null) { blueprintDescription.text = blueprint.description; } if (blueprintIcon != null) { blueprintIcon.item = blueprint.itemResult; blueprintIcon.item.currentStackSize = (uint)blueprint.itemResultCount; blueprintIcon.Repaint(); blueprintIcon.item.currentStackSize = 1; // Reset } if (blueprintCraftProgressSlider) { blueprintCraftProgressSlider.value = 0.0f; // Reset } if (blueprintCraftCostText != null) { if (InventoryManager.instance.inventory.gold < blueprint.craftCostPrice) { blueprintCraftCostText.color = itemsNotAvailableColor; } else { blueprintCraftCostText.color = itemsAvailableColor; } blueprintCraftCostText.text = InventorySettingsManager.instance.currencyFormatter.Format(blueprint.craftCostPrice); } blueprintRequiredItemsPool.DestroyAll(); foreach (var item in blueprint.requiredItems) { var ui = blueprintRequiredItemsPool.Get(); item.item.currentStackSize = (uint)item.amount; ui.transform.SetParent(blueprintRequiredItemsContainer); ui.item = item.item; if (InventoryManager.GetItemCount(item.item.ID, currentCategory.alsoScanBankForRequiredItems) >= item.amount) { ui.icon.color = itemsAvailableColor; } else { ui.icon.color = itemsNotAvailableColor; } ui.Repaint(); item.item.currentStackSize = 1; // Reset } }
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; } }
/// <summary> /// Tries to find a blueprint based on the current layout / items inside the UI item wrappers (items). /// </summary> /// <param name="cat"></param> /// <returns>Returns blueprint if found one, null if not.</returns> public virtual InventoryCraftingBlueprint GetBlueprintFromCurrentLayout(InventoryCraftingCategory cat) { if (items.Length != cat.cols * cat.rows) { Debug.LogWarning("Updating blueprint but blueprint layout cols/rows don't match the collection"); } int totalItemCountInLayout = 0; // Nr of items inside the UI wrappers. foreach (var item in items) { if (item.item != null) { totalItemCountInLayout++; } } foreach (var b in cat.blueprints) { foreach (var a in b.blueprintLayouts) { if (a.enabled) { var hasItems = new Dictionary <uint, uint>(); // ItemID, amount //var requiredItems = new Dictionary<uint, uint>(); // ItemID, amount currentBlueprintItemsDict.Clear(); int counter = 0; // Item index counter int shouldHaveCount = 0; // Amount we should have.. int hasCount = 0; // How many slots in our layout bool shouldBreak = false; foreach (var r in a.rows) { if (shouldBreak) { break; } foreach (var c in r.columns) { if (shouldBreak) { break; } if (c.item != null && c.amount > 0) { if (currentBlueprintItemsDict.ContainsKey(c.item.ID) == false) { currentBlueprintItemsDict.Add(c.item.ID, 0); } currentBlueprintItemsDict[c.item.ID] += (uint)c.amount; shouldHaveCount++; if (items[counter].item != null) { if (items[counter].item.ID != c.item.ID) { shouldBreak = true; break; // Item in the wrong place... } if (hasItems.ContainsKey(c.item.ID) == false) { hasItems.Add(c.item.ID, InventoryManager.GetItemCount(c.item.ID, cat.alsoScanBankForRequiredItems)); } hasCount++; } else if (items[counter].item == null && c != null) { shouldBreak = true; break; } } counter++; } } if (shouldBreak) { break; // Onto the next one } // Filled slots test if (totalItemCountInLayout != hasCount || shouldHaveCount != hasCount) { break; } // Check count foreach (var item in currentBlueprintItemsDict) { if (hasItems.ContainsKey(item.Key) == false || hasItems[item.Key] < item.Value) { shouldBreak = true; } } if (shouldBreak) { break; } return(b); } } } return(null); // Nothing found }