Beispiel #1
0
		//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 ReloadTaskLauncherLists (TaskLauncher TaskComp, bool Add)
		{
			if (TaskComp.UnitCreationTasks.Count > 0 && Units.Length > 0) { //Make sure that it can actually create units and that there units to spawn
				//go through the units to create:
				for (int i = 0; i < Units.Length; i++) {
					//loop through the unit creation tasks
					for (int j = 0; j < TaskComp.UnitCreationTasks.Count; j++) {
						int ID = TaskComp.UnitCreationTasks [j];
						if (Add == false) { //if this task launcher is getting removed 
							//and we found it in one of the lists:
							if (Units [i].SourceTaskLaunchers.Contains (TaskComp)) {
								Units [i].TasksIDs.RemoveAt (Units [i].SourceTaskLaunchers.IndexOf (TaskComp)); //remove this from the task IDs list								
								//remove the task launcher from the list:
								Units [i].SourceTaskLaunchers.Remove (TaskComp);
							}
						} else { //if we are adding this task launcher
							//do the units match? 
							if (Units [i].Prefab.Code == TaskComp.TasksList [ID].UnitCreationSettings.Prefabs[0].Code) {
								//if this task launcher is just built then the building will be added:
								Units [i].SourceTaskLaunchers.Add (TaskComp);
								//add the task ID as well:
								Units[i].TasksIDs.Add(ID);
							}
						}
					}
				}
			}
		}
Beispiel #2
0
 /// <summary>
 /// Called each time a new TaskLauncher instance is initialized.
 /// </summary>
 /// <param name="taskLauncher">The new added TaskLauncher instance.</param>
 /// <param name="taskID">None.</param>
 /// <param name="taskQueueID">None.</param>
 private void OnTaskLauncherAdded(TaskLauncher taskLauncher, int taskID, int taskQueueID)
 {
     if (taskLauncher.FactionEntity.FactionID == FactionID) //make sure the new task launcher belongs to the faction managed by this component
     {
         taskLaunchers.Add(taskLauncher);                   //add task launcher to list.
     }
 }
 void OnTaskLauncherAdded(TaskLauncher taskLauncher, int taskID = -1, int taskQueueID = -1) //called when a new task launcher has been added
 {
     if (taskLauncher.FactionEntity.FactionID == factionMgr.FactionID)                      //if the task launcher belongs to this faction
     {
         UpdateUpgradeTasks(taskLauncher, true);                                            //update upgrade tasks
     }
 }
 void OnTaskLauncherRemoved(TaskLauncher taskLauncher, int taskID = -1, int taskQueueID = -1) //called when a task launcher has been removed
 {
     if (factionMgr && taskLauncher.FactionEntity.FactionID == factionMgr.FactionID)          //if the task launcher belongs to this faction
     {
         UpdateUpgradeTasks(taskLauncher, false);                                             //update upgrade 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 }));
                    }
                }
            }
        }
Beispiel #6
0
 /// <summary>
 /// Called each time a TaskLauncher instance is removed/destroyed.
 /// </summary>
 /// <param name="taskLauncher">The removed/destroyed TaskLauncher instance.</param>
 /// <param name="taskID">None.</param>
 /// <param name="taskQueueID">None.</param>
 private void OnTaskLauncherRemoved(TaskLauncher taskLauncher, int taskID, int taskQueueID)
 {
     if (taskLauncher.FactionEntity.FactionID == FactionID) //make sure the new task launcher belongs to the faction managed by this component
     {
         taskLaunchers.Remove(taskLauncher);                //remove task launcher from list
     }
 }
        public override void OnEnable()
        {
            base.OnEnable();

            source          = (TaskLauncher)target;
            taskLauncher_SO = new SerializedObject(source);
        }
Beispiel #8
0
        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
        }
Beispiel #9
0
 /// <summary>
 /// Called when a TaskLauncher instance is destroyed.
 /// </summary>
 /// <param name="taskLauncher">The TaskLauncher instance that is removed.</param>
 /// <param name="taskID">Ignore.</param>
 /// <param name="taskQueueID">Ignore.</param>
 private void OnTaskLauncherRemoved(TaskLauncher taskLauncher, int taskID = -1, int taskQueueID = -1) //called when a task launcher has been removed
 {
     if (taskLauncher.FactionEntity.FactionID == factionMgr.FactionID)                                //if the task launcher belongs to this faction
     {
         RemoveTaskLauncher(taskLauncher);
     }
 }
Beispiel #10
0
 /// <summary>
 /// Called when a new TaskLauncher instance is initiated.
 /// </summary>
 /// <param name="taskLauncher">The TaskLauncher instance that is added.</param>
 /// <param name="taskID">Ignore.</param>
 /// <param name="taskQueueID">Ignore.</param>
 private void OnTaskLauncherAdded(TaskLauncher taskLauncher, int taskID = -1, int taskQueueID = -1)
 {
     if (taskLauncher.FactionEntity.FactionID == factionMgr.FactionID) //if the task launcher belongs to this faction
     {
         AddTaskLauncher(taskLauncher);
     }
 }
Beispiel #11
0
        //a method used to request to launch an upgrade task, returns true if the upgrade task is launched, false if not
        public bool OnUpgradeLaunchRequest(TaskLauncher taskLauncher, int taskID, bool auto)
        {
            //if this attempt is done automatically (from the NPC Unit Creator itself) and the regulator doesn't allow it
            if (auto == true && autoUpgrade == false)
            {
                return(false); //do not proceed.
            }

            //if this has been requested from another NPC component and the regulator doesn't allow it
            if (auto == false && upgradeOnDemand == false)
            {
                return(false); //do not proceed.
            }

            //randomly decide (based on input values) if this would be accepetd or not:
            if (Random.value <= acceptanceRange.getRandomValue())
            {
                //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);
            }
            else
            {
                return(false);
            }
        }
Beispiel #12
0
        //for local player only, not for NPC factions
        public bool CanAddTask(TaskLauncher TaskComp, int TaskID, TaskTypes TaskType)
        {
            //Always check that the health is above the minimal limit to launch tasks and that the building was built (to max health) at least once:
            if (TaskComp.GetTaskHolderHealth() < TaskComp.MinTaskHealth)
            {
                UIMgr.ShowPlayerMessage("Health is too low to launch task!", UIManager.MessageTypes.Error);
                AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, TaskComp.DeclinedTaskAudio, false); //Declined task audio.
                return(false);
            }
            else if (TaskComp.MaxTasks <= TaskComp.TasksQueue.Count)
            {
                //Notify the player that the maximum amount of tasks for this building has been reached
                UIMgr.ShowPlayerMessage("Maximum building tasks has been reached", UIManager.MessageTypes.Error);
                AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, TaskComp.DeclinedTaskAudio, false); //Declined task audio.
                return(false);
            }

            //Do we have enough resources for this task?
            if (TaskType == TaskManager.TaskTypes.TaskUpgrade)
            { //upgrade task:
              //Make sure there are enough resources
                if (ResourceMgr.CheckResources(TaskComp.TasksList[TaskID].UnitCreationSettings.Upgrades[TaskComp.TasksList[TaskID].CurrentUpgradeLevel].UpgradeResources, GameManager.PlayerFactionID, 1.0f) == false)
                {
                    UIMgr.ShowPlayerMessage("Not enough resources to launch upgrade task!", UIManager.MessageTypes.Error);
                    AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, TaskComp.DeclinedTaskAudio, false); //Declined task audio.
                    return(false);
                }
            }
            else
            {
                if (ResourceMgr.CheckResources(TaskComp.TasksList[TaskID].RequiredResources, GameManager.PlayerFactionID, 1) == false)
                {
                    UIMgr.ShowPlayerMessage("Not enough resources to launch task!", UIManager.MessageTypes.Error);
                    AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, TaskComp.DeclinedTaskAudio, false); //Declined task audio.
                    return(false);
                }

                if (TaskType == TaskManager.TaskTypes.CreateUnit)
                { //create unit task
                    if (GameMgr.Factions[GameManager.PlayerFactionID].CurrentPopulation >= GameMgr.Factions[GameManager.PlayerFactionID].MaxPopulation)
                    {
                        //Inform the player that there's no more room for new units.
                        UIMgr.ShowPlayerMessage("Maximum population has been reached!", UIManager.MessageTypes.Error);
                        AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, TaskComp.DeclinedTaskAudio, false); //Declined task audio.
                        return(false);
                    }
                    //if there's population slots but the local faction already hit the limit with this faction
                    else if (GameMgr.Factions[GameManager.PlayerFactionID].FactionMgr.HasReachedLimit(TaskComp.TasksList[TaskID].UnitCreationSettings.Prefabs[0].Code))
                    {
                        //inform the player that he can't create this unit
                        UIMgr.ShowPlayerMessage("This unit has reached its creation limit", UIManager.MessageTypes.Error);
                        AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, TaskComp.DeclinedTaskAudio, false); //Declined task audio.
                        return(false);
                    }
                }
            }

            return(true);
        }
Beispiel #13
0
 public void OnTaskCanceled(TaskLauncher TaskComp, int TaskID, int TaskQueueID)         //called when a task launcher cancels a task
 {
     if (DebugEnabled == true)
     {
         Debug.Log("Task Launcher canceled a pending task.");
     }
     TaskCanceled(TaskComp, TaskID, TaskQueueID);
 }
 /// <summary>
 /// Called whenever a FactionEntityTask instance is canceled while in progress in a TaskLauncher instance.
 /// </summary>
 /// <param name="taskLauncher">TaskLauncher instance whose task is cancelled.</param>
 /// <param name="taskID">ID of the cancelled task.</param>
 /// <param name="taskQueueID">None.</param>
 private void OnTaskCanceled(TaskLauncher taskLauncher, int taskID, int taskQueueID)
 {
     //if the canceled task is being tracked by this regulator.
     if (unitCreators.TryGetValue(taskLauncher, out List <int> taskIDList) && taskIDList.Contains(taskID))
     {
         Remove();
     }
 }
Beispiel #15
0
 /// <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());
     }
 }
Beispiel #16
0
        /* Unit creation */

        void OnTaskLauncherAdded(TaskLauncher taskLauncher, int taskID = -1, int taskQueueID = -1) //called when a new task launcher has been added
        {
            if (taskLauncher.FactionEntity.FactionID == factionMgr.FactionID)                      //if the task launcher belongs to this faction
            {
                //update the source task launchers:
                UpdateUnitCreatorList(taskLauncher, true);
            }
        }
Beispiel #17
0
 public void OnTaskCompleted(TaskLauncher TaskComp, int TaskID, int TaskQueueID)         //called when a task launcher completes a task
 {
     if (DebugEnabled == true)
     {
         Debug.Log("Task Launcher completed a task.");
     }
     TaskCompleted(TaskComp, TaskID, TaskQueueID);
 }
Beispiel #18
0
 public void OnTaskLauncherRemoved(TaskLauncher TaskComp) //called when a task launcher has been removed
 {
     if (DebugEnabled == true)
     {
         Debug.Log("Task Launcher has been removed.");
     }
     TaskLauncherRemoved(TaskComp);
 }
Beispiel #19
0
 //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);
     }
 }
 void OnTaskLauncherRemoved(TaskLauncher taskLauncher, int taskID = -1, int taskQueueID = -1) //called when a task launcher has been removed
 {
     if (taskLauncher.FactionID == factionMgr.FactionID)                                      //if the task launcher belongs to this faction
     {
         //update the source task launchers:
         UpdateUnitCreatorList(taskLauncher, false);
     }
 }
        /// <summary>
        /// Gets the task IDs of a TaskLauncher instance that can create the regulated Unit type.
        /// </summary>
        /// <param name="taskLauncher">TaskLauncher instance whose tasks will be searched.</param>
        /// <returns>IEnumerable instance of integers that represent the task IDs if there are any, otherwise null.</returns>
        public IEnumerable <int> GetUnitCreationTasks(TaskLauncher taskLauncher)
        {
            if (unitCreators.TryGetValue(taskLauncher, out List <int> taskIDList))
            {
                return(taskIDList);
            }

            return(null);
        }
Beispiel #22
0
        //set the target for the next army unit to build:
        public void ReloadArmyUnitsPriority(TaskLauncher TaskComp, bool Add)
        {
            //make sure the building has army units:
            if (TaskComp.ArmyUnits.Count > 0)
            {
                //loop through them:
                for (int i = 0; i < TaskComp.ArmyUnits.Count; i++)
                {
                    int NewPrefabID = 0;

                    bool Found = false;
                    int  j     = 0;

                    while (Found == false && j < ArmyUnits.Length)
                    {
                        int x = 0;
                        while (Found == false && x < ArmyUnits[j].Prefabs.Length)
                        {
                            if (ArmyUnits[j].Prefabs[x].Prefab.Code == TaskComp.TasksList[TaskComp.ArmyUnits[i]].UnitCreationSettings.Prefabs[0].Code)
                            { //if the prefab was found.
                                Found = true;

                                if (Add == true)
                                {
                                    //add the building:
                                    ArmyUnits[j].Prefabs[x].SpawnedTaskLaunchers.Add(TaskComp);

                                    NewPrefabID = x;
                                }
                                else
                                {
                                    ArmyUnits[j].Prefabs[x].SpawnedTaskLaunchers.Remove(TaskComp);

                                    //if there no more buildings to produce this unit:
                                    if (ArmyUnits[j].Prefabs[x].SpawnedTaskLaunchers.Count == 0)
                                    {
                                        //check for priority:
                                        CheckArmyUnitsPriority(j, j);
                                    }
                                }
                            }
                            x++;
                        }
                        j++;
                    }

                    if (Found == true && Add == true)
                    {
                        if (NewPrefabID < ArmyUnits[j - 1].PrefabID)
                        {
                            ArmyUnits[j - 1].PrefabID = NewPrefabID;
                            ArmyUnits[j - 1].Prefab   = ArmyUnits[j - 1].Prefabs[NewPrefabID].Prefab;
                        }
                    }
                }
            }
        }
Beispiel #23
0
        //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());
        }
Beispiel #24
0
        /// <summary>
        /// Adds a TaskLauncher instance's upgrade tasks.
        /// </summary>
        /// <param name="taskLauncher">The TaskLauncher instance to remove.</param>
        private void AddTaskLauncher(TaskLauncher taskLauncher)
        {
            //collect the upgrade tasks and add them as the value.
            upgradeTasks.Add(taskLauncher, taskLauncher.GetAll().Where(task => task.GetTaskType() == TaskTypes.upgrade).ToList());

            if (autoUpgrade)
            {
                Activate(); //enable component if we can auto upgrade without an external upgrade request
            }
        }
        public override void OnEnable()
        {
            base.OnEnable();

            source = target as TaskLauncher;

            elementID = source.editorElementID;
            tabID     = source.editorTabID;

            taskLauncher_SO = new SerializedObject(source);
        }
Beispiel #26
0
 //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--;
         }
     }
 }
Beispiel #27
0
        //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
                }
            }
        }
Beispiel #28
0
 //sync all upgraded unit creation tasks for a task launcher:
 private void SyncUnitCreationTasks(TaskLauncher taskLauncher)
 {
     //go through the registered upgraded unit tasks
     foreach (UpgradedUnitTask uut in upgradedUnitTasks)
     {
         //if this task launcher belongs to the faction ID that has the upgraded unit creation task:
         if (uut.factionID == taskLauncher.FactionEntity.FactionID)
         {
             //sync the unit creation tasks.
             UpdateUnitCreationTask(taskLauncher, uut.upgradedUnitCode, uut.targetUnitPrefab, uut.newTaskInfo);
         }
     }
 }
 //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)
 {
     //Make sure that this task launcher can actually create units and that there units to spawn
     if (taskLauncher.UnitCreationTasks.Count > 0)
     {
         //loop through the unit creation tasks
         foreach (int taskID in taskLauncher.UnitCreationTasks)
         {
             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++; //go to the next register unit creator
                     }
                 }
             }
             else //if we are adding this task launcher
             {
                 //if there are valid prefab(s) assigned to this unit creation task
                 if (taskLauncher.TasksList[taskID].UnitCreationSettings.Prefabs.Length > 0)
                 {
                     string prefabCode = taskLauncher.TasksList[taskID].UnitCreationSettings.Prefabs[0].Code;
                     //if the unit code is included in the list => that unit is managed by this regulator
                     if (prefabCode == code)
                     {
                         //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);
                     }
                 }
             }
         }
     }
 }
 //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.TasksList[taskID].TaskType == TaskManager.TaskTypes.CreateUnit && taskLauncher.FactionID == factionMgr.FactionID)
     {
         //the prefabs assigned have the same code.
         if (taskLauncher.TasksList[taskID].UnitCreationSettings.Prefabs.Length > 0)
         {
             //we'll pick the first unit in array.
             Unit firstUnit = taskLauncher.TasksList[taskID].UnitCreationSettings.Prefabs[0];
             if (firstUnit.Code == code)
             {
                 RemoveItem(firstUnit);
             }
         }
     }
 }