//public AnimationClip successAnimation; //public AnimationClip failedAnimation; //public AnimationClip canceledAnimation; #region Notifies public virtual void NotifyCraftSuccess(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, InventoryItemBase result) { InventoryManager.instance.lang.craftedItem.Show(blueprint.itemResult.name, blueprint.itemResult.description); if (successCraftItem != null) InventoryUtility.AudioPlayOneShot(successCraftItem); if (OnCraftSuccess != null) OnCraftSuccess(category, blueprint, result); }
public void OnCraftFailed(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint) { if (onCraftingFailed.Count > 0) { RunEvents(onCraftingFailed, new plyEventArg("itemID", (int)blueprint.itemResult.ID), new plyEventArg("category", category), new plyEventArg("categoryID", (int)category.ID), new plyEventArg("blueprint", blueprint), new plyEventArg("blueprintID", (int)blueprint.ID)); } }
public void OnCraftProgress(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, float progress) { if (onCraftingProgress.Count > 0) { RunEvents(onCraftingProgress, new plyEventArg("itemID", (int)blueprint.itemResult.ID), new plyEventArg("category", category), new plyEventArg("categoryID", (int)category.ID), new plyEventArg("blueprint", blueprint), new plyEventArg("blueprintID", (int)blueprint.ID), new plyEventArg("progress", progress)); } }
public void OnCraftSuccess(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, InventoryItemBase result) { if (onCraftingSuccess.Count > 0) { RunEvents(onCraftingSuccess, new plyEventArg("item", result), new plyEventArg("itemID", (int)result.ID), new plyEventArg("category", category), new plyEventArg("categoryID", (int)category.ID), new plyEventArg("blueprint", blueprint), new plyEventArg("blueprintID", (int)blueprint.ID)); } }
protected virtual void RemoveRequiredCraftItems(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint) { // Remove items from inventory foreach (var item in blueprint.requiredItems) InventoryManager.RemoveItem(item.item.ID, (uint)item.amount, category.alsoScanBankForRequiredItems); // * GetCraftInputFieldAmount() // Remove gold InventoryManager.RemoveCurrency(blueprint.craftingCost); }
protected virtual IEnumerator _CraftItem(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, int amount, float currentCraftTime) { // Override me! return null; }
public virtual void NotifyCraftCanceled(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, float progress) { InventoryManager.instance.lang.craftingCanceled.Show(currentBlueprint.itemResult.name, currentBlueprint.itemResult.description); activeCraft = null; if (canceledCraftItem != null) InventoryUtility.AudioPlayOneShot(canceledCraftItem); if (OnCraftCanceled != null) OnCraftCanceled(currentCategory, currentBlueprint, currentCraftProgress); }
public void OnCraftFailed(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint) { if (eventHandler != null) eventHandler.OnCraftFailed(category, blueprint); }
/// <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 } }
public virtual void NotifyCraftFailed(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint) { InventoryManager.instance.lang.craftingFailed.Show(blueprint.itemResult.name, blueprint.itemResult.description); if (failedCraftItem != null) InventoryUtility.AudioPlayOneShot(failedCraftItem); if (OnCraftFailed != null) OnCraftFailed(category, blueprint); }
protected override void RemoveRequiredCraftItems(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint) { // 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() } // Remove gold InventoryManager.RemoveCurrency(blueprint.craftingCost); }
/// <summary> /// Called when an item is being crafted. /// </summary> /// <param name="progress"></param> public override void NotifyCraftProgress(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, float progress) { base.NotifyCraftProgress(category, blueprint, progress); if (blueprintCraftProgressSlider != null) blueprintCraftProgressSlider.value = progress; }
public virtual void SetCraftingCategory(InventoryCraftingCategory category) { currentCategory = category; CancelActiveCraft(); // Just in case #if UNITY_EDITOR if (currentCategory == null) { Debug.LogWarning("Received a null object when trying to set the crafting category.", transform); return; } #endif 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 } }
public void OnCraftCanceled(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, float progress) { if (eventHandler != null) eventHandler.OnCraftCanceled(category, blueprint, progress); }
protected virtual bool GiveCraftReward(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint) { if (blueprint.successChanceFactor >= UnityEngine.Random.value) { // Store crafted item var c = GameObject.Instantiate<InventoryItemBase>(blueprint.itemResult); c.currentStackSize = (uint)(blueprint.itemResultCount); // * GetCraftInputFieldAmount() if (category.forceSaveInCollection != null) { bool added = category.forceSaveInCollection.AddItem(c); if (added == false) return false; } else { bool added = InventoryManager.AddItem(c); if(added == false) return false; } NotifyCraftSuccess(category, blueprint, c); return true; } NotifyCraftFailed(category, blueprint); return false; }
protected override 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; } RemoveRequiredCraftItems(category, blueprint); GiveCraftReward(category, blueprint); amount--; currentBlueprintItemsDict.Clear(); ValidateReferences(); 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; } }
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(); }); } }
/// <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 }
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; } }
/// <summary> /// Crafts the item and triggers the coroutine method to handle the crafting itself. /// </summary> /// <param name="category"></param> /// <param name="blueprint"></param> /// <param name="amount"></param> /// <returns></returns> 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; }
public virtual void NotifyCraftProgress(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, float progress) { currentCraftProgress = progress; if (OnCraftProgress != null) OnCraftProgress(category, blueprint, progress); }
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; } }
public void OnCraftSuccess(InventoryCraftingCategory category, InventoryCraftingBlueprint blueprint, InventoryItemBase result) { if (eventHandler != null) eventHandler.OnCraftSuccess(category, blueprint, result); }