public TaskUI Add(TaskUIAttributes attributes, int categoryIndex, TaskUI.Types type = TaskUI.Types.idle) { //if the task type is multiple selection, get the last element in the tasks list array, else get the task lists category index. TaskList currTaskList = (type == TaskUI.Types.multipleSelectionIndiv || type == TaskUI.Types.multipleSelectionMul) ? taskLists[taskLists.Length - 1] : ((type == TaskUI.Types.inProgress) ? taskLists[taskLists.Length - 2] : taskLists[categoryIndex]); TaskUI nextTask = (currTaskList.used < currTaskList.capacity) ? currTaskList.list[currTaskList.used] : null; if (nextTask == null) //if all tasks are used and none is available { nextTask = Object.Instantiate(taskUIPrefab.gameObject).GetComponent <TaskUI>(); //create and init new task UI nextTask.Init(gameMgr); nextTask.transform.SetParent(currTaskList.parent, true); //set its parent nextTask.transform.localScale = Vector3.one; currTaskList.list.Add(nextTask); //add a new task to the list currTaskList.capacity++; //increment capacity } currTaskList.used++; //increment amount of used tasks. nextTask.Reload(attributes, type); //initialize the task. return(nextTask); }
//reloads the attributes of the task UI public void Reload(TaskUIAttributes attributes, Types type) { this.attributes = attributes; this.type = type; if (attributes.source && attributes.source as FactionEntity == null) //if there's a source and it is not a faction entity (i.e: resource) { this.type = Types.idle; //force type to be idle } switch (this.type) { case Types.idle: case Types.multipleSelectionMul: progressBar.Toggle(false); break; //if it's an in progress task or a multiple selection task then show the progress bars case Types.multipleSelectionIndiv: case Types.inProgress: progressBar.Toggle(true); //default size and position of the progress bar: progressBar.Update(0.0f); break; } image.sprite = attributes.icon; //set the image icon if (attributes.color != Color.clear) //make sure a valid color is entered (not one that's fully transparent) { image.color = attributes.color; //set the icon's color } //enable the task UI button this.enabled = true; image.enabled = true; button.enabled = true; //only if this a multiple selection task for multiple entities, then show their amount label.text = (type == Types.multipleSelectionMul) ? attributes.sourceList.Count.ToString() : ""; }
//called to add a task to a task launcher public ErrorMessage AddTask(TaskUIAttributes attributes, bool playerCommand = false) { ErrorMessage addTaskMsg = ErrorMessage.none; FactionEntity sourceFactionEntity = attributes.source as FactionEntity; if (attributes.unitComponentTask == false && attributes.taskLauncher != null) //adding a task to task launcher { //if this task is simply cancelling a pending task, then execute it directly and don't proceed: if (attributes.type == TaskTypes.cancelPendingTask) { attributes.taskLauncher.CancelInProgressTask(attributes.ID); return(ErrorMessage.none); //instant success } //-> dealing with a task that gets added to the task queue addTaskMsg = CanAddTaskToLauncher(attributes.taskLauncher, attributes.taskLauncher.GetTask(attributes.ID)); //check if the task can be added if (addTaskMsg != ErrorMessage.none) //if it's not successful { if (playerCommand == true) //if this is a player command { AudioManager.Play(gameMgr.GetGeneralAudioSource(), attributes.taskLauncher.GetLaunchDeclinedAudio(), false); ErrorMessageHandler.OnErrorMessage(addTaskMsg, attributes.source); //display error } return(addTaskMsg); //then return failure reason and stop } attributes.taskLauncher.Add(attributes.ID); return(ErrorMessage.none); } else if ((addTaskMsg = CanAddTask(sourceFactionEntity)) != ErrorMessage.none) //not a task launcher but we still check whether the source is valid enough to launch task { if (playerCommand == true) //if this is a player command { ErrorMessageHandler.OnErrorMessage(addTaskMsg, attributes.source); //display error } return(addTaskMsg); //then return failure reason and stop } switch (attributes.type) { case TaskTypes.deselectIndiv: //deselecting individual units if (gameMgr.SelectionMgr.MultipleSelectionKeyDown == true) //if the player is holding the multiple selection key then... { gameMgr.SelectionMgr.Selected.Remove(attributes.source); //deselect the clicked entity } else { gameMgr.SelectionMgr.Selected.Add(attributes.source, SelectionTypes.single); //not holding the multiple key then select this unit only } break; case TaskTypes.deselectMul: //deselecting multiple units if (gameMgr.SelectionMgr.MultipleSelectionKeyDown == true) //if the player is holding the multiple selection key then... { gameMgr.SelectionMgr.Selected.Remove(attributes.sourceList); //deselect the clicked entity } else { gameMgr.SelectionMgr.Selected.Add(attributes.sourceList); //not holding the multiple key then select this unit only } break; case TaskTypes.generateResource: //generating resource task. ((Building)sourceFactionEntity).GeneratorComp.CollectResources(attributes.ID); break; case TaskTypes.APCEject: //APC release task. sourceFactionEntity.APCComp.Eject(sourceFactionEntity.APCComp.GetStoredUnit(attributes.ID), false); break; case TaskTypes.APCEjectAll: //APC release task. sourceFactionEntity.APCComp.EjectAll(false); break; case TaskTypes.APCCall: //apc calling units sourceFactionEntity.APCComp.CallUnits(); break; case TaskTypes.placeBuilding: gameMgr.PlacementMgr.StartPlacingBuilding(attributes.ID); break; case TaskTypes.attackTypeSelection: ErrorMessage errorMessage = sourceFactionEntity.MultipleAttackMgr.EnableAttack(attributes.ID); if (playerCommand && errorMessage != ErrorMessage.none) //if we can't switch to the target attack type { ErrorMessageHandler.OnErrorMessage(errorMessage, sourceFactionEntity); } break; case TaskTypes.toggleWander: ((Unit)sourceFactionEntity).WanderComp.Toggle(); //toggle the wandering behavior break; case TaskTypes.cancelPendingTask: attributes.taskLauncher.CancelInProgressTask(attributes.ID); break; default: if (attributes.unitComponentTask == true) { UnitComponent.SetAwaitingTaskType(attributes.type, attributes.icon); } break; } return(ErrorMessage.none); }