예제 #1
0
    public virtual bool MoveItem(int index, InventoryBase targetInventory, int targetIndex)
    {
        if (!IsValidIndex(index) ||
            targetInventory == null ||
            !targetInventory.IsValidIndex(targetIndex) ||
            !targetInventory.IsValidItemType(items[index]) ||
            (targetInventory.items[targetIndex] != null && !IsValidItemType(targetInventory.items[targetIndex])))
        {
            return(false);
        }

        // Add ExistingItem
        if (targetInventory.items[targetIndex]?.Info.AddCount(items[index].Info) ?? false)
        {
            if (items[index].Info.Count == 0)
            {
                DeleteItem(index);
            }

            // Update UI
            inventoryUI?.UpdateSlot(index);
            targetInventory.inventoryUI?.UpdateSlot(targetIndex);
            return(true);
        }

        // Move Item
        if (this != targetInventory && targetInventory.items[targetIndex] == null)
        {
            targetInventory.AddItem(items[index], targetIndex);
            DeleteItem(index, false);

            // On Item Move Callback
            targetInventory.items[targetIndex].OnMove();
        }
        // Swap Item
        else
        {
            Item fromBuffer = items[index];
            items[index] = targetInventory.items[targetIndex];
            targetInventory.items[targetIndex] = fromBuffer;

            if (this == targetInventory)
            {
                // Update UI
                inventoryUI?.MoveSlot(index, targetIndex);
            }
            else
            {
                items[index].OnAdd(this);
                targetInventory.items[targetIndex].OnAdd(targetInventory);

                // Update UI
                inventoryUI?.UpdateSlot(index);
                targetInventory.inventoryUI?.UpdateSlot(targetIndex);
            }

            // On Item Move Callback
            targetInventory.items[targetIndex].OnMove();
            items[index]?.OnMove();
        }

        return(true);
    }