bool MergeNeighbourTiles(NumberTile tile, ref List <NumberTile> mergedTiles) { int mergeCount = 0; foreach (Direction direction in mergeDirections) { NumberTile neighbour = tile.FindNeighbourTile(direction); if (NumberTile.IsActiveTile(neighbour) && tile.IsEqualNumber(neighbour)) { neighbour.DeActivate(); mergedTiles.Add(neighbour); mergeCount++; } } int multiplier = (int)Mathf.Pow(2, mergeCount); if (multiplier > 1) { tile.Multiply(multiplier); if (handler != null) { handler.OnTileMerged(tile); } return(true); } return(false); }
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()); } }