private void UpgradeTile(GameObject toDestroy, Tile destroyTile, GameObject toUpgrade, Tile upgradeTile) { Vector3 toDestroyPosition = toDestroy.transform.position; Vector3 toUpgradePosition = toUpgrade.transform.position; Vector2 upgradeGridPoint = WorldToGridPoint(toUpgradePosition.x, toUpgradePosition.y); Vector2 destroyGridPoint = WorldToGridPoint(toDestroyPosition.x, toDestroyPosition.y); // create the upgraded tile GameObject newTile = (GameObject)Instantiate(tilePrefabs[upgradeTile.power], toUpgradePosition, transform.rotation); // set the upgrade tile's grid value to double its current value grid[Mathf.RoundToInt(upgradeGridPoint.x), Mathf.RoundToInt(upgradeGridPoint.y)] = upgradeTile.value * 2; // clear out the destroyed tile's grid entry grid[Mathf.RoundToInt(destroyGridPoint.x), Mathf.RoundToInt(destroyGridPoint.y)] = 0; points += upgradeTile.value * 2; scoreText.text = points.ToString(); // destroy both tiles Destroy(toDestroy); Destroy(toUpgrade); currentTilesAmount--; TileAnimationHandler tileAnim = newTile.GetComponent <TileAnimationHandler>(); tileAnim.AnimateUpgrade(); }
private void UpgradeTile(GameObject toDestroy, Tile destroyTile, GameObject toUpgrade, Tile upgradeTile) { Vector3 toUpgradePosition = toUpgrade.transform.position; tiles.Remove(toDestroy); tiles.Remove(toUpgrade); SimplePool.Despawn(toDestroy); SimplePool.Despawn(toUpgrade); // create the upgraded tile GameObject newTile = SimplePool.Spawn(tilePrefabs[upgradeTile.power], toUpgradePosition, transform.rotation); tiles.Add(newTile); Tile tile = newTile.GetComponent <Tile>(); tile.upgradedThisTurn = true; points += upgradeTile.value * 2; scoreText.text = points.ToString(); TileAnimationHandler tileAnim = newTile.GetComponent <TileAnimationHandler>(); tileAnim.AnimateUpgrade(); }
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(); }
private void Awake() { tileAnimationHandler = GetComponent <TileAnimationHandler>(); tileStylesHandler = GetComponentInParent <TileStylesHandler>(); }