evershade/Assets/Scripts/Components/UI/ItemComponent.cs

92 lines
2.5 KiB
C#

using Handlers;
using TMPro;
using Types.Items;
using UnityEngine;
using UnityEngine.UI;
using Utils;
namespace Components.UI
{
public class ItemComponent : MonoBehaviour
{
public ItemHandler itemHandler;
public TMP_Text nameText;
public Image itemIcon;
public Text quantityText;
public GameItem Item;
private void Awake()
{
if (nameText == null) GameLogger.LogError("ItemName not found");
if (quantityText == null) GameLogger.LogError("qty not found");
if (itemIcon == null) GameLogger.LogError("Icon not found");
itemHandler = gameObject.AddComponent<ItemHandler>();
if (itemHandler == null) GameLogger.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 itemName)
{
// Find the ItemName child and get the TMP_Text component
nameText.text = itemName;
}
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
GameLogger.Log("Setting item, name: " + newItem.ItemName);
SetName(newItem.ItemName);
SetIcon(newItem.Icon);
SetQuantity(newItem.Quantity.ToString());
Item = newItem;
}
public void DeleteItem()
{
// need to delete the parent of this item Component
Destroy(transform.gameObject);
}
public GameItem GetItem()
{
return Item;
}
public void SetQuantity(int qty)
{
// Find the Quantity child and get the TMP_Text component
quantityText.text = qty.ToString();
}
public ItemComponent Copy()
{
GameLogger.Log("Copying item");
var newItem = gameObject.AddComponent<ItemComponent>();
newItem.SetItem(Item);
return newItem;
}
public void AddQuantity(int quantityToAdd)
{
// Add the quantity to the current quantity
Item.Quantity += quantityToAdd;
SetQuantity(Item.Quantity);
}
}
}