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

            //Test whether the player already has the item in their inventory and if there is enough room
            if (itemTest.Equals(item) && (itemSlot.itemAmount + amount) <= itemTest.maxStack)
            {
                AddToStack(i, amount);
                return true;
            }
        }

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

        //Unable to add item to inventory
        return false;
    }
コード例 #2
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);
        }