예제 #1
0
    public void AddItem(Item item, int amount)
    {
        slot.AddItem(item);
        slot.AddAmount(amount);

        UpdateSlot();
    }
예제 #2
0
        /// <summary>
        /// Adds a specified number of an item to any available free slots.
        /// </summary>
        /// <param name="item">Item to add to the inventory</param>
        /// <param name="amount">Amount of items to add to the inventory</param>
        /// <returns>
        /// True if operation is successful.
        /// </returns>
        public bool AddItems(Item item, int amount)
        {
            if (amount <= 0)
            {
                Debug.Log(INVALID_AMOUNT);
                return(false);
            }

            if (amount == 1)
            {
                return(AddItem(item));
            }

            Dictionary <Slot, int> toAdd = new Dictionary <Slot, int>();

            while (amount > 0)
            {
                int slotIndex = getNextFreeSlotIndex(item, new List <Slot>(toAdd.Keys));
                if (slotIndex == -1)
                {
                    Debug.Log(NO_SPACE);
                    return(false);
                }

                Slot s = inventory[slotIndex];

                int addAmount = GetAmountToAdd(s, item, amount);
                toAdd.Add(s, addAmount);
                amount -= addAmount;
            }

            foreach (Slot s in toAdd.Keys)
            {
                if (s.GetItem() == null)
                {
                    s.SetItem(item, toAdd[s]);
                }
                else
                {
                    s.AddAmount(toAdd[s]);
                }
            }
            InventoryUpdate?.Invoke();
            return(true);
        }
예제 #3
0
    public void OnDrop(GameObject receiveSlotGameObject)
    {
        DragItem receiveDragItem = receiveSlotGameObject.GetComponent <DragItem>();

        if (receiveDragItem == null)
        {
            return;
        }
        InventorySlotUI receiveInventorySlotUI = receiveDragItem.originalParent.GetComponent <InventorySlotUI>();
        Slot            receiveSlot            = receiveInventorySlotUI.GetSlot();

        if (receiveSlot == slot)
        {
            return;
        }
        if (receiveSlot == null || receiveSlot.item == null)
        {
            return;
        }
        if (readOnly || receiveInventorySlotUI.readOnly)
        {
            return;
        }
        if (Slot.hasEqualItemIDs(slot, receiveSlot) && slot.amount + receiveSlot.amount <= slot.maxAmount)
        {
            slot.AddAmount(receiveSlot.amount);
            UpdateSlot();
            receiveInventorySlotUI.SetSlot(new Slot(null, 0));
        }
        else
        {
            if (slot != null && slot.item != null && !receiveInventorySlotUI.canReceive || !canReceive)
            {
                return;
            }
            receiveInventorySlotUI.SetSlot(slot);
            SetSlot(receiveSlot);
        }
        InventoryUI slotInventoryUI        = GetComponentInParent <InventoryUI>();
        InventoryUI receiveSlotInventoryUI = receiveDragItem.originalParent.GetComponentInParent <InventoryUI>();

        slotInventoryUI.UpdateInvetory();
        receiveSlotInventoryUI.UpdateInvetory();
    }
예제 #4
0
        /// <summary>
        /// Adds a single item to the specified slot
        /// </summary>
        /// <param name="item">Item to add to the inventory</param>
        /// <param name="slotNum">Inventory slot to add the item to</param>
        /// <returns>
        /// True if operation is successful
        /// </returns>
        public bool AddItem(Item item, int slotNum)
        {
            if (!ValidateSlot(item, slotNum, 1))
            {
                return(false);
            }

            Slot s = inventory[slotNum];

            if (s.GetItem() == null)
            {
                s.SetItem(item, 1);
            }
            else
            {
                s.AddAmount(1);
            }

            InventoryUpdate?.Invoke();
            return(true);
        }
예제 #5
0
        /// <summary>
        /// Adds a stack of items to a given slot.
        /// </summary>
        /// <param name="item">Item to add to the inventory</param>
        /// <param name="amount">Amount of items to add to the inventory</param>
        /// <param name="slotNum">Inventory slot to add the item to</param>
        /// <returns>
        /// Number of items left in stack after adding to slot.
        /// </returns>
        public int AddItems(Item item, int amount, int slotNum)
        {
            if (!ValidateSlot(item, slotNum, 1))
            {
                return(amount);
            }

            Slot s         = inventory[slotNum];
            int  addAmount = GetAmountToAdd(s, item, amount);

            if (s.GetItem() == null)
            {
                s.SetItem(item, addAmount);
            }
            else
            {
                s.AddAmount(addAmount);
            }

            InventoryUpdate?.Invoke();
            return(amount - addAmount);
        }
예제 #6
0
 public void OnPointerDown(PointerEventData eventData)
 {
     if (eventData.button == PointerEventData.InputButton.Right)
     {
         if (MouseSlotUI.Instance.Slot.Amount <= 0)
         {
             int amount = Mathf.CeilToInt(Slot.Amount / 2f);
             MouseSlotUI.Instance.Slot.SetItemAmount(Slot.Item, amount);
             Slot.Consume(amount);
         }
         else if (Slot.TrySetItem(MouseSlotUI.Instance.Slot.Item))
         {
             int amount = Slot.AddAmount(1);
             MouseSlotUI.Instance.Slot.Consume(amount);
         }
     }
     else if (eventData.button == PointerEventData.InputButton.Left)
     {
         if (!Input.GetKey(KeyCode.LeftShift))
         {
             if (Slot.Item != null && MouseSlotUI.Instance.Slot.Item == Slot.Item)
             {
                 int amount = Slot.AddAmount(MouseSlotUI.Instance.Slot.Amount);
                 MouseSlotUI.Instance.Slot.Consume(amount);
             }
             else
             {
                 Item tempItem   = Slot.Item;
                 int  tempAmount = Slot.Amount;
                 SetSlotValues(MouseSlotUI.Instance.Slot);
                 MouseSlotUI.Instance.SetSlotValues(tempItem, tempAmount);
                 lastSlotClicked = Slot;
             }
         }
     }
 }