Пример #1
0
    void Update()
    {
        // Select item from hotkey slots
        if (CrossPlatformInputManager.GetButtonDown("Fire1"))
        {
            itemSelected = inventoryGrid.GetHotkeyItem(MousePosToHotkey());
        }

        if (isGridEnabled)
        {
            /* Determine which item the mouse is hovering over */
            int[] mouseCoords = MouseToTileCoords();
            itemMouseHover = null;
            if (mouseCoords[0] >= 0 && mouseCoords[1] >= 0)
            {
                Equipment item = inventoryGrid.ItemAtGridPoint(mouseCoords[0], mouseCoords[1]);
                if (item)
                {
                    itemMouseHover = item;
                }
            }

            /* Select item from grid */
            if (CrossPlatformInputManager.GetButtonDown("Fire1"))
            {
                if (itemMouseHover)
                {
                    itemSelected = itemMouseHover;
                }
            }

            /* Remove item from hotkey */
            if (CrossPlatformInputManager.GetButtonDown("Fire2"))
            {
                inventoryGrid.SetHotkeyItem(MousePosToHotkey(), null);
            }

            /* Click-and-drag to move items around */
            if (CrossPlatformInputManager.GetButtonDown("Fire1") && itemMouseHover)
            {
                itemMoving      = itemMouseHover;
                itemMovingDummy = new InventoryGridItem(itemMoving.inventoryGridItem);
                // Set pixel coords offset (mouse pos - corner of item)
                Rect corner = tileRects[itemMoving.inventoryGridItem.x, numTilesY - 1 - itemMoving.inventoryGridItem.y];
                dummyOffsetPixels.x = Input.mousePosition.x - corner.center.x;
                dummyOffsetPixels.y = Input.mousePosition.y - corner.center.y;
            }
            // Update dummy item's tile coords when user is moving an item around the grid
            // If not on the grid, set to (-1, -1)
            if (itemMoving)
            {
                int[] tileCoords = MouseToTileCoords_Unconstrained(Input.mousePosition - dummyOffsetPixels);
                if (tileCoords[0] < 0 || tileCoords[0] > numTilesX - itemMovingDummy.width ||
                    tileCoords[1] < 0 || tileCoords[1] > numTilesY - itemMovingDummy.height)
                {
                    itemMovingDummy.x = -1;
                    itemMovingDummy.y = -1;
                }
                else
                {
                    itemMovingDummy.x = tileCoords[0];
                    itemMovingDummy.y = tileCoords[1];
                }
            }
            // Attempt to drop an item off when user let's go after click-and-drag
            if (CrossPlatformInputManager.GetButtonUp("Fire1"))
            {
                if (itemMoving)
                {
                    // (1) Drop it into a new spot in the grid if it fits
                    bool moved = inventoryGrid.MoveItem(itemMoving, itemMovingDummy.x, itemMovingDummy.y);
                    // (2) If 1 didn't happen, drop it into a hotkey slot if possible
                    if (!moved)
                    {
                        moved = inventoryGrid.SetHotkeyItem(MousePosToHotkey(), itemMoving);
                    }

                    /*
                     * // (3) If 1 and 2 didn't happen, remove the item from the inventory and drop it in the world
                     * if (!moved) {
                     *      inventoryGrid.DropItem(itemMoving);
                     *      if (itemSelected = itemMoving)
                     *              itemSelected = null;
                     *      for (int i = 0; i < numHotkeys; i++)
                     *              if (inventoryGrid.GetHotkeyItem(i) == itemMoving)
                     *                      inventoryGrid.SetHotkeyItem(i, null);
                     * }
                     */
                }
                itemMoving      = null;
                itemMovingDummy = null;
            }
        }

        // Double click to equip
        if (itemSelected && Time.unscaledTime - timeLastClick <= doubleClickMaxDelay && itemLastClick == itemSelected)
        {
            inventoryGrid.SetCurrentItem(itemSelected);
        }
        timeLastClick = Time.unscaledTime;
        itemLastClick = itemSelected;
    }