evershade/Assets/Scripts/Handlers/SlotHandler.cs

123 lines
4.4 KiB
C#

using Managers;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
public class SlotHandler : MonoBehaviour, IDropHandler, IPointerClickHandler
{
public PlayerManager playerManager;
[FormerlySerializedAs("onSlotClicked")]
public UnityEvent OnSlotClicked = new();
private SlotComponent _slotComponent;
private void Awake()
{
playerManager = GameObject.Find("PlayerManager").GetComponent<PlayerManager>();
}
private void Start()
{
if (OnSlotClicked == null)
OnSlotClicked = new UnityEvent();
}
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.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)
{
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;
}
}
// dragged To (this)
playerManager.SwapItems(_slotComponent.GetSlotType(), _slotComponent.GetSlotId(), sourceSlot.GetSlotType(),
sourceSlot.GetSlotId());
// If the destination slot is empty, assign the dragged item to it
if (destinationItem == null)
{
currentSlot.AssignItemComponent(sourceSlotItem);
sourceSlot.DeleteItem();
// sourceSlotItem.transform.SetParent(transform, false);
// sourceSlotItem.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
// sourceSlotItem.GetComponent<RectTransform>().offsetMin = Vector2.zero;
// sourceSlotItem.GetComponent<RectTransform>().offsetMax = Vector2.zero;
}
else
{
// If the destination slot is not empty, swap the items
currentSlot.AssignItemComponent(sourceSlotItem);
sourceSlotItem.DeleteItem();
sourceSlot.AssignItemComponent(destinationItem);
destinationItem.DeleteItem();
// sourceSlotItem.transform.SetParent(transform, false);
// sourceSlotItem.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
// sourceSlotItem.GetComponent<RectTransform>().offsetMin = Vector2.zero;
// sourceSlotItem.GetComponent<RectTransform>().offsetMax = Vector2.zero;
}
}
public void OnPointerClick(PointerEventData eventData)
{
TriggerOnClick();
}
// onClick handler
public void TriggerOnClick()
{
OnSlotClicked.Invoke();
Debug.Log("Clicked on slot: " + _slotComponent.GetSlotId());
}
public void SetSlotComponent(SlotComponent slotComponent)
{
_slotComponent = slotComponent;
}
}