protected virtual void Plant(int plantNumber) { // Plant animation in that direction // Check if there is space in front to plant // If there is plant // Instatiate new plant object // position it in the world // Else make failure animation // Start cooldown timer/reduce seed count // TODO: use more general form of detecting direction // Vector3 dirr = Globals.DirectionToVector(direction); // PlantGridObject newPlant = (PlantGridObject)Instantiate(plants[plantNumber], transform.position + dirr, Quaternion.identity); if (Globals.inventory[plantNumber] > 0 && plantCooldown <= 0) { canPlant = true; foreach (KeyValuePair <Globals.PlantData, int> plant in Globals.plants) { if (plant.Key.PlantScene == Application.loadedLevelName) { if (Mathf.Abs(plant.Key.PlantLocation.x - this.gameObject.transform.position.x) < 0.5 && Mathf.Abs(plant.Key.PlantLocation.y - this.gameObject.transform.position.y) < 0.5) { canPlant = false; } } } if (canPlant == true) { //planting code PlantGridObject newPlant = (PlantGridObject)Instantiate(plants[plantNumber], new Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity); if (plantNumber == 1) { TurbinePlantObject turbinePlant = (TurbinePlantObject)newPlant; turbinePlant.playSound(); } newPlant.Rotate(direction); Globals.inventory[plantNumber]--; Globals.PlantData thisPlant = new Globals.PlantData(newPlant.transform.position, Application.loadedLevelName, newPlant.direction); Globals.plants.Add(thisPlant, plantNumber); canvas.UpdateUI(); //recheck if player can plant plantCooldown = plantDelay; canvas.UpdatePlantCooldown(plantCooldown <= 0); } else { audioSource.clip = invalidPlacement; audioSource.Play(); } } }
//start happens 1st frame void Start() { Globals.tileMap = this; rooms = new GameObject[transform.GetChild(0).childCount]; for (int i = 0; i < transform.GetChild(0).childCount; i++) { rooms[i] = transform.GetChild(0).GetChild(i).gameObject; } int plantType; Vector3 plantVector; foreach (KeyValuePair <Globals.PlantData, int> kvp in Globals.plants) { if (kvp.Key.PlantScene == Application.loadedLevelName) { plantVector = kvp.Key.PlantLocation; plantType = kvp.Value; PlantGridObject newPlant = (PlantGridObject)Instantiate(player.GetComponent <PlayerGridObject>().plants[plantType], plantVector, Quaternion.identity); newPlant.Rotate(kvp.Key.PlantDirection); } } // Get all Tiles that are children of this TileMap object Tile[] myTiles = GetComponentsInChildren <Tile>(); // For each tile: get the tile's position with respect // to the tilemap's position. foreach (Tile tile in myTiles) { Vector3 tilePosition = tile.transform.position; // Tile's position with respect to the tilemap's position tilePosition -= this.transform.position; //Debug.Log(tilePosition); grid[(int)tilePosition.x, (int)tilePosition.y] = tile; //nodeGrid[(int)tilePosition.x, (int)tilePosition.y] = new Node(tile, this); } }