IEnumerator SwitchTilesRoutine(Tile_Network clickedTile, Tile_Network targetTile) { m_playerInputEnabled = false; GamePiece_Network clickedPiece = m_allGamePieces[clickedTile.xIndex, clickedTile.yIndex]; GamePiece_Network 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); pieceMoveSound.Play(); yield return(new WaitForSeconds(swapTime)); List <GamePiece_Network> clickedPieceMatches = FindMatchesAt(clickedTile.xIndex, clickedTile.yIndex); List <GamePiece_Network> 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); yield return(new WaitForSeconds(swapTime)); m_playerInputEnabled = true; } else { yield return(new WaitForSeconds(swapTime)); ClearAndRefillBoard(clickedPieceMatches.Union(targetPieceMatches).ToList()); } } }
void ClearPieceAt(int x, int y) { GamePiece_Network pieceToClear = m_allGamePieces[x, y]; if (pieceToClear != null) { m_allGamePieces[x, y] = null; Destroy(pieceToClear.gameObject); } }
public void PlaceGamePiece(GamePiece_Network gamePiece, int x, int y) { if (gamePiece == null) { Debug.LogWarning("BOARD: Invalid GamePiece!"); return; } gamePiece.transform.position = new Vector3(x, y, 0); gamePiece.transform.rotation = Quaternion.identity; if (IsWithinBounds(x, y)) { m_allGamePieces[x, y] = gamePiece; } gamePiece.SetCoord(x, y); }
List <GamePiece_Network> FindMatches(int startX, int startY, Vector2 searchDirection, int minLength = 3) { List <GamePiece_Network> matches = new List <GamePiece_Network>(); GamePiece_Network startPiece = null; if (IsWithinBounds(startX, startY)) { startPiece = m_allGamePieces[startX, startY]; } if (startPiece != null) { matches.Add(startPiece); } else { return(null); } int nextX; int nextY; int maxValue = (width > height) ? width: height; for (int i = 1; i < maxValue - 1; i++) { nextX = startX + (int)Mathf.Clamp(searchDirection.x, -1, 1) * i; nextY = startY + (int)Mathf.Clamp(searchDirection.y, -1, 1) * i; if (!IsWithinBounds(nextX, nextY)) { break; } GamePiece_Network nextPiece = m_allGamePieces[nextX, nextY]; if (nextPiece == null) { break; } else { if (nextPiece.type == startPiece.type && !matches.Contains(nextPiece)) { matches.Add(nextPiece); } else { break; } } } if (matches.Count >= minLength) { return(matches); } return(null); }