Esempio n. 1
0
    public void OnEndDrag(PointerEventData data)
    {
        if (data.button == PointerEventData.InputButton.Left)
        {
            canvasGroup.blocksRaycasts = true;
            Transform newSlot = null;
            if (data.pointerEnter != null)
            {
                newSlot = data.pointerEnter.transform;
            }

            if (newSlot != null)
            {
                //getting the items from the slots, GameObjects and RectTransform
                GameObject    firstItemGameObject     = this.gameObject;
                GameObject    secondItemGameObject    = newSlot.parent.gameObject;
                RectTransform firstItemRectTransform  = this.gameObject.GetComponent <RectTransform>();
                RectTransform secondItemRectTransform = newSlot.parent.GetComponent <RectTransform>();

                if (secondItemRectTransform == null)
                {
                    return;
                }
                bool byitemonitem = false;
                Item firstItem    = rectTransform.GetComponent <ItemOnObject>().item;
                Item secondItem   = new Item();
                if (newSlot.parent.GetComponent <ItemOnObject>() != null)
                {
                    secondItem = newSlot.parent.GetComponent <ItemOnObject>().item;
                }

                if (secondItemRectTransform.gameObject.name != "PlayerGui")
                {
                    if (firstItemRectTransform.gameObject.name == "Item(Clone)" && secondItemRectTransform.gameObject.name == "Item(Clone)")
                    {
                        Debug.Log("Dragitem " + secondItemRectTransform.parent.parent.parent.name);
                        if (secondItemRectTransform.parent.parent.parent.tag == "MainInventory" || secondItemRectTransform.parent.parent.parent.tag == "Hotbar")
                        {
                            byitemonitem = true;
                        }
                    }
                    //Debug.Log("Error By " + firstItem.outbox.tag+"secondItemRectTransform "+ secondItemRectTransform.gameObject.name);
                    if ((firstItem.outbox.tag == "Shop" && secondItemRectTransform.parent.gameObject.tag == "MainInventory") || (firstItem.outbox.tag == "Shop" && secondItemRectTransform.parent.gameObject.tag == "Hotbar") || byitemonitem)
                    {
                        int pricemengnt = firstItem.price * 10;
                        int PlayerMoney = 0;
                        if (playerIO.isNetwork)
                        {
                            PlayerMoney = networkLayerClient.player.money;
                        }
                        else
                        {
                            PlayerMoney = GameObject.Find("GameLocal").GetComponent <Gamelocal>().PlayerMoney;
                        }

                        if (PlayerMoney < pricemengnt)
                        {
                            if (playerIO.isNetwork)
                            {
                                MenuManager.instance.Debuglog("Нехватает денег");
                            }
                            else
                            {
                                GameObject.Find("GameLocal").GetComponent <Gamelocal>().Debuglog("Нехватает денег");
                            }
                            Item      returitem     = firstItem;
                            Inventory shopinventory = returitem.outbox.GetComponent <Inventory>();
                            //Component[] components = returitem.outbox.GetComponents<Inventory>();
                            shopinventory.addItemToInventory(returitem.itemID, returitem.itemValue);
                            shopinventory.OnUpdateItemList();
                            Destroy(rectTransform.gameObject);
                            return;
                        }
                        else
                        {
                            if (playerIO.isNetwork)
                            {
                                PlayerMoney = networkLayerClient.player.money;
                                networkLayerClient.buy_items(firstItem, 1);
                                //buy_items
                            }
                            else
                            {
                                PlayerMoney -= pricemengnt;
                                PlayerMoney  = GameObject.Find("GameLocal").GetComponent <Gamelocal>().PlayerMoney;
                                Inventory playerinventory = GameObject.Find("PlayerGui").transform.GetChild(1).GetComponent <Inventory>();
                                //createDuplication(GameObject Item)
                                if (!playerinventory.checkIfItemAllreadyExist(firstItem.itemID, 1))
                                {
                                    playerinventory.addItemToInventory(firstItem.itemID);
                                }
                            }


                            Item      returitem     = firstItem;
                            Inventory shopinventory = returitem.outbox.GetComponent <Inventory>();
                            //Component[] components = returitem.outbox.GetComponents<Inventory>();
                            shopinventory.addItemToInventory(returitem.itemID, returitem.itemValue);

                            shopinventory.OnUpdateItemList();
                            Destroy(rectTransform.gameObject);

                            return;
                        }

                        //  Debug.Log("By item previem box" + firstItem.outbox.tag + " newSlot" + secondItemRectTransform.parent.gameObject.tag);
                    }
                }

                if (secondItemRectTransform.gameObject.name == "PlayerGui")
                {
                    Item      returitem     = firstItem;
                    Inventory shopinventory = returitem.outbox.GetComponent <Inventory>();
                    //Component[] components = returitem.outbox.GetComponents<Inventory>();
                    shopinventory.addItemToInventory(returitem.itemID, returitem.itemValue);
                    shopinventory.OnUpdateItemList();

                    Destroy(rectTransform.gameObject);
                    return;
                }


                if (firstItemRectTransform.gameObject.name == "Item(Clone)" && secondItemRectTransform.gameObject.name == "Item(Clone)")
                {
                    Debug.Log("Dragitem " + secondItemRectTransform.parent.parent.parent.name);
                    if (secondItemRectTransform.parent.parent.parent.tag == "Shop")
                    {
                        byitemonitem = true;
                    }
                }

                if ((firstItem.outbox.tag == "MainInventory" && secondItemRectTransform.parent.gameObject.tag == "Shop") || (firstItem.outbox.tag == "Hotbar" && secondItemRectTransform.parent.gameObject.tag == "Shop") || byitemonitem)
                {
                    // Gamelocal.instance.PlayerMoney = Gamelocal.instance.PlayerMoney + (firstItem.price * firstItem.itemValue);
                    if (playerIO.isNetwork)
                    {
                        networkLayerClient.sell_items(firstItem);
                    }
                    else
                    {
                        int PlayerMoney = GameObject.Find("GameLocal").GetComponent <Gamelocal>().PlayerMoney;
                        PlayerMoney += firstItem.price * firstItem.itemValue;
                        GameObject.Find("GameLocal").GetComponent <Gamelocal>().PlayerMoney = PlayerMoney;
                    }
                    Destroy(rectTransform.gameObject);
                    Debug.Log("Sell item previem box" + firstItem.outbox.tag + " newSlot" + secondItemRectTransform.parent.gameObject.tag);
                    return;
                }
                //get some informations about the two items
                bool sameItem            = firstItem.itemName == secondItem.itemName;
                bool sameItemRerferenced = firstItem.Equals(secondItem);
                bool secondItemStack     = false;
                bool firstItemStack      = false;
                if (sameItem)
                {
                    firstItemStack  = firstItem.itemValue < firstItem.maxStack;
                    secondItemStack = secondItem.itemValue < secondItem.maxStack;
                }

                // Debug.Log("Error By " + firstItemRectTransform.gameObject.name + " secondItemRectTransform "+ secondItemRectTransform.gameObject.name);
                if (secondItemRectTransform.gameObject.name == "PlayerGui")
                {
                    Item      returitem     = firstItem;
                    Inventory shopinventory = returitem.outbox.GetComponent <Inventory>();
                    //Component[] components = returitem.outbox.GetComponents<Inventory>();
                    shopinventory.addItemToInventory(returitem.itemID, returitem.itemValue);
                    shopinventory.OnUpdateItemList();

                    Destroy(rectTransform.gameObject);
                    return;
                }

                GameObject Inventory = secondItemRectTransform.parent.gameObject;
                if (Inventory.tag == "Slot")
                {
                    Inventory = secondItemRectTransform.parent.parent.parent.gameObject;
                }

                if (Inventory.tag.Equals("Slot"))
                {
                    Inventory = Inventory.transform.parent.parent.gameObject;
                }

                //dragging in an Inventory
                if (Inventory.GetComponent <Hotbar>() == null && Inventory.GetComponent <EquipmentSystem>() == null && Inventory.GetComponent <CraftSystem>() == null)
                {
                    //you cannot attach items to the resultslot of the craftsystem
                    if (newSlot.transform.parent.tag == "ResultSlot" || newSlot.transform.tag == "ResultSlot" || newSlot.transform.parent.parent.tag == "ResultSlot")
                    {
                        firstItemGameObject.transform.SetParent(oldSlot.transform);
                        firstItemRectTransform.localPosition = Vector3.zero;
                    }
                    else
                    {
                        int  newSlotChildCount = newSlot.transform.parent.childCount;
                        bool isOnSlot          = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                        //dragging on a slot where allready is an item on
                        if (newSlotChildCount != 0 && isOnSlot)
                        {
                            //check if the items fits into the other item
                            bool fitsIntoStack = false;
                            if (sameItem)
                            {
                                fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
                            }
                            //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items

                            if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
                            {
                                //if the item does not fit into the other item
                                if (fitsIntoStack && !sameItemRerferenced)
                                {
                                    secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
                                    secondItemGameObject.transform.SetParent(newSlot.parent.parent);
                                    Destroy(firstItemGameObject);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                    {
                                        GameObject dup = secondItemGameObject.GetComponent <ConsumeItem>().duplication;
                                        dup.GetComponent <ItemOnObject>().item.itemValue = secondItem.itemValue;
                                        dup.GetComponent <SplitItem>().inv.stackableSettings();
                                        dup.transform.parent.parent.parent.GetComponent <Inventory>().updateItemList();
                                    }
                                }

                                else
                                {
                                    //creates the rest of the item
                                    int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;

                                    //fill up the other stack and adds the rest to the other stack
                                    if (!fitsIntoStack && rest > 0)
                                    {
                                        firstItem.itemValue  = firstItem.maxStack;
                                        secondItem.itemValue = rest;

                                        firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                        secondItemGameObject.transform.SetParent(oldSlot.transform);

                                        firstItemRectTransform.localPosition  = Vector3.zero;
                                        secondItemRectTransform.localPosition = Vector3.zero;
                                    }
                                }
                            }
                            //if does not fit
                            else
                            {
                                //creates the rest of the item
                                int rest = 0;
                                if (sameItem)
                                {
                                    rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
                                }

                                //fill up the other stack and adds the rest to the other stack
                                if (!fitsIntoStack && rest > 0)
                                {
                                    secondItem.itemValue = firstItem.maxStack;
                                    firstItem.itemValue  = rest;

                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);

                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                }
                                //if they are different items or the stack is full, they get swapped
                                else if (!fitsIntoStack && rest == 0)
                                {
                                    //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
                                    if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
                                    {
                                        newSlot.transform.parent.parent.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                                        oldSlot.transform.parent.parent.GetComponent <Inventory>().EquiptItem(secondItem);

                                        firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                        secondItemGameObject.transform.SetParent(oldSlot.transform);
                                        secondItemRectTransform.localPosition = Vector3.zero;
                                        firstItemRectTransform.localPosition  = Vector3.zero;

                                        if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                        {
                                            Destroy(secondItemGameObject.GetComponent <ConsumeItem>().duplication);
                                        }
                                    }
                                    //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.
                                    else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
                                    {
                                        firstItemGameObject.transform.SetParent(oldSlot.transform);
                                        firstItemRectTransform.localPosition = Vector3.zero;
                                    }
                                    //swapping for the rest of the inventorys
                                    else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null)
                                    {
                                        firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                        secondItemGameObject.transform.SetParent(oldSlot.transform);
                                        secondItemRectTransform.localPosition = Vector3.zero;
                                        firstItemRectTransform.localPosition  = Vector3.zero;
                                    }
                                }
                            }
                        }

                        //empty slot
                        else
                        {
                            if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
                            {
                                firstItemGameObject.transform.SetParent(oldSlot.transform);
                                firstItemRectTransform.localPosition = Vector3.zero;
                            }
                            else
                            {
                                firstItemGameObject.transform.SetParent(newSlot.transform);
                                firstItemRectTransform.localPosition = Vector3.zero;

                                if (newSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                                {
                                    oldSlot.transform.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                                }
                            }
                        }
                    }
                }



                //dragging into a Hotbar
                if (Inventory.GetComponent <Hotbar>() != null)
                {
                    int  newSlotChildCount = newSlot.transform.parent.childCount;
                    bool isOnSlot          = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                    //dragging on a slot where allready is an item on
                    if (newSlotChildCount != 0 && isOnSlot)
                    {
                        //check if the items fits into the other item
                        bool fitsIntoStack = false;
                        if (sameItem)
                        {
                            fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
                        }
                        //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items

                        if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
                        {
                            //if the item does not fit into the other item
                            if (fitsIntoStack && !sameItemRerferenced)
                            {
                                secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
                                secondItemGameObject.transform.SetParent(newSlot.parent.parent);
                                Destroy(firstItemGameObject);
                                secondItemRectTransform.localPosition = Vector3.zero;
                                if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                {
                                    GameObject dup = secondItemGameObject.GetComponent <ConsumeItem>().duplication;
                                    dup.GetComponent <ItemOnObject>().item.itemValue = secondItem.itemValue;
                                    Inventory.GetComponent <Inventory>().stackableSettings();
                                    dup.transform.parent.parent.parent.GetComponent <Inventory>().updateItemList();
                                }
                            }

                            else
                            {
                                //creates the rest of the item
                                int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;

                                //fill up the other stack and adds the rest to the other stack
                                if (!fitsIntoStack && rest > 0)
                                {
                                    firstItem.itemValue  = firstItem.maxStack;
                                    secondItem.itemValue = rest;

                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);

                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                    secondItemRectTransform.localPosition = Vector3.zero;

                                    createDuplication(this.gameObject);
                                    secondItemGameObject.GetComponent <ConsumeItem>().duplication.GetComponent <ItemOnObject>().item = secondItem;
                                    secondItemGameObject.GetComponent <SplitItem>().inv.stackableSettings();
                                }
                            }
                        }
                        //if does not fit
                        else
                        {
                            //creates the rest of the item
                            int rest = 0;
                            if (sameItem)
                            {
                                rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
                            }

                            bool fromEquip = oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null;

                            //fill up the other stack and adds the rest to the other stack
                            if (!fitsIntoStack && rest > 0)
                            {
                                secondItem.itemValue = firstItem.maxStack;
                                firstItem.itemValue  = rest;

                                createDuplication(this.gameObject);

                                firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                secondItemGameObject.transform.SetParent(oldSlot.transform);

                                firstItemRectTransform.localPosition  = Vector3.zero;
                                secondItemRectTransform.localPosition = Vector3.zero;
                            }
                            //if they are different items or the stack is full, they get swapped
                            else if (!fitsIntoStack && rest == 0)
                            {
                                if (!fromEquip)
                                {
                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    firstItemRectTransform.localPosition  = Vector3.zero;

                                    if (oldSlot.transform.parent.parent.gameObject.Equals(GameObject.FindGameObjectWithTag("MainInventory")))
                                    {
                                        Destroy(secondItemGameObject.GetComponent <ConsumeItem>().duplication);
                                        createDuplication(firstItemGameObject);
                                    }
                                    else
                                    {
                                        createDuplication(firstItemGameObject);
                                    }
                                }
                                else
                                {
                                    firstItemGameObject.transform.SetParent(oldSlot.transform);
                                    firstItemRectTransform.localPosition = Vector3.zero;
                                }
                            }
                        }
                    }
                    //empty slot
                    else
                    {
                        if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
                        {
                            firstItemGameObject.transform.SetParent(oldSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;
                        }
                        else
                        {
                            firstItemGameObject.transform.SetParent(newSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;

                            if (newSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                            {
                                oldSlot.transform.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                            }
                            createDuplication(firstItemGameObject);
                        }
                    }
                }


                //dragging into a equipmentsystem/charactersystem
                if (Inventory.GetComponent <EquipmentSystem>() != null)
                {
                    ItemType[] itemTypeOfSlots   = GameObject.FindGameObjectWithTag("EquipmentSystem").GetComponent <EquipmentSystem>().itemTypeOfSlots;
                    int        newSlotChildCount = newSlot.transform.parent.childCount;
                    bool       isOnSlot          = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                    bool       sameItemType      = firstItem.itemType == secondItem.itemType;
                    bool       fromHot           = oldSlot.transform.parent.parent.GetComponent <Hotbar>() != null;

                    //dragging on a slot where allready is an item on
                    if (newSlotChildCount != 0 && isOnSlot)
                    {
                        //items getting swapped if they are the same itemtype
                        if (sameItemType && !sameItemRerferenced) //
                        {
                            Transform temp1 = secondItemGameObject.transform.parent.parent.parent;
                            Transform temp2 = oldSlot.transform.parent.parent;

                            firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                            secondItemGameObject.transform.SetParent(oldSlot.transform);
                            secondItemRectTransform.localPosition = Vector3.zero;
                            firstItemRectTransform.localPosition  = Vector3.zero;

                            if (!temp1.Equals(temp2))
                            {
                                if (firstItem.itemType == ItemType.UFPS_Weapon)
                                {
                                    Inventory.GetComponent <Inventory>().UnEquipItem1(secondItem);
                                    Inventory.GetComponent <Inventory>().EquiptItem(firstItem);
                                }
                                else
                                {
                                    Inventory.GetComponent <Inventory>().EquiptItem(firstItem);
                                    if (secondItem.itemType != ItemType.Backpack)
                                    {
                                        Inventory.GetComponent <Inventory>().UnEquipItem1(secondItem);
                                    }
                                }
                            }

                            if (fromHot)
                            {
                                createDuplication(secondItemGameObject);
                            }
                        }
                        //if they are not from the same Itemtype the dragged one getting placed back
                        else
                        {
                            firstItemGameObject.transform.SetParent(oldSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;

                            if (fromHot)
                            {
                                createDuplication(firstItemGameObject);
                            }
                        }
                    }
                    //if the slot is empty
                    else
                    {
                        for (int i = 0; i < newSlot.parent.childCount; i++)
                        {
                            if (newSlot.Equals(newSlot.parent.GetChild(i)))
                            {
                                //checking if it is the right slot for the item
                                if (itemTypeOfSlots[i] == transform.GetComponent <ItemOnObject>().item.itemType)
                                {
                                    transform.SetParent(newSlot);
                                    rectTransform.localPosition = Vector3.zero;

                                    if (!oldSlot.transform.parent.parent.Equals(newSlot.transform.parent.parent))
                                    {
                                        Inventory.GetComponent <Inventory>().EquiptItem(firstItem);
                                    }
                                }
                                //else it get back to the old slot
                                else
                                {
                                    transform.SetParent(oldSlot.transform);
                                    rectTransform.localPosition = Vector3.zero;
                                    if (fromHot)
                                    {
                                        createDuplication(firstItemGameObject);
                                    }
                                }
                            }
                        }
                    }
                }

                if (Inventory.GetComponent <CraftSystem>() != null)
                {
                    CraftSystem cS = Inventory.GetComponent <CraftSystem>();
                    int         newSlotChildCount = newSlot.transform.parent.childCount;


                    bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                    //dragging on a slot where allready is an item on
                    if (newSlotChildCount != 0 && isOnSlot)
                    {
                        //check if the items fits into the other item
                        bool fitsIntoStack = false;
                        if (sameItem)
                        {
                            fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
                        }
                        //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items

                        if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
                        {
                            //if the item does not fit into the other item
                            if (fitsIntoStack && !sameItemRerferenced)
                            {
                                secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
                                secondItemGameObject.transform.SetParent(newSlot.parent.parent);
                                Destroy(firstItemGameObject);
                                secondItemRectTransform.localPosition = Vector3.zero;


                                if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                {
                                    GameObject dup = secondItemGameObject.GetComponent <ConsumeItem>().duplication;
                                    dup.GetComponent <ItemOnObject>().item.itemValue = secondItem.itemValue;
                                    dup.GetComponent <SplitItem>().inv.stackableSettings();
                                    dup.transform.parent.parent.parent.GetComponent <Inventory>().updateItemList();
                                }
                                cS.ListWithItem();
                            }

                            else
                            {
                                //creates the rest of the item
                                int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;

                                //fill up the other stack and adds the rest to the other stack
                                if (!fitsIntoStack && rest > 0)
                                {
                                    firstItem.itemValue  = firstItem.maxStack;
                                    secondItem.itemValue = rest;

                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);

                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    cS.ListWithItem();
                                }
                            }
                        }
                        //if does not fit
                        else
                        {
                            //creates the rest of the item
                            int rest = 0;
                            if (sameItem)
                            {
                                rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
                            }

                            //fill up the other stack and adds the rest to the other stack
                            if (!fitsIntoStack && rest > 0)
                            {
                                secondItem.itemValue = firstItem.maxStack;
                                firstItem.itemValue  = rest;

                                firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                secondItemGameObject.transform.SetParent(oldSlot.transform);

                                firstItemRectTransform.localPosition  = Vector3.zero;
                                secondItemRectTransform.localPosition = Vector3.zero;
                                cS.ListWithItem();
                            }
                            //if they are different items or the stack is full, they get swapped
                            else if (!fitsIntoStack && rest == 0)
                            {
                                //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
                                if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
                                {
                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    firstItemRectTransform.localPosition  = Vector3.zero;

                                    oldSlot.transform.parent.parent.GetComponent <Inventory>().EquiptItem(secondItem);
                                    newSlot.transform.parent.parent.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                                }
                                //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.
                                else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
                                {
                                    firstItemGameObject.transform.SetParent(oldSlot.transform);
                                    firstItemRectTransform.localPosition = Vector3.zero;
                                }
                                //swapping for the rest of the inventorys
                                else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null)
                                {
                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
                        {
                            firstItemGameObject.transform.SetParent(oldSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;
                        }
                        else
                        {
                            firstItemGameObject.transform.SetParent(newSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;

                            if (newSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                            {
                                oldSlot.transform.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                            }
                        }
                    }
                }
            }

            else
            {
                if (GetComponent <ItemOnObject>().item.outbox.tag == "Shop")
                {
                    Item        returitem     = GetComponent <ItemOnObject>().item;
                    Inventory   shopinventory = returitem.outbox.GetComponent <Inventory>();
                    Component[] components    = returitem.outbox.GetComponents <Inventory>();
                    shopinventory.addItemToInventory(returitem.itemID, returitem.itemValue);

                    shopinventory.OnUpdateItemList();
                    Destroy(this.gameObject);
                }
                else
                {
                    GameObject dropItem = (GameObject)Instantiate(GetComponent <ItemOnObject>().item.itemModel);
                    dropItem.AddComponent <PickUpItem>();
                    dropItem.GetComponent <PickUpItem>().item = this.gameObject.GetComponent <ItemOnObject>().item;
                    dropItem.transform.localPosition          = GameObject.FindGameObjectWithTag("Player").transform.localPosition;
                    inventory.OnUpdateItemList();
                    if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                    {
                        inventory.GetComponent <Inventory>().UnEquipItem1(dropItem.GetComponent <PickUpItem>().item);
                    }
                    Destroy(this.gameObject);
                }
            }
        }
        inventory.OnUpdateItemList();
    }
Esempio n. 2
0
    public void OnEndDrag(PointerEventData data)
    {
        if (data.button == PointerEventData.InputButton.Left)
        {
            canvasGroup.blocksRaycasts = true;
            Transform newSlot = null;
            if (data.pointerEnter != null)
            {
                newSlot = data.pointerEnter.transform;
            }

            if (newSlot != null)
            {
                //getting the items from the slots, GameObjects and RectTransform
                GameObject    firstItemGameObject     = this.gameObject;
                GameObject    secondItemGameObject    = newSlot.parent.gameObject;
                RectTransform firstItemRectTransform  = this.gameObject.GetComponent <RectTransform>();
                RectTransform secondItemRectTransform = newSlot.parent.GetComponent <RectTransform>();
                Item          firstItem  = rectTransform.GetComponent <ItemOnObject>().item;
                Item          secondItem = new Item();
                if (newSlot.parent.GetComponent <ItemOnObject>() != null)
                {
                    secondItem = newSlot.parent.GetComponent <ItemOnObject>().item;
                }

                //get some informations about the two items
                bool sameItem            = firstItem.itemName == secondItem.itemName;
                bool sameItemRerferenced = firstItem.Equals(secondItem);
                bool secondItemStack     = false;
                bool firstItemStack      = false;
                if (sameItem)
                {
                    firstItemStack  = firstItem.itemValue < firstItem.maxStack;
                    secondItemStack = secondItem.itemValue < secondItem.maxStack;
                }

                GameObject Inventory = secondItemRectTransform.parent.gameObject;
                if (Inventory.tag == "Slot")
                {
                    Inventory = secondItemRectTransform.parent.parent.parent.gameObject;
                }

                if (Inventory.tag.Equals("Slot"))
                {
                    Inventory = Inventory.transform.parent.parent.gameObject;
                }

                //dragging in an Inventory
                if (Inventory.GetComponent <Hotbar>() == null && Inventory.GetComponent <EquipmentSystem>() == null && Inventory.GetComponent <CraftSystem>() == null)
                {
                    //you cannot attach items to the resultslot of the craftsystem
                    if (newSlot.transform.parent.tag == "ResultSlot" || newSlot.transform.tag == "ResultSlot" || newSlot.transform.parent.parent.tag == "ResultSlot")
                    {
                        firstItemGameObject.transform.SetParent(oldSlot.transform);
                        firstItemRectTransform.localPosition = Vector3.zero;
                    }
                    else
                    {
                        int  newSlotChildCount = newSlot.transform.parent.childCount;
                        bool isOnSlot          = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                        //dragging on a slot where allready is an item on
                        if (newSlotChildCount != 0 && isOnSlot)
                        {
                            //check if the items fits into the other item
                            bool fitsIntoStack = false;
                            if (sameItem)
                            {
                                fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
                            }
                            //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items

                            if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
                            {
                                //if the item does not fit into the other item
                                if (fitsIntoStack && !sameItemRerferenced)
                                {
                                    secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
                                    secondItemGameObject.transform.SetParent(newSlot.parent.parent);
                                    Destroy(firstItemGameObject);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                    {
                                        GameObject dup = secondItemGameObject.GetComponent <ConsumeItem>().duplication;
                                        dup.GetComponent <ItemOnObject>().item.itemValue = secondItem.itemValue;
                                        dup.GetComponent <SplitItem>().inv.stackableSettings();
                                        dup.transform.parent.parent.parent.GetComponent <Inventory>().updateItemList();
                                    }
                                }

                                else
                                {
                                    //creates the rest of the item
                                    int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;

                                    //fill up the other stack and adds the rest to the other stack
                                    if (!fitsIntoStack && rest > 0)
                                    {
                                        firstItem.itemValue  = firstItem.maxStack;
                                        secondItem.itemValue = rest;

                                        firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                        secondItemGameObject.transform.SetParent(oldSlot.transform);

                                        firstItemRectTransform.localPosition  = Vector3.zero;
                                        secondItemRectTransform.localPosition = Vector3.zero;
                                    }
                                }
                            }
                            //if does not fit
                            else
                            {
                                //creates the rest of the item
                                int rest = 0;
                                if (sameItem)
                                {
                                    rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
                                }

                                //fill up the other stack and adds the rest to the other stack
                                if (!fitsIntoStack && rest > 0)
                                {
                                    secondItem.itemValue = firstItem.maxStack;
                                    firstItem.itemValue  = rest;

                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);

                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                }
                                //if they are different items or the stack is full, they get swapped
                                else if (!fitsIntoStack && rest == 0)
                                {
                                    //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
                                    if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
                                    {
                                        newSlot.transform.parent.parent.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                                        oldSlot.transform.parent.parent.GetComponent <Inventory>().EquiptItem(secondItem);

                                        firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                        secondItemGameObject.transform.SetParent(oldSlot.transform);
                                        secondItemRectTransform.localPosition = Vector3.zero;
                                        firstItemRectTransform.localPosition  = Vector3.zero;

                                        if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                        {
                                            Destroy(secondItemGameObject.GetComponent <ConsumeItem>().duplication);
                                        }
                                    }
                                    //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.
                                    else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
                                    {
                                        firstItemGameObject.transform.SetParent(oldSlot.transform);
                                        firstItemRectTransform.localPosition = Vector3.zero;
                                    }
                                    //swapping for the rest of the inventorys
                                    else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null)
                                    {
                                        firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                        secondItemGameObject.transform.SetParent(oldSlot.transform);
                                        secondItemRectTransform.localPosition = Vector3.zero;
                                        firstItemRectTransform.localPosition  = Vector3.zero;
                                    }
                                }
                            }
                        }

                        //empty slot
                        else
                        {
                            if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
                            {
                                firstItemGameObject.transform.SetParent(oldSlot.transform);
                                firstItemRectTransform.localPosition = Vector3.zero;
                            }
                            else
                            {
                                firstItemGameObject.transform.SetParent(newSlot.transform);
                                firstItemRectTransform.localPosition = Vector3.zero;

                                if (newSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                                {
                                    oldSlot.transform.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                                }
                            }
                        }
                    }
                }



                //dragging into a Hotbar
                if (Inventory.GetComponent <Hotbar>() != null)
                {
                    int  newSlotChildCount = newSlot.transform.parent.childCount;
                    bool isOnSlot          = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                    //dragging on a slot where allready is an item on
                    if (newSlotChildCount != 0 && isOnSlot)
                    {
                        //check if the items fits into the other item
                        bool fitsIntoStack = false;
                        if (sameItem)
                        {
                            fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
                        }
                        //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items

                        if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
                        {
                            //if the item does not fit into the other item
                            if (fitsIntoStack && !sameItemRerferenced)
                            {
                                secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
                                secondItemGameObject.transform.SetParent(newSlot.parent.parent);
                                Destroy(firstItemGameObject);
                                secondItemRectTransform.localPosition = Vector3.zero;
                                if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                {
                                    GameObject dup = secondItemGameObject.GetComponent <ConsumeItem>().duplication;
                                    dup.GetComponent <ItemOnObject>().item.itemValue = secondItem.itemValue;
                                    Inventory.GetComponent <Inventory>().stackableSettings();
                                    dup.transform.parent.parent.parent.GetComponent <Inventory>().updateItemList();
                                }
                            }

                            else
                            {
                                //creates the rest of the item
                                int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;

                                //fill up the other stack and adds the rest to the other stack
                                if (!fitsIntoStack && rest > 0)
                                {
                                    firstItem.itemValue  = firstItem.maxStack;
                                    secondItem.itemValue = rest;

                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);

                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                    secondItemRectTransform.localPosition = Vector3.zero;

                                    createDuplication(this.gameObject);
                                    secondItemGameObject.GetComponent <ConsumeItem>().duplication.GetComponent <ItemOnObject>().item = secondItem;
                                    secondItemGameObject.GetComponent <SplitItem>().inv.stackableSettings();
                                }
                            }
                        }
                        //if does not fit
                        else
                        {
                            //creates the rest of the item
                            int rest = 0;
                            if (sameItem)
                            {
                                rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
                            }

                            bool fromEquip = oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null;

                            //fill up the other stack and adds the rest to the other stack
                            if (!fitsIntoStack && rest > 0)
                            {
                                secondItem.itemValue = firstItem.maxStack;
                                firstItem.itemValue  = rest;

                                createDuplication(this.gameObject);

                                firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                secondItemGameObject.transform.SetParent(oldSlot.transform);

                                firstItemRectTransform.localPosition  = Vector3.zero;
                                secondItemRectTransform.localPosition = Vector3.zero;
                            }
                            //if they are different items or the stack is full, they get swapped
                            else if (!fitsIntoStack && rest == 0)
                            {
                                if (!fromEquip)
                                {
                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    firstItemRectTransform.localPosition  = Vector3.zero;

                                    if (oldSlot.transform.parent.parent.gameObject.Equals(GameObject.FindGameObjectWithTag("MainInventory")))
                                    {
                                        Destroy(secondItemGameObject.GetComponent <ConsumeItem>().duplication);
                                        createDuplication(firstItemGameObject);
                                    }
                                    else
                                    {
                                        createDuplication(firstItemGameObject);
                                    }
                                }
                                else
                                {
                                    firstItemGameObject.transform.SetParent(oldSlot.transform);
                                    firstItemRectTransform.localPosition = Vector3.zero;
                                }
                            }
                        }
                    }
                    //empty slot
                    else
                    {
                        if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
                        {
                            firstItemGameObject.transform.SetParent(oldSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;
                        }
                        else
                        {
                            firstItemGameObject.transform.SetParent(newSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;

                            if (newSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                            {
                                oldSlot.transform.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                            }
                            createDuplication(firstItemGameObject);
                        }
                    }
                }


                //dragging into a equipmentsystem/charactersystem
                if (Inventory.GetComponent <EquipmentSystem>() != null)
                {
                    ItemType[] itemTypeOfSlots   = GameObject.FindGameObjectWithTag("EquipmentSystem").GetComponent <EquipmentSystem>().itemTypeOfSlots;
                    int        newSlotChildCount = newSlot.transform.parent.childCount;
                    bool       isOnSlot          = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                    bool       sameItemType      = firstItem.itemType == secondItem.itemType;
                    bool       fromHot           = oldSlot.transform.parent.parent.GetComponent <Hotbar>() != null;

                    //dragging on a slot where allready is an item on
                    if (newSlotChildCount != 0 && isOnSlot)
                    {
                        //items getting swapped if they are the same itemtype
                        if (sameItemType && !sameItemRerferenced) //
                        {
                            Transform temp1 = secondItemGameObject.transform.parent.parent.parent;
                            Transform temp2 = oldSlot.transform.parent.parent;

                            firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                            secondItemGameObject.transform.SetParent(oldSlot.transform);
                            secondItemRectTransform.localPosition = Vector3.zero;
                            firstItemRectTransform.localPosition  = Vector3.zero;

                            if (!temp1.Equals(temp2))
                            {
                                if (firstItem.itemType == ItemType.UFPS_Weapon)
                                {
                                    Inventory.GetComponent <Inventory>().UnEquipItem1(secondItem);
                                    Inventory.GetComponent <Inventory>().EquiptItem(firstItem);
                                }
                                else
                                {
                                    Inventory.GetComponent <Inventory>().EquiptItem(firstItem);
                                    if (secondItem.itemType != ItemType.Backpack)
                                    {
                                        Inventory.GetComponent <Inventory>().UnEquipItem1(secondItem);
                                    }
                                }
                            }

                            if (fromHot)
                            {
                                createDuplication(secondItemGameObject);
                            }
                        }
                        //if they are not from the same Itemtype the dragged one getting placed back
                        else
                        {
                            firstItemGameObject.transform.SetParent(oldSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;

                            if (fromHot)
                            {
                                createDuplication(firstItemGameObject);
                            }
                        }
                    }
                    //if the slot is empty
                    else
                    {
                        for (int i = 0; i < newSlot.parent.childCount; i++)
                        {
                            if (newSlot.Equals(newSlot.parent.GetChild(i)))
                            {
                                //checking if it is the right slot for the item
                                if (itemTypeOfSlots[i] == transform.GetComponent <ItemOnObject>().item.itemType)
                                {
                                    transform.SetParent(newSlot);
                                    rectTransform.localPosition = Vector3.zero;

                                    if (!oldSlot.transform.parent.parent.Equals(newSlot.transform.parent.parent))
                                    {
                                        Inventory.GetComponent <Inventory>().EquiptItem(firstItem);
                                    }
                                }
                                //else it get back to the old slot
                                else
                                {
                                    transform.SetParent(oldSlot.transform);
                                    rectTransform.localPosition = Vector3.zero;
                                    if (fromHot)
                                    {
                                        createDuplication(firstItemGameObject);
                                    }
                                }
                            }
                        }
                    }
                }

                if (Inventory.GetComponent <CraftSystem>() != null)
                {
                    CraftSystem cS = Inventory.GetComponent <CraftSystem>();
                    int         newSlotChildCount = newSlot.transform.parent.childCount;


                    bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                    //dragging on a slot where allready is an item on
                    if (newSlotChildCount != 0 && isOnSlot)
                    {
                        //check if the items fits into the other item
                        bool fitsIntoStack = false;
                        if (sameItem)
                        {
                            fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
                        }
                        //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items

                        if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
                        {
                            //if the item does not fit into the other item
                            if (fitsIntoStack && !sameItemRerferenced)
                            {
                                secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
                                secondItemGameObject.transform.SetParent(newSlot.parent.parent);
                                Destroy(firstItemGameObject);
                                secondItemRectTransform.localPosition = Vector3.zero;


                                if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                {
                                    GameObject dup = secondItemGameObject.GetComponent <ConsumeItem>().duplication;
                                    dup.GetComponent <ItemOnObject>().item.itemValue = secondItem.itemValue;
                                    dup.GetComponent <SplitItem>().inv.stackableSettings();
                                    dup.transform.parent.parent.parent.GetComponent <Inventory>().updateItemList();
                                }
                                cS.ListWithItem();
                            }

                            else
                            {
                                //creates the rest of the item
                                int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;

                                //fill up the other stack and adds the rest to the other stack
                                if (!fitsIntoStack && rest > 0)
                                {
                                    firstItem.itemValue  = firstItem.maxStack;
                                    secondItem.itemValue = rest;

                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);

                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    cS.ListWithItem();
                                }
                            }
                        }
                        //if does not fit
                        else
                        {
                            //creates the rest of the item
                            int rest = 0;
                            if (sameItem)
                            {
                                rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
                            }

                            //fill up the other stack and adds the rest to the other stack
                            if (!fitsIntoStack && rest > 0)
                            {
                                secondItem.itemValue = firstItem.maxStack;
                                firstItem.itemValue  = rest;

                                firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                secondItemGameObject.transform.SetParent(oldSlot.transform);

                                firstItemRectTransform.localPosition  = Vector3.zero;
                                secondItemRectTransform.localPosition = Vector3.zero;
                                cS.ListWithItem();
                            }
                            //if they are different items or the stack is full, they get swapped
                            else if (!fitsIntoStack && rest == 0)
                            {
                                //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
                                if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
                                {
                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    firstItemRectTransform.localPosition  = Vector3.zero;

                                    oldSlot.transform.parent.parent.GetComponent <Inventory>().EquiptItem(secondItem);
                                    newSlot.transform.parent.parent.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                                }
                                //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.
                                else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
                                {
                                    firstItemGameObject.transform.SetParent(oldSlot.transform);
                                    firstItemRectTransform.localPosition = Vector3.zero;
                                }
                                //swapping for the rest of the inventorys
                                else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null)
                                {
                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
                        {
                            firstItemGameObject.transform.SetParent(oldSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;
                        }
                        else
                        {
                            firstItemGameObject.transform.SetParent(newSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;

                            if (newSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                            {
                                oldSlot.transform.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                            }
                        }
                    }
                }
            }

            else
            {
                GameObject dropItem = (GameObject)Instantiate(GetComponent <ItemOnObject>().item.itemModel);
                dropItem.AddComponent <PickUpItem>();
                dropItem.GetComponent <PickUpItem>().item = this.gameObject.GetComponent <ItemOnObject>().item;
                dropItem.transform.localPosition          = GameObject.FindGameObjectWithTag("Player").transform.localPosition;
                inventory.OnUpdateItemList();
                if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                {
                    inventory.GetComponent <Inventory>().UnEquipItem1(dropItem.GetComponent <PickUpItem>().item);
                }
                Destroy(this.gameObject);
            }
        }
        inventory.OnUpdateItemList();
    }
Esempio n. 3
0
    public void OnEndDrag(PointerEventData data)
    {
        if (data.button == PointerEventData.InputButton.Left)
        {
            canvasGroup.blocksRaycasts = true;
            Transform newSlot = null;
            if (data.pointerEnter != null)
            {
                newSlot = data.pointerEnter.transform;
            }

            if (newSlot != null)
            {
                //getting the items from the slots, GameObjects and RectTransform
                GameObject    firstItemGameObject     = this.gameObject;
                GameObject    secondItemGameObject    = newSlot.parent.gameObject;
                RectTransform firstItemRectTransform  = this.gameObject.GetComponent <RectTransform>();
                RectTransform secondItemRectTransform = newSlot.parent.GetComponent <RectTransform>();
                Item          firstItem  = rectTransform.GetComponent <ItemOnObject>().item;
                Item          secondItem = new Item();
                if (newSlot.parent.GetComponent <ItemOnObject>() != null)
                {
                    secondItem = newSlot.parent.GetComponent <ItemOnObject>().item;
                }

                //get some informations about the two items
                bool sameItem            = firstItem.itemName == secondItem.itemName;
                bool sameItemRerferenced = firstItem.Equals(secondItem);
                bool secondItemStack     = false;
                bool firstItemStack      = false;
                if (sameItem)
                {
                    firstItemStack  = firstItem.itemValue < firstItem.maxStack;
                    secondItemStack = secondItem.itemValue < secondItem.maxStack;
                }

                GameObject Inventory = secondItemRectTransform.parent.gameObject;
                if (Inventory.tag == "Slot")
                {
                    Inventory = secondItemRectTransform.parent.parent.parent.gameObject;
                }

                if (Inventory.tag.Equals("Slot"))
                {
                    Inventory = Inventory.transform.parent.parent.gameObject;
                }

                //dragging in an Inventory
                if (Inventory.GetComponent <Hotbar>() == null && Inventory.GetComponent <EquipmentSystem>() == null && Inventory.GetComponent <CraftSystem>() == null)
                {
                    //you cannot attach items to the resultslot of the craftsystem
                    if (newSlot.transform.parent.tag == "ResultSlot" || newSlot.transform.tag == "ResultSlot" || newSlot.transform.parent.parent.tag == "ResultSlot")
                    {
                        firstItemGameObject.transform.SetParent(oldSlot.transform);
                        firstItemRectTransform.localPosition = Vector3.zero;
                    }
                    else
                    {
                        int  newSlotChildCount = newSlot.transform.parent.childCount;
                        bool isOnSlot          = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                        //dragging on a slot where allready is an item on
                        if (newSlotChildCount != 0 && isOnSlot)
                        {
                            //check if the items fits into the other item
                            bool fitsIntoStack = false;
                            if (sameItem)
                            {
                                fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
                            }
                            //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items

                            if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
                            {
                                //if the item does not fit into the other item
                                if (fitsIntoStack && !sameItemRerferenced)
                                {
                                    secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
                                    secondItemGameObject.transform.SetParent(newSlot.parent.parent);
                                    Destroy(firstItemGameObject);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                    {
                                        GameObject dup = secondItemGameObject.GetComponent <ConsumeItem>().duplication;
                                        dup.GetComponent <ItemOnObject>().item.itemValue = secondItem.itemValue;
                                        dup.GetComponent <SplitItem>().inv.stackableSettings();
                                        dup.transform.parent.parent.parent.GetComponent <Inventory>().updateItemList();
                                    }
                                }

                                else
                                {
                                    //creates the rest of the item
                                    int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;

                                    //fill up the other stack and adds the rest to the other stack
                                    if (!fitsIntoStack && rest > 0)
                                    {
                                        firstItem.itemValue  = firstItem.maxStack;
                                        secondItem.itemValue = rest;

                                        firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                        secondItemGameObject.transform.SetParent(oldSlot.transform);

                                        firstItemRectTransform.localPosition  = Vector3.zero;
                                        secondItemRectTransform.localPosition = Vector3.zero;
                                    }
                                }
                            }
                            //if does not fit
                            else
                            {
                                //creates the rest of the item
                                int rest = 0;
                                if (sameItem)
                                {
                                    rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
                                }

                                //fill up the other stack and adds the rest to the other stack
                                if (!fitsIntoStack && rest > 0)
                                {
                                    secondItem.itemValue = firstItem.maxStack;
                                    firstItem.itemValue  = rest;

                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);

                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                }
                                //if they are different items or the stack is full, they get swapped
                                else if (!fitsIntoStack && rest == 0)
                                {
                                    //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
                                    if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
                                    {
                                        newSlot.transform.parent.parent.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                                        oldSlot.transform.parent.parent.GetComponent <Inventory>().EquiptItem(secondItem);

                                        firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                        secondItemGameObject.transform.SetParent(oldSlot.transform);
                                        secondItemRectTransform.localPosition = Vector3.zero;
                                        firstItemRectTransform.localPosition  = Vector3.zero;

                                        if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                        {
                                            Destroy(secondItemGameObject.GetComponent <ConsumeItem>().duplication);
                                        }
                                    }
                                    //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.
                                    else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
                                    {
                                        firstItemGameObject.transform.SetParent(oldSlot.transform);
                                        firstItemRectTransform.localPosition = Vector3.zero;
                                    }
                                    //swapping for the rest of the inventorys
                                    else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null)
                                    {
                                        firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                        secondItemGameObject.transform.SetParent(oldSlot.transform);
                                        secondItemRectTransform.localPosition = Vector3.zero;
                                        firstItemRectTransform.localPosition  = Vector3.zero;
                                    }
                                }
                            }
                        }

                        //empty slot
                        else
                        {
                            if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
                            {
                                firstItemGameObject.transform.SetParent(oldSlot.transform);
                                firstItemRectTransform.localPosition = Vector3.zero;
                            }
                            else
                            {
                                firstItemGameObject.transform.SetParent(newSlot.transform);
                                firstItemRectTransform.localPosition = Vector3.zero;

                                if (newSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                                {
                                    oldSlot.transform.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                                }
                            }
                        }
                    }
                }



                //dragging into a Hotbar
                if (Inventory.GetComponent <Hotbar>() != null)
                {
                    int  newSlotChildCount = newSlot.transform.parent.childCount;
                    bool isOnSlot          = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                    //dragging on a slot where allready is an item on
                    if (newSlotChildCount != 0 && isOnSlot)
                    {
                        //check if the items fits into the other item
                        bool fitsIntoStack = false;
                        if (sameItem)
                        {
                            fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
                        }
                        //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items

                        if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
                        {
                            //if the item does not fit into the other item
                            if (fitsIntoStack && !sameItemRerferenced)
                            {
                                secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
                                secondItemGameObject.transform.SetParent(newSlot.parent.parent);
                                Destroy(firstItemGameObject);
                                secondItemRectTransform.localPosition = Vector3.zero;
                                if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                {
                                    GameObject dup = secondItemGameObject.GetComponent <ConsumeItem>().duplication;
                                    dup.GetComponent <ItemOnObject>().item.itemValue = secondItem.itemValue;
                                    Inventory.GetComponent <Inventory>().stackableSettings();
                                    dup.transform.parent.parent.parent.GetComponent <Inventory>().updateItemList();
                                }
                            }

                            else
                            {
                                //creates the rest of the item
                                int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;

                                //fill up the other stack and adds the rest to the other stack
                                if (!fitsIntoStack && rest > 0)
                                {
                                    firstItem.itemValue  = firstItem.maxStack;
                                    secondItem.itemValue = rest;

                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);

                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                    secondItemRectTransform.localPosition = Vector3.zero;

                                    createDuplication(this.gameObject);
                                    secondItemGameObject.GetComponent <ConsumeItem>().duplication.GetComponent <ItemOnObject>().item = secondItem;
                                    secondItemGameObject.GetComponent <SplitItem>().inv.stackableSettings();
                                }
                            }
                        }
                        //if does not fit
                        else
                        {
                            //creates the rest of the item
                            int rest = 0;
                            if (sameItem)
                            {
                                rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
                            }

                            bool fromEquip = oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null;

                            //fill up the other stack and adds the rest to the other stack
                            if (!fitsIntoStack && rest > 0)
                            {
                                secondItem.itemValue = firstItem.maxStack;
                                firstItem.itemValue  = rest;

                                createDuplication(this.gameObject);

                                firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                secondItemGameObject.transform.SetParent(oldSlot.transform);

                                firstItemRectTransform.localPosition  = Vector3.zero;
                                secondItemRectTransform.localPosition = Vector3.zero;
                            }
                            //if they are different items or the stack is full, they get swapped
                            else if (!fitsIntoStack && rest == 0)
                            {
                                if (!fromEquip)
                                {
                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    firstItemRectTransform.localPosition  = Vector3.zero;

                                    if (oldSlot.transform.parent.parent.gameObject.Equals(GameObject.FindGameObjectWithTag("MainInventory")))
                                    {
                                        Destroy(secondItemGameObject.GetComponent <ConsumeItem>().duplication);
                                        createDuplication(firstItemGameObject);
                                    }
                                    else
                                    {
                                        createDuplication(firstItemGameObject);
                                    }
                                }
                                else
                                {
                                    firstItemGameObject.transform.SetParent(oldSlot.transform);
                                    firstItemRectTransform.localPosition = Vector3.zero;
                                }
                            }
                        }
                    }
                    //empty slot
                    else
                    {
                        if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
                        {
                            firstItemGameObject.transform.SetParent(oldSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;
                        }
                        else
                        {
                            firstItemGameObject.transform.SetParent(newSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;

                            if (newSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                            {
                                oldSlot.transform.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                            }
                            createDuplication(firstItemGameObject);
                        }
                    }
                }


                //dragging into a equipmentsystem/charactersystem
                if (Inventory.GetComponent <EquipmentSystem>() != null)
                {
                    ItemType[] itemTypeOfSlots   = GameObject.FindGameObjectWithTag("EquipmentSystem").GetComponent <EquipmentSystem>().itemTypeOfSlots;
                    int        newSlotChildCount = newSlot.transform.parent.childCount;
                    bool       isOnSlot          = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                    bool       sameItemType      = firstItem.itemType == secondItem.itemType;
                    bool       fromHot           = oldSlot.transform.parent.parent.GetComponent <Hotbar>() != null;

                    //dragging on a slot where allready is an item on
                    if (newSlotChildCount != 0 && isOnSlot)
                    {
                        //items getting swapped if they are the same itemtype
                        if (sameItemType && !sameItemRerferenced) //
                        {
                            Transform temp1 = secondItemGameObject.transform.parent.parent.parent;
                            Transform temp2 = oldSlot.transform.parent.parent;

                            firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                            secondItemGameObject.transform.SetParent(oldSlot.transform);
                            secondItemRectTransform.localPosition = Vector3.zero;
                            firstItemRectTransform.localPosition  = Vector3.zero;

                            if (!temp1.Equals(temp2))
                            {
                                if (firstItem.itemType == ItemType.UFPS_Weapon)
                                {
                                    Inventory.GetComponent <Inventory>().UnEquipItem1(secondItem);
                                    Inventory.GetComponent <Inventory>().EquiptItem(firstItem);
                                }
                                else
                                {
                                    Inventory.GetComponent <Inventory>().EquiptItem(firstItem);
                                    if (secondItem.itemType != ItemType.Backpack)
                                    {
                                        Inventory.GetComponent <Inventory>().UnEquipItem1(secondItem);
                                    }
                                }
                            }

                            if (fromHot)
                            {
                                createDuplication(secondItemGameObject);
                            }
                        }
                        //if they are not from the same Itemtype the dragged one getting placed back
                        else
                        {
                            firstItemGameObject.transform.SetParent(oldSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;

                            if (fromHot)
                            {
                                createDuplication(firstItemGameObject);
                            }
                        }
                    }
                    //if the slot is empty
                    else
                    {
                        for (int i = 0; i < newSlot.parent.childCount; i++)
                        {
                            if (newSlot.Equals(newSlot.parent.GetChild(i)))
                            {
                                //checking if it is the right slot for the item
                                if (itemTypeOfSlots[i] == transform.GetComponent <ItemOnObject>().item.itemType)
                                {
                                    transform.SetParent(newSlot);
                                    rectTransform.localPosition = Vector3.zero;

                                    if (!oldSlot.transform.parent.parent.Equals(newSlot.transform.parent.parent))
                                    {
                                        Inventory.GetComponent <Inventory>().EquiptItem(firstItem);
                                    }
                                }
                                //else it get back to the old slot
                                else
                                {
                                    transform.SetParent(oldSlot.transform);
                                    rectTransform.localPosition = Vector3.zero;
                                    if (fromHot)
                                    {
                                        createDuplication(firstItemGameObject);
                                    }
                                }
                            }
                        }
                    }
                }

                if (Inventory.GetComponent <CraftSystem>() != null)
                {
                    CraftSystem cS = Inventory.GetComponent <CraftSystem>();
                    int         newSlotChildCount = newSlot.transform.parent.childCount;


                    bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
                    //dragging on a slot where allready is an item on
                    if (newSlotChildCount != 0 && isOnSlot)
                    {
                        //check if the items fits into the other item
                        bool fitsIntoStack = false;
                        if (sameItem)
                        {
                            fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
                        }
                        //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items

                        if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
                        {
                            //if the item does not fit into the other item
                            if (fitsIntoStack && !sameItemRerferenced)
                            {
                                secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
                                secondItemGameObject.transform.SetParent(newSlot.parent.parent);
                                Destroy(firstItemGameObject);
                                secondItemRectTransform.localPosition = Vector3.zero;


                                if (secondItemGameObject.GetComponent <ConsumeItem>().duplication != null)
                                {
                                    GameObject dup = secondItemGameObject.GetComponent <ConsumeItem>().duplication;
                                    dup.GetComponent <ItemOnObject>().item.itemValue = secondItem.itemValue;
                                    dup.GetComponent <SplitItem>().inv.stackableSettings();
                                    dup.transform.parent.parent.parent.GetComponent <Inventory>().updateItemList();
                                }
                                cS.ListWithItem();
                            }

                            else
                            {
                                //creates the rest of the item
                                int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;

                                //fill up the other stack and adds the rest to the other stack
                                if (!fitsIntoStack && rest > 0)
                                {
                                    firstItem.itemValue  = firstItem.maxStack;
                                    secondItem.itemValue = rest;

                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);

                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    cS.ListWithItem();
                                }
                            }
                        }
                        //if does not fit
                        else
                        {
                            //creates the rest of the item
                            int rest = 0;
                            if (sameItem)
                            {
                                rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
                            }

                            //fill up the other stack and adds the rest to the other stack
                            if (!fitsIntoStack && rest > 0)
                            {
                                secondItem.itemValue = firstItem.maxStack;
                                firstItem.itemValue  = rest;

                                firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                secondItemGameObject.transform.SetParent(oldSlot.transform);

                                firstItemRectTransform.localPosition  = Vector3.zero;
                                secondItemRectTransform.localPosition = Vector3.zero;
                                cS.ListWithItem();
                            }
                            //if they are different items or the stack is full, they get swapped
                            else if (!fitsIntoStack && rest == 0)
                            {
                                //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
                                if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
                                {
                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    firstItemRectTransform.localPosition  = Vector3.zero;

                                    oldSlot.transform.parent.parent.GetComponent <Inventory>().EquiptItem(secondItem);
                                    newSlot.transform.parent.parent.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                                }
                                //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.
                                else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
                                {
                                    firstItemGameObject.transform.SetParent(oldSlot.transform);
                                    firstItemRectTransform.localPosition = Vector3.zero;
                                }
                                //swapping for the rest of the inventorys
                                else if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null)
                                {
                                    firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
                                    secondItemGameObject.transform.SetParent(oldSlot.transform);
                                    secondItemRectTransform.localPosition = Vector3.zero;
                                    firstItemRectTransform.localPosition  = Vector3.zero;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
                        {
                            firstItemGameObject.transform.SetParent(oldSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;
                        }
                        else
                        {
                            firstItemGameObject.transform.SetParent(newSlot.transform);
                            firstItemRectTransform.localPosition = Vector3.zero;

                            if (newSlot.transform.parent.parent.GetComponent <EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                            {
                                oldSlot.transform.parent.parent.GetComponent <Inventory>().UnEquipItem1(firstItem);
                            }
                        }
                    }
                }
            }
            else
            {
                // position of the dropped item (i.e. gameObject)
                Vector3 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                mouse.z = 0;

                // position of the player
                Vector3 characPos = _player.transform.position;
                print("Mouse --> " + mouse);
                print("Characterpos --> " + characPos);
                float DistMaxtTest = Mathf.Abs(mouse.y - characPos.y);
                float distMax      = Vector2.Distance(mouse, characPos);
                print(distMax + " = " + DistMaxtTest);

                PlanetGenerator scriptGen          = GameObject.FindGameObjectWithTag("FrontGround").transform.parent.GetComponent <PlanetGenerator>();
                UnityEngine.Tilemaps.Tilemap world = GameObject.FindGameObjectWithTag("FrontGround").GetComponent <UnityEngine.Tilemaps.Tilemap>();

                // when dropping the item
                GameObject dropItem = (GameObject)Instantiate(GetComponent <ItemOnObject>().item.itemModel);
                dropItem.AddComponent <PickUpItem>();
                dropItem.GetComponent <PickUpItem>().item = this.gameObject.GetComponent <ItemOnObject>().item;

                if (dropItem.GetComponent <PickUpItem>().item.itemType == ItemType.FireWeapon1)
                {
                    if (_player.GetComponent <Animator>().GetBool("SwapToGun"))
                    {
                        GameObject.FindGameObjectWithTag("Hotbar").transform.GetChild(0).GetComponent <cursor>().UnequipWeapon();
                    }
                }
                if (dropItem.GetComponent <PickUpItem>().item.itemType == ItemType.FireWeapon2)
                {
                    if (_player.GetComponent <Animator>().GetBool("SwapToSub"))
                    {
                        GameObject.FindGameObjectWithTag("Hotbar").transform.GetChild(0).GetComponent <cursor>().UnequipWeapon();
                    }
                }
                if (dropItem.GetComponent <PickUpItem>().item.itemType == ItemType.MeleeWeapon)
                {
                    if (_player.GetComponent <Animator>().GetBool("SwapToKnife"))
                    {
                        GameObject.FindGameObjectWithTag("Hotbar").transform.GetChild(0).GetComponent <cursor>().UnequipWeapon();
                    }
                }
                if (distMax > 3f)
                {
                    if (mouse.x - characPos.x < 0)
                    {
                        mouse.x = characPos.x - 3f;
                    }
                    else if (mouse.x - characPos.x > 0)
                    {
                        mouse.x = characPos.x + 3f;
                    }
                }
                while (_database.getItemByID(scriptGen.planet.savedMapMatrix[world.WorldToCell(mouse).x, world.WorldToCell(mouse).y]).itemID > 0)
                {
                    mouse.y += 1;
                }

                dropItem.transform.position = mouse;
                // dropItem.tag = "DroppedObject";

                inventory.OnUpdateItemList();
                if (oldSlot.transform.parent.parent.GetComponent <EquipmentSystem>() != null)
                {
                    inventory.GetComponent <Inventory>().UnEquipItem1(dropItem.GetComponent <PickUpItem>().item);
                }
                Destroy(this.gameObject);
            }
        }
        inventory.OnUpdateItemList();
    }