using Managers; using UnityEngine; using UnityEngine.EventSystems; public class SlotHandler : MonoBehaviour, IDropHandler { public PlayerManager playerManager; private SlotComponent _slotComponent; private void Awake() { playerManager = GameObject.Find("PlayerManager").GetComponent(); } // get allowed item types 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.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.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 SetSlotComponent(SlotComponent slotComponent) { _slotComponent = slotComponent; } } // // var destinationSlot = playerManager.inventoryManager.GetSlot(_slotId); // var destinationItem = playerManager.inventoryManager.GetItemFromSlot(destinationSlot.slotId); // var draggedSlotController = eventData.pointerDrag.GetComponentInParent(); // var draggedComponent = eventData.pointerDrag.GetComponent(); // var draggedItem = draggedSlotController.GetItem(); // // var draggedSlotId = draggedSlotController.GetSlotId(); // // Debug.Log("Dropped item: " + draggedItem.name); // // // if (eventData.pointerDrag != null) // { // if (draggedItem == null) // { // Debug.Log("inventory item does not exist"); // // Delete visual representation of dragged item // Destroy(eventData.pointerDrag); // return; // } // // // Place the dragged item in this slot // // draggedComponent.GetComponent().anchoredPosition = // // if (destinationItem == null) // { // Debug.Log("Destination slot is empty"); // inventoryManager.SwapItems(destinationSlot.slotId, draggedSlotId); // // // Re parent to this slot // draggedComponent.transform.SetParent(transform, false); // draggedComponent.GetComponent().anchoredPosition = Vector2.zero; // draggedComponent.GetComponent().offsetMin = Vector2.zero; // draggedComponent.GetComponent().offsetMax = Vector2.zero; // } // else // { // Debug.Log("Destination slot is not empty"); // inventoryManager.SwapItems(draggedSlotId, destinationSlot.slotId); // // // Get the parent of the dragged component // // // originalSlot.SetSlotDetails(); // // // var selfItem = transform.Find("InventoryItem(Clone)").GetComponent(); // // if (selfItem == null) Debug.Log("Self item is null"); // // // selfItem.transform.SetParent(draggedSlotController.transform, false); // selfItem.GetComponent().anchoredPosition = Vector2.zero; // selfItem.GetComponent().offsetMin = Vector2.zero; // selfItem.GetComponent().offsetMax = Vector2.zero; // // // Re parent to this slot // draggedComponent.transform.SetParent(transform, false); // draggedComponent.GetComponent().anchoredPosition = Vector2.zero; // draggedComponent.GetComponent().offsetMin = Vector2.zero; // draggedComponent.GetComponent().offsetMax = Vector2.zero; // }