public void BuyItem() { //Bad Practise I know! Player cache = GameObject.FindWithTag("Player").GetComponent <Player>(); if (cache.interactingWithUnit != null) { cache.self_UnitInventory.currencyAmount -= (int)(basePrice * cache.self_UnitInventory.buySellFactor); cache.interactingWithUnit.self_UnitInventory.currencyAmount += (int)(basePrice * cache.interactingWithUnit.self_UnitInventory.buySellFactor); Unit_InventoryReader.TradeItem(this, cache.interactingWithUnit.self_UnitInventory, cache.self_UnitInventory); cache.interactingWithUnit.self_NPCUI.isUiRefreshNeeded = true; cache.self_PlayerUI.isUiRefreshNeeded = true; Debug.Log("Buying!"); return; } }
public static void TradeItem(Unit_Item item, Unit_InventoryReader removeFromInv, Unit_InventoryReader addToInv) { Unit_Item tempItem = (Unit_Item)Resources.Load("Items/" + item.name); tempItem.name = item.name; tempItem.Initialize(item.item_Icon, item.item_Text, item.isWearable, item.basePrice); int invIndex = removeFromInv.inventory.FindIndex(i => i.name == item.name); int strIndex = removeFromInv.inventoryStracture.FindIndex(i => i.name == item.name); removeFromInv.inventory[invIndex].ResetItem(); removeFromInv.inventoryStracture.RemoveAt(strIndex); foreach (Unit_Item i in addToInv.inventory) { if (string.IsNullOrWhiteSpace(i.name)) { i.Initialize(tempItem.item_Icon, tempItem.item_Text, tempItem.isWearable, tempItem.basePrice); break; } } addToInv.inventoryStracture.Add((Unit_Item)Resources.Load("Items/" + tempItem.name)); }
public void Initialize(Unit_Item item) { Unit_InventoryReader cacheInventory = GameObject.FindWithTag("Player").GetComponent <Player>().self_UnitInventory; Unit_InventoryReader tempMainAsset; self_UnitItem = item; self_Icon.sprite = self_UnitItem.item_Icon; self_Text.text = self_UnitItem.item_Text; if (transform.root.GetComponentInChildren <NPC>() == null) { tempMainAsset = transform.root.GetComponentInChildren <Unit>().self_UnitInventory; if (cacheInventory == tempMainAsset) { self_ItemPrice.text = "+" + (item.basePrice / tempMainAsset.buySellFactor).ToString(); self_tradingButton.onClick.AddListener(self_UnitItem.SellItem); return; } } else { self_ItemPrice.text = "-" + (item.basePrice * cacheInventory.buySellFactor).ToString(); self_tradingButton.onClick.AddListener(self_UnitItem.BuyItem); } }