evershade/Assets/Scripts/Handlers/SlotHandler.cs

170 lines
5.8 KiB
C#

using Managers;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class SlotHandler : MonoBehaviour, IDropHandler, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{
public UnityEvent OnSlotClicked = new();
private bool _isPointerDown;
private float _pointerDownTimer;
private SlotComponent _slotComponent;
private void Awake()
{
}
private void Start()
{
if (OnSlotClicked == null)
OnSlotClicked = new UnityEvent();
}
public void Update()
{
if (_isPointerDown) _pointerDownTimer += Time.deltaTime;
}
public void OnDrop(PointerEventData eventData)
{
Debug.Log("Dropped on internal slot: " + _slotComponent.GetSlotId());
// dragged From (new item)
var sourceSlot = eventData.pointerDrag.GetComponentInParent<SlotComponent>();
var sourceSlotItem = sourceSlot.GetItemComponent();
var destinationSlotData =
PlayerManager.Instance.GetSlot(_slotComponent.GetSlotType(), _slotComponent.GetSlotId());
var destinationItem = _slotComponent.GetItemComponent();
var currentSlot = GetComponent<SlotComponent>();
// check if valid swap
Debug.Log("Source Slot Type: " + sourceSlot.GetSlotType());
Debug.Log("Destination Slot Type: " + _slotComponent.GetSlotType());
/*if (sourceSlot.GetSlotType() != _slotComponent.GetSlotType())
{
Debug.Log("Invalid swap");
// Reset the item to its original position
sourceSlotItem.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
sourceSlotItem.GetComponent<RectTransform>().offsetMin = Vector2.zero;
sourceSlotItem.GetComponent<RectTransform>().offsetMax = Vector2.zero;
return;
}*/
// check item type to see if it can be placed in the slot for equipment
if (currentSlot.GetSlotType() == Slot.SlotType.Equipment)
{
Debug.Log("Slot is Equipment");
var sourceItem = sourceSlot.GetItemComponent();
// check if is allowed in slot
if (currentSlot.IsAllowedItemTypes(currentSlot.GetSlotType(), currentSlot.GetSlotId(),
sourceItem.GetItem().type) == false)
{
Debug.Log("Invalid swap");
// Reset the item to its original position
sourceSlotItem.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
sourceSlotItem.GetComponent<RectTransform>().offsetMin = Vector2.zero;
sourceSlotItem.GetComponent<RectTransform>().offsetMax = Vector2.zero;
return;
}
}
// the current Slot has no item, just reassing the item to the slot
if (destinationItem == null)
{
currentSlot.AssignItemComponent(sourceSlotItem);
sourceSlot.DeleteItem();
return;
}
// check if items are the same item and if they are check if they are stackable.
if (currentSlot.GetItemComponent().GetItem().itemName == sourceSlot.GetItemComponent().GetItem().itemName)
if (currentSlot.GetItemComponent().GetItem().isStackable)
{
// if stackable add to stack
Debug.Log("Stackable");
// check if the quantity will exceed the max stack size
if (currentSlot.GetItemComponent().GetItem().quantity +
sourceSlot.GetItemComponent().GetItem().quantity >
currentSlot.GetItemComponent().GetItem().stackSize)
{
Debug.Log("Stack size exceeded");
// Set the quantity to the max stack size
// get quantity to add
var quantityToAdd = currentSlot.GetItemComponent().GetItem().stackSize -
currentSlot.GetItemComponent().GetItem().quantity;
currentSlot.GetItemComponent().AddQuantity(quantityToAdd);
sourceSlot.GetItemComponent().AddQuantity(-quantityToAdd);
return;
}
currentSlot.GetItemComponent().AddQuantity(sourceSlot.GetItemComponent().GetItem().quantity);
sourceSlot.DeleteItem();
return;
}
PlayerManager.Instance.SwapItems(_slotComponent.GetSlotType(), _slotComponent.GetSlotId(),
currentSlot.GetSlotType(),
currentSlot.GetSlotId());
// If the destination slot is empty, assign the dragged item to it
if (destinationItem == null)
{
currentSlot.AssignItemComponent(sourceSlotItem);
sourceSlot.DeleteItem();
}
else
{
// If the destination slot is not empty, swap the items
currentSlot.AssignItemComponent(sourceSlotItem);
sourceSlotItem.DeleteItem();
sourceSlot.AssignItemComponent(destinationItem);
destinationItem.DeleteItem();
}
}
public void OnPointerClick(PointerEventData eventData)
{
TriggerOnClick();
}
public void OnPointerDown(PointerEventData eventData)
{
_isPointerDown = true;
}
public void OnPointerUp(PointerEventData eventData)
{
if (_isPointerDown && _pointerDownTimer < 0.2f) TriggerOnClick();
_isPointerDown = false;
_pointerDownTimer = 0.0f;
}
public void TriggerOnClick()
{
OnSlotClicked.Invoke();
Debug.Log("Clicked on slot: " + _slotComponent.GetSlotId());
//TODO: pass the click through to allow us to drag the item
}
public void SetSlotComponent(SlotComponent slotComponent)
{
_slotComponent = slotComponent;
}
}