/// <summary> /// Update vendor item display to hide / show buttons and data based on selected item in inventory /// </summary> public virtual void UpdateVendorItemDisplay() { if (isOpen) { if (_itemDetails != null) { Inventory clickedInventory = _inputManager.CurrentlySelectedInventorySlot.ParentInventoryDisplay.TargetInventory; InventorySlot slot = _inputManager.CurrentlySelectedInventorySlot; int index = slot.Index; InventoryItem item = clickedInventory.Content[index]; if (item != null) { _itemDetails.currencyImage.sprite = currencyItem.Icon; //clicked item is in main inventory or vendor roll back inventory if (clickedInventory == mainInventory) { _itemDetails.vendorPrice.GetComponent <Text>().text = item.Price.ToString(); _itemDetails.vendorQuantity.GetComponent <Text>().text = "1"; if (item.Quantity > 1) { _itemDetails.increaseButton.SetActive(true); _itemDetails.decreaseButton.SetActive(true); } else { _itemDetails.increaseButton.SetActive(false); _itemDetails.decreaseButton.SetActive(false); } _itemDetails.buyButton.SetActive(false); _itemDetails.sellButton.SetActive(true); } else if (clickedInventory == vendorRollBackInventory) { _itemDetails.vendorPrice.GetComponent <Text>().text = item.Price.ToString(); _itemDetails.vendorQuantity.GetComponent <Text>().text = "1"; if (item.Quantity > 1) { _itemDetails.increaseButton.SetActive(true); _itemDetails.decreaseButton.SetActive(true); } else { _itemDetails.increaseButton.SetActive(false); _itemDetails.decreaseButton.SetActive(false); } _itemDetails.buyButton.SetActive(true); _itemDetails.sellButton.SetActive(false); } //clicked item is in vendor inventory else if (clickedInventory == vendorInventory) { _itemDetails.vendorPrice.GetComponent <Text>().text = vendorContent[index].Price.ToString(); _itemDetails.vendorQuantity.GetComponent <Text>().text = "1"; _itemDetails.buyButton.SetActive(true); _itemDetails.sellButton.SetActive(false); //vendo has unlimited stock if (limitedStock == false) { if (item.MaximumStack > 1) { _itemDetails.increaseButton.SetActive(true); _itemDetails.decreaseButton.SetActive(true); } else { _itemDetails.increaseButton.SetActive(false); _itemDetails.decreaseButton.SetActive(false); } } //vedor has limited stock else { if (item.Quantity > 1) { _itemDetails.increaseButton.SetActive(true); _itemDetails.decreaseButton.SetActive(true); } else { _itemDetails.increaseButton.SetActive(false); _itemDetails.decreaseButton.SetActive(false); } } } } } } }
/// <summary> /// Buy item or items from vendor /// </summary> public virtual void BuyItem() { Vendor openedVendor = null; Vendor[] vendors = FindObjectsOfType <Vendor>(); foreach (Vendor vendor in vendors) { if (vendor.enabled && vendor.isOpen) { openedVendor = vendor; } } if (openedVendor != null) { InventorySlot slot = CurrentlySelectedInventorySlot; Inventory inventory = slot.ParentInventoryDisplay.TargetInventory; int index = slot.Index; InventoryItem item = inventory.Content[index]; InventoryDetailsExtended ide = FindObjectOfType <InventoryDetailsExtended>(); if (ide != null && item != null && ide.disabledItems.Contains(item) == false && (inventory.name == openedVendor.vendorInventory.name || inventory.name == openedVendor.vendorRollBackInventory.name)) { int quantity = Int32.Parse(ide.vendorQuantity.text); int price = Int32.Parse(ide.vendorPrice.text); int cash = openedVendor.GetPlayerCash(); if (cash >= price) { int freeSlot = FindFirstFreeSlot(openedVendor.mainInventory); if (freeSlot != -1) { openedVendor.PayForItem(price); openedVendor.mainInventory.Content[freeSlot] = item.Copy(); openedVendor.mainInventory.Content[freeSlot].Quantity = quantity; MMInventoryEvent.Trigger(MMInventoryEventType.ContentChanged, null, openedVendor.mainInventory.name, null, 0, 0); } if (inventory.name == openedVendor.vendorInventory.name) { //vendor has limited stock if (openedVendor.limitedStock) { if (quantity < item.Quantity) { item.Quantity -= quantity; } else { openedVendor.vendorInventory.DestroyItem(index); } MMInventoryEvent.Trigger(MMInventoryEventType.ContentChanged, null, openedVendor.vendorInventory.name, null, 0, 0); } } else if (inventory.name == openedVendor.vendorRollBackInventory.name) { if (quantity < item.Quantity) { item.Quantity -= quantity; } else { openedVendor.vendorRollBackInventory.DestroyItem(index); } MMInventoryEvent.Trigger(MMInventoryEventType.ContentChanged, null, openedVendor.vendorRollBackInventory.name, null, 0, 0); } } } } }
/// <summary> /// Decrease item quantity and price to buy / sell /// </summary> public virtual void DecreaseQuantity() { Vendor openedVendor = null; Vendor[] vendors = FindObjectsOfType <Vendor>(); foreach (Vendor vendor in vendors) { if (vendor.enabled && vendor.isOpen) { openedVendor = vendor; } } if (openedVendor != null) { InventorySlot slot = CurrentlySelectedInventorySlot; Inventory inventory = slot.ParentInventoryDisplay.TargetInventory; int index = slot.Index; InventoryItem item = inventory.Content[index]; if (item != null) { if (_itemDetails != null) { if (inventory == openedVendor.mainInventory || inventory == openedVendor.vendorRollBackInventory) { int currentQuantity = Int32.Parse(_itemDetails.vendorQuantity.text); int currentPrice = Int32.Parse(_itemDetails.vendorPrice.text); if (currentQuantity > 1) { currentPrice -= item.Price; currentQuantity--; _itemDetails.vendorQuantity.text = currentQuantity.ToString(); _itemDetails.vendorPrice.text = currentPrice.ToString(); } } else if (inventory == openedVendor.vendorInventory) { //vendor has limited stock if (openedVendor.limitedStock) { int currentQuantity = Int32.Parse(_itemDetails.vendorQuantity.text); int currentPrice = Int32.Parse(_itemDetails.vendorPrice.text); if (currentQuantity > 1) { currentPrice -= openedVendor.vendorContent[index].Price; currentQuantity--; _itemDetails.vendorQuantity.text = currentQuantity.ToString(); _itemDetails.vendorPrice.text = currentPrice.ToString(); } } //vendor has unlimited stock else { int currentQuantity = Int32.Parse(_itemDetails.vendorQuantity.text); int currentPrice = Int32.Parse(_itemDetails.vendorPrice.text); if (currentQuantity > 1) { currentPrice -= openedVendor.vendorContent[index].Price; currentQuantity--; _itemDetails.vendorQuantity.text = currentQuantity.ToString(); _itemDetails.vendorPrice.text = currentPrice.ToString(); } } } } } } }
/// <summary> /// Sell item or items to vendor /// </summary> public virtual void SellItem() { Vendor openedVendor = null; Vendor[] vendors = FindObjectsOfType <Vendor>(); foreach (Vendor vendor in vendors) { if (vendor.enabled && vendor.isOpen) { openedVendor = vendor; } } if (openedVendor != null) { InventorySlot slot = CurrentlySelectedInventorySlot; Inventory inventory = slot.ParentInventoryDisplay.TargetInventory; int index = slot.Index; InventoryItem item = inventory.Content[index]; InventoryDetailsExtended ide = FindObjectOfType <InventoryDetailsExtended>(); if (ide != null && item != null && inventory.name == openedVendor.mainInventory.name) { foreach (InventoryItem disabled in ide.disabledItems) { if (item.name == disabled.name) { return; } } int quantity = Int32.Parse(ide.vendorQuantity.text); int price = Int32.Parse(ide.vendorPrice.text); //we can buy back items from vendor if (openedVendor.canRollBackItems) { if (openedVendor.vendorRollBackInventory.AddItem(item, quantity)) { if (openedVendor.mainInventory.AddItem(openedVendor.currencyItem, price)) { //openedVendor.vendorRollBackInventory.AddItem(item, quantity); MMInventoryEvent.Trigger(MMInventoryEventType.ContentChanged, null, openedVendor.vendorRollBackInventory.name, null, 0, 0); if (quantity < item.Quantity) { item.Quantity -= quantity; } else { openedVendor.mainInventory.DestroyItem(index); } //openedVendor.GetCashForItem(price); MMInventoryEvent.Trigger(MMInventoryEventType.ContentChanged, null, openedVendor.mainInventory.name, null, 0, 0); } } } //we can't buy back items from vendor else { if (openedVendor.mainInventory.AddItem(openedVendor.currencyItem, price)) { if (quantity < item.Quantity) { item.Quantity -= quantity; } else { openedVendor.mainInventory.DestroyItem(index); } //openedVendor.GetCashForItem(price); MMInventoryEvent.Trigger(MMInventoryEventType.ContentChanged, null, openedVendor.mainInventory.name, null, 0, 0); } } } } }
/// <summary> /// Move method supporting move between inventories /// </summary> protected virtual void MultiInventoryMove(bool keyboard) { //We have selected first slot and hit Move button if (_isMoving == false) { _slotFrom = CurrentlySelectedInventorySlot; _inventoryFrom = _slotFrom.ParentInventoryDisplay.TargetInventory; _indexFrom = _slotFrom.Index; _inventoryItemFrom = _inventoryFrom.Content[_indexFrom]; //selected slot has an item in it so we gather rest of item data like item quantity and change sprite to Moved so we can see what happened if (_inventoryItemFrom != null) { _quantityFrom = _inventoryItemFrom.Quantity; //now we store none selected slot sprite to change it after moving _oldSprite = CurrentlySelectedInventorySlot.GetComponent <Image>().sprite; //now we change the slot sprite to Moved _slotFrom.GetComponent <Image>().sprite = _slotFrom.MovedSprite; //and by setting _isMoving to "true" we can wait for next click on slot _isMoving = true; if (keyboard) { _useKeyboardJoypad = true; } } } //We have selected second slot else { _slotTo = CurrentlySelectedInventorySlot; _inventoryTo = _slotTo.ParentInventoryDisplay.TargetInventory; _indexTo = _slotTo.Index; _inventoryItemTo = _inventoryTo.Content[_indexTo]; if (_inventoryTo.name == _inventoryFrom.name) { if (_indexTo == _indexFrom) { return; } } //second clicked slot is empty so we add item to this inventory, move to desired slot and remove it from first one if (_inventoryItemTo == null) { //we change first selected slot sprite back to old sprite _slotFrom.GetComponent <Image>().sprite = _oldSprite; //we add item to selected inventory _inventoryTo.Content[_indexTo] = _inventoryItemFrom.Copy(); //we remove item from first inventory _inventoryFrom.DestroyItem(_indexFrom); _inventoryFrom.Content[_indexFrom] = null; } else { _quantityTo = _inventoryItemTo.Quantity; //we check if item is the same as we try to move if (_inventoryItemFrom.ItemName == _inventoryItemTo.ItemName) { //we check what is the rest free quantity to close the full sack int _restStack = _inventoryItemTo.MaximumStack - _quantityTo; //if quantity of our item we try to move in less or equal than free quantity if (_quantityFrom <= _restStack) { //we update quantity of our item _inventoryItemTo.Quantity = _inventoryItemTo.Quantity + _quantityFrom; //we remove item from first inventory _inventoryFrom.DestroyItem(_indexFrom); } //if quantity of our item we try to move is higher than free quantity else if (_quantityFrom > _restStack) { //we update quantity of our item to full stack _inventoryItemTo.Quantity = _inventoryItemTo.MaximumStack; //we check what's left after moving some items int _restToAddQuantity = _quantityFrom - _restStack; //if there is free slot in second inventory if (_inventoryTo.NumberOfFreeSlots >= 1) { //we need to find first free slot int _fistFreeSlot = FindFirstFreeSlot(_inventoryTo); //we add rest of item quantity to inventory as new item _inventoryTo.Content[_fistFreeSlot] = _inventoryItemFrom; _inventoryTo.Content[_fistFreeSlot].Quantity = _restToAddQuantity; //we remove item from first inventory _inventoryFrom.DestroyItem(_indexFrom); } //there is no free slots in second inventory else { //we reduce item quantity in first inventory _inventoryFrom.Content[_indexFrom].Quantity = _inventoryFrom.Content[_indexFrom].Quantity - _restStack; } } } else { //we need to create copy of item from first inventory InventoryItem _tempItemFrom = _inventoryItemFrom.Copy(); //we need to create copy of item from second inventory InventoryItem _tempItemTo = _inventoryItemTo.Copy(); //we remove item from first inventory _inventoryFrom.DestroyItem(_indexFrom); //we remove item from second inventory _inventoryTo.DestroyItem(_indexTo); //we add item from first inventory to second inventory _inventoryTo.Content[_indexTo] = _tempItemFrom.Copy(); //we add item from second inventory to first inventory _inventoryFrom.Content[_indexFrom] = _tempItemTo.Copy(); } } _isMoving = false; if (keyboard) { _useKeyboardJoypad = false; } MMInventoryEvent.Trigger(MMInventoryEventType.ContentChanged, null, _inventoryFrom.name, null, 0, 0); MMInventoryEvent.Trigger(MMInventoryEventType.ContentChanged, null, _inventoryTo.name, null, 0, 0); } }
/// <summary> /// Sets the currently selected slot /// </summary> /// <param name="slot">Slot.</param> public virtual void SetCurrentlySelectedSlot(InventorySlot slot) { _currentlySelectedSlot = slot; }
/// <summary> /// Fills the various detail fields with the item's metadata /// </summary> /// <returns>The detail fields.</returns> /// <param name="item">Item.</param> /// <param name="initialDelay">Initial delay.</param> protected override IEnumerator FillDetailFields(InventoryItem item, float initialDelay) { yield return(new WaitForSeconds(initialDelay)); if (Title != null) { Title.text = item.ItemName; } if (ShortDescription != null) { ShortDescription.text = item.ShortDescription; } if (Description != null) { Description.text = item.Description; } if (Quantity != null) { Quantity.text = item.Quantity.ToString(); } if (Icon != null) { Icon.sprite = item.Icon; } #region ------------ Vendor UI (If you don't want vendor you can delete this) --------------- Vendor openedVendor = null; //we get all objects with Vendor script Vendor[] vendors = FindObjectsOfType <Vendor>(); //we loop all Vendors to see if any of them is enabled foreach (Vendor vendor in vendors) { //if vendor is enabled if (vendor.enabled == true && vendor.isOpen) { openedVendor = vendor; } } //we check if our vendor is not null if (openedVendor != null && openedVendor.isOpen) { //we are searching for InventoryInputManagerExtended InventoryInputManagerExtended iime = FindObjectOfType <InventoryInputManagerExtended>(); //and if it's not null if (iime != null) { //we get currently selected slot and data like inventory, index and item InventorySlot _slot = iime.CurrentlySelectedInventorySlot; Inventory _inventory = _slot.ParentInventoryDisplay.TargetInventory; int _index = _slot.Index; InventoryItem _item = _inventory.Content[_index]; //we loop over all disabled items foreach (InventoryItem disabledItem in disabledItems) { //if item is not null if (_item != null) { //and item selected item is disabled item we update item display and hide vendor UI to prevent sell / buy if (disabledItem.name == _item.name) { openedVendor.UpdateVendorItemDisplay(); vendorUI.SetActive(false); } //if item is not in disabled we can update item display and show vendor UI else { openedVendor.UpdateVendorItemDisplay(); vendorUI.SetActive(true); } } } } } #endregion }
/// <summary> /// Equips the item at the specified slot /// </summary> /// <param name="item">Item.</param> /// <param name="index">Index.</param> /// <param name="slot">Slot.</param> public virtual void EquipItem(InventoryItem item, int index, InventorySlot slot = null) { if (InventoryType == InventoryTypes.Main) { InventoryItem oldItem = null; if (InventoryItem.IsNull(item)) { MMInventoryEvent.Trigger(MMInventoryEventType.Error, slot, this.name, null, 0, index); return; } // if the object is not equipable, we do nothing and exit if (!item.IsEquippable) { return; } // if a target equipment inventory is not set, we do nothing and exit if (item.TargetEquipmentInventory == null) { Debug.LogWarning("InventoryEngine Warning : " + Content[index].ItemName + "'s target equipment inventory couldn't be found."); return; } // if the object can't be moved, we play an error sound and exit if (!item.CanMoveObject) { MMInventoryEvent.Trigger(MMInventoryEventType.Error, slot, this.name, null, 0, index); return; } // call the equip method of the item if (!item.Equip()) { return; } // if this is a mono slot inventory, we prepare to swap if (item.TargetEquipmentInventory.Content.Length == 1) { if (!InventoryItem.IsNull(item.TargetEquipmentInventory.Content[0])) { if ( (item.CanSwapObject) && (item.TargetEquipmentInventory.Content[0].CanMoveObject) && (item.TargetEquipmentInventory.Content[0].CanSwapObject) ) { // we store the item in the equipment inventory oldItem = item.TargetEquipmentInventory.Content[0].Copy(); item.TargetEquipmentInventory.EmptyInventory(); } } } // we add one to the target equipment inventory item.TargetEquipmentInventory.AddItem(item.Copy(), item.Quantity); // remove 1 from quantity RemoveItem(index, item.Quantity); if (oldItem != null) { oldItem.Swap(); AddItem(oldItem, oldItem.Quantity); } MMInventoryEvent.Trigger(MMInventoryEventType.ItemEquipped, slot, this.name, item, item.Quantity, index); } }