//called each time a unit/building is dead, only called when the mission type is set to "eliminate" or "produce" private void OnFactionEntityEvent (FactionEntity factionEntity) { //if the faction entity is dead and there are entities that the player is supposed to defend if(factionEntity.FactionID == GameManager.PlayerFactionID //only if the dead faction entity belongs to the player faction && defendFactionEntities.Length > 0 && factionEntity.EntityHealthComp.IsDead()) { foreach(CodeCategoryField codeCategory in defendFactionEntities) //go through the entities that shouldn't be dead { if (codeCategory.Contains(factionEntity.GetCode(), factionEntity.GetCategory())) //if the code/category matches { Forfeit(); //mission failed return; } } } if((factionEntity.EntityHealthComp.IsDead() && type == Type.eliminate && factionEntity.FactionID != GameManager.PlayerFactionID) //if this is an elimination mission and the entity doesn't belong to the player's faction || (!factionEntity.EntityHealthComp.IsDead() && type == Type.produce && factionEntity.FactionID == GameManager.PlayerFactionID)) //produce mission type and the entity belongs to the player faction foreach (CodeCategoryField codeCategory in targetCode) //go through all assigned codes { if (codeCategory.Contains(factionEntity.GetCode(), factionEntity.GetCategory())) //if the code/category matches { OnProgress(1); //positive mission progress break; } } }
//check if the attacker can engage target: public ErrorMessage CanEngageTarget(FactionEntity potentialTarget) { if (gameMgr.InPeaceTime() == true) { return(ErrorMessage.peaceTime); } else if (potentialTarget == null) //if there's no target assigned { return(!requireTarget ? ErrorMessage.none : ErrorMessage.attackTargetRequired); //see if the attack entity can attack without target } else if (potentialTarget.FactionID == FactionEntity.FactionID && engageFriendly == false) { return(ErrorMessage.targetSameFaction); } else if (potentialTarget.EntityHealthComp.CanBeAttacked == false) //peace time, same faction ID or target can't be attacked? -> nope { return(ErrorMessage.targetNoAttack); } if (engageAllTypes) //attack all types then yes! { return(ErrorMessage.none); } //if the target can't attack units/buildings and this is the case then nope if ((potentialTarget.Type == EntityTypes.building && engageBuildings == false) || (potentialTarget.Type == EntityTypes.unit && engageUnits == false)) { return(ErrorMessage.entityNotAllowed); } return(codesList.Contains(potentialTarget.GetCode(), potentialTarget.GetCategory()) == engageInList ? ErrorMessage.none : ErrorMessage.entityNotAllowed); }
//a method that returns the damage that is supposed to be dealt to a target. public int GetDamage(FactionEntity target) { foreach (CustomDamage cd in customDamages) //see if the target's code is in the custom damages list { if (cd.code.Contains(target.GetCode(), target.GetCategory())) //if the target is found then pick the custom damage { return(cd.damage); } } //no custom damage? pick either the damage for units or for buildings return(target.Type == EntityTypes.unit ? unitDamage : buildingDamage); }
//a method to select faction entities of the same type inside a defined range public void SelectFactionEntitiesInRange(FactionEntity source) { if (source.FactionID != GameManager.PlayerFactionID) //if the source doesn't belong to the player's faction { return; //nope } foreach (FactionEntity entity in GameManager.PlayerFactionMgr.GetFactionEntities()) //go through all faction entities in the player's faction { //if the entity's codes match and it's inside the double click select range if (entity.GetCode() == source.GetCode() && Vector3.Distance(entity.transform.position, source.transform.position) <= doubleClickSelectRange) { selected.Add(entity, SelectionTypes.multiple); //add to selection } } }
/// <summary> /// NPCRegulator constructor. /// </summary> /// <param name="data">Holds information regarding how the faction entity type that will be regulated.</param> /// <param name="prefab">FactionEntity derived 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> public NPCRegulator(NPCRegulatorData data, FactionEntity prefab, GameManager gameMgr, NPCManager npcMgr) { //assign components this.gameMgr = gameMgr; Assert.IsNotNull(this.gameMgr, "[NPCRegulator] Initializing without a reference to the GameManager instance is not allowed!"); this.npcMgr = npcMgr; Assert.IsNotNull(this.npcMgr, "[NPCRegulator] Initializing without a reference to the faction's NPCManager instance is not allowed!"); this.factionMgr = npcMgr.FactionMgr; Assert.IsNotNull(this.factionMgr, "[NPCRegulator] Initializing without a reference to the faction's FactionManager instance is not allowed!"); //pick the rest random settings from the given info. MaxAmount = data.GetMaxAmount(); MinAmount = data.GetMinAmount(); MaxPendingAmount = data.GetMaxPendingAmount(); Count = 0; //get the code and category Code = prefab.GetCode(); Category = prefab.GetCategory(); }
//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); }