예제 #1
0
    // coroutine for switching tiles
    IEnumerator SwitchedTilesRoutine(TileScript clickedTile, TileScript 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)
            {
                // Swap clickedPiece transform with targetPiece
                clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);

                // Swap targetPiece transfrom with clickedPiece
                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>();

                // Determine if one of the switched pieces was a colorBomb
                // If colorBomb is clickedPiece match clickedPiece color
                if (IsColorBomb(clickedPiece) && !IsColorBomb(targetPiece))
                {
                    clickedPiece.matchValue = targetPiece.matchValue;
                    colorMatches            = FindAllMatchValue(clickedPiece.matchValue);
                }
                // If colorBomb is targetPiece match targetPieces color
                else if (IsColorBomb(targetPiece) && !IsColorBomb(clickedPiece))
                {
                    targetPiece.matchValue = clickedPiece.matchValue;
                    colorMatches           = FindAllMatchValue(targetPiece.matchValue);
                }
                // If both pieces are colorBombs search all gamePieces
                // if not in colorMatches list add to list
                else if (IsColorBomb(clickedPiece) && IsColorBomb(targetPiece))
                {
                    foreach (GamePiece piece in m_allGamePieces)
                    {
                        if (!colorMatches.Contains(piece))
                        {
                            colorMatches.Add(piece);
                        }
                    }
                }

                // If no valid match has been made move pieces back to original location
                if (targetPieceMatches.Count == 0 && targetPieceMatches.Count == 0 && colorMatches.Count == 0)
                {
                    clickedPiece.Move(clickedTile.xIndex, clickedTile.yIndex, swapTime);
                    targetPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
                }

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

                    yield return(new WaitForSeconds(swapTime));

                    // determine swapDirection for bomb placement
                    Vector2 swapDirection = new Vector2(targetTile.xIndex - clickedTile.xIndex, targetTile.yIndex - clickedTile.yIndex);
                    m_clickedTileBomb = DropBomb(clickedTile.xIndex, clickedTile.yIndex, swapDirection, clickedPieceMatches);
                    m_targetTileBomb  = DropBomb(targetTile.xIndex, targetTile.yIndex, swapDirection, targetPieceMatches);

                    // Change the bombs sprite to match either the target piece or the clicked piece
                    if (m_clickedTileBomb != null && targetPiece != null)
                    {
                        // match the target piece
                        GamePiece clickedBomPiece = m_clickedTileBomb.GetComponent <GamePiece>();

                        if (!IsColorBomb(clickedBomPiece))
                        {
                            clickedBomPiece.ChangeSprite(targetPiece);
                        }
                    }

                    if (m_targetTileBomb != null && clickedPiece != null)
                    {
                        // match the clicked piece
                        GamePiece targetBombPiece = m_targetTileBomb.GetComponent <GamePiece>();

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

                    // Appends the lists colorMatches and TargetPieceMatches to clickedPieceMatches
                    ClearAndRefillBoard(clickedPieceMatches.Union(targetPieceMatches).ToList().Union(colorMatches).ToList());
                }
            }
        }
    }