/// <summary> /// Creates UI item, plays its "get" animation and adds it to list of active UI items /// </summary> /// <param name="key">Item to create a UI item from</param> public void AddItem(InteractableItemKey key) { // If slot already exists, make that item fill the slot foreach (InventoryItemUI slotItem in slotItems) { if (slotItem.itemKey == key) { slotItem.PlayItemGetAnimation(); activeItems.Add(slotItem); slotItems.Remove(slotItem); return; } } // else create new item and immediately fill the slot GameObject item = Instantiate(itemPrefab, itemParent); InventoryItemUI itemUI = item.GetComponent <InventoryItemUI>(); if (itemUI == null) { Debug.LogError("Item prefab in InventoryUI does not have a InventoryItemUI component"); Destroy(item); } else { itemUI.Init(key); itemUI.PlayItemGetAnimation(); activeItems.Add(itemUI); } }
/// <summary> /// Initializes the UI item /// </summary> /// <param name="key">Key to show in this UI</param> public void Init(InteractableItemKey key) { emptySlotImage.sprite = key.icon; iconImage.sprite = key.icon; itemKey = key; // Start in inventory slot position itemTransform.anchoredPosition = Vector3.zero; origScale = itemTransform.localScale; itemTransform.localScale = Vector3.zero; }
/// <summary> /// Adds an empty item slot to the UI /// </summary> /// <param name="key">Key to show empty slot for</param> public void AddItemSlot(InteractableItemKey key) { GameObject item = Instantiate(itemPrefab, itemParent); InventoryItemUI itemUI = item.GetComponent <InventoryItemUI>(); if (itemUI == null) { Debug.LogError("Item prefab in InventoryUI does not have a InventoryItemUI component"); Destroy(item); } else { itemUI.Init(key); slotItems.Add(itemUI); } }
/// <summary> /// Finds item from inventory, plays its use animation and removes it from list of active UI items /// </summary> /// <param name="key">Item to find</param> /// <param name="targetPos">Target screen position passed to item animation</param> public void UseItem(InteractableItemKey key, Vector3 targetPos) { InventoryItemUI foundItem = null; foreach (InventoryItemUI item in activeItems) { if (item.itemKey == key) { foundItem = item; break; } } if (foundItem != null) { foundItem.PlayItemUseAnimation(targetPos); activeItems.Remove(foundItem); } }