public void BuyItem(int index, int amount, NpcTrading npc) { if (health.current > 0 && 0 <= index && index < npc.saleItems.Length) { // valid amount? Item item = new Item(npc.saleItems[index]); if (1 <= amount && amount <= item.maxStack) { long price = item.buyPrice * amount; // player has enough gold and enough space in inventory? if (inventory.gold >= price && inventory.CanAdd(item, amount)) { // pay for it, add to inventory inventory.gold -= price; inventory.Add(item, amount); } } } }
public void SellItem(int index, int amount, NpcTrading npc) { if (health.current > 0 && 0 <= index && index < inventory.slots.Count) { // sellable? ItemSlot slot = inventory.slots[index]; if (slot.amount > 0 && slot.item.sellable) { // valid amount? if (1 <= amount && amount <= slot.amount) { // sell the amount long price = slot.item.sellPrice * amount; inventory.gold += price; slot.DecreaseAmount(amount); inventory.slots[index] = slot; } } } }
void Update() { GameObject player = Player.player; if (!player) { return; } PlayerInteraction interaction = player.GetComponent <PlayerInteraction>(); PlayerInventory inventory = player.GetComponent <PlayerInventory>(); PlayerNpcTrading trading = player.GetComponent <PlayerNpcTrading>(); if (interaction.current != null && ((MonoBehaviour)interaction.current).GetComponent <NpcTrading>() != null) { panel.SetActive(true); NpcTrading npc = ((MonoBehaviour)interaction.current).GetComponent <NpcTrading>(); // items for sale UIUtils.BalancePrefabs(slotPrefab.gameObject, npc.saleItems.Length, content); for (int i = 0; i < npc.saleItems.Length; ++i) { UINpcTradingSlot slot = content.GetChild(i).GetComponent <UINpcTradingSlot>(); ScriptableItem itemData = npc.saleItems[i]; // show item in UI int icopy = i; slot.button.onClick.SetListener(() => { buyIndex = icopy; }); slot.image.color = Color.white; slot.image.sprite = itemData.image; slot.tooltip.enabled = true; slot.tooltip.text = new ItemSlot(new Item(itemData)).ToolTip(); // with slot for {AMOUNT} } // buy if (buyIndex != -1 && buyIndex < npc.saleItems.Length) { ScriptableItem itemData = npc.saleItems[buyIndex]; // make valid amount, calculate price int amount = buyAmountInput.text.ToInt(); amount = Mathf.Clamp(amount, 1, itemData.maxStack); long price = amount * itemData.buyPrice; // show buy panel with item in UI buyAmountInput.text = amount.ToString(); buySlot.GetComponent <Image>().color = Color.white; buySlot.GetComponent <Image>().sprite = itemData.image; buySlot.GetComponent <UIShowToolTip>().enabled = true; buySlot.GetComponent <UIShowToolTip>().text = new Item(itemData).ToolTip(); buyCostsText.text = price.ToString(); buyButton.interactable = amount > 0 && price <= inventory.gold && inventory.CanAdd(new Item(itemData), amount); buyButton.onClick.SetListener(() => { trading.BuyItem(buyIndex, amount, npc); buyIndex = -1; buyAmountInput.text = "1"; }); } else { // show default buy panel in UI buySlot.GetComponent <Image>().color = Color.clear; buySlot.GetComponent <Image>().sprite = null; buySlot.GetComponent <UIShowToolTip>().enabled = false; buyCostsText.text = "0"; buyButton.interactable = false; } // sell if (sellIndex != -1 && sellIndex < inventory.slots.Count && inventory.slots[sellIndex].amount > 0) { ItemSlot itemSlot = inventory.slots[sellIndex]; // make valid amount, calculate price int amount = sellAmountInput.text.ToInt(); amount = Mathf.Clamp(amount, 1, itemSlot.amount); long price = amount * itemSlot.item.sellPrice; // show sell panel with item in UI sellAmountInput.text = amount.ToString(); sellSlot.GetComponent <Image>().color = Color.white; sellSlot.GetComponent <Image>().sprite = itemSlot.item.image; sellSlot.GetComponent <UIShowToolTip>().enabled = true; sellSlot.GetComponent <UIShowToolTip>().text = itemSlot.ToolTip(); sellCostsText.text = price.ToString(); sellButton.interactable = amount > 0; sellButton.onClick.SetListener(() => { trading.SellItem(sellIndex, amount, npc); sellIndex = -1; sellAmountInput.text = "1"; }); } else { // show default sell panel in UI sellSlot.GetComponent <Image>().color = Color.clear; sellSlot.GetComponent <Image>().sprite = null; sellSlot.GetComponent <UIShowToolTip>().enabled = false; sellCostsText.text = "0"; sellButton.interactable = false; } } else { panel.SetActive(false); } }