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 })); } } } }
//checks whether a task can be added or not and if not, provide the reason in the return value public ErrorMessage CanAddTaskToLauncher(TaskLauncher taskLauncher, FactionEntityTask task) { if (Status.IsTaskEnabled(task.GetCode(), taskLauncher.FactionEntity.FactionID, task.IsAvailable) == false) //if the task is disabled { return(ErrorMessage.componentDisabled); } if (taskLauncher.FactionEntity.EntityHealthComp.CurrHealth < taskLauncher.GetMinHealth()) //if the task holder does not have enough health to launch task { return(ErrorMessage.sourceLowHealth); } if (taskLauncher.GetMaxTasksAmount() <= taskLauncher.GetTaskQueueCount()) //if the maximum amount of pending tasks has been reached { return(ErrorMessage.sourceMaxCapacityReached); } if (task.HasRequiredResources() == false) { return(ErrorMessage.lowResources); } if (task.GetTaskType() == TaskTypes.createUnit) //if this is a unit creation task.. { int nextPopulation = task.UnitPopulationSlots + gameMgr.GetFaction(taskLauncher.FactionEntity.FactionID).GetCurrentPopulation(); if (nextPopulation > gameMgr.GetFaction(taskLauncher.FactionEntity.FactionID).GetMaxPopulation()) //check the population slots { return(ErrorMessage.maxPopulationReached); } if (taskLauncher.FactionEntity.FactionMgr.HasReachedLimit(task.UnitCode, task.UnitCode)) //did the unit type to create reach its limit, { return(ErrorMessage.factionLimitReached); } } return(CanAddTask(taskLauncher.FactionEntity)); }