//a method to add a new instance of a unit regulator public NPCUnitRegulator ActivateUnitRegulator(NPCUnitRegulator unitRegulator) { //see if the unit regulator is already active or not ActiveUnitRegulator aue = IsUnitRegulatorActive(unitRegulator); if (aue != null) //if it is { return(aue.instance); //return the already active instance. } //default ActiveUnitRegulator newUnitRegulator = new ActiveUnitRegulator() { //create new instance instance = Instantiate(unitRegulator), source = unitRegulator, //set the prefab/resource //initial spawning timer: regular spawn reload + start creating after value spawnTimer = unitRegulator.spawnReloadRange.getRandomValue() + unitRegulator.startCreatingAfter.getRandomValue() }; //add it to the active unit regulators list: activeUnitRegulators.Add(newUnitRegulator); newUnitRegulator.instance.Init(npcMgr, this); //initialize the unit regulator. //whenever a new regulator is added to the active regulators list, then move the unit creator into the active state isActive = true; return(newUnitRegulator.instance); }
/// <summary> /// Creates and activates a NPCUnitRegulator instance for a unit prefab. /// </summary> /// <param name="unit">The <c>Unit</c> prefab for which NPCUnitRegulator instance will be created.</param> /// <returns>The created and active NPCUnitRegulator instance.</returns> public NPCUnitRegulator ActivateUnitRegulator(Unit unit) { NPCUnitRegulatorData data = unit.GetRegulatorData(factionMgr.Slot.GetTypeInfo(), npcMgr.NPCType.GetCode()); //get the regulator data if (data == null) //invalid regulator data? { return(null); //do not proceed } //see if the unit regulator is already active or not NPCUnitRegulator activeInstance = GetActiveUnitRegulator(unit.GetCode()); if (activeInstance != null) //if it is { return(activeInstance); //return the already active instance. } ActiveUnitRegulator newUnitRegulator = new ActiveUnitRegulator() { //create new instance instance = new NPCUnitRegulator(data, unit, gameMgr, npcMgr, this), //initial spawning timer: regular spawn reload + start creating after value spawnTimer = data.GetCreationDelayTime() }; //add it to the active unit regulators list: activeUnitRegulators.Add(unit.GetCode(), newUnitRegulator); //whenever a new regulator is added to the active regulators list, then move the unit creator into the active state Activate(); return(newUnitRegulator.instance); }
//a method that removes one active instance by providing its source/prefab regulator: public void DestroyActiveRegulator(NPCUnitRegulator nur) { //go through the active regulators for (int i = 0; i < activeUnitRegulators.Count; i++) { //if this is the building regulator we're looking for: ActiveUnitRegulator aur = activeUnitRegulators[i]; if (nur == aur.source) { //destroy the active instance: Destroy(aur.instance); //remove from list: activeUnitRegulators.RemoveAt(i); break; //leave loop } } }