Example #1
0
    private bool FindNearestGridSlot(List <InventoryGrid> grids, out int fitColumn, out int fitRow, bool isPlayerOwned)
    {
        fitColumn = 0;
        fitRow    = 0;

        Vector3 bottomLeftPos = SelectedItem.transform.localPosition - new Vector3(SelectedItem.ColumnSize * BackpackGrid.BlockSize / 2, SelectedItem.RowSize * BackpackGrid.BlockSize / 2, 0);
        Vector3 centerPos     = SelectedItem.transform.localPosition;

        //see which grid we are at
        FocusedGrid = null;

        foreach (InventoryGrid grid in grids)
        {
            //check if item is allowed in this grid
            bool isItemAllowed = false;
            if (grid.AllowedItemTypes.Count > 0)
            {
                bool typeMatchFound = false;
                foreach (ItemType type in grid.AllowedItemTypes)
                {
                    if (SelectedItem.Item.Type == type)
                    {
                        typeMatchFound = true;
                    }
                }
                if (typeMatchFound)
                {
                    isItemAllowed = true;
                }
            }
            else
            {
                isItemAllowed = true;
            }

            if (centerPos.x >= grid.Grid.transform.localPosition.x && centerPos.x <= grid.Grid.transform.localPosition.x + grid.Columns * grid.BlockSize &&
                centerPos.y >= grid.Grid.transform.localPosition.y && centerPos.y <= grid.Grid.transform.localPosition.y + grid.Rows * grid.BlockSize &&
                grid.IsPlayerOwned == isPlayerOwned && isItemAllowed)
            {
                FocusedGrid = grid;
            }
        }
        if (FocusedGrid == null)
        {
            return(false);
        }

        //starting from 1 block down-left of bottomLeftPos, check if there's room to fit the item
        int  col    = 0;
        int  row    = 0;
        bool result = false;

        int [] xArray = new int[] { 0, -1, 0, -1, 1, 0, 1 };
        int [] yArray = new int[] { 0, -1, -1, 0, 0, 1, 1 };
        int    count  = 0;

        while (!result && count < 7)
        {
            result = FocusedGrid.GetColumnRowFromPos(bottomLeftPos + new Vector3(BackpackGrid.BlockSize * xArray[count], BackpackGrid.BlockSize * yArray[count], 0), out col, out row);
            if (result)
            {
                //check if this col/row can fit the item
                GridItem replace = null;
                if (ReplaceItem != null)
                {
                    ReplaceItem.Sprite.alpha = 1;
                }

                if (FocusedGrid.CanItemFitHere(col, row, SelectedItem.ColumnSize, SelectedItem.RowSize, out replace))
                {
                    ReplaceItem = replace;
                    if (ReplaceItem != null)
                    {
                        ReplaceItem.Sprite.alpha = 0.5f;
                    }

                    fitColumn = col;
                    fitRow    = row;
                    return(true);
                }
            }

            count++;
        }

        if (!result)
        {
            return(false);
        }


        return(false);
    }