Exemple #1
0
    // generates bombs over two swiped tiles
    public void ProcessBombs(Tile tileA, Tile tileB, GamePiece clickedPiece, GamePiece targetPiece, List <GamePiece> tileAPieces, List <GamePiece> tileBPieces)
    {
        // record the general vector of our swipe
        Vector2 swipeDirection = new Vector2(tileB.xIndex - tileA.xIndex, tileB.yIndex - tileA.yIndex);

        // convert the clicked GamePiece or target GamePiece to a bomb depending on matches and swipe direction
        board.clickedTileBomb = DropBomb(tileA.xIndex, tileA.yIndex, swipeDirection, tileAPieces);
        board.targetTileBomb  = DropBomb(tileB.xIndex, tileB.yIndex, swipeDirection, tileBPieces);

        // if the clicked GamePiece is a non-color Bomb, then change its color to the correct target color
        if (board.clickedTileBomb != null && targetPiece != null)
        {
            GamePiece clickedBombPiece = board.clickedTileBomb.GetComponent <GamePiece>();
            if (!board.boardQuery.IsColorBomb(clickedBombPiece))
            {
                clickedBombPiece.ChangeColor(targetPiece);
            }
        }

        // if the target GamePiece is a non-color Bomb, then change its color to the correct clicked color
        if (board.targetTileBomb != null && clickedPiece != null)
        {
            GamePiece targetBombPiece = board.targetTileBomb.GetComponent <GamePiece>();

            if (!board.boardQuery.IsColorBomb(targetBombPiece))
            {
                targetBombPiece.ChangeColor(clickedPiece);
            }
        }
    }
Exemple #2
0
    public void ChangePieceType(int x, int y, PieceType type)
    {
        GamePiece piece = m_allTiles[x, y].piece;

        if (piece != null)
        {
            piece.type = type;
            Color color = PieceUtil.Instance.GetTypeColor(type);
            piece.ChangeColor(color);
        }
    }
Exemple #3
0
    IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile)
    {
        if (m_playerInputEnabled)
        {
            GamePiece clickedPiece = m_allGamePieces[clickedTile.xIndex, clickedTile.yIndex];
            GamePiece targetPiece  = m_allGamePieces[targetTile.xIndex, targetTile.yIndex];

            if (targetPiece != null && clickedPiece != null)
            {
                clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                targetPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);

                yield return(new WaitForSeconds(swapTime));

                List <GamePiece> clickedPieceMatches = FindMatchesAt(clickedTile.xIndex, clickedTile.yIndex);
                List <GamePiece> targetPieceMatches  = FindMatchesAt(targetTile.xIndex, targetTile.yIndex);

                if (targetPieceMatches.Count == 0 && clickedPieceMatches.Count == 0)
                {
                    clickedPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);
                    targetPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                }
                else
                {
                    yield return(new WaitForSeconds(swapTime));

                    Vector2 swipeDirection = new Vector2(targetTile.xIndex - clickedTile.xIndex, targetTile.yIndex - clickedTile.yIndex);
                    m_clickedTileBomb = DropBomb(clickedTile.xIndex, clickedTile.yIndex, swipeDirection, clickedPieceMatches);
                    m_targetTileBomb  = DropBomb(targetTile.xIndex, targetTile.yIndex, swipeDirection, targetPieceMatches);

                    if (m_clickedTileBomb != null && targetPiece != null)
                    {
                        GamePiece clickedBombPiece = m_clickedTileBomb.GetComponent <GamePiece>();
                        clickedBombPiece.ChangeColor(targetPiece);
                    }

                    if (m_targetTileBomb != null && clickedPiece != null)
                    {
                        GamePiece targetBombPiece = m_targetTileBomb.GetComponent <GamePiece>();
                        targetBombPiece.ChangeColor(clickedPiece);
                    }


                    ClearAndRefillBoard(clickedPieceMatches.Union(targetPieceMatches).ToList());
                }
            }
        }
    }
Exemple #4
0
 //I've set things up so that instead of creating and deleting objects on the fly,
 //  we can simply toggle visibility. Jumping a piece requires an "empty" board space
 //  on the opposite end of the piece to jump. So we toggle off the jumping piece and
 //  the piece to jump, and toggle on the empty space. Then set the empty space color
 //  to match the jumping piece's color to make it appear that the piece has moved.
 //If we wanted to use sprites instead of color-shaded pieces, we can change from
 //  a color swap to a sprite swap just as easily.
 public void Jump(GameObject j)
 {
     if (CanJump(j))
     {
         ToggleVisible();
         j.GetComponent <GamePiece>().ToggleVisible();
         GamePiece landingSpace = j.GetComponent <GamePiece>().GetOppositePiece(gameObject).GetComponent <GamePiece>();
         landingSpace.ToggleVisible();
         landingSpace.ChangeColor(gameObject.GetComponent <SpriteRenderer>().color);
         GameBoard.instance.MovedPiece();
         ToggleSelect();
     }
     else
     {
         StartCoroutine(ShakeAnimation());
     }
 }
Exemple #5
0
    IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile)
    {
        if (_playerInputEnabled && !GameManager.Instance.IsGameOver)
        {
            GamePiece clickedPiece = _allGamePieces[clickedTile.XIndex, clickedTile.YIndex];
            GamePiece targetPiece  = _allGamePieces[targetTile.XIndex, targetTile.YIndex];

            if (targetPiece != null && clickedPiece != null)
            {
                clickedPiece.Move(targetTile.XIndex, targetTile.YIndex, SwapTime);
                targetPiece.Move(clickedTile.XIndex, clickedTile.YIndex, SwapTime);

                yield return(new WaitForSeconds(SwapTime));

                List <GamePiece> clickedPieceMatches = FindMatchesAt(clickedTile.XIndex, clickedTile.YIndex);
                List <GamePiece> targetPieceMatches  = FindMatchesAt(targetTile.XIndex, targetTile.YIndex);
                List <GamePiece> ColorMatches        = new List <GamePiece>();

                if (IsColorBomb(clickedPiece) && !IsColorBomb(targetPiece))
                {
                    clickedPiece.MatchValue = targetPiece.MatchValue;
                    ColorMatches            = FindAllMatchValue(clickedPiece.MatchValue);
                }
                else if (!IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    targetPiece.MatchValue = clickedPiece.MatchValue;
                    ColorMatches           = FindAllMatchValue(targetPiece.MatchValue);
                }
                else if (IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    foreach (GamePiece piece in _allGamePieces)
                    {
                        if (!ColorMatches.Contains(piece))
                        {
                            ColorMatches.Add(piece);
                        }
                    }
                }

                if (targetPieceMatches.Count == 0 && clickedPieceMatches.Count == 0 && ColorMatches.Count == 0)
                {
                    clickedPiece.Move(clickedTile.XIndex, clickedTile.YIndex, SwapTime);
                    targetPiece.Move(targetTile.XIndex, targetTile.YIndex, SwapTime);
                }
                else
                {
                    yield return(new WaitForSeconds(SwapTime));

                    Vector2 swipeDirection = new Vector2(targetTile.XIndex - clickedTile.XIndex, targetTile.YIndex - clickedTile.YIndex);
                    _clickedTileBomb = DropBomb(clickedTile.XIndex, clickedTile.YIndex, swipeDirection, clickedPieceMatches);
                    _targetTileBomb  = DropBomb(targetTile.XIndex, targetTile.YIndex, swipeDirection, targetPieceMatches);

                    if (_clickedTileBomb != null && targetPiece != null)
                    {
                        GamePiece clickedBombPiece = _clickedTileBomb.GetComponent <GamePiece>();
                        if (!IsColorBomb(clickedBombPiece))
                        {
                            clickedBombPiece.ChangeColor(targetPiece);
                        }
                    }
                    if (_targetTileBomb != null && clickedPiece != null)
                    {
                        GamePiece targetBombPiece = _targetTileBomb.GetComponent <GamePiece>();
                        if (!IsColorBomb(targetBombPiece))
                        {
                            targetBombPiece.ChangeColor(clickedPiece);
                        }
                    }

                    List <GamePiece> pieceToClear = clickedPieceMatches.Union(targetPieceMatches).ToList().Union(ColorMatches).ToList();

                    yield return(StartCoroutine(ClearAndRefillBoardRoutine(pieceToClear)));

                    if (GameManager.Instance != null)
                    {
                        GameManager.Instance.UpdateMoves();
                    }
                }
            }
        }
    }
Exemple #6
0
    IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile)
    {
        if (playerInputEnabled && !GameManager.Instance.IsGameOver && !GameManager.Instance.IsPaused)
        {
            GamePiece clickedPiece = allGamePieces[clickedTile.xIndex, clickedTile.yIndex];
            GamePiece targetPiece  = allGamePieces[targetTile.xIndex, targetTile.yIndex];

            if (targetPiece != null && clickedPiece != null)
            {
                clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                targetPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);

                yield return(new WaitForSeconds(swapTime));

                List <GamePiece> clickedPieceMatches = FindMatchesAt(clickedTile.xIndex, clickedTile.yIndex);
                List <GamePiece> targetPieceMatches  = FindMatchesAt(targetTile.xIndex, targetTile.yIndex);

                #region color bombs
                List <GamePiece> colorMatches = new List <GamePiece>();

                if (IsColorBomb(clickedPiece) && !IsColorBomb(targetPiece))
                {
                    clickedPiece.matchValue = targetPiece.matchValue;
                    colorMatches            = FindAllMatchValue(clickedPiece.matchValue);
                }
                else if (!IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    targetPiece.matchValue = clickedPiece.matchValue;
                    colorMatches           = FindAllMatchValue(targetPiece.matchValue);
                }
                else if (IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    foreach (GamePiece piece in allGamePieces)
                    {
                        if (!colorMatches.Contains(piece))
                        {
                            colorMatches.Add(piece);
                        }
                    }
                }
                #endregion

                if (clickedPieceMatches.Count == 0 && targetPieceMatches.Count == 0 && colorMatches.Count == 0)
                {
                    clickedPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);
                    targetPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                }
                else
                {
                    yield return(new WaitForSeconds(swapTime));

                    #region drop bombs
                    Vector2 swipeDirection = new Vector3(targetTile.xIndex - clickedTile.xIndex, targetTile.yIndex - clickedTile.yIndex);
                    clickedTileBomb = DropBomb(clickedTile.xIndex, clickedTile.yIndex, swipeDirection, clickedPieceMatches);
                    targetTileBomb  = DropBomb(targetTile.xIndex, targetTile.yIndex, swipeDirection, targetPieceMatches);

                    if (clickedTileBomb != null && targetPiece != null)
                    {
                        GamePiece clickedBombPiece = clickedTileBomb.GetComponent <GamePiece>();
                        if (!IsColorBomb(clickedBombPiece))
                        {
                            clickedBombPiece.ChangeColor(targetPiece);
                        }
                    }
                    if (targetTileBomb != null && clickedPiece != null)
                    {
                        GamePiece targetBombPiece = targetTileBomb.GetComponent <GamePiece>();
                        if (!IsColorBomb(targetBombPiece))
                        {
                            targetBombPiece.ChangeColor(clickedPiece);
                        }
                    }
                    #endregion

                    List <GamePiece> piecesToClear = clickedPieceMatches.Union(targetPieceMatches).ToList().Union(colorMatches).ToList();

                    yield return(StartCoroutine(ClearAndRefillBoardRoutine(piecesToClear)));

                    GameManager.Instance.UpdateMoves();
                }
            }
        }
    }
Exemple #7
0
    IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile)
    {
        if (m_playerInputEnabled && !GameManager.Instance.IsGameOver)
        {
            GamePiece clickedPiece = m_allGamePieces[clickedTile.xIndex, clickedTile.yIndex];
            GamePiece targetPiece  = m_allGamePieces[targetTile.xIndex, targetTile.yIndex];

            if (targetPiece != null && clickedPiece != null)
            {
                clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                targetPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);

                yield return(new WaitForSeconds(swapTime));

                List <GamePiece> clickedPieceMatches = FindMatchesAt(clickedTile.xIndex, clickedTile.yIndex);
                List <GamePiece> targetPieceMatches  = FindMatchesAt(targetTile.xIndex, targetTile.yIndex);

                // # region color bombs

                List <GamePiece> colorMatches = new List <GamePiece>();

                if (IsColorBomb(clickedPiece) && !IsColorBomb(targetPiece))
                {
                    clickedPiece.matchValue = targetPiece.matchValue;
                    colorMatches            = FindAllMatchValue(clickedPiece.matchValue);
                }
                else if (!IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    targetPiece.matchValue = clickedPiece.matchValue;
                    colorMatches           = FindAllMatchValue(targetPiece.matchValue);
                }
                else if (IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    foreach (GamePiece piece in m_allGamePieces)
                    {
                        if (!colorMatches.Contains(piece))
                        {
                            colorMatches.Add(piece);
                        }
                    }
                }

// # endregion

                if (targetPieceMatches.Count == 0 && clickedPieceMatches.Count == 0 && colorMatches.Count == 0)
                {
                    clickedPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);
                    targetPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                }
                else                 //clickedPiece와 targetPiece 교환 성공 및 colorbomb 여부 파악 완료
                {
                    //otherwise, we decrement our moves left.


                    yield return(new WaitForSeconds(swapTime));



                    //#region drop bombs
                    Vector2 swipeDirection = new Vector2(targetTile.xIndex - clickedTile.xIndex, targetTile.yIndex - clickedTile.yIndex);
                    m_clickedTileBomb = DropBomb(clickedTile.xIndex, clickedTile.yIndex, swipeDirection, clickedPieceMatches);
                    m_targetTileBomb  = DropBomb(targetTile.xIndex, targetTile.yIndex, swipeDirection, targetPieceMatches);

                    if (m_clickedTileBomb != null && targetPiece != null)
                    {
                        GamePiece clickedBombPiece = m_clickedTileBomb.GetComponent <GamePiece>();
                        if (!IsColorBomb(clickedBombPiece))
                        {
                            clickedBombPiece.ChangeColor(targetPiece);
                        }
                    }

                    if (m_targetTileBomb != null && clickedPiece != null)
                    {
                        GamePiece targetBombPiece = m_targetTileBomb.GetComponent <GamePiece>();
                        if (!IsColorBomb(targetBombPiece))
                        {
                            targetBombPiece.ChangeColor(clickedPiece);
                        }
                    }


                    // endregion


                    List <GamePiece> piecesToClear = clickedPieceMatches.Union(targetPieceMatches).ToList().Union(colorMatches).ToList();

                    //bug fix

                    yield return(StartCoroutine(ClearAndRefillBoardRoutine(piecesToClear)));

                    if (GameManager.Instance != null)
                    {
                        //GameManager.Instance.movesLeft--;
                        GameManager.Instance.UpdateMoves();
                    }

                    //bug fix end
                }
            }
        }
    }
Exemple #8
0
    IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile)
    {
        if (m_playerInputEnabled && !GameManager.Instance.IsGameOver)
        {
            GamePiece clickedPiece = m_allGamePieces[clickedTile.xIndex, clickedTile.yIndex];
            GamePiece targetPiece  = m_allGamePieces[targetTile.xIndex, targetTile.yIndex];

            if (targetPiece != null && clickedPiece != null)
            {
                clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                targetPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);

                yield return(new WaitForSeconds(swapTime));

                List <GamePiece> clickedPieceMatches = FindMatchesAt(clickedTile.xIndex, clickedTile.yIndex);
                List <GamePiece> targetPieceMatches  = FindMatchesAt(targetTile.xIndex, targetTile.yIndex);
                List <GamePiece> colorMatches        = new List <GamePiece>();

                if (IsColorBomb(clickedPiece) && !IsColorBomb(targetPiece))
                {
                    clickedPiece.matchValue = targetPiece.matchValue;
                    colorMatches            = FindAllMatchValue(clickedPiece.matchValue);
                }
                else if (!IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    targetPiece.matchValue = clickedPiece.matchValue;
                    colorMatches           = FindAllMatchValue(targetPiece.matchValue);
                }
                else if (IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    foreach (GamePiece piece in m_allGamePieces)
                    {
                        if (!colorMatches.Contains(piece))
                        {
                            colorMatches.Add(piece);
                        }
                    }
                }

                if (targetPieceMatches.Count == 0 && clickedPieceMatches.Count == 0 && colorMatches.Count == 0)
                {
                    clickedPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);
                    targetPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                }
                else
                {
                    if (GameManager.Instance != null)
                    {
                        // GameManager.Instance.movesLeft--;
                        GameManager.Instance.UpdateMoves();
                    }

                    yield return(new WaitForSeconds(swapTime));

                    Vector2 swipeDirection = new Vector2(targetTile.xIndex - clickedTile.xIndex, targetTile.yIndex - clickedTile.yIndex);
                    m_clickedTileBomb = DropBomb(clickedTile.xIndex, clickedTile.yIndex, swipeDirection,
                                                 clickedPieceMatches);
                    m_targetTileBomb = DropBomb(targetTile.xIndex, targetTile.yIndex, swipeDirection,
                                                targetPieceMatches);

                    if (m_clickedTileBomb != null && targetPiece != null)
                    {
                        GamePiece clickedBombPiece = m_clickedTileBomb.GetComponent <GamePiece>();

                        if (!IsColorBomb(clickedBombPiece))
                        {
                            clickedBombPiece.ChangeColor(targetPiece);
                        }
                    }

                    if (m_targetTileBomb != null && clickedPiece != null)
                    {
                        GamePiece targetBombPiece = m_targetTileBomb.GetComponent <GamePiece>();

                        if (!IsColorBomb(targetBombPiece))
                        {
                            targetBombPiece.ChangeColor(clickedPiece);
                        }
                    }

                    ClearAndRefillBoard(clickedPieceMatches.Union(targetPieceMatches).ToList().Union(colorMatches).ToList());
                }
            }
        }
    }
Exemple #9
0
    private IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile)
    {
        if (mPlayerInputEnabled && !GameManager.Instance.isGameOver)
        {
            GamePiece clickedPiece = mAllGamePieces[clickedTile.xIndex, clickedTile.yIndex];
            GamePiece targetPiece  = mAllGamePieces[targetTile.xIndex, targetTile.yIndex];

            if (clickedPiece != null && targetPiece != null)
            {
                // Swap the position of two tiles
                clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                targetPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);

                // Wait for the swap time before evaluation
                yield return(new WaitForSeconds(swapTime));

                // Check for matches in the new position
                List <GamePiece> clickedPieceMatches = FindMatchesAt(clickedTile.xIndex, clickedTile.yIndex);
                List <GamePiece> targetPieceMatches  = FindMatchesAt(targetTile.xIndex, targetTile.yIndex);

                List <GamePiece> colorMatches = new List <GamePiece>();

                // Color bomb logic to populate colorMatches list
                if (IsColorBomb(clickedPiece) && !IsColorBomb(targetPiece))
                {
                    clickedPiece.matchValue = targetPiece.matchValue;
                    colorMatches            = FindAllMatchValues(clickedPiece.matchValue);
                }
                else if (!IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    targetPiece.matchValue = clickedPiece.matchValue;
                    colorMatches           = FindAllMatchValues(targetPiece.matchValue);
                }
                else if (IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    foreach (GamePiece piece in mAllGamePieces)
                    {
                        if (!colorMatches.Contains(piece))
                        {
                            colorMatches.Add(piece);
                        }
                    }
                }

                // Return pieces to original coordinates if there are no valid matches
                if (targetPieceMatches.Count == 0 && clickedPieceMatches.Count == 0 && colorMatches.Count == 0)
                {
                    clickedPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);
                    targetPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                }
                // Clear pieces for valid matches
                else
                {
                    if (GameManager.Instance != null)
                    {
                        GameManager.Instance.movesLeft--;
                        GameManager.Instance.UpdateMoves();
                    }

                    // Wait for the swap time before evaluation
                    yield return(new WaitForSeconds(swapTime));

                    Vector2 swapDirection = new Vector2(targetTile.xIndex - clickedTile.xIndex, targetTile.yIndex - clickedTile.yIndex);

                    mClickedTileBomb = InitializeBomb(clickedTile.xIndex, clickedTile.yIndex, swapDirection, clickedPieceMatches);
                    mTargetTileBomb  = InitializeBomb(targetTile.xIndex, targetTile.yIndex, swapDirection, targetPieceMatches);

                    if (mClickedTileBomb != null && targetPiece != null)
                    {
                        GamePiece clickedBombPiece = mClickedTileBomb.GetComponent <GamePiece>();

                        if (!IsColorBomb(clickedBombPiece))
                        {
                            clickedBombPiece.ChangeColor(targetPiece);
                        }
                    }

                    if (mTargetTileBomb != null && clickedPiece != null)
                    {
                        GamePiece targetBombPiece = mTargetTileBomb.GetComponent <GamePiece>();

                        if (!IsColorBomb(targetBombPiece))
                        {
                            targetBombPiece.ChangeColor(clickedPiece);
                        }
                    }

                    ClearAndRefillBoard(clickedPieceMatches.Union(targetPieceMatches).ToList().Union(colorMatches).ToList());
                }
            }
        }
    }
Exemple #10
0
    private IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile)
    {
        if (playerInputEnabled && !GameManager.Instance.IsGameOver)
        {
            if (isNextTo(clickedTile, targetTile))
            {
                GamePiece clickedPiece = gamePiecesArray[clickedTile.xIndex, clickedTile.yIndex];
                GamePiece targetPiece  = gamePiecesArray[targetTile.xIndex, targetTile.yIndex];

                if (targetPiece != null && clickedPiece != null)
                {
                    clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, moveSpeed);
                    targetPiece.Move(clickedTile.xIndex, clickedTile.yIndex, moveSpeed);

                    yield return(new WaitForSeconds(moveSpeed));

                    List <GamePiece> clickedPieceMatches = FindMatchesAt(clickedTile.xIndex, clickedTile.yIndex);
                    List <GamePiece> targetPieceMatches  = FindMatchesAt(targetTile.xIndex, targetTile.yIndex);
                    List <GamePiece> colorMatches        = ProcessColorBombs(clickedPiece, targetPiece);

                    if (targetPieceMatches.Count == 0 && clickedPieceMatches.Count == 0 && colorMatches.Count == 0)
                    {
                        clickedPiece.Move(clickedTile.xIndex, clickedTile.yIndex, moveSpeed);
                        targetPiece.Move(targetTile.xIndex, targetTile.yIndex, moveSpeed);
                    }
                    else
                    {
                        yield return(new WaitForSeconds(moveSpeed));

                        Vector2 swipeDirection = new Vector2(targetTile.xIndex - clickedTile.xIndex, targetTile.yIndex - clickedTile.yIndex);
                        clickedTileBomb = DropBomb(clickedTile.xIndex, clickedTile.yIndex, swipeDirection, clickedPieceMatches);
                        targetTileBomb  = DropBomb(targetTile.xIndex, targetTile.yIndex, swipeDirection, targetPieceMatches);

                        if (clickedTileBomb != null && targetPiece != null)
                        {
                            GamePiece clickedBombPiece = clickedTileBomb.GetComponent <GamePiece>();
                            if (!IsColorBomb(clickedBombPiece))
                            {
                                clickedBombPiece.ChangeColor(targetPiece);
                            }
                        }
                        if (targetTileBomb != null && clickedPiece != null)
                        {
                            GamePiece targetBombPiece = targetTileBomb.GetComponent <GamePiece>();
                            if (!IsColorBomb(targetBombPiece))
                            {
                                targetBombPiece.ChangeColor(clickedPiece);
                            }
                        }
                        List <GamePiece> piecesToClear = clickedPieceMatches.Union(targetPieceMatches).ToList().Union(colorMatches).ToList();
                        yield return(StartCoroutine(ClearAndRefillBoardRoutine(piecesToClear)));

                        yield return(new WaitForSeconds(.5f));

                        if (GameManager.Instance != null)
                        {
                            // LevelGoal.Instance.movesLeft--;
                            GameManager.Instance.UpdateMoves();
                        }
                    }
                }
            }
        }
    }
Exemple #11
0
    private IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile)
    {
        if (isPlayerInputEnabled)
        {
            GamePiece clickedPiece = allGamePieces[clickedTile.XIndex, clickedTile.YIndex];
            GamePiece targetPiece  = allGamePieces[targetTile.XIndex, targetTile.YIndex];

            if (clickedPiece != null && targetPiece != null)
            {
                clickedPiece.Move(targetTile.XIndex, targetTile.YIndex, swapTime);
                targetPiece.Move(clickedTile.XIndex, clickedTile.YIndex, swapTime);

                yield return(new WaitForSeconds(swapTime));

                List <GamePiece> clickedPieceMatches = FindMatchesAt(clickedTile.XIndex, clickedTile.YIndex);
                List <GamePiece> targetPieceMatches  = FindMatchesAt(targetTile.XIndex, targetTile.YIndex);
                List <GamePiece> colorMatches        = new List <GamePiece>();

                if (IsColorBomb(clickedPiece) && !IsColorBomb(targetPiece))
                {
                    clickedPiece.MatchVal = targetPiece.MatchVal;
                    colorMatches          = FindAllMatcheValue(clickedPiece.MatchVal);
                }
                else if (!IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    targetPiece.MatchVal = clickedPiece.MatchVal;
                    colorMatches         = FindAllMatcheValue(targetPiece.MatchVal);
                }
                else if (IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    foreach (GamePiece piece in allGamePieces)
                    {
                        if (!colorMatches.Contains(piece))
                        {
                            colorMatches.Add(piece);
                        }
                    }
                }

                if (targetPieceMatches.Count == 0 && clickedPieceMatches.Count == 0 && colorMatches.Count == 0)
                {
                    clickedPiece.Move(clickedTile.XIndex, clickedTile.YIndex, swapTime);
                    targetPiece.Move(targetTile.XIndex, targetTile.YIndex, swapTime);
                }
                else
                {
                    if (GameManager.Instance != null)
                    {
                        GameManager.Instance.DecrementMoves();
                    }

                    yield return(new WaitForSeconds(swapTime));

                    Vector2 swapDirection = new Vector2(targetTile.XIndex - clickedTile.XIndex, targetTile.YIndex - clickedTile.YIndex);

                    clickedTileBomb = DropBomb(clickedTile.XIndex, clickedTile.YIndex, swapDirection, clickedPieceMatches);
                    targetTileBomb  = DropBomb(targetTile.XIndex, targetTile.YIndex, swapDirection, targetPieceMatches);

                    if (clickedTileBomb != null && targetPiece != null)
                    {
                        GamePiece clickedBombPiece = clickedTileBomb.GetComponent <GamePiece>();
                        if (!IsColorBomb(clickedBombPiece))
                        {
                            clickedBombPiece.ChangeColor(targetPiece);
                        }
                    }

                    if (targetTileBomb != null && clickedPiece != null)
                    {
                        GamePiece targetBombPiece = targetTileBomb.GetComponent <GamePiece>();
                        if (!IsColorBomb(targetBombPiece))
                        {
                            targetBombPiece.ChangeColor(clickedPiece);
                        }
                    }

                    ClearAndRefillBoard(clickedPieceMatches.Union(targetPieceMatches).ToList().Union(colorMatches).ToList());
                }
            }
        }
    }
Exemple #12
0
    // coroutine for swapping two Tiles
    IEnumerator SwitchTilesRoutine(Tile clickedTile, Tile targetTile)
    {
        // if the player input is enabled...
        if (m_playerInputEnabled)
        {
            // set the corresponding GamePieces to the clicked Tile and target Tile
            GamePiece clickedPiece = m_allGamePieces[clickedTile.xIndex, clickedTile.yIndex];
            GamePiece targetPiece  = m_allGamePieces[targetTile.xIndex, targetTile.yIndex];

            if (targetPiece != null && clickedPiece != null)
            {
                // move the clicked GamePiece to the target GamePiece and vice versa
                clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                targetPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);

                // wait for the swap time
                yield return(new WaitForSeconds(swapTime));

                // find all matches for each GamePiece after the swap
                List <GamePiece> clickedPieceMatches = FindMatchesAt(clickedTile.xIndex, clickedTile.yIndex);
                List <GamePiece> targetPieceMatches  = FindMatchesAt(targetTile.xIndex, targetTile.yIndex);

                // create a new List to hold potential color matches
                List <GamePiece> colorMatches = new List <GamePiece>();

                // if the clicked GamePiece is a Color Bomb, set the color matches to the first color
                if (IsColorBomb(clickedPiece) && !IsColorBomb(targetPiece))
                {
                    clickedPiece.matchValue = targetPiece.matchValue;
                    colorMatches            = FindAllMatchValue(clickedPiece.matchValue);
                }
                //... if the target GamePiece is a Color Bomb, set the color matches to the second color
                else if (!IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    targetPiece.matchValue = clickedPiece.matchValue;
                    colorMatches           = FindAllMatchValue(targetPiece.matchValue);
                }
                //... otherwise, if they are both Color bombs, choose the whole Board!
                else if (IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    foreach (GamePiece piece in m_allGamePieces)
                    {
                        if (!colorMatches.Contains(piece))
                        {
                            colorMatches.Add(piece);
                        }
                    }
                }

                // if we don't make any matches, then swap the pieces back
                if (targetPieceMatches.Count == 0 && clickedPieceMatches.Count == 0 && colorMatches.Count == 0)
                {
                    clickedPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);
                    targetPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                }
                else
                {
                    // otherwise, we decrement our moves left
                    if (GameManager.Instance != null)
                    {
                        GameManager.Instance.movesLeft--;
                        GameManager.Instance.UpdateMoves();
                    }

                    // wait for our swap time
                    yield return(new WaitForSeconds(swapTime));

                    // record the general vector of our swipe
                    Vector2 swipeDirection = new Vector2(targetTile.xIndex - clickedTile.xIndex, targetTile.yIndex - clickedTile.yIndex);

                    // conver the clicked GamePiece or target GamePiece to a bomb depending on matches and swipe direction
                    m_clickedTileBomb = DropBomb(clickedTile.xIndex, clickedTile.yIndex, swipeDirection, clickedPieceMatches);
                    m_targetTileBomb  = DropBomb(targetTile.xIndex, targetTile.yIndex, swipeDirection, targetPieceMatches);

                    // if the clicked GamePiece is a non-color Bomb, then change its color to the correct target color
                    if (m_clickedTileBomb != null && targetPiece != null)
                    {
                        GamePiece clickedBombPiece = m_clickedTileBomb.GetComponent <GamePiece>();
                        if (!IsColorBomb(clickedBombPiece))
                        {
                            clickedBombPiece.ChangeColor(targetPiece);
                        }
                    }

                    // if the target GamePiece is a non-color Bomb, then change its color to the correct clicked color
                    if (m_targetTileBomb != null && clickedPiece != null)
                    {
                        GamePiece targetBombPiece = m_targetTileBomb.GetComponent <GamePiece>();

                        if (!IsColorBomb(targetBombPiece))
                        {
                            targetBombPiece.ChangeColor(clickedPiece);
                        }
                    }

                    // clear matches and refill the Board
                    ClearAndRefillBoard(clickedPieceMatches.Union(targetPieceMatches).ToList().Union(colorMatches).ToList());
                }
            }
        }
    }