// coroutine for swapping two Tiles IEnumerator SwitchTilesRoutine(Tile tileA, Tile tileB) { // if the player input is enabled... if (playerInputEnabled && !GameManager.Instance.IsGameOver && !isSwiping) { isSwiping = true; // set the corresponding GamePieces to the clicked Tile and target Tile GamePiece clickedPiece = allGamePieces[tileA.xIndex, tileA.yIndex]; GamePiece targetPiece = allGamePieces[tileB.xIndex, tileB.yIndex]; if (targetPiece != null && clickedPiece != null) { // move the clicked GamePiece to the target GamePiece and vice versa clickedPiece.Move(tileB.xIndex, tileB.yIndex, swapTime); targetPiece.Move(tileA.xIndex, tileA.yIndex, swapTime); // wait for the swap time yield return(new WaitForSeconds(swapTime)); // find all matches for each GamePiece after the swap List <GamePiece> tileAMatches = boardMatcher.FindMatchesAt(tileA.xIndex, tileA.yIndex); List <GamePiece> tileBMatches = boardMatcher.FindMatchesAt(tileB.xIndex, tileB.yIndex); List <GamePiece> colorMatches = boardBomber.ProcessColorBombs(clickedPiece, targetPiece); // if we don't make any matches, then swap the pieces back if (tileBMatches.Count == 0 && tileAMatches.Count == 0 && colorMatches.Count == 0) { clickedPiece.Move(tileA.xIndex, tileA.yIndex, swapTime); targetPiece.Move(tileB.xIndex, tileB.yIndex, swapTime); yield return(new WaitForSeconds(swapTime)); isSwiping = false; } else { // wait for our swap time yield return(new WaitForSeconds(swapTime)); // record the general vector of our swipe Vector2 swipeDirection = new Vector2(tileB.xIndex - tileA.xIndex, tileB.yIndex - tileA.yIndex); // drop bombs on either tile as necessary boardBomber.ProcessBombs(tileA, tileB, clickedPiece, targetPiece, tileAMatches, tileBMatches); List <GamePiece> piecesToClear = tileAMatches.Union(tileBMatches).ToList().Union(colorMatches).ToList(); // clear matches and refill the Board yield return(StartCoroutine(ClearAndRefillBoardRoutine(piecesToClear))); // otherwise, we decrement our moves left if (GameManager.Instance != null) { GameManager.Instance.UpdateMoves(); } isSwiping = false; } } } }
IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile) { // If player input is enabled... if (playerInputEnabled && !GameManager.Instance.IsGameOver) { // ...set the corresponding Bubbles to the clicked Tile and target Tile Bubble clickedBubble = allBubbles[clickedTile.xIndex, clickedTile.yIndex]; Bubble targetBubble = allBubbles[targetTile.xIndex, targetTile.yIndex]; if (clickedBubble != null && targetBubble != null) { isBusy = true; // Move the clicked Bubble to the target Bubble and vice versa clickedBubble.Move(targetTile.xIndex, targetTile.yIndex, m_swapTime); targetBubble.Move(clickedTile.xIndex, clickedTile.yIndex, m_swapTime); // Wait for the swap time yield return(new WaitForSeconds(m_swapTime)); // Find all matches for each Bubble after the swap List <Bubble> clickedBubbleMatches = boardMatcher.FindMatchesAt(clickedTile.xIndex, clickedTile.yIndex); List <Bubble> targetBubbleMatches = boardMatcher.FindMatchesAt(targetTile.xIndex, targetTile.yIndex); // Check if color bomb was triggered, and if so get the list of corresponding bubbles List <Bubble> colorMatches = new List <Bubble>(); if (boardQuery.IsColorBomb(clickedBubble) && !boardQuery.IsColorBomb(targetBubble)) { clickedBubble.matchValue = targetBubble.matchValue; colorMatches = boardMatcher.FindAllMatchValue(clickedBubble.matchValue); } else if (!boardQuery.IsColorBomb(clickedBubble) && boardQuery.IsColorBomb(targetBubble)) { targetBubble.matchValue = clickedBubble.matchValue; colorMatches = boardMatcher.FindAllMatchValue(targetBubble.matchValue); } else if (boardQuery.IsColorBomb(clickedBubble) && boardQuery.IsColorBomb(targetBubble)) { foreach (Bubble bubble in allBubbles) { if (!colorMatches.Contains(bubble)) { colorMatches.Add(bubble); } } } // If no matches are found, then swap the Bubbles back if (clickedBubbleMatches.Count == 0 && targetBubbleMatches.Count == 0 && colorMatches.Count == 0) { clickedBubble.Move(clickedTile.xIndex, clickedTile.yIndex, m_swapTime); targetBubble.Move(targetTile.xIndex, targetTile.yIndex, m_swapTime); yield return(new WaitForSeconds(m_swapTime)); } else { if (GameManager.Instance != null) { GameManager.Instance.DecrementMoves(); } // Clear matches and refill the Board Vector2 swipeDirection = new Vector2(targetTile.xIndex - clickedTile.xIndex, targetTile.yIndex - clickedTile.yIndex); // Drop bomb in-place m_clickedTileBomb = boardBomber.DropBomb(clickedTile.xIndex, clickedTile.yIndex, swipeDirection, clickedBubbleMatches); m_targetTileBomb = boardBomber.DropBomb(targetTile.xIndex, targetTile.yIndex, swipeDirection, targetBubbleMatches); // Change bomb color to match if (m_clickedTileBomb != null && targetBubble != null) { Bomb clickedBomb = m_clickedTileBomb.GetComponent <Bomb>(); if (!boardQuery.IsColorBomb(clickedBomb)) { clickedBomb.ChangeColor(targetBubble); } } if (m_targetTileBomb != null && clickedBubble != null) { Bomb targetBomb = m_targetTileBomb.GetComponent <Bomb>(); if (!boardQuery.IsColorBomb(targetBomb)) { targetBomb.ChangeColor(clickedBubble); } } // Add short pause if bomb was generated if (m_clickedTileBomb != null || m_targetTileBomb != null) { yield return(new WaitForSeconds(m_delay * 0.5f)); } List <Bubble> bubblesToClear = clickedBubbleMatches.Union(targetBubbleMatches).ToList() .Union(colorMatches).ToList(); yield return(StartCoroutine(ClearAndRefillBoardRoutine(bubblesToClear))); isBusy = false; } } } }