Example #1
0
    public void Update()
    {
        mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        State    currentState    = state;
        Location currentLocation = location;

        // Mouse in range for a scrollover?
        if (Vector2.Distance(thisTransform.position, mousePoint) < grabRadius)        // YES:
        {
            //Make sure: We're not holding something (or this), and we're not already scrolled over.
            if (!crafter.grabbing && state != State.Scrolled)
            {
                state = State.Scrolled;                 //okay we scrolled over just now
            }
        }
        else         // NO:
        {
            //Make sure: we were scrolled over before, we're not holding anything
            if (!crafter.grabbing && state == State.Scrolled)
            {
                state = State.Sitting;                 //okay we scrolled out
            }
        }


        //Are we being grabbed?
        if (state != State.Grabbed && state != State.Hovering && state != State.Swapping)         // Obviously not if we're already grabbed.
        {
            //If this is scrolled over,
            if (state == State.Scrolled)
            {
                //and we're not grabbing anything else and this isn't the preview
                if (Input.GetKeyDown("mouse 0") && !crafter.grabbing && !isPreview)
                {
                    //we've been grabbed
                    grabbedAt           = new Vector2(thisTransform.position.x, thisTransform.position.y) - mousePoint;
                    state               = State.Grabbed;
                    crafter.grabbing    = true;
                    crafter.grabbedItem = this;
                }

                //also while we're scrolled over, and not being grabbed, are we being manually removed from a slot?
                if (Input.GetKeyDown("mouse 1") && !crafter.grabbing)
                {
                    if (!isPreview)
                    {
                        if (location == Location.Slot)
                        {
                            crafter.AddToInventoryBar(0);
                            barSlot  = 0;
                            location = Location.Bar;
                            crafter.UpdateBarPosition(this);
                            crafter.ClearSlot(aspectSlot);
                        }
                    }
                    else
                    {
                        crafter.FinalizeCraftSlots();
                    }
                }
            }
        }
        else                             //this is grabbed
        {
            if (Input.GetKey("mouse 0")) //and it's going to be grabbed for as long as they're holding down
            {
                //update position before calculating distance

                MovingVisuals();

                bool slotCheck = false;
                for (int i = 0; i < allAspects.Length; i++)
                {
                    if (Vector2.Distance(thisTransform.position, slotPositions[i]) < crafter.slotRadius + grabRadius)
                    {
                        aspectHover = i;
                        if (location != Location.Slot || aspectSlot != aspectHover)
                        {
                            state     = State.Hovering;
                            slotCheck = true;
                            break;
                        }
                    }
                }

                if (!slotCheck)
                {
                    state = State.Grabbed;
                }
            }
            else             // oops they let go
            {
                crafter.grabbing = false;
                if (state == State.Hovering)
                {
                    if (location == Location.Bar)
                    {
                        crafter.FillSlot(aspectHover, craftable);
                        aspectSlot = aspectHover;
                        crafter.RemoveFromInventoryBar(barSlot);
                        location = Location.Slot;
                        barSlot  = -1;
                    }
                    if (location == Location.Slot)
                    {
                        crafter.ClearSlot(aspectSlot);
                        crafter.FillSlot(aspectHover, craftable);
                        aspectSlot = aspectHover;
                    }
                }
                state = State.Sitting;
                thisTransform.position = homePosition;
            }
        }

        if (state != currentState || location != currentLocation)
        {
            SnapVisuals();
        }
    }