/// <summary> /// Called when a task is either canceled or completed. /// </summary> /// <param name="taskLauncher">TaskLauncher instance where the task is canceled or completed.</param> /// <param name="taskID">Index of the task.</param> /// <param name="taskQueueID">Index of the task on pending queue.</param> private void OnTaskStopped(TaskLauncher taskLauncher, int taskID, int taskQueueID) { if (taskLauncher.FactionEntity.FactionID == factionMgr.FactionID && //only if task launcher belongs to NPC faction taskLauncher.GetTask(taskID).GetTaskType() == TaskTypes.upgrade) //if this is an upgrade task, then add the upgrade source code to the pending upgrades list { pendingUpgrades.RemoveAll(code => code == taskLauncher.GetTask(taskID).GetUpgradeSourceCode()); } }
//called whenever a task is cancelled void OnTaskCanceled(TaskLauncher taskLauncher, int taskID, int taskQueueID) { //if this task is supposed to create a unit and the task launcher belongs to this faction: if (taskLauncher.GetTask(taskID).GetTaskType() == TaskTypes.createUnit && taskLauncher.FactionEntity.FactionID == factionMgr.FactionID) { if (taskLauncher.GetTask(taskID).GetCode() == code) { amount--; pendingAmount--; } } }
public void UpdateTaskLauncherTasks(FactionEntity sourceEntity, TaskLauncher taskLauncher) { if (taskLauncher == null || taskLauncher.Initiated == false || taskLauncher.GetTasksCount() == 0) //if the task launcher is invalid or the source can't manage a task { return; } for (int taskID = 0; taskID < taskLauncher.GetTasksCount(); taskID++) //go through all tasks { FactionEntityTask task = taskLauncher.GetTask(taskID); if (task.IsEnabled() == true) { TaskUI taskUI = Add(new TaskUIAttributes { ID = taskID, type = task.GetTaskType(), icon = task.GetIcon(), source = sourceEntity, taskLauncher = taskLauncher }, task.GetTaskPanelCategory()); if (task.GetTaskType() == TaskTypes.createUnit) //if this is a unit creation task, check if it has reached its limit and change task ui image color accordinly { taskUI.GetComponent <Image>().color = sourceEntity.FactionMgr.HasReachedLimit(task.UnitCode, task.UnitCategory) == true ? Color.red : Color.white; } } } UpdateInProgressTasks(taskLauncher); //show the in progress tasks }
/// <summary> /// Adds a TaskLauncher instance to be tracked by the NPCUnitRegulator if it includes tasks that allow the regulated unit type. /// </summary> /// <param name="taskLauncher">TaskLauncher instance to add.</param> public void AddTaskLauncher(TaskLauncher taskLauncher) { if (unitCreators.ContainsKey(taskLauncher) || //if the task launcher is already registered taskLauncher.FactionEntity.FactionID != factionMgr.FactionID) //or the task launcher doesn't belong to the same NPC faction. { return; } for (int taskID = 0; taskID < taskLauncher.GetTasksCount(); taskID++) { FactionEntityTask nextTask = taskLauncher.GetTask(taskID); //if the task's type is unit creation and it the unit to create code matches the code of unit type that is regulated by this component if (nextTask.GetTaskType() == TaskTypes.createUnit && nextTask.UnitCode == Code) { if (unitCreators.TryGetValue(taskLauncher, out List <int> taskIDList)) //task launcher already is registered as key, just append value list { taskIDList.Add(taskID); } else //register task launcher as new key and task ID as the only element in the value list. { unitCreators.Add(taskLauncher, new List <int>(new int[] { taskID })); } } } }
//update an upgraded unit creation task's info: private void UpdateUnitCreationTask(TaskLauncher taskLauncher, string upgradedUnitCode, Unit targetUnitPrefab, Upgrade.NewTaskInfo newTaskInfo) { //go through the tasks: for (int i = 0; i < taskLauncher.GetTasksCount(); i++) { taskLauncher.GetTask(i).Update(upgradedUnitCode, targetUnitPrefab, newTaskInfo); } }
//a method that is given a task in a task launcher and an upgrade component and that decides if the task handles that upgrade private bool IsUpgradeMatch(TaskLauncher taskLauncher, int taskID, Upgrade upgrade) { if (upgrade == null || taskLauncher == null) { return(false); } //true only if the source unit/building code match return(taskLauncher.GetTask(taskID).GetUpgradeSourceCode() == upgrade.Source.GetCode()); }
//called each time a task launcher status is updated (task added, cancelled or completed) private void OnTaskLauncherStatusUpdated(TaskLauncher taskLauncher, int taskID, int taskQueueID) { //only show the task launcher tasks if the task launcher is the only player entity selected if (SelectionManager.IsSelected(taskLauncher.FactionEntity.GetSelection(), true, true)) { Update(); if (taskLauncher.GetTask(taskID).IsAvailable == false) //if the task is no longer available { gameMgr.UIMgr.HideTooltip(); //hide the tooltip } } }
//whenever a task launcher is added or removed, this method will be called to add/remove it to/from the upgrade tasks list: public void UpdateUpgradeTasks(TaskLauncher taskLauncher, bool add, Upgrade upgrade = null) { //if taskID is set to -1 and add = false, then all the tasks with the assigned task launcher will be removed //if taskID >= 0 is valid, then only the upgrade task with taskID in the task launcher will be removed. if (add == false) //if this task launcher is getting removed. { int i = 0; //go through all registerd task launchers in this component while (i < upgradeTasks.Count) { if (upgradeTasks[i].taskLauncher == taskLauncher || (upgrade != null && IsUpgradeMatch(upgradeTasks[i].taskLauncher, upgradeTasks[i].taskID, upgrade) == true)) //if the task launcher matches: //remove it: { upgradeTasks.RemoveAt(i); } else { i++; //move on } } } else if (taskLauncher.GetTasksCount() > 0) //if we're adding tasks and this task launcher has tasks. { //loop through the task launcher's task for (int taskID = 0; taskID < taskLauncher.GetTasksCount(); taskID++) { //if this is the upgrade task that we're looking for. if (taskLauncher.GetTask(taskID).GetTaskType() == TaskTypes.upgrade) { //go ahead and add it: UpgradeTask newUpgradeTask = new UpgradeTask { taskLauncher = taskLauncher, taskID = taskID }; //add it to the list: upgradeTasks.Add(newUpgradeTask); //and activate the upgrade manager: Activate(); } } } }
//whenever a building is added or removed, this method will be called to add/remove it to the lists in the spawn units: public void UpdateUnitCreatorList(TaskLauncher taskLauncher, bool add) { if (add == false) //if this task launcher is getting removed. { int i = 0; //go through all registerd task launchers in this component while (i < unitCreatorList.Count) { if (unitCreatorList[i].taskLauncher == taskLauncher) //if the task launcher matches: //remove it: { unitCreatorList.RemoveAt(i); } else { i++; //move on } } } //Make sure that this task launcher can actually create units and that there units to spawn else if (taskLauncher.GetTasksCount() > 0) //if we're adding tasks and this task launcher has tasks. { //loop through the task launcher's tasks for (int taskID = 0; taskID < taskLauncher.GetTasksCount(); taskID++) { //if this is a unit creation task: if (taskLauncher.GetTask(taskID).GetTaskType() == TaskTypes.createUnit && taskLauncher.GetTask(taskID).UnitCode == code) { //if the unit code is included in the list => that unit is managed by this regulator //go ahead and add it: UnitCreatorInfo newUnitCreator = new UnitCreatorInfo { //set the unit creator info: taskLauncher = taskLauncher, taskID = taskID }; //add it to the list: unitCreatorList.Add(newUnitCreator); } } } }
/// <summary> /// Launches upgrade tasks if all requirements are met. /// </summary> /// <param name="taskLauncher">The TaskLauncher instance that has the upgrade task.</param> /// <param name="taskID">The upgrade's task ID.</param> /// <param name="auto">True if this has been called from the NPCUpgradeManager instance, otherwise false</param> /// <returns></returns> public bool OnUpgradeLaunchRequest(TaskLauncher taskLauncher, int taskID, bool auto) { if (taskLauncher == null || //if the task launcher is invalid auto && !autoUpgrade || //or NPCUpgradeManager instance called this but auto upgrading is disabled !auto && !upgradeOnDemand || //or this was called by another NPC component but upgrading on demand is disabled pendingUpgrades.Contains(taskLauncher.GetTask(taskID).GetUpgradeSourceCode()) || //or the source upgrade faction entity already has an ongoing upgrade Random.value > acceptanceRange.getRandomValue() //or this is not accepted due to generated random value ) //do not proceed { return(false); } //finally attempt to launch the task ErrorMessage addTaskMsg = gameMgr.TaskMgr.AddTask(new TaskUIAttributes { taskLauncher = taskLauncher, ID = taskID, type = TaskTypes.upgrade }); //TO BE ADDED: Handling insufficient resources. //if adding the upgrade task was successful, this would return true, if not then false. return(addTaskMsg == ErrorMessage.none); }