evershade/Assets/Scripts/Components/ItemComponent.cs

75 lines
1.9 KiB
C#

using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ItemComponent : MonoBehaviour
{
public ItemHandler itemHandler;
public GameItem item;
public TMP_Text nameText;
public Image itemIcon;
public Text quantityText;
private void Awake()
{
nameText = transform.Find("ItemName").GetComponent<TMP_Text>();
if (nameText == null) Debug.LogError("ItemName not found");
quantityText = transform.Find("qty").GetComponent<Text>();
if (quantityText == null) Debug.LogError("qty not found");
itemIcon = transform.Find("Icon").GetComponent<Image>();
if (itemIcon == null) Debug.LogError("Icon not found");
itemHandler = gameObject.AddComponent<ItemHandler>();
if (itemHandler == null) Debug.LogError("ItemHandler not found");
}
public void SetQuantity(string qty)
{
// Find the Quantity child and get the TMP_Text component
quantityText.text = qty;
}
public void SetName(string name)
{
// Find the ItemName child and get the TMP_Text component
nameText.text = name;
}
public void SetIcon(Sprite icon)
{
// Find the Icon child and get the Image component
itemIcon.sprite = icon;
}
public void SetItem(GameItem newItem)
{
// todo: Use the new Item to update the ItemName, Icon, qty
SetName(newItem.name);
SetIcon(newItem.icon);
SetQuantity(newItem.quantity);
item = newItem;
}
public void DeleteItem()
{
// need to delete the parent of this item Component
Destroy(transform.gameObject);
}
public GameItem GetItem()
{
return item;
}
public ItemComponent Copy()
{
Debug.Log("Copying item");
var newItem = gameObject.AddComponent<ItemComponent>();
newItem.SetItem(item);
return newItem;
}
}