コード例 #1
0
        //Add specified item to the players inventory if possible
        public bool AddToInventory(ItemBase item, int amount = 1)
        {
            // Cycle through all inventory slots
            for (int i = 0; i < inventory.itemList.Count; i++)
            {
                InventoryItem itemSlot = inventory.itemList[i];
                ItemBase      itemTest = itemSlot.item;

                // If the player has the item already in their inventory, attempt to add to stack
                if (itemTest && itemTest.Equals(item) && (itemSlot.itemAmount + amount) <= itemTest.maxStack)
                {
                    AddToStack(i, amount);
                    UpdateInventoryEvent?.Invoke();
                    return(true);
                }
            }

            //If there is an empty slot available, use that instead
            if (inventory.itemList.Count < inventory.maxSlots)
            {
                inventory.itemList.Add(new InventoryItem(item, amount));
                return(true);
            }

            //Unable to add item to inventory
            return(false);
        }
コード例 #2
0
        //Remove specified item/amount from inventory
        public bool RemoveFromInventory(ItemBase item, int amount)
        {
            for (int i = 0; i < inventory.itemList.Count; i++)
            {
                //When the item is found, remove the set amount
                if (inventory.itemList[i].item == item)
                {
                    InventoryItem itemSlot = inventory.itemList[i];
                    itemSlot.itemAmount  -= amount;
                    inventory.itemList[i] = itemSlot;

                    if (itemSlot.itemAmount <= 0)
                    {
                        inventory.itemList.RemoveAt(i);
                    }
                    UpdateInventoryEvent?.Invoke();
                    return(true);
                }
            }

            Debug.Log("Unable to remove item!");
            return(false);
        }
コード例 #3
0
 //Update inventory information with the currently selected item
 void UpdateInventory()
 {
     UpdateInventoryEvent?.Invoke();
 }