using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.UI; using RPGCreationKit; using System.Linq; using RPGCreationKit.Player; using UnityEngine.EventSystems; namespace RPGCreationKit { public enum ConfirmTradeAction { BuyFromMerchant = 0, SellToMerchant = 1, TakeFromNPC = 2, GiveToNPC = 3 } public enum GoldsSounds { Spend, Gain }; public class TradeSystemUI : MonoBehaviour { public static TradeSystemUI instance; private void Awake() { if (instance == null) instance = this; else { Debug.Log("Anomaly detected with the singleton pattern of 'LootingInventoryUI', do you have more than one LootingInventoryUI?"); Destroy(this); } } public Inventory inventoryToDisplay; // Is this a merchant or a Companion? public bool isMerchant = false; public Merchant curMerchant; public bool isBuyingOrDrawing = true; public ItemsInListPoolManager itemsPoolManager; public InventoryTabs selectedTab; [Header("UI Elements")] public GameObject[] UIToDisable; [Space(5)] public GameObject UI; public Transform scrollView; public Toggle defaultToggle; public DropItemsPanel takeDepositItemsPanel; public ConfirmTradePanel confirmTradePanel; public TextMeshProUGUI currentWeightText; public TextMeshProUGUI maxWeightText; public GameObject DialogueUI; [Space(5)] public TextMeshProUGUI merchantName; public TextMeshProUGUI playerGolds; public TextMeshProUGUI merchantGolds; public bool isOpen = false; [Header("Sounds")] public AudioClip goldSpent; public AudioClip goldGained; [SerializeField] private Transform inventoryItemsContent; public void OpenTradePanel(Merchant _merchant, bool _referenceMerchantInventory = true) { isMerchant = true; curMerchant = _merchant; if (_referenceMerchantInventory) inventoryToDisplay = _merchant.inventory; //else // get the inventory of the npc we want to exchange stuff itemsPoolManager.inventoryToPool = inventoryToDisplay; // Open isOpen = true; itemsPoolManager.PopulateAll(true); RckPlayer.instance.input.SwitchCurrentActionMap("TradeUI"); OpenTradeUI(); } /* public void OpenTradePanel(NPC_Behaviour _npc, bool _referenceNPCInventory = true) { isMerchant = true; // TO CHANGE: This should be called for friendly npc that doesn't sell but just carry your burdens curNPC = _npc; curMerchant = curNPC.GetComponent(); // Open isOpen = true; itemsPoolManager.PopulateAll(); if (_referenceNPCInventory) inventoryToDisplay = _npc.GetComponent(); OpenTradeUI(); } */ private void OpenTradeUI() { itemsPoolManager.inventoryToPool = inventoryToDisplay; UpdateStatsUI(); ShowTabItems(); UI.SetActive(true); foreach (GameObject go in UIToDisable) go.SetActive(false); if (RckPlayer.instance.isInConversation) DialogueUI.SetActive(false); } public void SwitchInventory(bool _isPlayerInventory) { itemsPoolManager.ResetPools(); isBuyingOrDrawing = !_isPlayerInventory; if (_isPlayerInventory) { inventoryToDisplay = Inventory.PlayerInventory; itemsPoolManager.inventoryToPool = inventoryToDisplay; if (isMerchant) OpenTradePanel(curMerchant, false); //else // OpenTradePanel(curNPC, false); } else { if (isMerchant) inventoryToDisplay = curMerchant.inventory; itemsPoolManager.inventoryToPool = inventoryToDisplay; if (isMerchant) OpenTradePanel(curMerchant, false); //else // OpenTradePanel(curNPC, false); } } public void ChangeTab(InventoryTabs tab) { selectedTab = tab; /// Show Tab ShowTabItems(); } public void ShowTabItems() { switch (selectedTab) { case InventoryTabs.All: if (isBuyingOrDrawing) itemsPoolManager.ShowAll(); else itemsPoolManager.MerchantTradeShowAll(curMerchant.WantsToBuy); break; case InventoryTabs.Armors: if (isBuyingOrDrawing) itemsPoolManager.ArmorsTab(); else itemsPoolManager.MerchantTradeArmorsTab(curMerchant.WantsToBuy); break; case InventoryTabs.Keys: if (isBuyingOrDrawing) itemsPoolManager.KeysTab(); else itemsPoolManager.MerchantTradeKeysTab(curMerchant.WantsToBuy); break; case InventoryTabs.MiscItems: if (isBuyingOrDrawing) itemsPoolManager.MiscTab(); else itemsPoolManager.MerchantTradeMiscTab(curMerchant.WantsToBuy); break; case InventoryTabs.Weapons: if (isBuyingOrDrawing) itemsPoolManager.WeaponsTab(); else itemsPoolManager.MerchantTradeWeaponsTab(curMerchant.WantsToBuy); break; } SelectFirstButton(); } public void UpdateStatsUI() { currentWeightText.text = ((int)Inventory.PlayerInventory.CurTotalWeight).ToString(); merchantName.text = curMerchant.merchantName; playerGolds.text = RCKFunctions.GetItemCount(Inventory.PlayerInventory, "Gold001").ToString(); merchantGolds.text = RCKFunctions.GetItemCount(curMerchant.inventory, "Gold001").ToString(); } public void CloseUI() { itemsPoolManager.ResetPools(); defaultToggle.isOn = true; UI.SetActive(false); foreach (GameObject go in UIToDisable) go.SetActive(true); isOpen = false; confirmTradePanel.CancelButton(); RckPlayer.instance.input.SwitchCurrentActionMap("Player"); if(RckPlayer.instance.isInConversation) StartCoroutine(SelectDialogueOption()); if (RckPlayer.instance.isInConversation) DialogueUI.SetActive(true); if (RckInput.isUsingGamepad) RckPlayer.instance.uiManager.PreventUnselectedQuestionWithGamepad(); } IEnumerator SelectDialogueOption() { yield return new WaitForEndOfFrame(); RckPlayer.instance.input.SwitchCurrentActionMap("DialogueUI"); RckPlayer.instance.uiManager.playerQuestionsContent.GetComponentInChildren