// Function responsible to generate a correctly match 3 board public void CreateBoard() { tiles = new GameObject[8, 8]; for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { // Create a new random TobleFood in the board GameObject newTile = Instantiate(TobleGem, new Vector3(0, 0, 0), TobleGem.transform.rotation); tiles[x, y] = newTile; // Search possible TobleFoods that can be used List <TobleSO> possibleSO = new List <TobleSO>(); possibleSO.AddRange(TobleSOs); // Check above and left to avoid matches if (x - 2 >= 0) { if (tiles[x - 1, y].tag == tiles[x - 2, y].tag) { possibleSO.Remove(tiles[x - 1, y].GetComponent <TobleGem>().TobleSO); } } if (y - 2 >= 0) { if (tiles[x, y - 1].tag == tiles[x, y - 2].tag) { possibleSO.Remove(tiles[x, y - 1].GetComponent <TobleGem>().TobleSO); } } TobleSO newSO = possibleSO[Random.Range(0, possibleSO.Count)]; // Inform the characteristics of this TobleFood TobleScript = newTile.GetComponent <TobleGem>(); TobleScript.TobleSO = newSO; TobleScript.x = x; TobleScript.y = y; newTile.gameObject.tag = newSO.tag; newTile.transform.SetParent(transform, false); // BoardManager is the parent of all TobleFoods } } Verify_Board(); }
// Function that applies gravity so that Toblefoods can fall if there is an empty space public void ApplyGravityToTobleFood() { // Search for empty spaces for (int x = 0; x < 8; x++) { for (int y = 7; y >= 0; y--) { // Search for Destroyed TobleFoods on the game board // If the search finds it, check above to fill the empty space if (tiles[x, y].tag != "TobleDestroyed") { continue; } for (int ny = (y - 1); ny >= -1; ny--) { if (ny >= 0) // Searching in the grid { // If it is not a Destroyed TobleFood, make it fall if (tiles[x, ny].tag == "TobleDestroyed") { continue; } FlipGems(GetGem(x, y), GetGem(x, ny), false, true); } else // Hit the end of the game board { // Create new random TobleFood to fill the destroyed TobleFood TobleSO newSO = TobleSOs[Random.Range(0, TobleSOs.Count)]; // Inform the characteristics of this TobleFood TobleScript = tiles[x, y].GetComponent <TobleGem>(); TobleScript.TobleSO = newSO; TobleScript.UpdateInterface(); // Change its position to fall at the top of the board TobleScript.Gem_r.anchoredPosition = Board_side.instance.getPosition(x, -1 - fills[x]) + new Vector2(Board_side.instance.size / 16, Board_side.instance.size / 16); ResetGem(TobleScript); fills[x]++; } break; } } } }