/// Spawns new candy in columns that have missing ones private DistinctCandyCreator_4 CreateNewCandyInSpecificColumns(IEnumerable <int> columnsWithMissingCandy) { DistinctCandyCreator_4 newCandyInfo = new DistinctCandyCreator_4(); //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_4>().Assign(go.GetComponent <Shape_4>().Type, item.Row, item.Column); if (ConstantsVariable_4.Rows - item.Row > newCandyInfo.MaxDistance) { newCandyInfo.MaxDistance = ConstantsVariable_4.Rows - item.Row; } shapes[item.Row, item.Column] = newCandy; newCandyInfo.AddCandy(newCandy); } } return(newCandyInfo); }
/// Collapses the array on the specific columns, after checking for empty items on them public DistinctCandyCreator_4 Collapse(IEnumerable <int> columns) { DistinctCandyCreator_4 collapseInfo = new DistinctCandyCreator_4(); ///search in every column foreach (var column in columns) { //begin from bottom row for (int row = 0; row < ConstantsVariable_4.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_4.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_4>().Row = row; shapes[row, column].GetComponent <Shape_4>().Column = column; collapseInfo.AddCandy(shapes[row, column]); break; } } } } } return(collapseInfo); }