Ejemplo n.º 1
0
        /// <summary>
        /// Adds a stack of items to a given slot.
        /// </summary>
        /// <param name="amount">Amount of items to remove the inventory</param>
        /// <param name="slotNum">Inventory slot to remove the item from</param>
        /// <returns>
        /// Number of items left in stack after adding to slot.
        /// </returns>
        public bool RemoveItems(int amount, int slotNum)
        {
            if (!IsValidSlotNumber(slotNum))
            {
                Debug.Log(INVALID_SLOT);
                return(false);
            }

            Slot s = inventory[slotNum];

            if (s.IsEmpty())
            {
                Debug.Log(SLOT_EMPTY);
                return(false);
            }

            if (amount > s.GetAmount())
            {
                Debug.Log(NOT_ENOUGH_ITEMS);
                return(false);
            }

            s.RemoveAmount(amount);
            InventoryUpdate?.Invoke();
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a stack of items to a given slot.
        /// </summary>
        /// <param name="name">Name of the item to remove</param>
        /// <param name="amount">Amount of items to remove from the inventory</param>
        /// <returns>
        /// Number of items left in stack after adding to slot.
        /// </returns>
        public bool RemoveItemsByName(string name, int amount)
        {
            Dictionary <Slot, int> toRemove = new Dictionary <Slot, int>();

            foreach (Slot s in inventory)
            {
                if (s.GetItem() != null && s.GetItem().Name.Equals(name))
                {
                    int removeAmount = Mathf.Min(s.GetAmount(), amount);
                    toRemove.Add(s, removeAmount);
                    amount -= removeAmount;
                    if (amount <= 0)
                    {
                        break;
                    }
                }
            }

            if (amount > 0)
            {
                return(false);
            }
            foreach (Slot s in toRemove.Keys)
            {
                s.RemoveAmount(toRemove[s]);
            }
            InventoryUpdate?.Invoke();
            return(true);
        }
Ejemplo n.º 3
0
 // Adds a pickup of type to the inventory
 public void AddPickup(Pickup pickup)
 {
     pickups[(int)pickup - 1]++;
     if (InventoryUpdateCall != null)
     {
         InventoryUpdateCall.Invoke(pickup);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Store a given item in the first available slot in this inventory.
        /// </summary>
        /// <param name="item">The item to store.</param>
        /// <returns>
        /// Returns true if the given item was stored in this inventory.
        /// Otherwise, returns false.
        /// </returns>
        public bool Store(Item item)
        {
            var wasStored = false;

            for (int i = 0; i < items.Count; ++i)
            {
                var storedItem = items[i];
                if (storedItem.Equals(Item.NullItem))
                {
                    items[i]  = item;
                    wasStored = true;
                    break;
                }
            }
            onInventoryUpdate.Invoke(this);
            return(wasStored);
        }
Ejemplo n.º 5
0
 // Returns true if a pickup can and was used, else returns false
 public bool UsePickup(Pickup pickup)
 {
     if (pickups[(int)pickup - 1] == 0)
     {
         return(false);
     }
     pickups[(int)pickup - 1]--;
     InventoryUpdateCall?.Invoke(pickup);
     return(true);
 }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
 internal void OnInventoryUpdate()
 {
     InventoryUpdate?.Invoke(this, EventArgs.Empty);
 }