public void toggleGrid() { showGrid = !showGrid; // Do something to the materials if (!inToggle) { inToggle = true; GameObject rGrid = GameObject.Find("Grid"); foreach (Transform PlacementPlane in rGrid.transform) { if (showGrid) { PlacementPlane.GetComponent <Renderer>().material.mainTexture = grassG; } else { PlacementPlane.GetComponent <Renderer>().material.mainTexture = grass; } } inToggle = false; } }
void starSpawner() { if (!starSpawned) { while (!starSpawned) { int tempX = Random.Range(0, 9); int tempY = Random.Range(0, 9); foreach (Transform PlacementPlane in rGrid.transform) { // Break if the x and y is same as the previous star if (tempX == starPrevX && tempY == starPrevY) { break; } // Get the grid data of the grid GridData gData = PlacementPlane.GetComponent <GridData>(); // If this grid is same as the randomed if (gData.posX == tempX && gData.posY == tempY) { // if is it not a building and not occupied if (!gData.isBuilding && !gData.isOccupied) { // Set the plane tag to opened PlacementPlane.tag = "Open"; // Create a star there gData.isStar = true; GameObject obj = GameObject.Find("Star"); GameObject createdObj = Instantiate(obj, PlacementPlane.transform.position, Quaternion.identity) as GameObject; createdObj.name = "Star"; // ONLY CAUSE I AM USING MOUNTAIN AS STAR. TO BE REMOVED ONCE USING REAL STAR createdObj.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); //creates the obj as the child of the grid createdObj.transform.parent = PlacementPlane.transform; // Hide the obj obj.transform.position += new Vector3(0, 100, 0); starSpawned = true; break; } } } } } }
// NCA: Function that will create the object on the grid // Currently to create train on start grid. void populateGrid() { // Change the list of building into array for easy reading ObjData[] temp = levelData.vObjects.ToArray(); for (int i = 0; i < temp.Length; i++) { foreach (Transform PlacementPlane in rGrid.transform) { GridData gData = PlacementPlane.GetComponent <GridData>(); if (gData.posX == temp[i].posX && gData.posY == temp[i].posY) { // Create object GameObject buildingObj; if (temp[i].isTrack) { // Track buildingObj = GameObject.Find(temp[i].trackType.ToString()); } else if (temp[i].isBuilding) { // Building buildingObj = GameObject.Find(temp[i].type.ToString()); } else { // Train buildingObj = GameObject.Find(temp[i].trainType.ToString()); } GameObject TargetObj = Instantiate(buildingObj, PlacementPlane.transform.position, Quaternion.identity) as GameObject; if (temp[i].isTrack) { // Track buildingObj = GameObject.Find(temp[i].trackType.ToString()); PlacementPlane.tag = "Track"; gData.isTrack = true; gData.TrackType = temp[i].trackType; TargetObj.name = "Track Created"; //creates the obj as the child of the grid TargetObj.transform.parent = PlacementPlane.transform; gData.isNotRemoveable = true; } else if (temp[i].isBuilding) { // Building buildingObj = GameObject.Find(temp[i].type.ToString()); PlacementPlane.tag = "Building"; gData.isBuilding = true; gData.buildingType = temp[i].type; TargetObj.name = "Building Created"; if (temp[i].type == Building.Depot) { TargetObj.name = "Depot Created"; TargetObj.tag = "Track"; gData.isDepot = true; gData.isTrack = true; gData.TrackType = Track.Vertical; } if (temp[i].type == Building.Windmill || temp[i].type == Building.Windmill2) { TargetObj.transform.Rotate(0, 180, 0); } //creates the obj as the child of the grid TargetObj.transform.parent = PlacementPlane.transform; // Need to do something to stop the user from moving the buildings // probably need to add in a bool in inputcontroller concerning the modes gData.isNotRemoveable = true; } else { // Train buildingObj = GameObject.Find(temp[i].trainType.ToString()); } buildingObj.transform.position += new Vector3(0, 100, 0); if (gData.posX == levelData.eGridX && gData.posY == levelData.eGridY) { gData.isDestination = true; } // If it is starting grid create a train if (gData.posX == levelData.sGridX && gData.posY == levelData.sGridY) { // Create Train // Add a train here GameObject TrainObj; TrainObj = GameObject.Find("Electric"); //create Target (current track selected) at lastHitObj.transform.position (center of the grid which cursor is in) GameObject Train = Instantiate(TrainObj, PlacementPlane.transform.position, Quaternion.identity) as GameObject; // Give the gameobject a name Train.name = "Diesel"; Train.GetComponent <TrainAI>().setTrainPos(gData.posX, gData.posY); //temporarily hide the train that is following the cursor by changing its position to be the same as the new track created TrainObj.transform.position += new Vector3(0, 100, 0); // Add train to train control Train.transform.parent = trainControl.transform; } } } } }
// Update is called once per frame void Update() { //---GUI if (livesCount <= 0) { GameOverPanel(); return; } if (Input.GetKeyDown(KeyCode.Escape)) { ToggleSettingsPanel(); } if (buildPanelOpen) { var ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 1000, placementLayerMask)) { if (lastHitObj) { (lastHitObj.GetComponent <MeshRenderer>()).material = originalMat; } lastHitObj = hit.collider.gameObject; originalMat = (lastHitObj.GetComponent <MeshRenderer>()).material; (lastHitObj.GetComponent <MeshRenderer>()).material = hoverMat; } else { if (lastHitObj) { (lastHitObj.GetComponent <MeshRenderer>()).material = originalMat; lastHitObj = null; } } //drop turrets on click if (Input.GetMouseButtonDown(0) && lastHitObj && // 0 - left button !upgradePanelOpen && !GameController.IsPause) { focusedPlane = lastHitObj.GetComponent <PlacementPlane>(); int turretCost = allStructures[structureIndex].myCost; if (focusedPlane.isOpen && turretCost <= cashCount) { GameObject newStructure = Instantiate(allStructures[structureIndex].gameObject, lastHitObj.transform.position, Quaternion.identity) as GameObject; newStructure.transform.localEulerAngles = new Vector3( newStructure.transform.localEulerAngles.x, Random.Range(0, 360), newStructure.transform.localEulerAngles.z); focusedPlane.myStructure = newStructure; focusedPlane.isOpen = false; UpgradeCash(-turretCost); //update the Graph // rebuild the Graph AstarController.Scan(); foreach (GameObject theEnemy in GameObject.FindGameObjectsWithTag("Ground Enemy")) { theEnemy.GetComponent <AI_Pather>().GetNewPath(); } } else if (focusedPlane.myStructure != null) { ShowUpgradeGUI(); } } } //---End GUI //wave if (waveActive) { if (Time.time >= waveEndTime) { //stop spawn enemies spawnEnemies = false; if (enemyCount == 0) { // Корутин StartCoroutine(FinishWave()); } } if (spawnEnemies) { // if wave still going, spawn enemies if (Time.time > (lastSpawnTime + respawnInterval)) { SpawnNewEnemy(); } } } }
public void CreateNavigation(PlacementPlane navPlane) { placementPlane = Instantiate(navPlane); placementPlane.Setup(this); }