コード例 #1
0
        //a method called that configures NPC faction components in case of a unit upgrade:
        private void LaunchNPCUnitUpgrade(NPCManager npcMgrIns, Unit source, Unit target)
        {
            //we need access to the NPC Unit Creator in order to find the active regulator instance that manages the unit type to be upgraded:
            NPCUnitCreator unitCreator_NPC = npcMgrIns.unitCreator_NPC;

            NPCUnitRegulator unitRegulator       = npcMgrIns.GetUnitRegulatorAsset(source);  //will hold the unit's regulator that is supposed to be upgraded.
            NPCUnitRegulator targetUnitRegulator = npcMgrIns.GetUnitRegulatorAsset(target);; //will hold the target unit's regulator

            //we expect both above regulators to be valid:
            if (unitRegulator == null)
            {
                Debug.LogError("[Upgrade Manager] Can not find a valid NPC Unit Regulator for the upgrade source.");
                return;
            }
            if (targetUnitRegulator == null)
            {
                Debug.LogError("[Upgrade Manager] Can not find a valid NPC Unit Regulator for the upgrade target.");
                return;
            }

            //destroy the old building regulator
            unitCreator_NPC.DestroyActiveRegulator(unitRegulator);

            //if the unit to be upgraded was either, the main builder, collector or one of the army units
            //then we'll update that as well.
            if (unitRegulator == npcMgrIns.buildingConstructor_NPC.builderRegulator)
            {
                npcMgrIns.buildingConstructor_NPC.builderRegulator = unitRegulator;
                npcMgrIns.buildingConstructor_NPC.ActivateBuilderRegulator(); //activate the new unit regulator
            }
            if (unitRegulator == npcMgrIns.resourceCollector_NPC.collectorRegulator)
            {
                npcMgrIns.resourceCollector_NPC.collectorRegulator = unitRegulator;
                npcMgrIns.resourceCollector_NPC.ActivateCollectorRegulator();          //activate the new unit regulator
            }
            if (npcMgrIns.armyCreator_NPC.armyUnitRegulators.Contains(unitRegulator))  //is the unit to upgrade an army unit?
            {
                npcMgrIns.armyCreator_NPC.armyUnitRegulators.Remove(unitRegulator);    //remove old regulator from list
                npcMgrIns.armyCreator_NPC.armyUnitRegulators.Add(targetUnitRegulator); //add new regulator asset
                npcMgrIns.armyCreator_NPC.ActivateArmyUnitRegulators();                //activate army regulators.
            }

            //activate the new regulator:
            unitCreator_NPC.ActivateUnitRegulator(targetUnitRegulator);
        }
コード例 #2
0
        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;
        }
コード例 #3
0
        public NPCUnitCreator unitCreator_NPC; //the NPC Unit Creator used to create all instances of the units regulated here.

        /* Initializing everything */
        public void Init(NPCManager npcMgr, NPCUnitCreator unitCreator_NPC)
        {
            //assign the appropriate faction manager and unit creator settings
            base.InitItem(npcMgr);
            this.unitCreator_NPC = unitCreator_NPC;

            //set the building code:
            code = prefabs[0].Code;

            //pick the rest random settings from the given info.
            ratio = ratioRange.getRandomValue();

            //update the target amount
            UpdateTargetAmount(GameManager.Instance.Factions[this.factionMgr.FactionID].GetMaxPopulation());

            //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.Units)
            {
                //only if the unit belongs to this regulator:
                if (u.Code == code)
                {
                    amount++;
                    currentInstances.Add(u);
                }
            }

            //go through all spawned task launchers of the faction and see if there's one that can create this unit type:
            foreach (TaskLauncher tl in this.factionMgr.TaskLaunchers)
            {
                UpdateUnitCreatorList(tl, true);
            }

            //start listening to the required delegate events:
            CustomEvents.UnitCreated          += OnUnitCreated;
            CustomEvents.UnitConverted        += OnUnitConverted;
            CustomEvents.UnitDead             += OnUnitDead;
            CustomEvents.TaskLaunched         += OnTaskLaunched;
            CustomEvents.TaskCanceled         += OnTaskCanceled;
            CustomEvents.TaskLauncherAdded    += OnTaskLauncherAdded;
            CustomEvents.TaskLauncherRemoved  += OnTaskLauncherRemoved;
            CustomEvents.MaxPopulationUpdated += OnMaxPopulationUpdated;
        }