public NPCBuildingRegulator ActivateBuildingRegulator(NPCBuildingRegulator buildingRegulator, BuildingCenterRegulator centerRegulator) { BuildingCenterRegulator.ActiveBuildingRegulator abr = IsRegulatorActive(buildingRegulator, centerRegulator); if (abr != null) //regulator is already active { return(abr.instance); //return the instance. } //we will be activating the building regulator for the input center only BuildingCenterRegulator.ActiveBuildingRegulator newBuildingRegulator = new BuildingCenterRegulator.ActiveBuildingRegulator() { //create new instance instance = Instantiate(buildingRegulator), source = buildingRegulator, //assign the source/prefab. //initial spawning timer: regular spawn reload + start creating after value spawnTimer = buildingRegulator.spawnReloadRange.getRandomValue() + buildingRegulator.startCreatingAfter.getRandomValue() }; //add it to the active building regulators list: centerRegulator.activeBuildingRegulators.Add(newBuildingRegulator); newBuildingRegulator.instance.Init(npcMgr, this, centerRegulator.buildingCenter); //initialize the building regulator. //whenever a new regulator is added to the active regulators list, then move the building creator into the active state isActive = true; //return the new created instance: return(newBuildingRegulator.instance); }
/// <summary> /// Activates the NPCBuildingRegulator instances for the NPC faction's population buildings /// </summary> public void ActivatePopulationBuildingRegulators() { populationBuildingsMonitor.Init(factionMgr); //Go ahead and add the army units regulators (if there are valid ones) foreach (Building building in populationBuildings) { Assert.IsNotNull(building, $"[NPCPopulationManager] NPC Faction ID: {factionMgr.FactionID} 'Population Buildings' list has some unassigned elements."); Assert.IsTrue(building.GetAddedPopulationSlots() > 0, $"[NPCPopulationManager] NPC Faction ID: {factionMgr.FactionID} 'Population Buildings' list has includes Building of code: {building.GetCode()} that does not add population slots."); NPCBuildingRegulator nextRegulator = null; //only add the army unit regulators that match this NPC faction's type if ((nextRegulator = npcMgr.GetNPCComp <NPCBuildingCreator>().ActivateBuildingRegulator( building, npcMgr.GetNPCComp <NPCBuildingCreator>().GetCapitalBuildingRegualtor())) != null) { populationBuildingsMonitor.Replace("", nextRegulator.Code); } } Assert.IsTrue(populationBuildingsMonitor.GetCount() > 0, $"[NPCPopulationManager] NPC Faction ID: {factionMgr.FactionID} doesn't have any active NPCBuildingRegulator instances for population buildings."); }
//a method to add a new instance of a unit regulator public void ActivateBuildingRegulator(NPCBuildingRegulator buildingRegulator) { //go through the building cenetr regulators: foreach (BuildingCenterRegulator bcr in buildingCenterRegulators) { ActivateBuildingRegulator(buildingRegulator, bcr); } }
public void ActivateCenterRegulator() { //activate the center regulator: if (centerRegulator != null) { centerRegulatorIns = npcMgr.buildingCreator_NPC.ActivateBuildingRegulator(centerRegulator, npcMgr.buildingCreator_NPC.GetCapitalRegualtor()); } else { Debug.LogError("The building center regulator hasn't been assigned in the NPC Territory Manager component!"); } }
//is a certain NPC Building Regulator already active? public BuildingCenterRegulator.ActiveBuildingRegulator IsRegulatorActive(NPCBuildingRegulator nbr, BuildingCenterRegulator centerRegulator) { //go through the building center's active building regulators: foreach (BuildingCenterRegulator.ActiveBuildingRegulator abr in centerRegulator.activeBuildingRegulators) { if (abr.source == nbr) //the regulator is already active: { return(abr); } } return(null); //regulator hasn't been found. }
//a method to remove an active instance of a building regulator by providing its source/prefab regulator: public void DestroyActiveRegulator(NPCBuildingRegulator nbr) { foreach (BuildingCenterRegulator bcr in buildingCenterRegulators) //go through the active regulators { for (int i = 0; i < bcr.activeBuildingRegulators.Count; i++) { //if this is the building regulator we're looking for: BuildingCenterRegulator.ActiveBuildingRegulator abr = bcr.activeBuildingRegulators[i]; if (nbr == abr.source) { //destroy the active instance: Destroy(abr.instance); //remove from list: bcr.activeBuildingRegulators.RemoveAt(i); break; } } } }
//supply a building regulator prefab and get a valid active instance: NPCBuildingRegulator GetActiveInstance(NPCBuildingRegulator source) { //search for the first suitable instance that has the same building source regulator: //go through all the building center regulators: foreach (BuildingCenterRegulator bcr in buildingCenterRegulators) { //go through the active unit regulators: foreach (BuildingCenterRegulator.ActiveBuildingRegulator abr in bcr.activeBuildingRegulators) { //if this active building regulator hasn't reached its max amount and it has the same source/prefab: if (!abr.instance.HasReachedMaxAmount() && abr.source == source) { //set the instance: return(abr.instance); } } } return(null); }
//a method called that configures NPC faction components in case of a building upgrade: private void LaunchNPCBuildingUpgrade(NPCManager npcMgrIns, Building source, Building target) { //we need access to the NPC Building Creator in order to find the active regulator instance that manages the building type to be upgraded: NPCBuildingCreator buildingCreator_NPC = npcMgrIns.buildingCreator_NPC; NPCBuildingRegulator buildingRegulator = npcMgrIns.GetBuildingRegulatorAsset(source); //will hold the building's regulator that is supposed to be upgraded. NPCBuildingRegulator targetBuildingRegulator = npcMgrIns.GetBuildingRegulatorAsset(target);; //will hold the target building's regulator. //we expect both above regulators to be valid: if (buildingRegulator == null) { Debug.LogError("[Upgrade Manager] Can not find a valid NPC Building Regulator for the upgrade source."); return; } if (targetBuildingRegulator == null) { Debug.LogError("[Upgrade Manager] Can not find a valid NPC Building Regulator for the upgrade target."); return; } //destroy the old building regulator buildingCreator_NPC.DestroyActiveRegulator(buildingRegulator); //if the building to be upgraded was either, the main population building or the main center building //then we'll update that as well. if (buildingRegulator == npcMgrIns.populationManager_NPC.populationBuilding) { npcMgrIns.populationManager_NPC.populationBuilding = targetBuildingRegulator; npcMgrIns.populationManager_NPC.ActivatePopulationBuilding(); //activate the new population building regulator. } if (buildingRegulator == npcMgrIns.territoryManager_NPC.centerRegulator) { npcMgrIns.territoryManager_NPC.centerRegulator = targetBuildingRegulator; //assign new regulator for the center building npcMgrIns.territoryManager_NPC.ActivateCenterRegulator(); //activate the new regulator. } //activate the new regulator: buildingCreator_NPC.ActivateBuildingRegulator(targetBuildingRegulator); }
/// <summary> /// Activates the main NPCBuildingRegulator instance for the main building center. /// </summary> public void ActivateCenterRegulator() { centerMonitor.Init(factionMgr); //Go ahead and add the building center regulator foreach (Border center in centers) { Assert.IsNotNull(center, $"[NPCTerritoryManager] NPC Faction ID: {factionMgr.FactionID} 'Centers' list has some unassigned elements."); NPCBuildingRegulator nextRegulator = null; //as soon a center prefab produces a valid building regulator instance (matches the faction type and faction npc manager), add to monitoring if ((nextRegulator = npcMgr.GetNPCComp <NPCBuildingCreator>().ActivateBuildingRegulator( center.GetComponent <Building>(), npcMgr.GetNPCComp <NPCBuildingCreator>().GetCapitalBuildingRegualtor())) != null) { centerMonitor.Replace("", nextRegulator.Code); } } Assert.IsTrue(centerMonitor.GetCount() > 0, $"[NPCTerritoryManager] NPC Faction ID: {factionMgr.FactionID} doesn't have a center building regulator assigned!"); }
//before a building is placed, a request must be done to create it: public void OnCreateBuildingRequest(NPCBuildingRegulator instance, bool auto, Building buildingCenter, bool sourceRegulator = false) { //if the building regulator that has been used is the parameter is the source/prefab not the actual instance: if (sourceRegulator == true) { instance = GetActiveInstance(instance); } //if an active instance isn't provided: if (instance == null) { return; //do not proceed } //if this has been requested from another NPC component and the regulator doesn't allow it if (auto == false && instance.createOnDemand == false) { return; //do not proceed. } if (!instance.HasReachedMaxAmount()) //as long as we haven't reached the max amount { //pick a building prefab: Building buildingPrefab = instance.prefabs[Random.Range(0, instance.prefabs.Count)]; //check if faction has enough resources to place the chosen prefab above. if (resourceMgr.CheckResources(buildingPrefab.BuildingResources, factionMgr.FactionID) == true) { //if the building center hasn't been chosen: if (buildingCenter == null) { //need to look for one: //this method will look for a center that will get a center that can have this building placed. //if none is found then stop here. if ((buildingCenter = GetFreeBuildingCenter(buildingPrefab)) == null) { //TO BE MODIFIED -> no building center is found -> request to place a building center? return; } } GameObject buildAround = null; //this is the object that the building will be built around //go through all the building placement option cases: switch (instance.placementOption) { case NPCBuildingRegulator.placementOptions.aroundResource: //building will be placed around a resource: //get the list of the resources in the building center where the building will be placed with the requested resource name List <Resource> availableResourceList = ResourceManager.FilterResourceList(buildingCenter.BorderMgr.ResourcesInRange, instance.placementOptionInfo); if (availableResourceList.Count > 0) //if there are resources found: { //pick one randomly: buildAround = availableResourceList[Random.Range(0, availableResourceList.Count)].gameObject; } break; case NPCBuildingRegulator.placementOptions.aroundBuilding: //building will be placed around another building //get the list of the buildings that match the requested code around the building center List <Building> buildingList = buildingCenter.BorderMgr.BuildingsInRange; buildingList.Add(buildingCenter); List <Building> availableBuildingList = BuildingManager.FilterBuildingList(buildingList, instance.placementOptionInfo); if (availableBuildingList.Count > 0) //if there are buildings found: { //pick one randomly: buildAround = availableBuildingList[Random.Range(0, availableBuildingList.Count)].gameObject; } break; default: //no option? buildAround = buildingCenter.gameObject; //build around building center. break; } npcMgr.buildingPlacer_NPC.OnBuildingPlacementRequest(buildingPrefab, buildAround, buildingCenter, instance.buildAroundDistance); } else { //TO BE MODIFIED -> NO RESOURCES FOUND -> ASK FOR SOME. } } }