Exemplo n.º 1
0
    /// <summary>
    /// Releases the item to the given slot, if it's null then nothing happens
    /// </summary>
    public void releaseItem(MenuSlot releaseTo)
    {
        Debug.Log("vvvvvvvvv Start releasing vvvvvvvvvv");
        //Note: "this" always refers to the item slot that holds the grabbed item

        HomeBaseUIController hbuic = GetComponentInParent <HomeBaseUIController>();

        GetComponent <Image>().color = Color.white;
        isGrabbed = false;

        //If the item is actually dropped onto a slot
        //And the slot is different than the one it was picked up in
        if (releaseTo != null && releaseTo != this)
        {
            //First thing to do is see if the player is "splitting" items
            //If there is a visible item with the same id in the player inventory or the home inventory then merge the two items together
            if (releaseTo.type == SlotType.HomeInventory || releaseTo.type == MenuSlot.SlotType.DisplaySlot)
            {
                //Get a matching slot
                MenuSlot matchingSlot = hbuic.findSlotWithItem(this.getItem());
                //Make sure there is a matching slot and it's not this one and it has more than 0 items
                if (matchingSlot != null && matchingSlot != this && matchingSlot.getCount() > 0)
                {
                    //If the player didn't already try to merge the items
                    if (matchingSlot != releaseTo)
                    {
                        if (releaseTo.slotIndex != 0 || releaseTo.type == SlotType.DisplaySlot)
                        {
                            //Pretend the player clicked on the matching slot
                            matchingSlot.swapWith(releaseTo);
                        }
                        else
                        {
                            releaseTo = matchingSlot;
                        }
                    }
                }
            }

            //This only happens if the player is moving something to a slot
            bool iCanMove, uCanMove, weSwap;
            iCanMove = canMoveToSlot(releaseTo);
            uCanMove = releaseTo.canMoveToSlot(this);
            Debug.Log("iCanMove: " + iCanMove + " uCanMove: " + uCanMove);
            weSwap = (iCanMove && uCanMove);
            if (weSwap)
            {
                this.swapWith(releaseTo);
            }
            else //It can't be a straight up swap
            {
                //If my item can move to the given slot
                if (iCanMove)
                {
                    //If it can't combine them then
                    if (!combineSuccess(releaseTo))
                    {
                        //Store the other one in its respective inventory
                        releaseTo.storeItem(false);
                        //Make this one empty by swapping the two items
                        this.swapWith(releaseTo);
                    }
                }
                else
                {
                    //If this item isn't a decoration slot
                    if (this.type != SlotType.DecorationSlot)
                    {
                        //Store this item into its proper home inventory (and set it to null)
                        this.storeItem(true);
                    }
                    else
                    {
                        //The the player is trying to store an item that was picked up from a decoration slot
                        returnItemToSlot();
                    }
                    //If the item in the released slot can move
                    if (uCanMove)
                    {
                        //Only swap the items if the other slot isn't a decoration slot
                        if (releaseTo.type != SlotType.DecorationSlot)
                        {
                            this.swapWith(releaseTo);
                        }
                    }
                }
            }
        }
        else //If the item was dropped somewhere that's not a slot (or dropped back onto itself)
        {
            this.returnItemToSlot();
        }

        Debug.Log("^^^^^^^^^^ Done releasing ^^^^^^^^^^");
    }