} // end : RemoveFromInventory /* RemoveFromInventory() * Removes the fetched item from the inventory or stack and * returns its data via the 'item' reference. * If the last item is removed from stack, the dictionary * entry is dumped. * * returns false, if no items id are left in inventory */ public bool RemoveFromInventory(int id, out Item item) { // find item item = new Item(); if (dictItems.ContainsKey(id)) { // remove one item item = dictItems[id].data; RemoveFromStack(id); // remove stored data, if no item remains if (dictItems[id].count <= 0) { dictItems.Remove(id); } // item successfully popped // notify children BroadcastMessage("RemoveItem", id, SendMessageOptions.DontRequireReceiver); return(true); } else { // no more items of id return(false); } } // end : RemoveFromInventory
/// <summary> /// Update the grabbedItemObj and values grabbedItem and grabbedAmount /// </summary> /// <param name="item">The grabbedItem</param> /// <param name="amount">The amount of the grabbed item</param> public static void UpdateGrabbedItemObj(ItemDatabase.Item item, int amount) { grabbedItem = item; grabbedAmount = amount; grabbedItemObj.transform.GetComponent <Image>().sprite = grabbedItem.sprite; grabbedItemObj.transform.GetChild(0).gameObject.GetComponent <TextMeshProUGUI>().text = grabbedAmount.ToString(); }
} // end : AddToStack /* AddNewStack() * Adds a new stack for id in dictionary. * The id must not exist, yet. * throws System.ArgumentException */ private void AddNewStack(Item item) { TupleItem newTuple = new TupleItem(); newTuple.count = 1; newTuple.data = item; dictItems.Add(item.ID, newTuple); } // end : AddNewStack
/// <summary> /// Switch the items and amount in itemSlot and grabbed item /// </summary> /// <param name="item">The grabbedItem</param> /// <param name="amount">The amount of the grabbed item</param> public void SwitchItems(ItemDatabase.Item item, int amount) { ItemDatabase.Item itemTemp = this.item; int amountTemp = this.amount; InitiateItem(item, amount); Slot.UpdateGrabbedItemObj(itemTemp, amountTemp); }
/// <summary> /// Class for the item slots /// </summary> /// <param name="slotObj">GameObject of the item slot</param> /// <param name="slotID">ID of the item slot</param> /// <param name="item">The item in the item slot</param> /// <param name="amount">How much of the item is in the slot</param> /// <param name="hotbarObj">GameObject of the hotbar slot, is null if the item slot is not a hotbar slot (not the first 5 item slots at the bottom in the inventory)</param> public ItemSlot(GameObject slotObj, int slotID, ItemDatabase.Item item, int amount, GameObject hotbarObj) { this.slotObj = slotObj; this.slotObj.transform.GetComponent <Slot>().itemSlot = this; this.slotID = slotID; this.hotbarObj = hotbarObj; this.itemObj = this.slotObj.transform.GetChild(0).gameObject; InitiateItem(item, amount); }
bool CheckIfItemInInv(ItemDatabase.Item item) // checks if the item is stackable { for (int i = 0; i < items.Count; i++) { if (items[i].ID == item.ID) { return(true); } } return(false); }
/// <summary> /// <para>Add the amount of the grabbed item to itemSlot</para> /// If the amount is higher than the stackLimit, the amount of the itemSlot will be the stackLimit, returns the rest /// </summary> /// <param name="amount">The item amount of the grabbedItem</param> public int SumItems(ItemDatabase.Item itemAdd, int amount) { this.amount += amount; int rest = 0; if (this.amount > itemAdd.stackLimit) //If more than stacklimit { rest = this.amount - itemAdd.stackLimit; this.amount = itemAdd.stackLimit; } InitiateItem(itemAdd, this.amount); return(rest); }
/// <summary> /// Refreshes the item in the item slot with a new item /// </summary> /// <param name="item">The new item in the item slot</param> /// <param name="amount">How much of the item there is</param> public void InitiateItem(ItemDatabase.Item item, int amount) { this.item = item; this.amount = amount; this.itemObj.GetComponent <Image>().sprite = this.item.sprite; this.itemObj.name = this.item.title; if (this.hotbarObj != null) //If it's not a hotbar item slot { this.hotbarObj.transform.GetChild(0).GetComponent <Image>().sprite = this.item.sprite; this.hotbarObj.transform.GetChild(0).name = this.item.title; } UpdateAmount(); }
} // end : Update /* AddToInventory() * Adds one element of item inside the inventory. * If the item already exists, stack them up to stackSize * (see above) instances. * * returns false, if no more items id fit on the stack * true, in all other cases */ public bool AddToInventory(Item item) { if (dictItems.ContainsKey(item.ID)) { // try to add on top of stack if (!AddToStack(item.ID)) { BroadcastMessage("FullStack", item.ID, SendMessageOptions.DontRequireReceiver); return(false); } } else { AddNewStack(item); } // Notify children BroadcastMessage("AddItem", item.ID, SendMessageOptions.DontRequireReceiver); // item was stacked return(true); } // end : Add to Inventory
public void AddItem(int id) // add item to slot { ItemDatabase.Item ItemToAdd = database.FetchItemByID(id); // fetch the item if (ItemToAdd.Stackable && CheckIfItemInInv(ItemToAdd)) { for (int i = 0; i < items.Count; i++) { if (items[i].ID == id) { ItemData data = slots[i].transform.GetChild(0).GetComponent <ItemData>(); data.amount++; data.transform.GetChild(0).GetComponent <TMP_Text>().text = data.amount.ToString(); break; } } } else { for (int i = 0; i < items.Count; i++) // check the slots { if (items[i].ID == -1) { items[i] = ItemToAdd; GameObject itemObj = Instantiate(inventoryItem); // create item game object itemObj.GetComponent <ItemData>().item = ItemToAdd; itemObj.GetComponent <ItemData>().slot = i; itemObj.transform.SetParent(slots[i].transform); // set item object to the same as slot itemObj.transform.position = Vector2.zero; // set item position itemObj.GetComponent <Image>().sprite = ItemToAdd.Sprite; // set item sprite itemObj.name = ItemToAdd.Title; break; } } } }
/// <summary> /// Fixes some switching scene bugs. /// </summary> public void Awake() { grabbedItemObj = null; grabbedItem = null; grabbedAmount = 0; }
/// <summary> /// Gets called if the player picks up an item off the ground or if the game gives the player an item /// </summary> /// <param name="itemAdd">The item the player picked up off the ground</param> /// <param name="amountAdd">How much of the item there is</param> public static int PickUpItem(ItemDatabase.Item itemAdd, int amountAdd) { int count = 0; ItemSlot itemSlotDrop = null; foreach (ItemSlot itemSlot in inventoryList) //Search for itemslot that's not fully stacked and with the same item { if (itemSlot.item == itemAdd && itemSlot.item.stackLimit > itemSlot.amount) { itemSlotDrop = itemSlot; } if (count == 4 && itemSlotDrop != null) //If found hotbarslot that's not fully stacked and with the same item, break out of loop { break; } count += 1; } if (itemSlotDrop != null) //If found itemslot, sumItems { int rest = itemSlotDrop.SumItems(itemAdd, amountAdd); if (rest > 0) //If there are items left, call same method again, else return 0 { return(PickUpItem(itemAdd, rest)); } return(0); } else //If not found itemslot { int count2 = 0; foreach (ItemSlot itemSlot in inventoryList) //Search for empty itemslot { if (itemSlot.amount == 0) { itemSlotDrop = itemSlot; } if (count2 == 4 && itemSlotDrop != null) //If found empty hotbarslot, break out of loop { break; } count2 += 1; } if (itemSlotDrop != null) //If found empty itemslot, move item and amount to that itemslot and return 0 { itemSlotDrop.InitiateItem(itemAdd, amountAdd); return(0); } else //If inventory full, return the amount { Debug.Log("Inventory Full"); return(amountAdd); } } }