示例#1
0
    public void Swap(int index1, int index2)
    {
        //Get the items from both indexes
        ItemSlot firstSlot  = itemSlots[index1];
        ItemSlot secondSlot = itemSlots[index2];

        //If both items are the same, return as nothing should happen
        if (firstSlot.Equals(secondSlot))
        {
            return;
        }

        if (secondSlot.inventoryItem != null)
        {
            if (firstSlot.inventoryItem == secondSlot.inventoryItem)
            {
                //Check how much remaining space is in the second slot
                int secondSlotRemainingSpace = secondSlot.CheckRemainingItemSpace();

                //Check if the first slots quantity is lower than the remaining space
                if (firstSlot.quantity <= secondSlotRemainingSpace)
                {
                    //Add the items to the second slot
                    secondSlot.quantity += firstSlot.quantity;

                    //Resets the first item slot
                    itemSlots[index1] = new ItemSlot();

                    onInventoryItemsUpdated.Invoke();

                    return;
                }
            }
        }

        //Put the second slot item in the index1 slot and the first slot item in the index2 slot
        itemSlots[index1] = secondSlot;
        itemSlots[index2] = firstSlot;

        onInventoryItemsUpdated.Invoke();
    }