Example #1
0
        /// <summary>
        /// Will add an item to the given slot if possible. If there is already
        /// a stack of this type, it will add to the existing stack. Otherwise,
        /// it will be added to the first empty slot.
        /// </summary>
        /// <param name="slot">The slot to attempt to add to.</param>
        /// <param name="item">The item type to add.</param>
        /// <returns>True if the item was added anywhere in the inventory.</returns>
        public bool AddItemToSlot(int slot, InventoryItem item, int number)
        {
            //TODO: Check for max size and handle behavoir

            //IF the slot we are attempt to add to has the item and its stackable then add to that slot.
            if (object.ReferenceEquals(m_slots[slot].item, item) && item.IsStackable())
            {
                m_slots[slot].number += number;
                inventoryUpdated();
                return(true);
            }

            //if the slot we are trying to add the item too is not null then add to first available
            //This is for items that do not stack.
            if (m_slots[slot].item != null)
            {
                return(AddToFirstEmptySlot(item, number));
            }

            //Slot is empty so add the item.
            m_slots[slot].item    = item;
            m_slots[slot].number += number;

            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }

            return(true);
        }
Example #2
0
        // PUBLIC

        /// <summary>
        /// Set the vital data after creating the prefab.
        /// </summary>
        /// <param name="item">The type of item this prefab represents.</param>
        /// <param name="number">The number of items represented.</param>
        public void Setup(InventoryItem item, int number)
        {
            this.item = item;
            if (!item.IsStackable())
            {
                number = 1;
            }
            this.number = number;
        }
Example #3
0
            public int GetRandomNumber(int level)
            {
                if (!item.IsStackable())
                {
                    return(1);
                }
                int min = GetByLevel(minNumber, level);
                int max = GetByLevel(maxNumber, level);

                return(UnityEngine.Random.Range(min, max + 1));
            }
Example #4
0
        /// <summary>
        /// Find an existing stack of this item type.
        /// </summary>
        /// <returns>-1 if no stack exists or if the item is not stackable.</returns>
        private int FindStack(InventoryItem item)
        {
            if (!item.IsStackable())
            {
                return(-1);
            }

            for (int i = 0; i < slots.Length; i++)
            {
                if (object.ReferenceEquals(slots[i].item, item))
                {
                    return(i);
                }
            }
            return(-1);
        }
Example #5
0
        /// <summary>
        /// See if a stake of an item exists.
        /// </summary>
        /// <param name="item"></param>
        /// <returns>return -1 if no stake exists</returns>
        private int FindStack(InventoryItem item)
        {
            //Check to see if the item is stackable
            if (!item.IsStackable())
            {
                return(-1);
            }

            for (int i = 0; i < m_InventorySize; i++)
            {
                if (object.ReferenceEquals(m_slots[i].item, item))
                {
                    return(i);
                }
            }
            return(-1);
        }