using Kryz.CharacterStats.Examples; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class CraftingUI : MonoBehaviour { public GameObject craftingUI; [Header("Components:")] [SerializeField] private Button craftButton; [SerializeField] private TMP_Text itemSlotName; [SerializeField] private TMP_Text stoneSlotName; [SerializeField] private TMP_Text added_Total_UniqueStats; [SerializeField] private GameObject maxUniqueStatsExceededText; [Header("Stone Texts:")] [SerializeField] private StatElement flat_atk; [SerializeField] private StatElement flat_spell; [SerializeField] private StatElement flat_critchance; [SerializeField] private StatElement flat_critdamage; [SerializeField] private StatElement percent_atk; [SerializeField] private StatElement percent_spell; [SerializeField] private StatElement percent_critchance; [SerializeField] private StatElement percent_critdamage; [SerializeField] private StatElement flat_health; [SerializeField] private StatElement flat_armor; [SerializeField] private StatElement flat_magicresistance; [SerializeField] private StatElement percent_health; [SerializeField] private StatElement percent_armor; [SerializeField] private StatElement percent_magicresistance; [Space] [Header("Events:")] [SerializeField] private GameEvent onCraftingUIClosed; [SerializeField] private GameEvent onVendorReleased; [SerializeField] private GameEvent onItemCraftSuccess; [SerializeField] private GameEvent onItemCraftFailed; [Header("Listeners:")] [SerializeField] private GameEventListener onCraftingAnvilInteracted; [SerializeField] private GameEventListener onCraftingAnvilReleased; [Header("Runtime:")] public EquippableItemInstance currentlySelectedCraftingItem; public CraftingSlot craftingSlot; public CraftingStatStone currentlySelectedCraftingStatStone; public CraftingStoneSlot craftingStoneSlot; private void Awake() { onCraftingAnvilInteracted.Response.AddListener(OpenCrafting); onCraftingAnvilReleased.Response.AddListener(CloseCrafting); Inventory.Instance.OnItemRightClickedEventWhileCrafting.AddListener(AddToCrafting); craftingSlot.OnRightClickEvent.AddListener(RemoveFromCrafting); craftingStoneSlot.OnRightClickEvent.AddListener(RemoveFromStoneSlot); craftButton.onClick.AddListener(PerformCraft); InitializeCraftingUI(); craftingUI.SetActive(false); } private void InitializeCraftingUI() { if (currentlySelectedCraftingItem == null || string.IsNullOrEmpty(currentlySelectedCraftingItem.ItemName)) { itemSlotName.text = "Add a Modular Item to craft on"; } else { itemSlotName.text = currentlySelectedCraftingItem.ItemName; } if(currentlySelectedCraftingStatStone == null || string.IsNullOrEmpty(currentlySelectedCraftingStatStone.ItemName)) { stoneSlotName.text = "Add a Crafting Stat Stone to use"; } else { stoneSlotName.text = currentlySelectedCraftingStatStone.ItemName; } if (craftingStoneSlot.Item != currentlySelectedCraftingStatStone && currentlySelectedCraftingStatStone != null) { craftingStoneSlot.Item = currentlySelectedCraftingStatStone; stoneSlotName.text = currentlySelectedCraftingStatStone.ItemName; } //Add currently selected crafting item to the crafting item slot if not already present if (craftingSlot.Item != currentlySelectedCraftingItem && currentlySelectedCraftingItem != null) { craftingSlot.Item = currentlySelectedCraftingItem; itemSlotName.text = currentlySelectedCraftingItem.ItemName; } UpdateStatTexts(); ToggleCraftButtonInteractable(); } private void ToggleCraftButtonInteractable() { craftButton.interactable = craftingSlot.Item != null && !string.IsNullOrEmpty(craftingSlot.Item.ItemName) && craftingStoneSlot.Item != null && !string.IsNullOrEmpty(craftingStoneSlot.Item.ItemName); if (craftingSlot.Item == null || string.IsNullOrEmpty(craftingSlot.Item.ItemName) || craftingStoneSlot.Item == null || string.IsNullOrEmpty(craftingStoneSlot.Item.ItemName)) return; craftButton.interactable = currentlySelectedCraftingItem.CanAddCraftingStone(currentlySelectedCraftingStatStone); } public void OpenCrafting() { InitializeCraftingUI(); UpdateStatTexts(); craftingUI.SetActive(true); Inventory.Instance.isVendoring = false; Inventory.Instance.isCrafting = true; Inventory.Instance.OpenInventory(); //Close remaining vendors in case there is one open to avoid conflicts onVendorReleased.Raise(); ToggleCraftButtonInteractable(); } public void CloseCrafting() { craftingUI.SetActive(false); Inventory.Instance.isCrafting = false; Inventory.Instance.CloseInventory(); onCraftingUIClosed.Raise(); } public void PerformCraft() { if (craftingSlot.Item == null || string.IsNullOrEmpty(craftingSlot.Item.ItemName) || craftingStoneSlot.Item == null || string.IsNullOrEmpty(craftingStoneSlot.Item.ItemName)) return; EquippableItemInstance modularItem = craftingSlot.Item as EquippableItemInstance; CraftingStatStone stone = craftingStoneSlot.Item as CraftingStatStone; if (modularItem.TryAddCraftingStone(stone)) { craftingStoneSlot.Item = null; currentlySelectedCraftingStatStone = null; InitializeCraftingUI(); onItemCraftSuccess.Raise(); } else { onItemCraftFailed.Raise(); } ToggleCraftButtonInteractable(); } private void AddToCrafting(ItemInstance item) { if (item is CraftingStatStone craftingStone) { SlotCraftingStone(craftingStone); } else if (item is EquippableItemInstance equippable) { if (equippable.CraftableBase) SlotItem(equippable); } ToggleCraftButtonInteractable(); } private void RemoveFromCrafting(ItemInstance item) { if (!Inventory.Instance.IsFull()) { Inventory.Instance.AddItem(item); currentlySelectedCraftingItem = null; craftingSlot.Item = null; InitializeCraftingUI(); } } private void RemoveFromStoneSlot(ItemInstance item) { if (!Inventory.Instance.IsFull()) { Inventory.Instance.AddItem(item); currentlySelectedCraftingStatStone = null; craftingStoneSlot.Item = null; InitializeCraftingUI(); } } public void SlotCraftingStone(CraftingStatStone stone) { if (Inventory.Instance.RemoveItem(stone)) { ItemTooltip.Instance.HideTooltip(); EquippedItemTooltip.Instance.HideTooltip(); CraftingStatStone previousItem; if (AddStone(stone, out previousItem)) { if (previousItem != null && !string.IsNullOrEmpty(previousItem.ItemName)) { Inventory.Instance.AddItem(previousItem); //previousItem.Unequip(this); //statPanel.UpdateStatValues(); } } else { Inventory.Instance.AddItem(stone); } } } public void SlotItem(EquippableItemInstance item) { if (Inventory.Instance.RemoveItem(item)) { ItemTooltip.Instance.HideTooltip(); EquippedItemTooltip.Instance.HideTooltip(); EquippableItemInstance previousItem; if (AddItem(item, out previousItem)) { if (previousItem != null && !string.IsNullOrEmpty(previousItem.ItemName)) { Inventory.Instance.AddItem(previousItem); //previousItem.Unequip(this); //statPanel.UpdateStatValues(); } } else { Inventory.Instance.AddItem(item); } } } public bool AddItem(EquippableItemInstance item, out EquippableItemInstance previousItem) { previousItem = currentlySelectedCraftingItem; currentlySelectedCraftingItem = item; craftingSlot.Item = currentlySelectedCraftingItem; itemSlotName.text = currentlySelectedCraftingItem.ItemName; UpdateStatTexts(); //PlayerDataHandler.Instance.SaveCharacterInventoryData(PlayerDataHandler.Instance.currentPlayerName.Value, PhotonNetwork.NickName, equipmentData); //TODO: SAVE Crafting Slot Data return true; } public bool AddStone(CraftingStatStone stone, out CraftingStatStone previousStone) { previousStone = currentlySelectedCraftingStatStone; currentlySelectedCraftingStatStone = stone; craftingStoneSlot.Item = currentlySelectedCraftingStatStone; stoneSlotName.text = currentlySelectedCraftingStatStone.ItemName; UpdateStatTexts(); //PlayerDataHandler.Instance.SaveCharacterInventoryData(PlayerDataHandler.Instance.currentPlayerName.Value, PhotonNetwork.NickName, equipmentData); //TODO: SAVE Crafting Slot Data return true; } List stoneStatsList = new List(); private void UpdateStatTexts() { flat_atk.gameObject.SetActive(false); flat_spell.gameObject.SetActive(false); flat_critchance.gameObject.SetActive(false); flat_critdamage.gameObject.SetActive(false); percent_atk.gameObject.SetActive(false); percent_spell.gameObject.SetActive(false); percent_critchance.gameObject.SetActive(false); percent_critdamage.gameObject.SetActive(false); flat_health.gameObject.SetActive(false); flat_armor.gameObject.SetActive(false); flat_magicresistance.gameObject.SetActive(false); percent_health.gameObject.SetActive(false); percent_armor.gameObject.SetActive(false); percent_magicresistance.gameObject.SetActive(false); added_Total_UniqueStats.text = ""; if (currentlySelectedCraftingItem == null || string.IsNullOrEmpty(currentlySelectedCraftingItem.ItemName)) return; added_Total_UniqueStats.text = "Total Unique Stats Added through crafting: " + (currentlySelectedCraftingItem).AddedStoneStats.Count.ToString() + " / " + (currentlySelectedCraftingItem).MaxTotalUniqueStatsIncreasedByStones.ToString(); for (int i = 0; i < ((EquippableItemInstance)currentlySelectedCraftingItem).AddedStoneStats.Count; i++) { switch (((EquippableItemInstance)currentlySelectedCraftingItem).AddedStoneStats[i]) { case "AttackDamageBonus": { flat_atk.gameObject.SetActive(true); flat_atk.statValue.text = "+"; } break; case "SpellDamageBonus": { flat_spell.gameObject.SetActive(true); flat_spell.statValue.text = "+"; } break; case "CritChanceBonus": { flat_critchance.gameObject.SetActive(true); flat_critchance.statValue.text = "+"; } break; case "CritDamageBonus": { flat_critdamage.gameObject.SetActive(true); flat_critdamage.statValue.text = "+"; } break; case "MaxHealthBonus": { flat_health.gameObject.SetActive(true); flat_health.statValue.text = "+"; } break; case "ArmorBonus": { flat_armor.gameObject.SetActive(true); flat_armor.statValue.text = "+"; } break; case "MagicResistanceBonus": { flat_magicresistance.gameObject.SetActive(true); flat_magicresistance.statValue.text = "+"; } break; case "AttackDamagePercentBonus": { percent_atk.gameObject.SetActive(true); percent_atk.statValue.text = "+"; } break; case "SpellDamagePercentBonus": { percent_spell.gameObject.SetActive(true); percent_spell.statValue.text = "+"; } break; case "CritChancePercentBonus": { percent_critchance.gameObject.SetActive(true); percent_critchance.statValue.text = "+"; } break; case "CritDamagePercentBonus": { percent_critdamage.gameObject.SetActive(true); percent_critdamage.statValue.text = "+"; } break; case "MaxHealthPercentBonus": { percent_health.gameObject.SetActive(true); percent_health.statValue.text = "+"; } break; case "ArmorPercentBonus": { percent_armor.gameObject.SetActive(true); percent_armor.statValue.text = "+"; } break; case "MagicResistancePercentBonus": { percent_magicresistance.gameObject.SetActive(true); percent_magicresistance.statValue.text = "+"; } break; default: break; } } if (currentlySelectedCraftingStatStone == null || string.IsNullOrEmpty(currentlySelectedCraftingStatStone.ItemName)) return; stoneStatsList = currentlySelectedCraftingStatStone.GetNonZeroStats(); maxUniqueStatsExceededText.SetActive(!currentlySelectedCraftingItem.CanAddCraftingStone(currentlySelectedCraftingStatStone)); for (int i = 0; i < stoneStatsList.Count; i++) { switch (stoneStatsList[i]) { case "AttackDamageBonus": { flat_atk.gameObject.SetActive(true); flat_atk.statValue.text = "+" + currentlySelectedCraftingStatStone.AttackDamageBonus; } break; case "SpellDamageBonus": { flat_spell.gameObject.SetActive(true); flat_spell.statValue.text = "+" + currentlySelectedCraftingStatStone.SpellDamageBonus; } break; case "CritChanceBonus": { flat_critchance.gameObject.SetActive(true); flat_critchance.statValue.text = "+" + currentlySelectedCraftingStatStone.CritChanceBonus; } break; case "CritDamageBonus": { flat_critdamage.gameObject.SetActive(true); flat_critdamage.statValue.text = "+" + currentlySelectedCraftingStatStone.CritDamageBonus; } break; case "MaxHealthBonus": { flat_health.gameObject.SetActive(true); flat_health.statValue.text = "+" + currentlySelectedCraftingStatStone.MaxHealthBonus; } break; case "ArmorBonus": { flat_armor.gameObject.SetActive(true); flat_armor.statValue.text = "+" + currentlySelectedCraftingStatStone.ArmorBonus; } break; case "MagicResistanceBonus": { flat_magicresistance.gameObject.SetActive(true); flat_magicresistance.statValue.text = "+" + currentlySelectedCraftingStatStone.MagicResistanceBonus; } break; case "AttackDamagePercentBonus": { percent_atk.gameObject.SetActive(true); percent_atk.statValue.text = "+" + currentlySelectedCraftingStatStone.AttackDamagePercentBonus; } break; case "SpellDamagePercentBonus": { percent_spell.gameObject.SetActive(true); percent_spell.statValue.text = "+" + currentlySelectedCraftingStatStone.SpellDamagePercentBonus; } break; case "CritChancePercentBonus": { percent_critchance.gameObject.SetActive(true); percent_critchance.statValue.text = "+" + currentlySelectedCraftingStatStone.CritChancePercentBonus; } break; case "CritDamagePercentBonus": { percent_critdamage.gameObject.SetActive(true); percent_critdamage.statValue.text = "+" + currentlySelectedCraftingStatStone.CritDamagePercentBonus; } break; case "MaxHealthPercentBonus": { percent_health.gameObject.SetActive(true); percent_health.statValue.text = "+" + currentlySelectedCraftingStatStone.MaxHealthPercentBonus; } break; case "ArmorPercentBonus": { percent_armor.gameObject.SetActive(true); percent_armor.statValue.text = "+" + currentlySelectedCraftingStatStone.ArmorPercentBonus; } break; case "MagicResistancePercentBonus": { percent_magicresistance.gameObject.SetActive(true); percent_magicresistance.statValue.text = "+" + currentlySelectedCraftingStatStone.MagicResistancePercentBonus; } break; default: break; } } } }