using BulletHellTemplate; using TMPro; using UnityEngine; using UnityEngine.UI; namespace BulletHellTemplate { /// /// Represents one entry in the Inventory UI, displaying basic info /// and providing an option to open detailed item info. /// public class InventoryEntry : MonoBehaviour { [Header("UI Elements")] [Tooltip("Icon representing the item.")] public Image icon; [Tooltip("Highlight indicating selection.")] public Image selected; [Tooltip("Icon or overlay indicating the item is equipped.")] public GameObject equipped; [Tooltip("Frame background based on rarity.")] public Image frame; [Tooltip("Text to display item title.")] public TextMeshProUGUI title; [Tooltip("Text to display item level.")] public TextMeshProUGUI level; // Rarity frames [Header("Rarity Frames")] public Sprite commonFrame; public Sprite uncommonFrame; public Sprite rareFrame; public Sprite epicFrame; public Sprite legendaryFrame; private string uniqueItemGuid; // Each purchased item is identified by a unique GUID private InventoryItem baseScriptable; /// /// Sets up the inventory entry with the provided item details. /// /// Unique GUID of the purchased item instance. /// Scriptable object for item data. /// Current upgrade level of this item instance. public void Setup(string guid, InventoryItem scriptableItem, int itemLevel) { uniqueItemGuid = guid; baseScriptable = scriptableItem; if (icon != null) icon.sprite = scriptableItem.itemIcon; if (title != null) title.text = scriptableItem.title; if (level != null) level.text = itemLevel.ToString(); SetFrameByRarity(scriptableItem.rarity); } /// /// Called when the user clicks this entry to open the detailed item info. /// public void OnClickOpenItemInfo() { UIInventoryMenu.Singleton.DeselectAllEntries(); Select(); if (baseScriptable != null) { // Activate and pass the uniqueItemGuid to the item info panel var itemInfoPanel = UIInventoryMenu.Singleton.inventoryItemInfoPrefab; itemInfoPanel.gameObject.SetActive(true); itemInfoPanel.OpenItemInfo(baseScriptable, uniqueItemGuid); } } /// /// Highlights this entry as selected. /// public void Select() => selected.gameObject.SetActive(true); /// /// Removes selection highlight. /// public void Deselect() => selected.gameObject.SetActive(false); /// /// Sets the frame sprite according to the item's rarity. /// public void SetFrameByRarity(Rarity rarity) { switch (rarity) { case Rarity.Common: frame.sprite = commonFrame; break; case Rarity.Uncommon: frame.sprite = uncommonFrame; break; case Rarity.Rare: frame.sprite = rareFrame; break; case Rarity.Epic: frame.sprite = epicFrame; break; case Rarity.Legendary: frame.sprite = legendaryFrame; break; default: frame.sprite = commonFrame; break; } } /// /// Returns an integer representing the rarity, for sorting. /// public int RarityValue() { return (int)baseScriptable.rarity; } /// /// Returns the slot name of the item, for sorting. /// public string SlotValue() { return baseScriptable.slot; } } }