Beispiel #1
0
        private void RemoveFromSlot(int index, ItemConfig itemToAdd, int amountToAdd)
        {
            inventory[index].amount -= amountToAdd;

            if (GetAmountOfItemInSlot(index) <= 0)
            {
                inventory[index].item   = null;
                inventory[index].amount = 0;
            }
            SortItems();
            if (OnInventoryUpdate != null)
            {
                OnInventoryUpdate();
            }
        }
Beispiel #2
0
        private void AddToSlot(int index, ItemConfig itemToAdd, int amountToAdd)
        {
            inventory[index].item    = itemToAdd;
            inventory[index].amount += amountToAdd;

            if (GetAmountOfItemInSlot(index) > maxAmountPerSlot)
            {
                inventory[index].amount = maxAmountPerSlot;
            }
            SortItems();
            if (OnInventoryUpdate != null)
            {
                OnInventoryUpdate();
            }
        }
Beispiel #3
0
        private bool AddToEmptySlot(ItemConfig item, int amount)
        {
            bool foundSlotToAdd = false;

            for (int i = 0; i < inventory.Length; i++)
            {
                if (inventory[i].item == null)
                {
                    AddToSlot(i, item, amount);
                    foundSlotToAdd = true;
                    i = inventory.Length;
                }
            }
            if (OnInventoryUpdate != null)
            {
                OnInventoryUpdate();
            }

            return(foundSlotToAdd);
        }
Beispiel #4
0
        public void AddItem(ItemConfig item, int amount)
        {
            bool slotFound = false;

            if (item.IsStackable())
            {
                slotFound = AddStackableItemToSlot(item, amount);
                if (!slotFound)
                {
                    slotFound = AddToEmptySlot(item, amount);
                }
            }
            else
            {
                slotFound = AddToEmptySlot(item, amount);
            }
            if (!slotFound)
            {
                Debug.LogError("There's no slot to add " + item.GetDisplayName());
            }
        }
Beispiel #5
0
        public void RemoveItem(ItemConfig item, int amount)
        {
            bool foundItem = false;

            for (int i = 0; i < inventory.Length; i++)
            {
                if (item.GetItemId() == inventory[i].item.GetItemId())
                {
                    RemoveFromSlot(i, item, amount);
                    foundItem = true;
                    i         = inventory.Length;
                }
            }
            if (!foundItem)
            {
                Debug.LogError("Item " + item.GetDisplayName() + " not found on inventory");
            }
            if (OnInventoryUpdate != null)
            {
                OnInventoryUpdate();
            }
        }
Beispiel #6
0
        private bool AddStackableItemToSlot(ItemConfig item, int amount)
        {
            bool foundSlotToAdd = false;

            for (int i = 0; i < inventory.Length; i++)
            {
                if (IsEmptySlot(i))
                {
                    continue;
                }

                if (inventory[i].item.GetItemId() == item.GetItemId())
                {
                    AddToSlot(i, item, amount);
                    foundSlotToAdd = true;
                    i = inventory.Length;
                }
            }
            if (OnInventoryUpdate != null)
            {
                OnInventoryUpdate();
            }
            return(foundSlotToAdd);
        }