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(); } } }
public void changeDirection(Globals.Direction directionToMoveTo) { for (int i = 0; i < moveList.Count; i++) { if (moveList[i].CompareTag("Turbine")) { TurbinePlantObject plant = moveList[i].GetComponent <TurbinePlantObject>(); plant.direction = directionToMoveTo; plant.setDirection(); moveList.Remove(plant.gameObject); } } }