Esempio n. 1
0
    public void Add(PlacebleItem item)
    {
        _items.Add(item);

        if (OnItemChangedCallback != null)
        {
            OnItemChangedCallback.Invoke();
        }
    }
        public bool CollectItem(Item item, int quantity = 1) // to backpack
        {
            if (item == null)
            {
                Debug.LogWarning("Error: CollectItem(null).");
                return(false);
            }

            if (quantity < 1)
            {
                Debug.LogWarning("Error: Trying to add 0 items.");
                return(false);
            }

            var  backpackInventoryItem = new InventoryItem(item, quantity);
            var  backpackItem          = Backpack.SingleOrDefault(i => i.Key.ItemDetails.Name.Equals(item.Name)).Key;
            var  itemToAdd             = backpackInventoryItem;
            bool hasFreeSlotInBackpack = HasFreeSlotInBackpack();

            if (backpackItem == null && !hasFreeSlotInBackpack)
            {
                Debug.Log("Backpack is full."); ///TODO: UI popup
                return(false);
            }
            else if (backpackItem != null)
            {
                if (!hasFreeSlotInBackpack && Backpack[backpackItem] % backpackItem.ItemDetails.MaxStackedCount == 0)
                {
                    Debug.Log("Backpack is full."); ///TODO: UI popup
                    return(false);
                }

                Backpack[backpackItem] += quantity;
                itemToAdd = new InventoryItem(backpackItem.ItemDetails, Backpack[backpackItem]);

                Backpack.Remove(backpackItem);
            }

            AddItemToBackpack(itemToAdd);

            OnItemChangedCallback?.Invoke();

            DisplayItems();

            return(true);
        }
        public bool EquipItem(InventoryItem item) ///TODO: equip on a slot type - helmet, charm, trinket, chest, weapon, shield, etc.
        {
            if (item == null)
            {
                Debug.LogWarning("Error: EquipItem(null).");
                return(false);
            }

            bool isInInventory = Inventory.SingleOrDefault(i => i.ItemDetails.Name.Equals(item.ItemDetails.Name)) != null;

            if (isInInventory)
            {
                Debug.LogWarning($"The item '{item.ItemDetails.Name}' is already equipped (in the inventory).");
                return(false);
            }

            var  backpackItem = Backpack.SingleOrDefault(kvp => kvp.Key.ItemDetails.Name.Equals(item.ItemDetails.Name)).Key;
            bool isInBackpack = backpackItem != null;

            if (!isInBackpack)
            {
                Debug.LogWarning($"The item '{item.ItemDetails.Name}' is not in the backpack or inventory. The player has no such item.");
                return(false);
            }

            Backpack[backpackItem] = Mathf.Clamp(Backpack[backpackItem] - 1, 0, backpackItem.ItemDetails.MaxStackedCount);

            backpackItem.IsEquipped = false;

            Inventory.Add(new InventoryItem(backpackItem.ItemDetails, 1, true));

            if (Backpack[backpackItem] < 1)
            {
                Backpack.Remove(backpackItem);
            }

            OnItemChangedCallback?.Invoke();

            DisplayItems();

            return(true);
        }
        public bool UseItem(Item item)
        {
            if (player == null || player.Status != ManagerStatus.Started)
            {
                Debug.LogWarning("Error: Inventory.UseItem: PlayerManager not ready yet.");
                return(false);
            }

            if (item == null)
            {
                Debug.LogWarning("Error: UseItem(null).");
                return(false);
            }

            if (!item.IsUsable)
            {
                Debug.LogWarning($"{item.Name} is not usable.");
                return(false);
            }

            var inventoryItem = Inventory.SingleOrDefault(i => i.ItemDetails.Name.Equals(item.Name));
            var backpackItem  = Backpack.SingleOrDefault(kvp => kvp.Key.ItemDetails.Name.Equals(item.Name)).Key;

            if (inventoryItem == null && backpackItem == null)
            {
                Debug.LogWarning($"{item.Name} is missing from the backpack and the inventory.");
                return(false);
            }

            item.Use(player);

            var hasDiscarded = DiscardItem(item);

            if (hasDiscarded)
            {
                OnItemChangedCallback?.Invoke();
            }

            return(hasDiscarded);
        }
        public bool DiscardItem(Item item, int quantity = 1) // from backpack or inventory
        {
            if (item == null)
            {
                Debug.LogWarning("Error: DiscardItem(null).");
                return(false);
            }

            var backpackItem  = Backpack.SingleOrDefault(i => i.Key.ItemDetails.Name.Equals(item.Name)).Key;
            var inventoryItem = Inventory.SingleOrDefault(i => i.ItemDetails.Name.Equals(item.Name));

            if (backpackItem == null && inventoryItem != null)
            {
                var hasUnequipped = UnequipItem(inventoryItem);
                if (hasUnequipped)
                {
                    OnItemChangedCallback?.Invoke();
                }

                return(hasUnequipped);
            }
            else if (backpackItem == null)
            {
                Debug.LogWarning($"There is no such item to discard either from backpack or from inventory.");
                return(false);
            }

            Backpack[backpackItem] = Mathf.Clamp(Backpack[backpackItem] - quantity, 0, backpackItem.ItemDetails.MaxStackedCount);

            if (Backpack[backpackItem] < 1)
            {
                Backpack.Remove(backpackItem);
            }

            OnItemChangedCallback?.Invoke();

            DisplayItems();

            return(true);
        }
        public bool UnequipItem(InventoryItem item)
        {
            if (item == null)
            {
                Debug.LogWarning("Error: UnequipItem(null).");
                return(false);
            }

            var  inventoryItem = Inventory.SingleOrDefault(i => i.ItemDetails.Name.Equals(item.ItemDetails.Name));
            bool isInInventory = Inventory.SingleOrDefault(i => i.ItemDetails.Name.Equals(item.ItemDetails.Name)) != null;

            if (!isInInventory)
            {
                Debug.LogWarning($"The item '{item.ItemDetails.Name}' is not equipped (not in the inventory).");
                return(false);
            }

            var  backpackItem = Backpack.SingleOrDefault(kvp => kvp.Key.ItemDetails.Name.Equals(item.ItemDetails.Name));
            bool isInBackpack = backpackItem.Key != null;

            if (isInBackpack)
            {
                Backpack[backpackItem.Key]++;
            }
            else
            {
                Backpack.Add(inventoryItem, 1);
            }

            Inventory.Remove(inventoryItem);

            OnItemChangedCallback?.Invoke();

            DisplayItems();

            return(true);
        }