/// <summary> Calculates and places tiles </summary> private void AddTiles(string spew) { int count = 0; for (int col = 0; col < TileCountScale; ++col) { for (int row = 0; row < TileCountScale; ++row, ++count) { // Create object GameObject newTile = Instantiate(TilePrefab, PlayArea.transform); newTile.GetComponent <RectTransform>().localPosition = new Vector3(0, 1000, 0); // get refference to tile class MatchTile tile = newTile.GetComponent <MatchTile>(); // Random image System.Random rand = new System.Random((int)DateTime.Now.Ticks); tile.MatchImage.sprite = Matchables[rand.Next(0, Matchables.Count)]; tile.Transitioning = true; tile.game = this; tile.currentCell = new Vector2(col, row); if (spew == "random") { tile.SetupDelay = UnityEngine.Random.Range(0f, 1f); } else if (spew == "ordered") { tile.SetupDelay = 0.5f + (count * addTileDelay); } // set position tile.FinalPos = Cells[col][row].Position; Cells[col][row].Tile = tile; } } }
/// <summary> Checks a tile for match and adds to the </summary> public void SelectTile(MatchTile tile) { if (matchingTiles.Count == 0) { tile.Selected = true; matchingTiles.Add(tile); SoundManager.Instance.PlaySound("MatchGameOpen", SFX); return; } else { for (int i = 0; i < matchingTiles.Count; ++i) { if (matchingTiles[i].MatchImage.sprite == tile.MatchImage.sprite) { tile.Selected = true; matchingTiles.Add(tile); SoundManager.Instance.PlaySound("MatchGameOpen", SFX); if (!AnyMatches()) { EndCombo(); SoundManager.Instance.PlaySound("ComboMatch", SFX); } return; } else { SoundManager.Instance.PlaySound("FailMatch", SFX); EndCombo(); } } } }
/// <summary> Allocates points for the game </summary> public void AllocatePoints(int value, MatchTile tile) { Score += value; UI.Score_Text.text = Score.ToString(); GameObject go = Instantiate(ScoreIndicator, gameObject.transform); go.transform.position = tile.gameObject.transform.position; go.GetComponent <ScoreIndicator>().ScrollDest = UI.Score_Text.transform; go.GetComponent <ScoreIndicator>().Init(value, true); }
void CleanGrid() { for (int i = 0; i < Tiles.Count; i++) { Destroy(Tiles[i].gameObject); } Tiles.Clear(); _lastUncoveredTile = null; }
public bool OnTileFlip(MatchTile tile) { if (_matchState.HasPotentialMatch) { StartCoroutine(VerifyTiles(_matchState.Tile, tile)); _matchState = new TileMatchState(); } else { _matchState = new TileMatchState(tile); } return(true); }
int CountMatches(MatchTile tile, int count, int prevTileType, Directions direction, List<MatchTile> matchedTiles, List<int> checkedList) { if(tile == null || checkedList.Contains(tile.Id) || tile.TileType != prevTileType || tile.IsEmpty){ return count; } count++; MatchTile nextTile = new MatchTile(); checkedList.Add(tile.Id); matchedTiles.Add(tile); if(direction == Directions.Left){ nextTile = tile.Left; } else if(direction == Directions.Right){ nextTile = tile.Right; } else if (direction == Directions.Up){ nextTile = tile.Up; } else if (direction == Directions.Down){ nextTile = tile.Down; } return CountMatches(nextTile, count, tile.TileType, direction, matchedTiles, checkedList); }
/// <summary> Calculates and places a single tile </summary> private void AddTile(MatchCell cell) { // Create object GameObject newTile = Instantiate(TilePrefab, PlayArea.transform); newTile.GetComponent <RectTransform>().localPosition = new Vector3(0, 1000, 0); // get refference to tile class MatchTile tile = newTile.GetComponent <MatchTile>(); // Random image System.Random rand = new System.Random((int)DateTime.Now.Ticks); tile.MatchImage.sprite = Matchables[rand.Next(0, Matchables.Count)]; tile.Transitioning = true; tile.game = this; tile.currentCell = cell.Cell; tile.SetupDelay = 0f; // set position tile.FinalPos = cell.Position; cell.Tile = tile; }
IEnumerator VerifyTiles(MatchTile first, MatchTile second) { bool isMatch = first.Color == second.Color; yield return(new WaitForSeconds(0.6f)); if (isMatch) { first.ConfirmMatch(); second.ConfirmMatch(); _lastUncoveredTile = null; UpdateScore(); } else { first.Reset(); second.Reset(); _lastUncoveredTile = second; } }
public void BoardSetup(int rows, int columns) { board = new Board<MatchTile>(rows, columns); tileIDsToCheckForMatches = new List<int>(); tileHolder = new GameObject("TileHolder").transform; tileHolder.position = new Vector3(-columns*0.5f+0.5f,rows*0.5f-0.5f,0f); gameObjectDict = new Dictionary<int, GameObject>(); checkedHorizontalTileIDs = new List<int>(); checkedVerticalTileIDs = new List<int>(); newDeletedTiles = new List<MatchTile>(); deletedIdsLastTurn = new List<int>(); tileBreakAudio = GetComponent<AudioSource>(); tileBreakAudio.pitch = 0.7f; int id = 0; for(int y = 0; y < rows; y++) { for(int x = 0; x < columns; x++) { MatchTile tmpTile = new MatchTile(y,x, id); board.Add(tmpTile, y,x); board.SetLeftAndUpNeighbors(y,x); GameObject toInstantiate = GetValidTileToInstantiate(y,x); GameObject instance = (GameObject)Object.Instantiate(toInstantiate, new Vector3 (tileHolder.position.x+x, tileHolder.position.y-y, 0f), Quaternion.identity); instance.GetComponent<TileMover>().Row = y; instance.GetComponent<TileMover>().Col = x; instance.GetComponent<TileMover>().Id = id; instance.transform.SetParent(tileHolder); MatchTile tile = new MatchTile(instance.GetComponent<TileMover>().tileType, y,x, id); board.Add(tile,y,x); board.SetLeftAndUpNeighbors(y,x); gameObjectDict.Add(id, instance); id++; } } isRunning = true; }
public TileMatchState(MatchTile tile) { HasPotentialMatch = tile != null; Tile = tile; }
void SearchMatches(MatchTile tile) { if(tile == null || (tile.IsEmpty && !newDeletedTiles.Contains(tile))){ return; } checkedHorizontalTileIDs.Add(tile.Id); int countLeft = 0; int countRight= 0; List<MatchTile> leftDeletedTiles = new List<MatchTile>(); List<MatchTile> rightDeletedTiles = new List<MatchTile>(); if(tile.Left != null){ countLeft = CountMatches(tile.Left, 0, tile.TileType, Directions.Left, leftDeletedTiles, checkedHorizontalTileIDs); } if(tile.Right != null){ countRight = CountMatches(tile.Right, 0, tile.TileType, Directions.Right, rightDeletedTiles, checkedHorizontalTileIDs); } int totalHorizontalCount = countLeft+countRight+1; if(totalHorizontalCount > GameManager.instance.MAX_ALLOWED_TILES){ for(int i = leftDeletedTiles.Count-1; i >= 0; i--){ leftDeletedTiles[i].IsEmpty = true; newDeletedTiles.Add(leftDeletedTiles[i]); } tile.IsEmpty = true; newDeletedTiles.Add(tile); foreach(MatchTile t in rightDeletedTiles){ t.IsEmpty = true; newDeletedTiles.Add(t); } } if(GameManager.instance.VerticalMatchingEnabled){ checkedVerticalTileIDs.Add(tile.Id); int countUp = 0; int countDown = 0; List<MatchTile> upDeletedTiles = new List<MatchTile>(); List<MatchTile> downDeletedTiles = new List<MatchTile>(); if(tile.Up != null){ countUp = CountMatches(tile.Up, 0, tile.TileType, Directions.Up, upDeletedTiles, checkedVerticalTileIDs); } if(tile.Down != null){ countDown = CountMatches(tile.Down, 0, tile.TileType, Directions.Down, downDeletedTiles, checkedVerticalTileIDs); } int totalVerticalCount = countUp+countDown+1; if(totalVerticalCount > GameManager.instance.MAX_ALLOWED_TILES){ for(int i = upDeletedTiles.Count-1; i >= 0; i--){ upDeletedTiles[i].IsEmpty = true; newDeletedTiles.Add(upDeletedTiles[i]); } if(totalHorizontalCount <= GameManager.instance.MAX_ALLOWED_TILES){ tile.IsEmpty = true; newDeletedTiles.Add(tile); } foreach(MatchTile t in downDeletedTiles){ t.IsEmpty = true; newDeletedTiles.Add(t); } } } SearchMatches(tile.Up); }
MatchTile MoveDeletedTileToTop(MatchTile tile, int count, MatchTile longestMover) { if(tile.Up == null || ((tile.Up.IsEmpty && !deletedIdsLastTurn.Contains(tile.Up.Id)) && (tile.Up.IsEmpty && !newDeletedTiles.Contains(tile.Up)))){ return longestMover; } if(!tile.Up.IsEmpty){ TileMover tileHolder = gameObjectDict[tile.Up.Id].GetComponent<TileMover>(); tileHolder.StepsMoved++; tileHolder.Row++; if((tileHolder.StepsMoved > longestMover.StepsMoved) && !newDeletedTiles.Contains(tile.Up)){ longestMover.StepsMoved = tileHolder.StepsMoved; longestMover.Id = tileHolder.Id; } } MatchTile up = tile.Up; board.SwapTilePositions(tile.Row,tile.Col,up.Row, up.Col); MatchTile left = tile.Left; MatchTile right = tile.Right; MatchTile down = tile.Down; MatchTile upUp = tile.Up.Up; MatchTile upLeft = tile.Up.Left; MatchTile upRight = tile.Up.Right; if(upUp != null) upUp.Down = tile; if(upLeft != null) upLeft.Right = tile; if(upRight != null) upRight.Left = tile; if(down != null) down.Up = up; if(left != null) left.Right = up; if(right != null) right.Left = up; tile.Up = upUp; tile.Down = up; tile.Left = upLeft; tile.Right = upRight; up.Up = tile; up.Down = down; up.Left = left; up.Right = right; count++; return MoveDeletedTileToTop(tile, count, longestMover); }
void MoveAllTiles() { if(newDeletedTiles.Count != 0){ foreach(MatchTile deleted in newDeletedTiles){ if(deleted.Up != null && !deleted.Up.IsEmpty){ tileIDsToCheckForMatches.Add(deleted.Up.Id); } } MatchTile longestMover = new MatchTile(); longestMover.Id = -1; foreach(MatchTile deleted in newDeletedTiles){ deletedIdsLastTurn.Add(deleted.Id); longestMover = MoveDeletedTileToTop(deleted,0, longestMover); } if(longestMover.Id != -1){ SetLongestMover(longestMover.Id); } StartCoroutine(DestroyGameObjects()); } else{ GoToNextTurn(); } }
int CountSetupMatches(MatchTile tile, int count, int tileType, Directions direction) { if(tile == null || tile.TileType != tileType){ return count; } count++; MatchTile nextTile = new MatchTile(); if(direction == Directions.Left){ nextTile = tile.Left; } else if (direction == Directions.Up){ nextTile = tile.Up; } return CountSetupMatches(nextTile,count, tileType, direction); }