IEnumerator FallTile(NumberTile tile) { if (tile.IsOnFloor()) { yield break; } List <NumberTile> aboveTiles = NumberTile.FindActiveTilesInDirection(tile, Direction.up); do { yield return(fallWait); tile.MoveTile(Direction.down); foreach (NumberTile aboveTile in aboveTiles) { aboveTile.MoveTile(Direction.down); } } while (!tile.IsOnFloor()); // Add tiles to the queue to process later fallingTiles.Enqueue(tile); foreach (NumberTile aboveTile in aboveTiles) { fallingTiles.Enqueue(aboveTile); } }
IEnumerator FallAndMergeTile(NumberTile tile) { yield return(FallTile(tile)); if (activeTile != null && tile == activeTile) { DisableControl(); } if (fallingTiles.Count != 0) { yield return(new WaitForSeconds(0.2f)); } List <NumberTile> mergedTiles = new List <NumberTile>(); while (fallingTiles.Count != 0) { NumberTile fallingTile = fallingTiles.Dequeue(); if (MergeNeighbourTiles(fallingTile, ref mergedTiles)) { yield return(null); // Play and wait for animation to finish fallingTile.View.PlayUpdateAnimation(); yield return(new WaitForSeconds(1)); // Add the Tile again to see if it can merge with anything else if (fallingTile.IsOnFloor()) { fallingTiles.Enqueue(fallingTile); } } } Queue <Coroutine> fallingTilesQueue = new Queue <Coroutine>(); foreach (NumberTile mergedTile in mergedTiles) { NumberTile neighbour = NumberTile.TryFindActiveTopTile(mergedTile); if (NumberTile.IsActiveTile(neighbour)) { Coroutine c = StartCoroutine(FallAndMergeTile(neighbour)); fallingTilesQueue.Enqueue(c); } } while (fallingTilesQueue.Count != 0) { yield return(fallingTilesQueue.Dequeue()); } }