I am currently working on a puzzle game where blocks drop and you match nearby colors Blocks are stored within a int 2D array that stores with a value between 1-3 once placed, and it should find connected blocks of the same value afterwards.
I believe this should be done by creating a function that when called, recursively finds same values next to the block, and add those within a list of 2D locations, then remove the values at those positions once all have been found.
How would this be done?
//this coroutine checks for all nearby blocks of same color//
public IEnumerator Flood(int x, int y, int OldCol, int NewCol) {
WaitForSecondsRealtime wait = new WaitForSecondsRealtime(0.016f); //small delay//
if(x >= 0 && x < BloackBoardWidth && y >= 0 && y < BlockBoardHeight) {//is the check in range?//
yield return wait;
if(GridCol[x,y] == OldCol) {//is the grid color same same as the old color?//
//removes the color//
GridCol[x,y] = 0;
StartCoroutine(Flood(x + 1, y, OldCol, NewCol));
StartCoroutine(Flood(x - 1, y, OldCol, NewCol));
StartCoroutine(Flood(x, y + 1, OldCol, NewCol));
StartCoroutine(Flood(x, y - 1, OldCol, NewCol));
}
}
}