protected virtual bool CraftItem(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, int amount) { if (activeCraft != null) { return(false); // Already crafting } activeCraft = _CraftItem(category, blueprint, amount, blueprint.craftingTimeDuration); StartCoroutine(activeCraft); return(true); }
/// <summary> /// Called when an item is being crafted. /// </summary> /// <param name="progress"></param> protected virtual void NotifyCraftProgress(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, float progress) { currentCraftProgress = progress; if (blueprintCraftProgressSlider != null) { blueprintCraftProgressSlider.value = currentCraftProgress; } if (OnCraftProgress != null) { OnCraftProgress(category, blueprint, progress); } }
public virtual void SetCraftingCategory(InventoryCraftingCategory category) { currentCategory = category; CancelActiveCraft(); // Just in case if (category.cols * category.rows > items.Length) { AddSlots((uint)(category.cols * category.rows - items.Length)); // Increase } else if (category.cols * category.rows < items.Length) { RemoveSlots((uint)(items.Length - category.cols * category.rows)); // Decrease } }
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; } }
public virtual void SetCraftingCategory(InventoryCraftingCategory category) { categoryPool.DestroyAll(); blueprintPool.DestroyAll(); currentCategory = category; if (blueprintCraftAmountInput != null) { blueprintCraftAmountInput.text = "1"; // Reset } CancelActiveCraft(); // Just in case if (currentCategoryTitle != null) { currentCategoryTitle.text = currentCategory.name; } if (currentCategoryDescription != null) { currentCategoryDescription.text = currentCategory.description; } if (noBlueprintSelectedPage != null) { noBlueprintSelectedPage.Show(); } if (blueprintCraftPage == null && currentCategory.blueprints.Length > 0) { SetBlueprint(currentCategory.blueprints[0]); // Select first blueprint } int lastItemCategory = -1; foreach (var b in currentCategory.blueprints) { if (b.playerLearnedBlueprint == false) { continue; } var blue = blueprintPool.Get(); blue.transform.SetParent(blueprintsContainer); blue.Set(b); if (blueprintCategoryPrefab != null) { if (lastItemCategory != b.itemResult.category.ID) { lastItemCategory = (int)b.itemResult.category.ID; var uiCategory = categoryPool.Get(); uiCategory.Set(b.itemResult.category.name); uiCategory.transform.SetParent(blueprintsContainer); blue.transform.SetParent(uiCategory.container); } } var bTemp = b; // Store capture list, etc. blue.button.onClick.AddListener(() => { currentBlueprint = bTemp; SetBlueprint(currentBlueprint); CancelActiveCraft(); // Just in case if (blueprintCraftPage != null && blueprintCraftPage.isVisible == false) { blueprintCraftPage.Show(); } }); } }
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 }