Advertisement
Guest User

Drag Item.cs for Inventory Master Assets - Unity3d

a guest
Mar 15th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 35.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5.  
  6. public class DragItem : MonoBehaviour, IDragHandler, IPointerDownHandler, IEndDragHandler
  7. {
  8.     private Vector2 pointerOffset;
  9.     private RectTransform rectTransform;
  10.     private RectTransform rectTransformSlot;
  11.     private CanvasGroup canvasGroup;
  12.     private GameObject oldSlot;
  13.     private Inventory inventory;
  14.     private Transform draggedItemBox;
  15.     private Inventory DragI;
  16.     private GameObject _player;
  17.     private Transform transformItem;
  18.     private Inventory mainInventory;
  19.     public delegate void ItemDelegate();
  20.     public static event ItemDelegate updateInventoryList;
  21.     void Start()
  22.     {
  23.         rectTransform = GetComponent<RectTransform>();
  24.         canvasGroup = GetComponent<CanvasGroup>();
  25.         rectTransformSlot = GameObject.FindGameObjectWithTag("DraggingItem").GetComponent<RectTransform>();
  26.  
  27.         GameObject canvas = GameObject.FindGameObjectWithTag("Canvas");
  28.  
  29.         /*for (int i = 0; i < canvas.transform.childCount; i++)
  30.         {
  31.             GameObject child = canvas.transform.GetChild(i).gameObject;
  32.             if (child.tag == "MainInventory")
  33.             {
  34.                
  35.                     Inventory DragI = child.GetComponent<Inventory>();
  36.             }
  37.  
  38.  
  39.  
  40.         }*/
  41.         //GameObject DragI = gameObject;
  42.         //inventory = DragI.transform.parent.parent.parent.GetComponent<Inventory> ();
  43.  
  44.             inventory = transform.parent.parent.parent.parent.GetComponent<Inventory> ();
  45.             draggedItemBox = GameObject.FindGameObjectWithTag ("DraggingItem").transform;
  46.  
  47.     }
  48.  
  49.  
  50.     public void OnDrag(PointerEventData data)
  51.     {
  52.         if (rectTransform == null)
  53.             return;
  54.  
  55.         if (data.button == PointerEventData.InputButton.Left && transform.parent.GetComponent<CraftResultSlot>() == null)
  56.         {
  57.             rectTransform.SetAsLastSibling();
  58.             transform.SetParent(draggedItemBox);
  59.             Vector2 localPointerPosition;
  60.             canvasGroup.blocksRaycasts = false;
  61.             if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransformSlot, Input.mousePosition, data.pressEventCamera, out localPointerPosition))
  62.             {
  63.                 rectTransform.localPosition = localPointerPosition - pointerOffset;
  64.                 if (transform.GetComponent<ConsumeItem>().duplication != null)
  65.                     Destroy(transform.GetComponent<ConsumeItem>().duplication);
  66.             }
  67.         }
  68.  
  69.         inventory.OnUpdateItemList();
  70.     }
  71.  
  72.  
  73.  
  74.     public void OnPointerDown(PointerEventData data)
  75.     {
  76.         if (data.button == PointerEventData.InputButton.Left)
  77.         {
  78.             RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, data.position, data.pressEventCamera, out pointerOffset);
  79.             oldSlot = transform.parent.gameObject;
  80.         }
  81.         if (updateInventoryList != null)
  82.             updateInventoryList();
  83.     }
  84.  
  85.     public void createDuplication(GameObject Item)
  86.     {
  87.         Item item = Item.GetComponent<ItemOnObject>().item;
  88.         GameObject duplication = GameObject.FindGameObjectWithTag("MainInventory").GetComponent<Inventory>().addItemToInventory(item.itemID, item.itemValue);
  89.         duplication.transform.parent.parent.parent.GetComponent<Inventory>().stackableSettings();
  90.         Item.GetComponent<ConsumeItem>().duplication = duplication;
  91.         duplication.GetComponent<ConsumeItem>().duplication = Item;
  92.     }
  93.  
  94.     public void OnEndDrag(PointerEventData data)
  95.     {
  96.         if (data.button == PointerEventData.InputButton.Left)
  97.         {
  98.             canvasGroup.blocksRaycasts = true;
  99.             Transform newSlot = null;
  100.             if (data.pointerEnter != null)
  101.                 newSlot = data.pointerEnter.transform;
  102.  
  103.             if (newSlot != null)
  104.             {
  105.                 //getting the items from the slots, GameObjects and RectTransform
  106.                 GameObject firstItemGameObject = this.gameObject;
  107.                 GameObject secondItemGameObject = newSlot.parent.gameObject;
  108.                 RectTransform firstItemRectTransform = this.gameObject.GetComponent<RectTransform>();
  109.                 RectTransform secondItemRectTransform = newSlot.parent.GetComponent<RectTransform>();
  110.                 Item firstItem = rectTransform.GetComponent<ItemOnObject>().item;
  111.                 Item secondItem = new Item();
  112.                 if (newSlot.parent.GetComponent<ItemOnObject>() != null)
  113.                     secondItem = newSlot.parent.GetComponent<ItemOnObject>().item;
  114.  
  115.                 //get some informations about the two items
  116.                 bool sameItem = firstItem.itemName == secondItem.itemName;
  117.                 bool sameItemRerferenced = firstItem.Equals(secondItem);
  118.                 bool secondItemStack = false;
  119.                 bool firstItemStack = false;
  120.                 if (sameItem)
  121.                 {
  122.                     firstItemStack = firstItem.itemValue < firstItem.maxStack;
  123.                     secondItemStack = secondItem.itemValue < secondItem.maxStack;
  124.                 }
  125.  
  126.                 GameObject Inventory = secondItemRectTransform.parent.gameObject;
  127.                 if (Inventory.tag == "Slot")
  128.                     Inventory = secondItemRectTransform.parent.parent.parent.gameObject;
  129.  
  130.                 if (Inventory.tag.Equals("Slot"))
  131.                     Inventory = Inventory.transform.parent.parent.gameObject;
  132.  
  133.                 //dragging in an Inventory      
  134.                 if (Inventory.GetComponent<Hotbar>() == null && Inventory.GetComponent<EquipmentSystem>() == null && Inventory.GetComponent<CraftSystem>() == null)
  135.                 {
  136.                     //you cannot attach items to the resultslot of the craftsystem
  137.                     if (newSlot.transform.parent.tag == "ResultSlot" || newSlot.transform.tag == "ResultSlot" || newSlot.transform.parent.parent.tag == "ResultSlot")
  138.                     {
  139.                         firstItemGameObject.transform.SetParent(oldSlot.transform);
  140.                         firstItemRectTransform.localPosition = Vector3.zero;
  141.                     }
  142.                     else
  143.                     {
  144.                         int newSlotChildCount = newSlot.transform.parent.childCount;
  145.                         bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
  146.                         //dragging on a slot where allready is an item on
  147.                         if (newSlotChildCount != 0 && isOnSlot)
  148.                         {
  149.                             //check if the items fits into the other item
  150.                             bool fitsIntoStack = false;
  151.                             if (sameItem)
  152.                                 fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
  153.                             //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items
  154.  
  155.                             if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
  156.                             {
  157.                                 //if the item does not fit into the other item
  158.                                 if (fitsIntoStack && !sameItemRerferenced)
  159.                                 {
  160.                                     secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
  161.                                     secondItemGameObject.transform.SetParent(newSlot.parent.parent);
  162.                                     Destroy(firstItemGameObject);
  163.                                     secondItemRectTransform.localPosition = Vector3.zero;
  164.                                     if (secondItemGameObject.GetComponent<ConsumeItem>().duplication != null)
  165.                                     {
  166.                                         GameObject dup = secondItemGameObject.GetComponent<ConsumeItem>().duplication;
  167.                                         dup.GetComponent<ItemOnObject>().item.itemValue = secondItem.itemValue;
  168.                                         dup.GetComponent<SplitItem>().inv.stackableSettings();
  169.                                         dup.transform.parent.parent.parent.GetComponent<Inventory>().updateItemList();
  170.                                     }
  171.                                 }
  172.  
  173.                                 else
  174.                                 {
  175.                                     //creates the rest of the item
  176.                                     int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  177.  
  178.                                     //fill up the other stack and adds the rest to the other stack
  179.                                     if (!fitsIntoStack && rest > 0)
  180.                                     {
  181.                                         firstItem.itemValue = firstItem.maxStack;
  182.                                         secondItem.itemValue = rest;
  183.  
  184.                                         firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  185.                                         secondItemGameObject.transform.SetParent(oldSlot.transform);
  186.  
  187.                                         firstItemRectTransform.localPosition = Vector3.zero;
  188.                                         secondItemRectTransform.localPosition = Vector3.zero;
  189.                                     }
  190.                                 }
  191.  
  192.                             }
  193.                             //if does not fit
  194.                             else
  195.                             {
  196.                                 //creates the rest of the item
  197.                                 int rest = 0;
  198.                                 if (sameItem)
  199.                                     rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  200.  
  201.                                 //fill up the other stack and adds the rest to the other stack
  202.                                 if (!fitsIntoStack && rest > 0)
  203.                                 {
  204.                                     secondItem.itemValue = firstItem.maxStack;
  205.                                     firstItem.itemValue = rest;
  206.  
  207.                                     firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  208.                                     secondItemGameObject.transform.SetParent(oldSlot.transform);
  209.  
  210.                                     firstItemRectTransform.localPosition = Vector3.zero;
  211.                                     secondItemRectTransform.localPosition = Vector3.zero;
  212.                                 }
  213.                                 //if they are different items or the stack is full, they get swapped
  214.                                 else if (!fitsIntoStack && rest == 0)
  215.                                 {
  216.                                     //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
  217.                                     if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
  218.                                     {
  219.                                         newSlot.transform.parent.parent.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  220.                                         oldSlot.transform.parent.parent.GetComponent<Inventory>().EquiptItem(secondItem);
  221.  
  222.                                         firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  223.                                         secondItemGameObject.transform.SetParent(oldSlot.transform);
  224.                                         secondItemRectTransform.localPosition = Vector3.zero;
  225.                                         firstItemRectTransform.localPosition = Vector3.zero;
  226.  
  227.                                         if (secondItemGameObject.GetComponent<ConsumeItem>().duplication != null)
  228.                                             Destroy(secondItemGameObject.GetComponent<ConsumeItem>().duplication);
  229.  
  230.                                     }
  231.                                     //if you are dragging an item from the equipmentsystem to the inventory and they are not from the same itemtype they do not get swapped.                                    
  232.                                     else if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
  233.                                     {
  234.                                         firstItemGameObject.transform.SetParent(oldSlot.transform);
  235.                                         firstItemRectTransform.localPosition = Vector3.zero;
  236.                                     }
  237.                                     //swapping for the rest of the inventorys
  238.                                     else if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null)
  239.                                     {
  240.                                         firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  241.                                         secondItemGameObject.transform.SetParent(oldSlot.transform);
  242.                                         secondItemRectTransform.localPosition = Vector3.zero;
  243.                                         firstItemRectTransform.localPosition = Vector3.zero;
  244.                                     }
  245.                                 }
  246.  
  247.                             }
  248.  
  249.                         }
  250.  
  251.                         //empty slot
  252.                         else
  253.                         {
  254.                             if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
  255.                             {
  256.                                 firstItemGameObject.transform.SetParent(oldSlot.transform);
  257.                                 firstItemRectTransform.localPosition = Vector3.zero;
  258.                             }
  259.                             else
  260.                             {                                
  261.                                 firstItemGameObject.transform.SetParent(newSlot.transform);
  262.                                 firstItemRectTransform.localPosition = Vector3.zero;
  263.  
  264.                                 if (newSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null)
  265.                                     oldSlot.transform.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  266.                             }
  267.                         }
  268.                     }
  269.                 }
  270.  
  271.  
  272.  
  273.                 //dragging into a Hotbar            
  274.                 if (Inventory.GetComponent<Hotbar>() != null)
  275.                 {
  276.                     int newSlotChildCount = newSlot.transform.parent.childCount;
  277.                     bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
  278.                     //dragging on a slot where allready is an item on
  279.                     if (newSlotChildCount != 0 && isOnSlot)
  280.                     {
  281.                         //check if the items fits into the other item
  282.                         bool fitsIntoStack = false;
  283.                         if (sameItem)
  284.                             fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
  285.                         //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items
  286.  
  287.                         if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
  288.                         {
  289.                             //if the item does not fit into the other item
  290.                             if (fitsIntoStack && !sameItemRerferenced)
  291.                             {
  292.                                 secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
  293.                                 secondItemGameObject.transform.SetParent(newSlot.parent.parent);
  294.                                 Destroy(firstItemGameObject);
  295.                                 secondItemRectTransform.localPosition = Vector3.zero;
  296.                                 if (secondItemGameObject.GetComponent<ConsumeItem>().duplication != null)
  297.                                 {
  298.                                     GameObject dup = secondItemGameObject.GetComponent<ConsumeItem>().duplication;
  299.                                     dup.GetComponent<ItemOnObject>().item.itemValue = secondItem.itemValue;
  300.                                     Inventory.GetComponent<Inventory>().stackableSettings();
  301.                                     dup.transform.parent.parent.parent.GetComponent<Inventory>().updateItemList();
  302.                                 }
  303.                             }
  304.  
  305.                             else
  306.                             {
  307.                                 //creates the rest of the item
  308.                                 int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  309.  
  310.                                 //fill up the other stack and adds the rest to the other stack
  311.                                 if (!fitsIntoStack && rest > 0)
  312.                                 {
  313.                                     firstItem.itemValue = firstItem.maxStack;
  314.                                     secondItem.itemValue = rest;
  315.  
  316.                                     firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  317.                                     secondItemGameObject.transform.SetParent(oldSlot.transform);
  318.  
  319.                                     firstItemRectTransform.localPosition = Vector3.zero;
  320.                                     secondItemRectTransform.localPosition = Vector3.zero;
  321.  
  322.                                     createDuplication(this.gameObject);
  323.                                     secondItemGameObject.GetComponent<ConsumeItem>().duplication.GetComponent<ItemOnObject>().item = secondItem;
  324.                                     secondItemGameObject.GetComponent<SplitItem>().inv.stackableSettings();
  325.  
  326.                                 }
  327.                             }
  328.  
  329.                         }
  330.                         //if does not fit
  331.                         else
  332.                         {
  333.                             //creates the rest of the item
  334.                             int rest = 0;
  335.                             if (sameItem)
  336.                                 rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  337.  
  338.                             bool fromEquip = oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null;
  339.  
  340.                             //fill up the other stack and adds the rest to the other stack
  341.                             if (!fitsIntoStack && rest > 0)
  342.                             {
  343.                                 secondItem.itemValue = firstItem.maxStack;
  344.                                 firstItem.itemValue = rest;
  345.  
  346.                                 createDuplication(this.gameObject);
  347.  
  348.                                 firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  349.                                 secondItemGameObject.transform.SetParent(oldSlot.transform);
  350.  
  351.                                 firstItemRectTransform.localPosition = Vector3.zero;
  352.                                 secondItemRectTransform.localPosition = Vector3.zero;
  353.  
  354.                             }
  355.                             //if they are different items or the stack is full, they get swapped
  356.                             else if (!fitsIntoStack && rest == 0)
  357.                             {
  358.                                 if (!fromEquip)
  359.                                 {
  360.                                     firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  361.                                     secondItemGameObject.transform.SetParent(oldSlot.transform);
  362.                                     secondItemRectTransform.localPosition = Vector3.zero;
  363.                                     firstItemRectTransform.localPosition = Vector3.zero;
  364.  
  365.                                     if (oldSlot.transform.parent.parent.gameObject.Equals(GameObject.FindGameObjectWithTag("MainInventory")))
  366.                                     {
  367.                                         Destroy(secondItemGameObject.GetComponent<ConsumeItem>().duplication);
  368.                                         createDuplication(firstItemGameObject);
  369.                                     }
  370.                                     else
  371.                                     {
  372.                                         createDuplication(firstItemGameObject);
  373.                                     }
  374.                                 }
  375.                                 else
  376.                                 {
  377.                                     firstItemGameObject.transform.SetParent(oldSlot.transform);
  378.                                     firstItemRectTransform.localPosition = Vector3.zero;
  379.                                 }
  380.                             }
  381.  
  382.                         }
  383.                     }
  384.                     //empty slot
  385.                     else
  386.                     {
  387.                         if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
  388.                         {
  389.                             firstItemGameObject.transform.SetParent(oldSlot.transform);
  390.                             firstItemRectTransform.localPosition = Vector3.zero;
  391.                         }
  392.                         else
  393.                         {                            
  394.                             firstItemGameObject.transform.SetParent(newSlot.transform);
  395.                             firstItemRectTransform.localPosition = Vector3.zero;
  396.  
  397.                             if (newSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null)
  398.                                 oldSlot.transform.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  399.                             createDuplication(firstItemGameObject);
  400.                         }
  401.                     }
  402.  
  403.                 }
  404.  
  405.  
  406.                 //dragging into a equipmentsystem/charactersystem
  407.                 if (Inventory.GetComponent<EquipmentSystem>() != null)
  408.                 {
  409.                     ItemType[] itemTypeOfSlots = GameObject.FindGameObjectWithTag("EquipmentSystem").GetComponent<EquipmentSystem>().itemTypeOfSlots;
  410.                     int newSlotChildCount = newSlot.transform.parent.childCount;
  411.                     bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
  412.                     bool sameItemType = firstItem.itemType == secondItem.itemType;
  413.                     bool fromHot = oldSlot.transform.parent.parent.GetComponent<Hotbar>() != null;
  414.  
  415.                     //dragging on a slot where allready is an item on
  416.                     if (newSlotChildCount != 0 && isOnSlot)
  417.                     {
  418.                         //items getting swapped if they are the same itemtype
  419.                         if (sameItemType && !sameItemRerferenced) //
  420.                         {
  421.                             Transform temp1 = secondItemGameObject.transform.parent.parent.parent;
  422.                             Transform temp2 = oldSlot.transform.parent.parent;                            
  423.  
  424.                             firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  425.                             secondItemGameObject.transform.SetParent(oldSlot.transform);
  426.                             secondItemRectTransform.localPosition = Vector3.zero;
  427.                             firstItemRectTransform.localPosition = Vector3.zero;
  428.  
  429.                             if (!temp1.Equals(temp2))
  430.                             {
  431.                                 if (firstItem.itemType == ItemType.UFPS_Weapon)
  432.                                 {
  433.                                     Inventory.GetComponent<Inventory>().UnEquipItem1(secondItem);
  434.                                     Inventory.GetComponent<Inventory>().EquiptItem(firstItem);
  435.                                 }
  436.                                 else
  437.                                 {
  438.                                     Inventory.GetComponent<Inventory>().EquiptItem(firstItem);
  439.                                     if (secondItem.itemType != ItemType.Backpack)
  440.                                         Inventory.GetComponent<Inventory>().UnEquipItem1(secondItem);
  441.                                 }
  442.                             }
  443.  
  444.                             if (fromHot)
  445.                                 createDuplication(secondItemGameObject);
  446.  
  447.                         }
  448.                         //if they are not from the same Itemtype the dragged one getting placed back
  449.                         else
  450.                         {
  451.                             firstItemGameObject.transform.SetParent(oldSlot.transform);
  452.                             firstItemRectTransform.localPosition = Vector3.zero;
  453.  
  454.                             if (fromHot)
  455.                                 createDuplication(firstItemGameObject);
  456.                         }
  457.  
  458.                     }
  459.                     //if the slot is empty
  460.                     else
  461.                     {
  462.                         for (int i = 0; i < newSlot.parent.childCount; i++)
  463.                         {
  464.                             if (newSlot.Equals(newSlot.parent.GetChild(i)))
  465.                             {
  466.                                 //checking if it is the right slot for the item
  467.                                 if (itemTypeOfSlots[i] == transform.GetComponent<ItemOnObject>().item.itemType)
  468.                                 {
  469.                                     transform.SetParent(newSlot);
  470.                                     rectTransform.localPosition = Vector3.zero;
  471.  
  472.                                     if (!oldSlot.transform.parent.parent.Equals(newSlot.transform.parent.parent))
  473.                                         Inventory.GetComponent<Inventory>().EquiptItem(firstItem);
  474.  
  475.                                 }
  476.                                 //else it get back to the old slot
  477.                                 else
  478.                                 {
  479.                                     transform.SetParent(oldSlot.transform);
  480.                                     rectTransform.localPosition = Vector3.zero;
  481.                                     if (fromHot)
  482.                                         createDuplication(firstItemGameObject);
  483.                                 }
  484.                             }
  485.                         }
  486.                     }
  487.  
  488.                 }
  489.  
  490.                 if (Inventory.GetComponent<CraftSystem>() != null)
  491.                 {
  492.                     CraftSystem cS = Inventory.GetComponent<CraftSystem>();
  493.                     int newSlotChildCount = newSlot.transform.parent.childCount;
  494.  
  495.  
  496.                     bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
  497.                     //dragging on a slot where allready is an item on
  498.                     if (newSlotChildCount != 0 && isOnSlot)
  499.                     {
  500.                         //check if the items fits into the other item
  501.                         bool fitsIntoStack = false;
  502.                         if (sameItem)
  503.                             fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
  504.                         //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items
  505.  
  506.                         if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
  507.                         {
  508.                             //if the item does not fit into the other item
  509.                             if (fitsIntoStack && !sameItemRerferenced)
  510.                             {
  511.                                 secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
  512.                                 secondItemGameObject.transform.SetParent(newSlot.parent.parent);
  513.                                 Destroy(firstItemGameObject);
  514.                                 secondItemRectTransform.localPosition = Vector3.zero;
  515.  
  516.  
  517.                                 if (secondItemGameObject.GetComponent<ConsumeItem>().duplication != null)
  518.                                 {
  519.                                     GameObject dup = secondItemGameObject.GetComponent<ConsumeItem>().duplication;
  520.                                     dup.GetComponent<ItemOnObject>().item.itemValue = secondItem.itemValue;
  521.                                     dup.GetComponent<SplitItem>().inv.stackableSettings();
  522.                                     dup.transform.parent.parent.parent.GetComponent<Inventory>().updateItemList();
  523.                                 }
  524.                                 cS.ListWithItem();
  525.                             }
  526.  
  527.                             else
  528.                             {
  529.                                 //creates the rest of the item
  530.                                 int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  531.  
  532.                                 //fill up the other stack and adds the rest to the other stack
  533.                                 if (!fitsIntoStack && rest > 0)
  534.                                 {
  535.                                     firstItem.itemValue = firstItem.maxStack;
  536.                                     secondItem.itemValue = rest;
  537.  
  538.                                     firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  539.                                     secondItemGameObject.transform.SetParent(oldSlot.transform);
  540.  
  541.                                     firstItemRectTransform.localPosition = Vector3.zero;
  542.                                     secondItemRectTransform.localPosition = Vector3.zero;
  543.                                     cS.ListWithItem();
  544.  
  545.  
  546.                                 }
  547.                             }
  548.  
  549.                         }
  550.                         //if does not fit
  551.                         else
  552.                         {
  553.                             //creates the rest of the item
  554.                             int rest = 0;
  555.                             if (sameItem)
  556.                                 rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  557.  
  558.                             //fill up the other stack and adds the rest to the other stack
  559.                             if (!fitsIntoStack && rest > 0)
  560.                             {
  561.                                 secondItem.itemValue = firstItem.maxStack;
  562.                                 firstItem.itemValue = rest;
  563.  
  564.                                 firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  565.                                 secondItemGameObject.transform.SetParent(oldSlot.transform);
  566.  
  567.                                 firstItemRectTransform.localPosition = Vector3.zero;
  568.                                 secondItemRectTransform.localPosition = Vector3.zero;
  569.                                 cS.ListWithItem();
  570.  
  571.                             }
  572.                             //if they are different items or the stack is full, they get swapped
  573.                             else if (!fitsIntoStack && rest == 0)
  574.                             {
  575.                                 //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
  576.                                 if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
  577.                                 {                                  
  578.  
  579.                                     firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  580.                                     secondItemGameObject.transform.SetParent(oldSlot.transform);
  581.                                     secondItemRectTransform.localPosition = Vector3.zero;
  582.                                     firstItemRectTransform.localPosition = Vector3.zero;
  583.  
  584.                                     oldSlot.transform.parent.parent.GetComponent<Inventory>().EquiptItem(secondItem);
  585.                                     newSlot.transform.parent.parent.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  586.                                 }
  587.                                 //if you are dragging an item from the equipmentsystem to the inventory and they are not from the same itemtype they do not get swapped.                                    
  588.                                 else if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
  589.                                 {
  590.                                     firstItemGameObject.transform.SetParent(oldSlot.transform);
  591.                                     firstItemRectTransform.localPosition = Vector3.zero;
  592.                                 }
  593.                                 //swapping for the rest of the inventorys
  594.                                 else if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null)
  595.                                 {
  596.                                     firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  597.                                     secondItemGameObject.transform.SetParent(oldSlot.transform);
  598.                                     secondItemRectTransform.localPosition = Vector3.zero;
  599.                                     firstItemRectTransform.localPosition = Vector3.zero;
  600.                                 }
  601.                             }
  602.  
  603.                         }
  604.                     }
  605.                     else
  606.                     {
  607.                         if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
  608.                         {
  609.                             firstItemGameObject.transform.SetParent(oldSlot.transform);
  610.                             firstItemRectTransform.localPosition = Vector3.zero;
  611.                         }
  612.                         else
  613.                         {                            
  614.                             firstItemGameObject.transform.SetParent(newSlot.transform);
  615.                             firstItemRectTransform.localPosition = Vector3.zero;
  616.  
  617.                             if (newSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null)
  618.                                 oldSlot.transform.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  619.                         }
  620.                     }
  621.  
  622.                 }
  623.  
  624.  
  625.             }
  626.  
  627.             else
  628.             {
  629.                 GameObject dropItem = (GameObject)Instantiate(GetComponent<ItemOnObject>().item.itemModel);
  630.                 dropItem.AddComponent<PickUpItem>();
  631.                 dropItem.GetComponent<PickUpItem>().item = this.gameObject.GetComponent<ItemOnObject>().item;              
  632.                 dropItem.transform.localPosition = GameObject.FindGameObjectWithTag("MyPlayer").transform.localPosition;
  633.                 inventory.OnUpdateItemList();
  634.                 if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null)
  635.                     inventory.GetComponent<Inventory>().UnEquipItem1(dropItem.GetComponent<PickUpItem>().item);
  636.                 Destroy(this.gameObject);
  637.  
  638.             }
  639.         }
  640.         inventory.OnUpdateItemList();
  641.     }
  642.  
  643. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement