Placido_GDD

DragAndDropHandler

Mar 8th, 2022 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6.  
  7. public class DragAndDropHandler : MonoBehaviour
  8. {
  9.     [SerializeField] private UIItemSlot cursorSlot = null;
  10.     private ItemSlot cursorItemSlot;
  11.  
  12.     [SerializeField] private GraphicRaycaster m_Raycaster = null;
  13.     private PointerEventData m_PointerEventData;
  14.     [SerializeField] private EventSystem m_EventSystem = null;
  15.  
  16.     World world;
  17.     private void Start()
  18.     {
  19.         world = GameObject.Find("World").GetComponent<World>();
  20.         cursorItemSlot = new ItemSlot(cursorSlot);
  21.     }
  22.  
  23.     private void Update()
  24.     {
  25.        if (!world.inUI)
  26.         {
  27.             return;
  28.         }
  29.  
  30.         cursorSlot.transform.position = Input.mousePosition;
  31.        if(Input.GetMouseButtonDown(0))
  32.         {
  33.             HandleSlotClick(CheckForSlot());
  34.         }
  35.     }
  36.  
  37.     private void HandleSlotClick(UIItemSlot clickedSlot)
  38.     {
  39.         if (clickedSlot == null)
  40.         {
  41.             return;
  42.         }
  43.  
  44.         if (!cursorSlot.HasItem && !clickedSlot.HasItem)
  45.         {
  46.             return;
  47.         }
  48.         if (clickedSlot.itemSlot.isCreative)
  49.         {
  50.             cursorItemSlot.EmptySlot();
  51.             cursorItemSlot.InsertStack(clickedSlot.itemSlot.stack);
  52.         }
  53.         if (!cursorItemSlot.HasItem && clickedSlot.HasItem)
  54.         {
  55.             cursorItemSlot.InsertStack(clickedSlot.itemSlot.TakeAll());
  56.             return;
  57.         }
  58.         if (cursorItemSlot.HasItem && !clickedSlot.HasItem)
  59.         {
  60.             clickedSlot.itemSlot.InsertStack(cursorItemSlot.TakeAll());
  61.             return;
  62.         }
  63.         if (cursorItemSlot.HasItem && clickedSlot.HasItem)
  64.         {
  65.            if (cursorSlot.itemSlot.stack.id != clickedSlot.itemSlot.stack.id)
  66.             {
  67.                 ItemStack oldCursorSlot = cursorSlot.itemSlot.TakeAll();
  68.                 ItemStack oldSlot = clickedSlot.itemSlot.TakeAll();
  69.  
  70.                 clickedSlot.itemSlot.InsertStack(oldCursorSlot);
  71.                 cursorSlot.itemSlot.InsertStack(oldSlot);
  72.             }
  73.            if(cursorSlot.itemSlot.stack.id == clickedSlot.itemSlot.stack.id)
  74.             {
  75.                 Debug.Log("same stack type");
  76.             }
  77.         }
  78.     }
  79.     public UIItemSlot CheckForSlot()
  80.     {
  81.         m_PointerEventData = new PointerEventData(m_EventSystem);
  82.         m_PointerEventData.position = Input.mousePosition;
  83.         List<RaycastResult> results = new List<RaycastResult>();
  84.         m_Raycaster.Raycast(m_PointerEventData, results);
  85.  
  86.         foreach(RaycastResult result in results)
  87.         {
  88.             if(result.gameObject.tag == "UIItemSlot")
  89.                 return result.gameObject.GetComponent<UIItemSlot>();
  90.         }
  91.  
  92.         return null;
  93.     }
  94. }
  95.  
Add Comment
Please, Sign In to add comment