/// <summary> /// Checks the entire grid matrix for maches. /// This runs after an explosion happened on the grid and new tiles are created. /// </summary> bool CheckAllTilesForMatch() { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { TileMatches match = gridTiles.CheckTileForMaches(gridTiles[i, j]); if (match != null && match.MatchedTiles.Count() > 2) { StartCoroutine(DisableMatchedTiles(match)); return(true); } } } //All matchings complete, ready for input _gameState = GameState.idle; OnMatchHappened(); if (_gameState != GameState.ending) { HideSelection(false); SelectTileGroup(lastTouchPosition); if (!gridTiles.CheckPotentialMatches()) { GameOver(); } } return(false); }
/// <summary> /// Here starts the disabling of the matched tiles /// </summary> IEnumerator DisableMatchedTiles(TileMatches matches) { print("Hiding the matched ones..."); if (matches.MatchedTiles.Count() < 3) { yield break; } HideSelection(true); print("Stopping rotation"); StopCoroutine(RotateCoroutine); _gameState = GameState.matching; AddScore(matches.MatchedTiles.Count()); foreach (var t in matches.MatchedTiles) { t.SetActive(false); /* t.GetComponent<SpriteRenderer>().color = new Color( * t.GetComponent<SpriteRenderer>().color.r, * t.GetComponent<SpriteRenderer>().color.g, * t.GetComponent<SpriteRenderer>().color.b, 0f * );*/ } yield return(new WaitForSeconds(Values.MatchDelay)); StartCoroutine(MoveDownTiles(matches)); }
/// <summary> /// Collapses the columns with empty tiles /// </summary> IEnumerator MoveDownTiles(TileMatches match) { _gameState = GameState.matching; foreach (var tile in match.MatchedTiles) { List <GameObject> tilesToMove = new List <GameObject>(); for (int i = tile.GetComponent <Tile>().row; i < rows; i++) { tilesToMove.Add(gridTiles[i, tile.GetComponent <Tile>().column]); } for (int i = 0; i < tilesToMove.Count; i++) { if (match.MatchedTiles.Contains(tilesToMove[i])) { continue; } StartCoroutine(tile.GetComponent <Tile>().SwapDownRow(gridTiles, tilesToMove[i].GetComponent <Tile>())); yield return(new WaitForSeconds(Values.TileSwapDelay)); } //For each removed tile, create a new one. InstantiateNewTile(tile.GetComponent <Tile>().row, tile.GetComponent <Tile>().column, tile.transform.position); Destroy(tile); } _gameState = GameState.matchingComplete; }
/// <summary> /// 2 overload functions for checking a single tile /// and checking a selection group. /// </summary> public TileMatches CheckTileForMaches(TileGroup group) { TileMatches matches = new TileMatches(); //_gameState = GameState.matching; foreach (var go in group.tiles) { matches.AddObjectRange(CheckTileForMaches(go.gameObject).MatchedTiles); } return(matches); }
/// <summary> /// Rotates a tile group /// </summary> IEnumerator RotateTileGroup(SwipeDirection _swipeDirection) { _gameState = GameState.rotating; SwapDirection _swapDirection = CalculateRotateDirection(_swipeDirection); print("Rotating..."); //Rotating 120 degrees, 3 times if no matches found for (int i = 0; i < 3; i++) { tileGroup.SetParent(tileGroupCenterObj); float elapsedTime = 0; Quaternion currentRotation = tileGroupCenterObj.transform.rotation; Quaternion targetRotation = currentRotation; //Clockwise or anti-clockwise rotation targetRotation *= Quaternion.Euler(Vector3.forward * 120 * (int)_swapDirection); //Slerp rotation to have a smooth movement effect while (elapsedTime < Values.GroupRotationTime) { elapsedTime += Time.deltaTime; tileGroupCenterObj.transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, elapsedTime / Values.GroupRotationTime); yield return(null); } tileGroupCenterObj.transform.rotation = targetRotation; //Actual swapping of the tiles on the grid matrix tileGroup.SwapTiles(gridTiles, _swapDirection); tileGroup.RemoveParent(); TileMatches groupMatches = gridTiles.CheckTileForMaches(tileGroup); print("Checking group match..."); StartCoroutine(DisableMatchedTiles(groupMatches)); yield return(new WaitForSeconds(Values.RotationDelay)); } _gameState = GameState.idle; }
public TileMatches CheckTileForMaches(GameObject go) { TileMatches matches = new TileMatches(); //_gameState = GameState.matching; GetMatchesTest(go); if (totalMatches.Count > 2) { matches.AddObjectRange(totalMatches); Explode(Helpers.FindGroupCenter(totalMatches), totalMatches[0].GetComponent <Tile>().color); } ClearMatches(); return(matches); }