private IEnumerator heightCheckCoroutine; //this coroutine is running as long as there's a building to be placed and it allows NPC factions to place buildings on different heights //a method that other NPC components use to request to place a building. public void OnBuildingPlacementRequest(Building buildingPrefab, GameObject buildAround, float buildAroundRadius, Building buildingCenter, float buildAroundDistance, bool rotate) { //if the building center or the build around object hasn't been specified: if (buildAround == null || buildingCenter == null) { Debug.LogError("Build Around object or Building Center for " + buildingPrefab.GetName() + " hasn't been specified in the Building Placement Request!"); return; } //take resources to place building. gameMgr.ResourceMgr.UpdateRequiredResources(buildingPrefab.GetResources(), false, factionMgr.FactionID); //pick the building's spawn pos: Vector3 buildAroundPos = buildAround.transform.position; //for the sample height method, the last parameter presents the navigation layer mask and 0 stands for the built-in walkable layer where buildings can be placed buildAroundPos.y = gameMgr.TerrainMgr.SampleHeight(buildAround.transform.position, buildAroundRadius, 0) + gameMgr.PlacementMgr.GetBuildingYOffset(); Vector3 buildingSpawnPos = buildAroundPos; buildingSpawnPos.x += buildAroundDistance; //create new instance of building and add it to the pending buildings list: PendingBuilding newPendingBuilding = new PendingBuilding { buildingPrefab = buildingPrefab, buildingInstance = Instantiate(buildingPrefab.gameObject, buildingSpawnPos, buildingPrefab.transform.rotation).GetComponent <Building>(), buildAroundPos = buildAroundPos, buildingCenter = buildingCenter, buildAroundDistance = buildAroundDistance, rotate = rotate }; //initialize the building instance for placement: newPendingBuilding.buildingInstance.InitPlacementInstance(gameMgr, factionMgr.FactionID, buildingCenter.BorderComp); //we need to hide the building initially, when its turn comes to be placed, appropriate settings will be applied. newPendingBuilding.buildingInstance.gameObject.SetActive(false); newPendingBuilding.buildingInstance.ToggleModel(false); //Hide the building's model: //Call the start building placement custom event: CustomEvents.OnBuildingStartPlacement(newPendingBuilding.buildingInstance); //add the new pending building to the list: pendingBuildings.Add(newPendingBuilding); if (pendingBuildings.Count == 1) //if the queue was empty before adding the new pending building { StartPlacingNextBuilding(); //immediately start placing it. heightCheckCoroutine = HeightCheck(heightCheckReload); //Start the height check coroutine to keep the building always on top of the terrain StartCoroutine(heightCheckCoroutine); } }
//a method that other NPC components use to request to place a building. public void OnBuildingPlacementRequest(Building buildingPrefab, GameObject buildAround, Building buildingCenter, float buildAroundDistance) { //if the building center or the build around object hasn't been specified: if (buildAround == null || buildingCenter == null) { Debug.LogError("Build Around object or Building Center hasn't been specified in the Building Placement Request!"); return; } //take resources to place building. resourceMgr.TakeResources(buildingPrefab.BuildingResources, factionMgr.FactionID); //pick the building's spawn pos: Vector3 buildAroundPos = new Vector3(buildAround.transform.position.x, GameManager.Instance.TerrainMgr.SampleHeight(buildAround.transform.position) + BuildingPlacement.instance.BuildingYOffset, buildAround.transform.position.z); Vector3 buildingSpawnPos = buildAroundPos; buildingSpawnPos.x += buildAroundDistance; //create new instance of building and add it to the pending buildings list: PendingBuilding newPendingBuilding = new PendingBuilding { buildingPrefab = buildingPrefab, buildingInstance = Instantiate(buildingPrefab.gameObject, buildingSpawnPos, buildingPrefab.transform.rotation).GetComponent <Building>(), buildAroundPos = buildAroundPos, buildingCenter = buildingCenter, buildAroundDistance = buildAroundDistance }; //initialize the building instance for placement: newPendingBuilding.buildingInstance.InitPlacementInstance(factionMgr.FactionID, buildingCenter.BorderMgr); //we need to hide the building initially, when its turn comes to be placed, appropriate settings will be applied. newPendingBuilding.buildingInstance.gameObject.SetActive(false); //Hide the building's model: newPendingBuilding.buildingInstance.BuildingModel.SetActive(false); newPendingBuilding.buildingInstance.BuildingPlane.SetActive(false); //hide the building's selection texture //Call the start building placement custom event: if (GameManager.Instance.Events) { GameManager.Instance.Events.OnBuildingStartPlacement(newPendingBuilding.buildingInstance); } //add the new pending building to the list: pendingBuildings.Add(newPendingBuilding); if (pendingBuildings.Count == 1) //if the queue was empty before adding the new pending building { StartPlacingNextBuilding(); //immediately start placing it. } }
void Awake() { slider = GetComponentInChildren <Slider>(); pendingBuilding = GetComponentInParent <HeirarchyNode>().GetComponentInChildren <PendingBuilding>(); }