private AlteredCandyInfo CreateNewCandyInSpecificColumns(IEnumerable <int> columnsWithMissingCandy) { AlteredCandyInfo newCandyInfo = new AlteredCandyInfo(); // find how many null values the column has foreach (int column in columnsWithMissingCandy) { var emptyItems = shapes.GetEmptyItemsOnColumn(column); int offset = 0; foreach (var item in emptyItems) { var go = GetRandomCandy(); GameObject newCandy = Instantiate(go, spawnPositions[column] + new Vector2(0.0f, offset * CandySize.y), Quaternion.identity, shapesContainer) as GameObject; newCandy.GetComponent <Shape>().Assign(go.GetComponent <Shape>().Type, item.Row, item.Column); shapes[item.Row, item.Column] = newCandy; newCandyInfo.AddCandy(newCandy); offset++; } } return(newCandyInfo); }
public AlteredCandyInfo Collapse(IEnumerable <int> columns) { AlteredCandyInfo collapseInfo = new AlteredCandyInfo(); foreach (var column in columns) { for (int row = 0; row < GameVariables.Rows - 1; row++) { if (candies[row, column] == null) { for (int row2 = row + 1; row2 < GameVariables.Rows; row2++) { if (candies[row2, column] != null) { candies [row, column] = candies [row2, column]; candies [row2, column] = null; if (row2 - row > collapseInfo.maxDistance) { collapseInfo.maxDistance = row2 - row; } candies [row, column].GetComponent <Candy> ().Row = row; candies [row, column].GetComponent <Candy> ().Column = column; collapseInfo.AddCandy(candies[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 AlteredCandyInfo CreateNewCandyInSpecificColumns(IEnumerable <int> columnsWithMissingCandy) { AlteredCandyInfo newCandyInfo = new AlteredCandyInfo(); //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); }
private AlteredCandyInfo CreateNewCandyInSpecificColumns(IEnumerable <int> columnsWithMissingCandies) { AlteredCandyInfo newCandyInfo = new AlteredCandyInfo(); foreach (int column in columnsWithMissingCandies) { var emptyItems = candies.GetEmptyItemsOnColumn(column); foreach (var item in emptyItems) { var go = GetRandomCandy(); GameObject newCandy = Instantiate(go, SpawnPositions [column], Quaternion.identity) as GameObject; newCandy.GetComponent <Candy> ().Initialize(go.GetComponent <Candy> ().Type, item.Row, item.Column); if (GameVariables.Rows - item.Row > newCandyInfo.maxDistance) { newCandyInfo.maxDistance = GameVariables.Rows - item.Row; } candies [item.Row, item.Column] = newCandy; newCandyInfo.AddCandy(newCandy); } } return(newCandyInfo); }
/// <summary> /// Find all the candies that are collapsing downward /// </summary> /// <param name="columns"></param> /// <returns></returns> public AlteredCandyInfo Collapse(IEnumerable <int> columns) { AlteredCandyInfo collapseInfo = new AlteredCandyInfo(); // loop through all columns foreach (var column in columns) { // Loop through each row for (int row = 0; row < GameVariables.Rows - 1; row++) { // Search for a destroyed candy location if (candies[row, column] == null) { // Now navigate the column upward to find the first non-null candy for (int row2 = row + 1; row2 < GameVariables.Rows; row2++) { // Did we find a non-null candy spot? if (candies[row2, column] != null) { // We found a candy, so set the object at that destroyed position to the first non null object candies[row, column] = candies[row2, column]; // nullify the found candy position candies[row2, column] = null; // Update the max distance between the destroyed bean and its first non-null bean above it if (row2 - row > collapseInfo.maxDistance) { collapseInfo.maxDistance = row2 - row; } // Update the candy array positioning information candies[row, column].GetComponent <Candy>().Row = row; candies[row, column].GetComponent <Candy>().Column = column; // Add the candy to the collapsing info collapseInfo.AddCandy(candies[row, column]); // break loop break; } } } } } return(collapseInfo); } // Collapse()
/// <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 AlteredCandyInfo Collapse(IEnumerable <int> columns) { AlteredCandyInfo collapseInfo = new AlteredCandyInfo(); ///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> /// Create new candies in empty spaces within each column /// </summary> /// <param name="columnsWithMissingCandies"></param> /// <returns></returns> private AlteredCandyInfo CreateNewCandyInSpecificColumns(IEnumerable <int> columnsWithMissingCandies) { AlteredCandyInfo newCandyInfo = new AlteredCandyInfo(); // loop through each column to find the empty spaces foreach (int column in columnsWithMissingCandies) { var emptyItems = candies.GetEmptyItemsOnColumn(column); // for each missing item, generate a new candy foreach (var item in emptyItems) { // generate a new candy object var go = GetRandomCandy(); // instantiate this candy GameObject newCandy = Instantiate(go, spawnPositions[column], Quaternion.identity) as GameObject; // initialize this candy and put it in its position newCandy.GetComponent <Candy>().Initialize(go.GetComponent <Candy>().Type, item.Row, item.Column); // if (GameVariables.Rows - item.Row > newCandyInfo.maxDistance) { // Update the max distance newCandyInfo.maxDistance = GameVariables.Rows - item.Row; } // add the new candy into the array candies[item.Row, item.Column] = newCandy; // add the candy to the new candy info newCandyInfo.AddCandy(newCandy); } } return(newCandyInfo); }
public AlteredCandyInfo Collapse(IEnumerable <int> columns) { AlteredCandyInfo collapseInfo = new AlteredCandyInfo(); // search in every column foreach (var column in columns) { // begin from bottom row for (int row = 0; row < Constants.Rows - 1; row++) { // null item found if (shapes[row, column] == null) { // start searching for the first non-null item for (int row2 = row + 1; row2 < Constants.Rows; row2++) { // let it fall down if (shapes[row2, column] != null) { shapes[row, column] = shapes[row2, column]; shapes[row2, column] = null; // assign new row and column 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 AlteredCandyInfo CreateNewCandyInSpecificColumns(IEnumerable<int> columnsWithMissingCandy) { AlteredCandyInfo newCandyInfo = new AlteredCandyInfo(); //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; }
/// <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 AlteredCandyInfo Collapse(IEnumerable<int> columns) { AlteredCandyInfo collapseInfo = new AlteredCandyInfo(); ///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; }