// add a certain quanity of a particular item to inventory public void AddToInventory(SO_Item newItem, int quantity) { Debug.Log("count = " + entries.Count); // loop through all items in this inventory foreach (InventoryEntry entry in entries) { // if item already exists, increment its quantity by `quantity` if (newItem == entry.item) { entry.quantity += quantity; Debug.Log("cycle 1" + entry.quantity); _UpdateUi(); return; } Debug.Log("cycle 2"); } // get first free slot index int freeSlotIndex = System.Array.FindIndex(slots, slot => slot.item == null); // reaching here means that item to be added is missing in this inventory // hence, initiate new entry and set quantity // TODO: handle out of bounds cases (i.e., freeSlotIndex = -1) InventoryEntry newEntry = new InventoryEntry(newItem, quantity, freeSlotIndex); entries.Add(newEntry); _UpdateUi(); }
private void _DrawDraggedItem(SO_Item item) { Image draggedItemImage = draggedItem.GetComponent <Image>(); draggedItemImage.sprite = item.itemIcon; draggedItem.SetActive(true); }
private void _DropItemInWorld() { // calculate in-world position where item will be dropped Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); worldPos.z = 0; SO_Item item = gameObject.GetComponent <Inventory>().slots[dragOriginIndex].item; // instantiate new game object from item prefab GameObject go = Instantiate( itemPrefab, worldPos, Quaternion.identity, itemsParent.transform ); // set item code and icon Item goItem = go.GetComponent <Item>(); goItem.itemCode = item.itemCode; SpriteRenderer image = go.GetComponent <SpriteRenderer>(); image.sprite = item.itemIcon; // reduce item in inventory bool sup = gameObject.GetComponent <Inventory>().RemoveFromInventory(dragOriginIndex, 1); Debug.Log(sup); }
private void AddButton(SO_Item item) { bool buttonIsFound = false; foreach (MyButton itButton in myButtonList) { if (itButton.item != null) { if (itButton.item.name == item.name) { buttonIsFound = true; itButton.StackItem(); myButtonList.Add(itButton); break; } } } if (!buttonIsFound) { GameObject newInstOfButton = Instantiate(itemButtonPrefab); MyButton myButton = newInstOfButton.GetComponent <MyButton>(); myButtonList.Add(myButton); myButton.item = item; newInstOfButton.transform.SetParent(itemGrid); newInstOfButton.GetComponent <UnityEngine.UI.Button>().onClick.AddListener(() => { selectedButton = myButton; tooltip.UpdateTooltip(selectedButton); }); } }
private void OnTriggerEnter2D(Collider2D other) { Item itemCollidedWith = other.GetComponent <Item>(); // exit if collided object is not an "item" if (itemCollidedWith == null) { return; } SO_Item itemDetails = null; // get all details of item by matching itemCode foreach (SO_Item item in gameInventory.items) { if (itemCollidedWith.itemCode == item.itemCode) { itemDetails = item; } } if (itemDetails != null) { Debug.Log("Name: " + itemDetails.itemName + "; Desc: " + itemDetails.itemDescription); inventory.AddToInventory(itemDetails, 1); } else { Debug.Log("Unknown object!"); } }
public void Receive(SO_Item itemToReceive) { if (itemToReceive != null) { inventoryList.Add(itemToReceive); crtWeight += itemToReceive.weight; UIManager.Instance.Notify(UIRequest.Inventory, UIRequestMode.Update); } }
public override void RemoveItems(SO_Item _Item) { spellsList.Remove(_Item as SO_Spell); UpdateSlots(); if (inventoryFull == true && spellsList.Contains(null)) { inventoryFull = false; } }
public virtual void RemoveItems(SO_Item _Item) { itemsList.Remove(_Item); UpdateSlots(); if (inventoryFull == true && itemsList.Contains(null)) { inventoryFull = false; } }
public void AddItem(SO_Item _Item) { for (int i = 0; i < itemsList.Count; i++) { if (itemsList[i] == null) { itemsList[i] = _Item; if (!itemsList.Contains(null)) { inventoryFull = true; } break; } } }
public virtual void AddItem(SO_Item _Item) { for (int n = 0; n < itemsList.Count; n++) { if (itemsList[n] == null) { itemsList[n] = _Item; UpdateSlots(); if (!itemsList.Contains(null)) { inventoryFull = true; } break; } } }
public override void AddItem(SO_Item _Item) { for (int n = 0; n < spellSlotsList.Count; n++) { if (spellsList[n] == null) { spellsList[n] = _Item as SO_Spell; var blank = Instantiate((_Item as SO_Spell).blank, spellBlanksParent.transform); (_Item as SO_Spell).activeBlank = blank; UpdateSlots(); if (!spellsList.Contains(null)) { inventoryFull = true; } break; } } }
public int slotIndex; // position of this entry in relevant inventory public InventoryEntry(SO_Item item, int quantity, int slotIndex) { this.item = item; this.quantity = quantity; this.slotIndex = slotIndex; }
public CL_Resource(SO_Item dataPass) { ResourceName = dataPass.ResourceName; Quantity = dataPass.Quantity; ResourceData = dataPass; }
// logic for setting icon & text in slot UI public void _UpdateUi(InventoryEntry entry) { this.item = entry.item; image.sprite = entry.item.itemIcon; // item icon gui.text = entry.quantity.ToString(); // item count }
// reset public void ResetUi() { this.item = null; image.sprite = null; gui.text = ""; }
public void Remove() { _Item = null; UpdateInfo(); }
public void CreateDroppedItem(SO_Item itemToDrop) { item = itemToDrop; }