コード例 #1
0
        //method called when a unit/building upgrade is launched
        public void LaunchUpgrade(Upgrade upgrade, int upgradeID, FactionEntity upgradeLauncher, bool oneInstance)
        {
            int         factionID  = upgradeLauncher.FactionID;
            string      sourceCode = upgrade.Source.GetCode();
            EntityTypes sourceType = upgrade.Source.Type;

            Assert.IsTrue(upgrade.GetTarget(upgradeID).Type == sourceType, "[UpgradeManager] The upgrade target doesn't have the same type as the upgrade source!");

            if (oneInstance) //if this is a one instance upgrade type
            {
                //if this is a one instance upgrade, make sure that the upgrade source code and the task holder are the same
                if (upgradeLauncher.Type != sourceType || upgradeLauncher.GetCode() != sourceCode)
                {
                    Debug.LogError("[UpgradeManager] Can not launch a one instance upgrade where the upgrade source and the source task launcher are different!");
                    return;
                }

                UpgradeInstance(upgradeLauncher, upgrade.GetTarget(upgradeID), factionID,
                                gameMgr.GetFaction(factionID).PlayerControlled ? upgrade.GetUpgradeEffect() : null);
            }
            else if (upgrade.CanUpgradeSpawnedInstances()) //if we can upgrade all spawned instances of the source upgrade
            {
                List <FactionEntity> currEntities = upgradeLauncher.FactionMgr.GetFactionEntities().ToList();
                //go through the spawned instances list of the faction:
                foreach (FactionEntity instance in currEntities)
                {
                    //if this building/unit matches the instance to be upgraded
                    if (instance.GetCode() == sourceCode)
                    {
                        //upgrade it
                        UpgradeInstance(instance, upgrade.GetTarget(upgradeID), factionID,
                                        gameMgr.GetFaction(factionID).PlayerControlled ? upgrade.GetUpgradeEffect() : null);
                    }
                }
            }

            switch (sourceType) //depending on the type of the source
            {
            case EntityTypes.building:

                if (!oneInstance)     //if this is not a one instance upgrade then update the placable buildings list for the player/NPC faction
                {
                    //is this the local player's faction:
                    if (gameMgr.GetFaction(factionID).PlayerControlled == true)
                    {
                        //search for the building instance inside the buildings list that the player is able to place.
                        gameMgr.PlacementMgr.ReplaceBuilding(sourceCode, (Building)upgrade.GetTarget(upgradeID));
                    }
                    //& if the faction belongs is NPC:
                    else if (gameMgr.GetFaction(factionID).GetNPCMgrIns() != null)
                    {
                        LaunchNPCBuildingUpgrade(
                            gameMgr.GetFaction(factionID).GetNPCMgrIns(),
                            (Building)upgrade.Source,
                            (Building)upgrade.GetTarget(upgradeID));
                    }
                }

                //trigger the upgrade event:
                CustomEvents.OnBuildingUpgraded(upgrade);

                break;

            case EntityTypes.unit:

                if (!oneInstance)     //if this is not a one instance upgrade then update all source unit type creation tasks
                {
                    //search for a task that creates the unit to upgrade inside the task launchers
                    List <TaskLauncher> taskLaunchers = gameMgr.GetFaction(factionID).FactionMgr.TaskLaunchers;

                    //go through the active task launchers:
                    foreach (TaskLauncher tl in taskLaunchers)
                    {
                        //and sync the upgraded tasks
                        UpdateUnitCreationTask(tl, sourceCode, (Unit)upgrade.GetTarget(upgradeID), upgrade.GetNewTaskInfo());
                    }

                    //register the upgraded unit creation task:
                    UpgradedUnitTask uut = new UpgradedUnitTask()
                    {
                        factionID        = factionID,
                        upgradedUnitCode = sourceCode,
                        targetUnitPrefab = (Unit)upgrade.GetTarget(upgradeID),
                        newTaskInfo      = upgrade.GetNewTaskInfo()
                    };
                    //add it to the list:
                    upgradedUnitTasks.Add(uut);

                    //if the faction belongs is NPC:
                    if (gameMgr.GetFaction(factionID).GetNPCMgrIns() != null)
                    {
                        LaunchNPCUnitUpgrade(
                            gameMgr.GetFaction(factionID).GetNPCMgrIns(),
                            (Unit)upgrade.Source,
                            (Unit)upgrade.GetTarget(upgradeID));
                    }
                }

                //trigger the upgrade event:
                CustomEvents.OnUnitUpgraded(upgrade);

                break;
            }

            //trigger upgrades?
            LaunchTriggerUpgrades(upgrade.GetTriggerUpgrades(), upgradeLauncher);
        }