165 lines
6.4 KiB
C#
165 lines
6.4 KiB
C#
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<PlayerManager>();
|
|
}
|
|
|
|
// 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<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 SetSlotComponent(SlotComponent slotComponent)
|
|
{
|
|
_slotComponent = slotComponent;
|
|
}
|
|
}
|
|
|
|
|
|
//
|
|
|
|
// var destinationSlot = playerManager.inventoryManager.GetSlot(_slotId);
|
|
// var destinationItem = playerManager.inventoryManager.GetItemFromSlot(destinationSlot.slotId);
|
|
// var draggedSlotController = eventData.pointerDrag.GetComponentInParent<SlotComponent>();
|
|
// var draggedComponent = eventData.pointerDrag.GetComponent<ItemHandler>();
|
|
// 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<RectTransform>().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<RectTransform>().anchoredPosition = Vector2.zero;
|
|
// draggedComponent.GetComponent<RectTransform>().offsetMin = Vector2.zero;
|
|
// draggedComponent.GetComponent<RectTransform>().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<ItemHandler>();
|
|
//
|
|
// if (selfItem == null) Debug.Log("Self item is null");
|
|
//
|
|
//
|
|
// selfItem.transform.SetParent(draggedSlotController.transform, false);
|
|
// selfItem.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
|
|
// selfItem.GetComponent<RectTransform>().offsetMin = Vector2.zero;
|
|
// selfItem.GetComponent<RectTransform>().offsetMax = Vector2.zero;
|
|
//
|
|
// // Re parent to this slot
|
|
// draggedComponent.transform.SetParent(transform, false);
|
|
// draggedComponent.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
|
|
// draggedComponent.GetComponent<RectTransform>().offsetMin = Vector2.zero;
|
|
// draggedComponent.GetComponent<RectTransform>().offsetMax = Vector2.zero;
|
|
// } |