示例#1
0
    /// Spawns new candy in columns that have missing ones
    private DistinctCandyCreator CreateNewCandyInSpecificColumns(IEnumerable<int> columnsWithMissingCandy)
    {
        DistinctCandyCreator newCandyInfo = new DistinctCandyCreator();

        //find how many null values the column has
        foreach (int column in columnsWithMissingCandy)
        {
            var emptyItems = shapes.GetEmptyItemsOnColumn(column);
            foreach (var item in emptyItems)
            {
                var go = GetRandomCandy();
                GameObject newCandy = Instantiate(go, SpawnPositions[column], Quaternion.identity)
                    as GameObject;

                newCandy.GetComponent<Shape>().Assign(go.GetComponent<Shape>().Type, item.Row, item.Column);

                if (ConstantsVariable.Rows - item.Row > newCandyInfo.MaxDistance)
                    newCandyInfo.MaxDistance = ConstantsVariable.Rows - item.Row;

                shapes[item.Row, item.Column] = newCandy;
                newCandyInfo.AddCandy(newCandy);
            }
        }
        return newCandyInfo;
    }
示例#2
0
    /// Collapses the array on the specific columns, after checking for empty items on them
    public DistinctCandyCreator Collapse(IEnumerable <int> columns)
    {
        DistinctCandyCreator collapseInfo = new DistinctCandyCreator();


        ///search in every column
        foreach (var column in columns)
        {
            //begin from bottom row
            for (int row = 0; row < ConstantsVariable.Rows - 1; row++)
            {
                //if you find a null item
                if (shapes[row, column] == null)
                {
                    //start searching for the first non-null
                    for (int row2 = row + 1; row2 < ConstantsVariable.Rows; row2++)
                    {
                        //if you find one, bring it down (i.e. replace it with the null you found)
                        if (shapes[row2, column] != null)
                        {
                            shapes[row, column]  = shapes[row2, column];
                            shapes[row2, column] = null;

                            //calculate the biggest distance
                            if (row2 - row > collapseInfo.MaxDistance)
                            {
                                collapseInfo.MaxDistance = row2 - row;
                            }

                            //assign new row and column (name does not change)
                            shapes[row, column].GetComponent <Shape>().Row    = row;
                            shapes[row, column].GetComponent <Shape>().Column = column;

                            collapseInfo.AddCandy(shapes[row, column]);
                            break;
                        }
                    }
                }
            }
        }

        return(collapseInfo);
    }