コード例 #1
0
    /// <summary>
    /// Used when the player is moving an itemUI. When called, check if the player can place
    /// the itemUI at the spot it's in.
    /// </summary>
    /// <param name="itemUI"></param>
    /// <param name="position"></param>
    /// <returns></returns>
    public bool PlaceItemUI(ItemUI itemUI, Vector2 position)
    {
        // Get the slot where the current UI is hovering over
        Vector2       index        = GetSlotFromPosition(position);
        InventorySlot originalSlot = itemUI.GetTopLeftSlot();

        //Debug.Log("move to:" + index);

        // Remove the data from the itemUI's previous spots
        RemoveItemFromSlot(itemUI);
        slotsUsed -= itemUI.GetItem().itemHeight *itemUI.GetItem().itemHeight;

        // Check if we can place the ui in that slot
        Item item = itemUI.GetItem();

        if (!CheckInventorySlot((int)index.y, (int)index.x, item.itemWidth, item.itemHeight))
        {
            //Debug.Log("Cannot place here");
            // Give the reference to the slot back so it can call this method again
            itemUI.SetTopLeftSlot(originalSlot);
            return(false);
        }
        else
        {
            Debug.Log("Can place here");

            // Then add the itemUI to this new slot
            AddItemToSlot(itemUI.GetItem(), (int)index.y, (int)index.x, itemUI);
            //Debug.Log("topleftslot:" + itemUI.GetTopLeftSlot());
            return(true);
        }
    }
コード例 #2
0
    /// <summary>
    /// Remove an item from designated slots by marking all those slots as not occupied
    /// and a removing the corresponding references. Note that this does not destroy the
    /// given UI.
    /// </summary>
    /// <param name="itemUI"></param>
    private void RemoveItemFromSlot(ItemUI itemUI)
    {
        // Get the item from the itemUI
        Item item   = itemUI.GetItem();
        int  width  = item.itemWidth;
        int  height = item.itemHeight;

        // Get the top left slot
        InventorySlot topLeft = itemUI.GetTopLeftSlot();

        // Remove the topLeftSlot from the itemUI
        itemUI.SetTopLeftSlot(null);

        // Find all the slots that this item occupies
        // and set them
        Vector2 index    = topLeft.GetIndex();
        int     startRow = (int)index.y;
        int     startCol = (int)index.x;

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                // Debug
                //int y = startRow + i;
                //int x = startCol + j;
                //Debug.Log(gameObject.name + " set " + y + "," + x + " to NOT occupied");

                // Get the slot at this place
                InventorySlot slot = inventorySlots[startRow + i, startCol + j];

                // Set its references to remove the item from it
                slot.SetOccupancy(false);
                slot.SetParentItemUI(null);
            }
        }

        // Done!
    }