/// <summary> /// Collapses the array on the specific columns, after checking for empty items on them /// </summary> /// <param name="columns"></param> /// <returns>Info about the GameObjects that were moved</returns> public AlteredCandy Collapse(IEnumerable<int> columns) { AlteredCandy collapseInfo = new AlteredCandy(); ///search in every column foreach (var column in columns) { //begin from bottom row for (int row = 0; row < Constants.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 < Constants.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; }
/// <summary> /// Spawns new candy in columns that have missing ones /// </summary> /// <param name="columnsWithMissingCandy"></param> /// <returns>Info about new candies created</returns> private AlteredCandy CreateNewCandyInSpecificColumns(IEnumerable<int> columnsWithMissingCandy) { AlteredCandy newCandyInfo = new AlteredCandy(); //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 (Constants.Rows - item.Row > newCandyInfo.MaxDistance) newCandyInfo.MaxDistance = Constants.Rows - item.Row; shapes[item.Row, item.Column] = newCandy; newCandyInfo.AddCandy(newCandy); } } return newCandyInfo; }