Ejemplo n.º 1
0
    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;
                }
            }
        }
    }