/// <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); }
private NPCUnitCreator unitCreator_NPC; //the NPCUnitCreator instance used to create all instances of the units regulated here. #endregion #region Initializing/Terminating /// <summary> /// NPCUnitRegulator constructor. /// </summary> /// <param name="data">Holds information regarding how the unit type that will be regulated.</param> /// <param name="prefab">Actual Unit prefab to regulate.</param> /// <param name="gameMgr">GameManager instance of the currently active game.</param> /// <param name="npcMgr">NPCManager instance that manages the NPC faction to whome the regulator component belongs.</param> /// <param name="unitCreator_NPC">NPCUnitCreator instance of the NPC faction that's responsible for creating units.</param> public NPCUnitRegulator(NPCUnitRegulatorData data, Unit prefab, GameManager gameMgr, NPCManager npcMgr, NPCUnitCreator unitCreator_NPC) : base(data, prefab, gameMgr, npcMgr) { this.Data = data; Assert.IsNotNull(this.Data, $"[NPCUnitRegulator] Initializing without NPCUnitRegulatorData instance is not allowed!"); this.unitCreator_NPC = unitCreator_NPC; Assert.IsNotNull(this.unitCreator_NPC, $"[NPCUnitRegulator] Initializing without a reference to the NPCBuildingCreator instance is not allowed!"); Assert.IsNotNull(prefab, $"[NPCUnitRegulator] Initializing without a reference to the unit's prefab is not allowed!"); //pick the rest random settings from the given info. ratio = Data.GetRatio(); //update the target amount UpdateTargetCount(); //go through all spawned units to see if the units that should be regulated by this instance are created or not: foreach (Unit u in this.factionMgr.GetUnits()) { Add(u); } //go through all spawned task launchers of the faction and track the ones that include tasks to create the regulated unit type. foreach (TaskLauncher tl in this.factionMgr.GetTaskLaunchers()) { AddTaskLauncher(tl); } //start listening to the required delegate events: CustomEvents.UnitCreated += Add; CustomEvents.UnitConversionComplete += OnUnitConversionComplete; CustomEvents.UnitDead += Remove; CustomEvents.TaskLaunched += OnTaskLaunched; CustomEvents.TaskCanceled += OnTaskCanceled; CustomEvents.TaskLauncherAdded += OnTaskLauncherAdded; CustomEvents.TaskLauncherRemoved += OnTaskLauncherRemoved; CustomEvents.MaxPopulationUpdated += OnMaxPopulationUpdated; }