private Collapse GenerateNewItems(IEnumerable <int> columns) { var collapse = new Collapse(); //find how many null values the column has foreach (int column in columns) { var emptyCells = board.GetEmptyCellsOnColumn(column); foreach (var cell in emptyCells) { var prefab = GetRandomItemPrefab(); var item = ((GameObject)Instantiate(prefab.gameObject, spawnPoints[column], Quaternion.identity)).GetComponent <Item>(); item.transform.SetParent(board.transform); cell.SetItem(item); if (board.rows - cell.Row > collapse.MaxDistance) { collapse.MaxDistance = board.rows - cell.Row; } collapse.AddCell(cell); } } return(collapse); }
public Collapse CollapseColumns(IEnumerable <int> columnArray) { Collapse collapse = new Collapse(); //search in every column foreach (var column in columnArray) { //begin from bottom row for (int row = 0; row < rows - 1; row++) { //if you find a null item if (cells[row, column].IsEmpty) { //start searching for the first non-null for (int row2 = row + 1; row2 < rows; row2++) { //if you find one, bring it down (i.e. replace it with the null you found) if (!cells[row2, column].IsEmpty) { Cell cell1 = cells[row, column]; Cell cell2 = cells[row2, column]; //assign the item cell1.SetItem(cell2.Item); cell2.Clear(); //calculate the biggest distance collapse.MaxDistance = Mathf.Max(row2 - row, collapse.MaxDistance); collapse.AddCell(cell1); break; } } } } } return(collapse); }