public void SwapPieces(MatchPiece piece, SwapDirection direction, bool checkMatches = true) { currentPiece = piece; currentDirection = direction; bool validMove = CheckForValidMove(piece, direction); if (!validMove) { if (!dragMode) { piece.gameObject.transform.DOShakePosition(0.5f, 0.3f); } return; } canMove = false; MatchPiece otherPiece = GetPieceByDirection(piece, direction); var piecePosition = piece.transform.position; var otherPiecePosition = otherPiece.transform.position; piece.ChangeSortingLayer("ballFront"); otherPiece.ChangeSortingLayer("ballBack"); DOTween.Sequence() .Append(piece.transform.DOMove(otherPiecePosition, timeToSwap)) .Join(otherPiece.transform.DOMove(piecePosition, timeToSwap)) .SetEase(Ease.OutCirc) .OnComplete(() => { SwapPieces(piece, otherPiece, checkMatches); }); }
private MatchPiece GetPieceByDirection(MatchPiece piece, SwapDirection direction) { var c = piece.column + (direction == SwapDirection.LEFT ? (-1) : direction == SwapDirection.RIGHT ? 1 : 0); var r = piece.row + (direction == SwapDirection.DOWN ? (-1) : direction == SwapDirection.UP ? 1 : 0); return(board[c][r]); }
private bool CheckForValidMove(MatchPiece piece, SwapDirection direction) { return(!(direction == SwapDirection.LEFT && piece.column == 0 || direction == SwapDirection.RIGHT && piece.column == columns - 1 || direction == SwapDirection.UP && piece.row == rows - 1 || direction == SwapDirection.DOWN && piece.row == 0)); }
private void SwapPieces(MatchPiece piece, MatchPiece otherPiece, bool checkMatches = true) { var pieceRow = piece.row; var pieceColumn = piece.column; piece.row = otherPiece.row; piece.column = otherPiece.column; otherPiece.row = pieceRow; otherPiece.column = pieceColumn; board[piece.column][piece.row] = piece; board[otherPiece.column][otherPiece.row] = otherPiece; canMove = true; if (needCheckMatches || !dragMode && checkMatches) { needCheckMatches = false; StartCoroutine(CheckForMatches()); } }
private void CreateBoard(float xOffset, float yOffset) { float startX = transform.position.x; float startY = transform.position.y; MatchPieceType[] previousLeft = new MatchPieceType[columns]; MatchPieceType previousBelow = null; board = new MatchPiece[rows][]; for (int x = 0; x < rows; x++) { board[x] = new MatchPiece[columns]; for (int y = 0; y < columns; y++) { var tile = Instantiate( matchPieceObject, new Vector3(startX + (xOffset * x), startY + (yOffset * y), 2), matchPieceObject.transform.rotation).AddComponent <MatchPiece>(); List <MatchPieceType> possibletypes = new List <MatchPieceType>(); possibletypes.AddRange(pieceTypes); possibletypes.Remove(previousLeft[y]); possibletypes.Remove(previousBelow); MatchPieceType type = possibletypes[Random.Range(0, possibletypes.Count)]; tile.SetupPiece(y, x, type, TIME_TO_EXPLODE); previousLeft[y] = type; previousBelow = type; board[x][y] = tile; } } }