/// <summary> /// Generate all of the tiles in a new row. /// </summary> private void GenerateRow(int row, int width, bool isOffscreen = false, PuzzleTileType overrideType = PuzzleTileType.None) { // Iterate all of the tiles in this row and create them. for (int x = 0; x < width; x++) { PuzzleTileType tileType = overrideType; if (tileType == PuzzleTileType.None) { tileType = ChooseRandomNonMatchingTile(x, row); } PuzzleTile newTile = GenerateTile(tileType, x, row); newTile.IsOffscreen = isOffscreen; } if (row > m_lastAddedRow) { m_lastAddedRow = row; } }
/// <summary> /// Generate a tile from the type. /// </summary> private PuzzleTile GenerateTile(PuzzleTileType tileType, int x, int y) { GameObject resource = Resources.Load(PuzzleTileResourceMap[tileType]) as GameObject; GameObject tileObject = GameObject.Instantiate(resource); PuzzleTile puzzleTile = tileObject.GetComponent <PuzzleTile>(); m_createdTiles.Add(puzzleTile); PuzzleGrid[x, y] = puzzleTile; puzzleTile.X = x; puzzleTile.Y = y; puzzleTile.PuzzleTileType = tileType; // Set the position and check if it should fall. puzzleTile.transform.localPosition = GetLocalPositionOfTileCoordinate(x, y); CheckUnderBlockAndFall(puzzleTile.X, puzzleTile.Y); puzzleTile.transform.SetParent(TileContainer.transform, false); return(puzzleTile); }
/// <summary> /// If the specified type was at the specified position, return the tiles that would be a match as a result. /// </summary> private List <Vector2> GetMatchesFromTypeAtPosition(PuzzleTileType type, int x, int y, bool ignoreUnmatchableTag = false) { // Don't allow junk pieces to match. if (type == PuzzleTileType.Junk) { return(new List <Vector2>()); } HashSet <Vector2> matchingTiles = new HashSet <Vector2>(); // Check Row List <Vector2> matchingInRow = new List <Vector2>(); int width = DefaultPuzzleWidth; for (int checkX = 0; checkX < width; checkX++) { bool shouldClearList = false; if (checkX == x || (PuzzleGrid[checkX, y] != null && PuzzleGrid[checkX, y].PuzzleTileType == type && (ignoreUnmatchableTag || PuzzleGrid[checkX, y].CanBeMatched))) { matchingInRow.Add(new Vector2(checkX, y)); } else { shouldClearList = true; } // If the row matched 3 , // and that match contained our tile, if (matchingInRow.Count >= MinimumMatch) { if (matchingInRow.Any(v => (int)v.x == x)) { // and we hit the end of a row or a new color or an unmatchable tile, then add what we have as a match. if (checkX == width - 1 || (checkX != x && PuzzleGrid[checkX, y] == null) || (checkX != x && PuzzleGrid[checkX, y].PuzzleTileType != type) || (checkX != x && !ignoreUnmatchableTag && !PuzzleGrid[checkX, y].CanBeMatched)) { matchingInRow.ForEach(t => matchingTiles.Add(t)); break; } } } if (shouldClearList) { matchingInRow.Clear(); } } // Check Column List <Vector2> matchingInColumn = new List <Vector2>(); int numberToCheck = m_rowsPastTopOfScreen + DefaultPuzzleHeight + 3; for (int checkY = 0; checkY < numberToCheck; checkY++) { bool shouldClearList = false; if (checkY == y || (PuzzleGrid[x, checkY] != null && PuzzleGrid[x, checkY].PuzzleTileType == type && (ignoreUnmatchableTag || PuzzleGrid[x, checkY].CanBeMatched))) { matchingInColumn.Add(new Vector2(x, checkY)); } else { shouldClearList = true; } // If the column matched at least 3, // and that match contained our tile if (matchingInColumn.Count >= MinimumMatch) { if (matchingInColumn.Any(v => (int)v.y == y)) { // and we hit the end of a column or a new color or an unmatchable tile, then add what we have as a match. if (checkY == numberToCheck - 1 || (checkY != y && PuzzleGrid[x, checkY] == null) || (checkY != y && PuzzleGrid[x, checkY].PuzzleTileType != type) || (checkY != y && !ignoreUnmatchableTag && !PuzzleGrid[x, checkY].CanBeMatched)) { matchingInColumn.ForEach(t => matchingTiles.Add(t)); break; } } } if (shouldClearList) { matchingInColumn.Clear(); } } return(new List <Vector2>(matchingTiles)); }