private void LoadBoard() { for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { Vector2 worldPosition = GridToWorldPoint(x, y); if (PlayerPrefs.HasKey(worldPosition.ToString())) { state = State.WaitingForInput; if (PlayerPrefs.GetInt(worldPosition.ToString()) == 999) { SimplePool.Spawn(noTile, worldPosition, transform.rotation); continue; } SimplePool.Spawn(tilePrefabs[PlayerPrefs.GetInt(worldPosition.ToString())], worldPosition, transform.rotation); GameObject obj = GetObjectAtGridPosition(x, y); tiles.Add(obj); TileAnimationHandler tileAnimManager = obj.GetComponent <TileAnimationHandler>(); tileAnimManager.AnimateEntry(); } } } points = 0; if (PlayerPrefs.HasKey("Score")) { points = PlayerPrefs.GetInt("Score"); //scoreText.text = "0"; } scoreText.text = points.ToString(); }
public void GenerateRandomTile() { // make sure we can create tiles if (currentTilesAmount >= 16) { throw new UnityException("Unable to create new tile - grid is already full"); } int value; // find out if we are generating a tile with the lowest or highest value float highOrLowChance = Random.Range(0f, 0.99f); if (highOrLowChance >= 0.9f) { value = highestNewTileValue; } else { value = lowestNewTileValue; } // attempt to get the starting position int x = Random.Range(0, cols); int y = Random.Range(0, rows); // starting from the random starting position, loop through // each cell in the grid until we find an empty positio bool found = false; while (!found) { if (grid[x, y] == 0) { found = true; grid[x, y] = value; Vector2 worldPosition = GridToWorldPoint(x, y); GameObject obj; if (value == lowestNewTileValue) { obj = (GameObject)Instantiate(tilePrefabs[0], worldPosition, transform.rotation); } else { obj = (GameObject)Instantiate(tilePrefabs[1], worldPosition, transform.rotation); } currentTilesAmount++; TileAnimationHandler tileAnimManager = obj.GetComponent <TileAnimationHandler>(); tileAnimManager.AnimateEntry(); } x++; if (x >= cols) { y++; x = 0; } if (y >= rows) { y = 0; } } }
public void GenerateRandomTile() { if (tiles.Count >= rows * cols) { throw new UnityException("Unable to create new tile - grid is already full"); } int value; // find out if we are generating a tile with the lowest or highest value float highOrLowChance = Random.Range(0f, 0.99f); if (highOrLowChance >= 0.9f) { value = highestNewTileValue; } else { value = lowestNewTileValue; } // attempt to get the starting position int x = Random.Range(0, cols); int y = Random.Range(0, rows); // starting from the random starting position, loop through // each cell in the grid until we find an empty positio bool found = false; while (!found) { if (GetObjectAtGridPosition(x, y) == noTile) { found = true; Vector2 worldPosition = GridToWorldPoint(x, y); GameObject obj; if (value == lowestNewTileValue) { obj = SimplePool.Spawn(tilePrefabs[0], worldPosition, transform.rotation); } else { obj = SimplePool.Spawn(tilePrefabs[1], worldPosition, transform.rotation); } tiles.Add(obj); TileAnimationHandler tileAnimManager = obj.GetComponent <TileAnimationHandler>(); tileAnimManager.AnimateEntry(); } x++; if (x >= cols) { y++; x = 0; } if (y >= rows) { y = 0; } } SaveBoard(); }