public void RemoveItem(ItemClass item, uGUIMerchantSlot slot) { for (int i = 0; i < selectedMerchant.tabs.Count; i++) { for (int j = 0; j < selectedMerchant.tabs[i].items.Count; j++) { if (selectedTab.tabType == selectedMerchant.tabs[i].tabType) { if (selectedMerchant.tabs[i].items[j].itemName == slot.item.itemName) { selectedMerchant.tabs[i].items.Remove(selectedMerchant.tabs[i].items[j]); for (int k = 0; k < item.height; k++) { for (int l = 0; l < item.width; l++) { items[slot.itemStartNumber + merchantWidth * k + l].item = new ItemClass(); items[slot.itemStartNumber + merchantWidth * k + l].item.itemName = ""; items[slot.itemStartNumber + merchantWidth * k + l].GetComponent <Image>().color = Color.white; items[slot.itemStartNumber + merchantWidth * k + l].transform.Find("ItemIcon").gameObject.SetActive(false); items[slot.itemStartNumber + merchantWidth * k + l].transform.Find("ItemFrame").gameObject.SetActive(false); } } return; } } } } }
//Called when a merchant slot is clicked public void OnMerchantSlotClick(GameObject obj, int mouseIndex) { uGUIMerchantSlot slot = obj.GetComponent <uGUIMerchantSlot>(); if (inventory.dragging) { int i = inventory.dragStartIndex; inventory.ReturnDraggedItem(); inventory.SellItem(inventory.items[i]); return; } //Currently nothing happens when left clicking on a slot if (mouseIndex == 0) { if (slot.item.itemName != "") { draggedItem = obj.GetComponent <uGUIMerchantSlot>(); draggedItem.item.stackSize = 1; dragging = true; inventory.dragItem.sprite = draggedItem.item.icon; inventory.dragItem.gameObject.SetActive(true); inventory.dragItem.GetComponent <RectTransform>().sizeDelta = new Vector2(draggedItem.item.width * iconSlotSize, draggedItem.item.height * iconSlotSize); } else { } } //If the slot is right clicked then buy the item else if (mouseIndex == 1) { if (slot.item.itemName != "") { bool succes; string failMessage; AudioClip sound; BuyItem(items[slot.itemStartNumber], out succes, out failMessage, out sound); if (!succes) { messageManager.AddMessage(Color.red, failMessage); } else { transform.root.GetComponent <AudioSource>().PlayOneShot(sound); } } } }
//Reset the items in the slots public void ResetItems() { for (int i = 0; i < items.Count; i++) { Destroy(items[i].gameObject); } items = new List <uGUIMerchantSlot>(); for (int i = 0; i < merchantWidth * merchantHeight; i++) { GameObject slot = Instantiate(slotPrefab) as GameObject; slot.transform.SetParent(merchantSlots); slot.name = i.ToString(); slot.transform.localScale = Vector3.one; uGUIMerchantSlot merchantSlot = slot.GetComponent <uGUIMerchantSlot>(); merchantSlot.item = new ItemClass(); merchantSlot.item.itemName = ""; items.Add(merchantSlot); } }
//Buy the item of the slot public void BuyItem(uGUIMerchantSlot slot, out bool succes, out string failMessage, out AudioClip sound) { ItemClass item = uGUIInventory.DeepCopy(slot.item); sound = item.itemSound; succes = false; failMessage = ""; //If the player has enough gold if (player.money >= slot.item.buyPrice) { //If the item is stackable then add the stackable item if (item.stackable) { item.stackSize = 1; if (inventory.AddStackableItem(item)) { player.money -= slot.item.buyPrice; if (removeStackableItemsWhenBought) { for (int i = 0; i < selectedMerchant.tabs.Count; i++) { for (int j = 0; j < selectedMerchant.tabs[i].items.Count; j++) { if (selectedMerchant.tabs[i].items[j] == slot.item) { selectedMerchant.tabs[i].items.Remove(slot.item); } } } for (int i = 0; i < item.height; i++) { for (int j = 0; j < item.width; j++) { items[slot.itemStartNumber + merchantWidth * i + j].item = new ItemClass(); items[slot.itemStartNumber + merchantWidth * i + j].item.itemName = ""; items[slot.itemStartNumber + merchantWidth * i + j].GetComponent <Image>().color = Color.white; items[slot.itemStartNumber + merchantWidth * i + j].transform.Find("ItemIcon").gameObject.SetActive(false); items[slot.itemStartNumber + merchantWidth * i + j].transform.Find("ItemFrame").gameObject.SetActive(false); } } } avaliableGoldText.text = player.money.ToString(); succes = true; failMessage = ""; return; } //There's no open slots in the inventory so display a fail message else { succes = false; failMessage = "I need more space!"; } } //Add the item to the inventory else if (inventory.AddItem(uGUIInventory.DeepCopy(slot.item))) { player.money -= slot.item.buyPrice; for (int i = 0; i < selectedMerchant.tabs.Count; i++) { for (int j = 0; j < selectedMerchant.tabs[i].items.Count; j++) { if (selectedTab.tabType == selectedMerchant.tabs[i].tabType) { if (selectedMerchant.tabs[i].items[j].itemName == slot.item.itemName) { selectedMerchant.tabs[i].items.Remove(selectedMerchant.tabs[i].items[j]); for (int k = 0; k < item.height; k++) { for (int l = 0; l < item.width; l++) { items[slot.itemStartNumber + merchantWidth * k + l].item = new ItemClass(); items[slot.itemStartNumber + merchantWidth * k + l].item.itemName = ""; items[slot.itemStartNumber + merchantWidth * k + l].GetComponent <Image>().color = Color.white; items[slot.itemStartNumber + merchantWidth * k + l].transform.Find("ItemIcon").gameObject.SetActive(false); items[slot.itemStartNumber + merchantWidth * k + l].transform.Find("ItemFrame").gameObject.SetActive(false); } } avaliableGoldText.text = player.money.ToString(); succes = true; failMessage = ""; return; } } } } } //There's no open slots in the inventory so display a fail message else { succes = false; failMessage = "I need more space!"; } ResetTabs(); } //The player doesn't have enough gold to purchase the item else { //Display a fail message succes = false; failMessage = "Not enough gold!"; } }