private bool ValidateMatch(Vector2Int coordinate, out List <Vector2Int> matchList) { matchList = null; BoardTile boardTile = tiles[coordinate.x, coordinate.y]; if (boardTile == null) { return(false); } return(CheckForMatch(coordinate, boardTile, out matchList)); }
private void SwapeTile(Vector2Int tile01, Vector2Int tile02, bool counting) { BoardTile boardTile01 = tiles[tile01.x, tile01.y]; BoardTile boardTile02 = tiles[tile02.x, tile02.y]; tiles[tile01.x, tile01.y] = boardTile02; tiles[tile02.x, tile02.y] = boardTile01; Vector3 boardTile01Pos = boardTile01.Position; Vector3 boardTile02Pos = boardTile02.Position; boardTile02.SetPosition(boardTile01Pos); boardTile01.SetPosition(boardTile02Pos, () => OnSwapeTileComplete(tile02, counting)); }
private void DrawBoard(Vector3 originPosition, BoardProfile profile) { if (originPosition == null || profile.tilePack == null) { return; } Vector3 startPosition = originPosition; BoardTile prefab = profile.tilePack.boardTiles[0]; for (int x = 0; x < profile.size.x; x++) { for (int y = 0; y < profile.size.y; y++) { Vector3 position = new Vector3(startPosition.x + (prefab.size.x * 0.5f) + (prefab.size.x * x), startPosition.y - (prefab.size.y * 0.5f) - (prefab.size.y * y), 0); DrawRect(position, prefab.size, x, y); if (debug) { // --- Debug Coordinates --- if (debugCoordinates.x == x && debugCoordinates.y == y) { foreach (Vector2Int adjacentItem in GetAdjacent(new Vector2Int(x, y))) { Vector3 position2 = new Vector3(startPosition.x + (prefab.size.x * 0.5f) + (prefab.size.x * adjacentItem.x), startPosition.y - (prefab.size.y * 0.5f) - (prefab.size.y * adjacentItem.y), 0); DrawRect(position2, prefab.size, adjacentItem.x, adjacentItem.y); } } } } } }
private bool CheckForMatch(Vector2Int coordinate, BoardTile boardTile, out List <Vector2Int> matchList) { matchList = new List <Vector2Int>(); List <Vector2Int> neighbours = GetAdjacent(coordinate); Vector2Int heading = Vector2Int.zero; foreach (Vector2Int neighbour in neighbours) { if (tiles[neighbour.x, neighbour.y] == null) { continue; } if (tiles[neighbour.x, neighbour.y].id == boardTile.id) { bool neighbourFound = true; heading = neighbour - coordinate; int matchCount = 2; // Forward Check Vector2Int lastPosition = neighbour; while (neighbourFound) { Vector2Int newPosition = lastPosition + heading; if (newPosition.x > -1 && newPosition.x < tilesDimesions.x && newPosition.y > -1 && newPosition.y < tilesDimesions.y) { if (tiles[newPosition.x, newPosition.y] == null) { neighbourFound = false; continue; } // Found next Match if (tiles[lastPosition.x, lastPosition.y].id == tiles[newPosition.x, newPosition.y].id) { lastPosition = newPosition; matchList.Add(newPosition); ++matchCount; continue; } } // Fallback if not match neighbourFound = false; } if (matchCount > 2) { if (!matchList.Contains(neighbour)) { matchList.Add(neighbour); } if (!matchList.Contains(coordinate)) { matchList.Add(coordinate); } } } } return(matchList.Count > 2); }
private IEnumerator ValidateBoard(UnityAction callback) { // Check for Matches int removedTileCount = 0; for (int x = 0; x < tilesDimesions.x; x++) { for (int y = 0; y < tilesDimesions.y; y++) { Vector2Int coordinate = new Vector2Int(x, y); if (ValidateMatch(coordinate, out List <Vector2Int> matchList)) { float duration = 0f; // --- Clean Matches --- for (int i = 0; i < matchList.Count; i++) { duration = tiles[matchList[i].x, matchList[i].y].Despawn(); tiles[matchList[i].x, matchList[i].y] = null; ++removedTileCount; } // Event if (OnMatch != null) { OnMatch.Invoke(matchList.Count); } if (removedTileCount > 0) { yield return(new WaitForSeconds(tweenMatchTileDelay)); } } } } // --- Shifting/Refill --- if (removedTileCount > 0) { Vector3 startPosition = boardOriginPosition; for (int x = 0; x < tilesDimesions.x; x++) { for (int y = tilesDimesions.y - 1; y >= 0; y--) { if (tiles[x, y] == null) { bool found = false; for (int y2 = y; y2 >= 0; y2--) { if (tiles[x, y2] != null) { Vector3 position = new Vector3(startPosition.x + (tileSize.x * 0.5f) + (tileSize.x * x), startPosition.y - (tileSize.y * 0.5f) - (tileSize.y * y), 0); tiles[x, y2].SetPosition(position); tiles[x, y] = tiles[x, y2]; tiles[x, y2] = null; found = true; break; } } // Refill if (!found) { // -- Create Tileset -- BoardTile[] tilesetLibrary = boardProfile.tilePack.boardTiles; // TileSet //int tileIndex = possibleBoardTiles[Random.Range(0, possibleBoardTiles.Count)]; BoardTile prefab = tilesetLibrary[Random.Range(0, tilesetLibrary.Length)]; Vector3 position = new Vector3(startPosition.x + (prefab.size.x * 0.5f) + (prefab.size.x * x), startPosition.y - (prefab.size.y * 0.5f) - (prefab.size.y * y), 0); BoardTile newTile = Instantiate(prefab, position + new Vector3(0f, 10f), prefab.transform.rotation, boardOrigin); tiles[x, y] = newTile; tiles[x, y].SetPosition(position); } } } yield return(new WaitForSeconds(tweenRefillRowDelay)); } // Event if (OnRefill != null) { OnRefill.Invoke(); } // --- Revalidate Board --- StartCoroutine(ValidateBoard(callback)); } else { if (OnNoMatch != null) { OnNoMatch.Invoke(); } callback?.Invoke(); } }
private void CreateBoard(BoardProfile profile) { // --- Set Board Variables --- BoardTile[] tilesetLibrary = profile.tilePack.boardTiles; // TileSet int tilesetLength = tilesetLibrary.Length; tiles = new BoardTile[profile.size.x, profile.size.y]; // board array2D tileSize = profile.tilePack.boardTiles[0].size; tilesDimesions = new Vector2Int(profile.size.x, profile.size.y); boardOriginPosition = boardOrigin.position + new Vector3(-tilesDimesions.x * (tileSize.x * 0.5f), tilesDimesions.y * (tileSize.y * 0.5f), 0f); // Variables for recognition int[] previousLeft = new int[profile.size.y]; int previousAbove = -1; previousLeftAbove = new int[profile.size.y]; tweenMatchTileDelay = profile.matchTileDelay; tweenRefillRowDelay = profile.refillRowDelay; // --- Board Loop --- for (int x = 0; x < profile.size.x; x++) { for (int y = 0; y < profile.size.y; y++) { // -- Create Possible TileList -- List <int> possibleBoardTiles = new List <int>(); for (int i = 0; i < tilesetLength; i++) { if (i != previousAbove && i != previousLeft[y]) { if (diagonalOption && x > 0 && y > 0) { bool digAbove = tiles[x - 1, y - 1].id != tilesetLibrary[i].id; if (y < tilesDimesions.y - 1) { bool digBelow = tiles[x - 1, y + 1].id != tilesetLibrary[i].id; if (digAbove && digBelow) { possibleBoardTiles.Add(i); } } else if (digAbove) { possibleBoardTiles.Add(i); } } else { possibleBoardTiles.Add(i); } } } // -- Create Tileset -- int tileIndex = possibleBoardTiles[Random.Range(0, possibleBoardTiles.Count)]; BoardTile prefab = tilesetLibrary[tileIndex]; Vector3 position = new Vector3(boardOriginPosition.x + (prefab.size.x * 0.5f) + (prefab.size.x * x), boardOriginPosition.y - (prefab.size.y * 0.5f) - (prefab.size.y * y), 0); BoardTile newTile = Instantiate(prefab, position, prefab.transform.rotation, boardOrigin); tiles[x, y] = newTile; // -- Remember last Tile -- previousLeft[y] = tileIndex; if (y > 0) { previousLeftAbove[y - 1] = tileIndex; } previousAbove = tileIndex; } } // --- Check Playability --- if (!CheckBoardPlayable()) { RecreateBoard(); } }