/// <summary> /// Adds a NPCTask instance to a task queue. /// </summary> /// <param name="task">NPCTask instance to be added.</param> /// <param name="initialPriority">Priority of the initial queue ti add the task to.</param> /// <returns></returns> public bool AddTask(NPCTask task, int initialPriority) { initialPriority = Mathf.Clamp(initialPriority, 0, queues.Length - 1); queues[initialPriority].tasks.Add(task); Activate(); //in case there were no tasks and this component was disabled. Count++; return(true); }
/// <summary> /// Removes a task from a queue by providing its NPCTask instance. /// </summary> /// <param name="queueID">ID of the queue to remove the task from.</param> /// <param name="task">NPCTask instance to remove.</param> /// <returns>True if the task has been successfully removed, otherwise false.</returns> public bool RemoveTask(int queueID, NPCTask task) { Assert.IsTrue(queueID >= 0 && queueID < queues.Length, $"[NPCTaskManager] Invalid queue ID!"); if (queues[queueID].tasks.Remove(task)) { Count--; return(true); } return(false); }
/// <summary> /// Runs the NPC task promotion timer. /// </summary> protected override void OnActiveUpdate() { base.OnActiveUpdate(); if (promotionTimer > 0.0f) { promotionTimer -= Time.deltaTime; } else { promotionTimer = promotionTimeRange.getRandomValue(); //start the promotion timer //if there's more than one queue: if (queues.Length > 1) { //go through the queues (excluding the first one): for (int i = 1; i < queues.Length; i++) { //move tasks to +1 higher priority level queues: queues[i - 1].tasks.AddRange(queues[i].tasks); //free tasks in current queue priority level: queues[i].tasks.Clear(); } } } if (executeTaskTimer > 0.0f) { executeTaskTimer -= Time.deltaTime; } else { executeTaskTimer = executeTaskTimeRange.getRandomValue(); //start the promotion timer int i = 0; //if a task is in queue 0 -> this means that task must be executed ASAP: //go through the queue 0 tasks: while (i < queues[0].tasks.Count) { NPCTask nextTask = queues[0].tasks[i]; //task type? switch (nextTask.type) { case NPCTaskType.constructBuilding: //construct a building? //making sure that the building is still under construction if (npcMgr.GetNPCComp <NPCBuildingConstructor>().IsBuildingUnderConstruction(nextTask.target as Building)) { //request the building constructor and force it to construct building. npcMgr.GetNPCComp <NPCBuildingConstructor>().OnBuildingConstructionRequest(nextTask.target as Building, false, true); i++; } else { //remove task: RemoveTask(0, i); } break; default: break; } } } }