//called whenever a border component is activated: void OnBorderActivated(Border border) { //if the building center that activated the border belongs to this faction if (border.MainBuilding.FactionID == factionMgr.FactionID) { //when border is activated then we'd send some of them to the resources to collect list of the NPC Resource Collector... //...and leave the rest idle and that is all depending on the resource exploit ratio defined in this component: //create a new slot in the faction resources for the new border: factionResources.Add(new FactionResources { buildingCenter = border.MainBuilding, idleResources = new List <Resource>() }); FactionResources newElem = factionResources[factionResources.Count - 1]; //go through the border resources: foreach (Resource r in border.ResourcesInRange) { //randomly decided if this resource is to be exploited: if (Random.Range(0.0f, 1.0f) <= resourceExploitRatioRange.getRandomValue()) { //if yes, then add it: newElem.exploitedResources.Add(r); npcMgr.resourceCollector_NPC.AddResource(r); } else { //if not add resource to idle list: newElem.idleResources.Add(r); } } } }
//a method that initializes resources for factions. public void InitFactionResources() { //Create as many faction slots as the amount of the spawned factions. factionsResources = new FactionResources[gameMgr.GetFactionCount()]; //Loop through all the factions: for (int i = 0; i < factionsResources.Length; i++) { factionsResources[i] = new FactionResources( mapResources, i == GameManager.PlayerFactionID, i == GameManager.PlayerFactionID || GameManager.MultiplayerGame ? 1.0f : gameMgr.GetFaction(i).GetNPCMgrIns().resourceManager_NPC.resourceNeedRatioRange.getRandomValue()); //init the faction resource var if (i == GameManager.PlayerFactionID) { PlayerFactionResources = factionsResources[i]; } //go through the buildings with border components for each faction and refresh the resources in their territory foreach (Building center in gameMgr.GetFaction(i).FactionMgr.GetBuildingCenters()) { center.BorderComp.CheckAllResources(); } } PlayerFactionResources.UpdateUI(); //right after setting up the resource settings above, refresh the resource UI. }
//called whenver a building is destroyed: void OnBuildingDestroyed(Building building) { //if the building belongs to this faction & has a Border component: if (building.FactionID == factionMgr.FactionID && building.BorderMgr == building.CurrentCenter) { FactionResources elemToRemove = null; //go through building centers and idle/exploited resources under their control: //go through the border resources: foreach (FactionResources fr in factionResources) { //if this is the building center that has been destroyed: if (fr.buildingCenter == building) { //this element will be removed from the faction resources list later elemToRemove = fr; //go through exploited resources: foreach (Resource r in fr.exploitedResources) { //remove them from the resources to collect list in the NPC Resource Collector npcMgr.resourceCollector_NPC.RemoveResource(r); //see if resource has collectors: for (int i = 0; i < r.WorkerMgr.WorkerPositions.Length; i++) { //if this is a valid collector: if (r.WorkerMgr.WorkerPositions[0].CurrentUnit != null) { //cancel collection & stop mvt: r.WorkerMgr.WorkerPositions[0].CurrentUnit.StopMvt(); r.WorkerMgr.WorkerPositions[0].CurrentUnit.CancelCollecting(); } } } } } if (elemToRemove != null) //if the building center has been identified { factionResources.Remove(elemToRemove); //remove it. } } }
//a method that initializes resources for factions. public void InitFactionResources() { //Create as many faction slots as the amount of the spawned factions. factionsResources = new FactionResources[gameMgr.GetFactionCount()]; //Loop through all the factions: for (int i = 0; i < factionsResources.Length; i++) { factionsResources[i] = new FactionResources( mapResources, i == GameManager.PlayerFactionID, 1.0f); if (i == GameManager.PlayerFactionID) { PlayerFactionResources = factionsResources[i]; } } PlayerFactionResources.UpdateUI(); //right after setting up the resource settings above, refresh the resource UI. }
//called whenver a building is destroyed: void OnBuildingDestroyed(Building building) { //if the building belongs to this faction & has a Border component: if (building.FactionID == factionMgr.FactionID && building.BorderComp == building.CurrentCenter) { FactionResources elemToRemove = null; //go through building centers and idle/exploited resources under their control: //go through the border resources: foreach (FactionResources fr in factionResources) { //if this is the building center that has been destroyed: if (fr.buildingCenter == building) { //this element will be removed from the faction resources list later elemToRemove = fr; //go through exploited resources: foreach (Resource r in fr.exploitedResources) { //remove them from the resources to collect list in the NPC Resource Collector npcMgr.resourceCollector_NPC.RemoveResource(r, false); Unit[] workersList = r.WorkerMgr.GetAll(); //get all workers in the worker manager for (int i = 0; i < workersList.Length; i++) { //cancel collection & stop mvt: workersList[i].MovementComp.Stop(); workersList[i].CancelJob(Unit.jobType.collecting); } } } } if (elemToRemove != null) //if the building center has been identified { factionResources.Remove(elemToRemove); //remove it. } } }
/// Should listen to changes in Faction resources public override void OnRaise(Faction data) { if (data is PlayerFaction) { FactionResources resources = data.Resources; foreach (var(resource, quantity) in resources.GetResources()) { /// Instantiates Resource Row if not already present /// Later, this could be used for refined resources /// so that they would not be displayed at the start of the game /// when the player is far from obtaining them if (!this.resourceRows.ContainsKey(resource)) { ResourceRow row = Instantiate(this.resourceRowPrefab) as ResourceRow; row.transform.SetParent(this.resourceRowsParent, false); this.resourceRows[resource] = row; this.resourceRows[resource].UpdateResourceNameLabel(resource.Name); } this.resourceRows[resource].UpdateAmountLabel(quantity); } } }