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(); var sourceSlotItem = sourceSlot.GetItemComponent(); var destinationSlotData = PlayerManager.Instance.GetSlot(_slotComponent.GetSlotType(), _slotComponent.GetSlotId()); var destinationItem = _slotComponent.GetItemComponent(); var currentSlot = GetComponent(); // 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().anchoredPosition = Vector2.zero; sourceSlotItem.GetComponent().offsetMin = Vector2.zero; sourceSlotItem.GetComponent().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().anchoredPosition = Vector2.zero; sourceSlotItem.GetComponent().offsetMin = Vector2.zero; sourceSlotItem.GetComponent().offsetMax = Vector2.zero; return; } } // dragged To (this) PlayerManager.Instance.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().anchoredPosition = Vector2.zero; // sourceSlotItem.GetComponent().offsetMin = Vector2.zero; // sourceSlotItem.GetComponent().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().anchoredPosition = Vector2.zero; // sourceSlotItem.GetComponent().offsetMin = Vector2.zero; // sourceSlotItem.GetComponent().offsetMax = Vector2.zero; } } 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; } // onClick handler 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; } }