Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
1.7 kB
3
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;

// 
public class Slot : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDropHandler, IPointerClickHandler
{
    [SerializeField] private Image itemIcon;
    [SerializeField] private TextMeshProUGUI itemName;
    [SerializeField] private GameObject stack;
    [SerializeField] private TextMeshProUGUI stackLabel;

    private int index;
    private float lastTimeClick;

    void Awake()
    {
       
    }

    public void Set(Item item)
    {
        index = InventorySystem.Instance.Inventory.IndexOf(item);
        itemIcon.sprite = item.data.Icon;
        itemName.text = item.data.Name;

        if (item.stackSize <= 1)
        {
            stack.SetActive(false);
            return;
        }

        stackLabel.text = item.stackSize.ToString();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        InventorySystem.Instance.Event("DragBegin", index);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        InventorySystem.Instance.Event("DragEnd", index);
    }

    public void OnDrop(PointerEventData eventData)
    {

        InventorySystem.Instance.Event("Drop", index);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        float currentTimeClick = eventData.clickTime;

        if(Mathf.Abs(currentTimeClick - lastTimeClick) < 0.75f)
        {
            InventorySystem.Instance.Event("Select", index);
        }

        lastTimeClick = currentTimeClick;
    }
}