public void AddTask(AIUnitTask task) { Debug.Log("Adding ai unit task " + task.ToString()); taskStack.AddFirst(task); if (IsReassignable == true && currentTask != null && currentTask.Equals(null) == false) { // Abort what we are currently doing... Debug.Log("Cancelling ai unit's temporary task: " + currentTask.ToString()); currentTask.Abort(); taskStack.Remove(currentTask); } IsReassignable = false; }
void mineTree() { if (gameObject.GetComponent <PlayerUnit>() == null) { return; } { if (GetPlayerBase().GetInventory().GetTotalCrystals() >= resourceCount) { // Remove this task when the required limit is reached AIUnitTask returnTask = gameObject.AddComponent <ReturnCrystalToStorageTask>().SetStorage(crystalCarryNum); GetPlayerUnit().AddTask(returnTask); this.crystalCarryNum = 0; this.mineStartTime = 0.0f; Complete(); return; } if (selectedCrystal == null) { Abort(); return; } MiningResult result = selectedCrystal.Mine(harvestRate); this.crystalCarryNum += harvestRate; this.totalResourceMined += harvestRate; Debug.Log("Mined stone for " + harvestRate + " remaining: " + result.remainingQuantity); if (this.crystalCarryNum >= maxCarryCapacity) { // When this unit's wood storage capability is exceeded, // we carry it back to the townhall, or the nearest wood camp storage site. if (selectedCrystal != null) { selectedCrystal.isOccupied = false; } if (selectedCrystal != null) { selectedCrystal.isOccupied = false; } AIUnitTask returnTask = gameObject.AddComponent <ReturnCrystalToStorageTask>().SetStorage(crystalCarryNum); PauseUntilTask(returnTask); GetPlayerUnit().AddTask(returnTask); this.crystalCarryNum = 0; this.mineStartTime = 0.0f; return; } } }
public void QueueLoop() { if (taskStack == null) { return; //fatal } bool shouldPickNewItem = false; if (currentTask == null) { shouldPickNewItem = true; } else { if (currentTask.isCompleted || currentTask.isAborted) { Debug.Log("Removing ai task " + currentTask.ToString()); taskStack.Remove(currentTask); DestroyImmediate(currentTask); shouldPickNewItem = true; } else if (currentTask.isPaused) { shouldPickNewItem = true; } } if (shouldPickNewItem) { if (taskStack != null && taskStack.Count > 0) { currentTask = null; LinkedListNode <AIUnitTask> taskPntr = taskStack.First; while (taskPntr != null) { if (taskPntr.Value.isAborted || taskPntr.Value.isCompleted) { // Remove node taskStack.Remove(taskPntr); taskPntr = taskPntr.Next; } else if (taskPntr.Value.isPaused) { if (taskPntr.Value.isPausedUntilOtherTask && (taskPntr.Value.pauseUntilTaskComplete == null || (taskPntr.Value.pauseUntilTaskComplete.isPaused == false || taskPntr.Value.pauseUntilTaskComplete.isCompleted == true || taskPntr.Value.pauseUntilTaskComplete.isAborted == true))) { // The task on the stack is paused until this task is complete currentTask = taskPntr.Value; break; } else { taskPntr = taskPntr.Next; } } else if (taskPntr.Value.isExecuting) { taskPntr = taskPntr.Next; } else { currentTask = taskPntr.Value; break; } } } if (currentTask != null) { currentTask.Resume(); } else { // Ask the base for a task. beginIdle(); } } }
public void Reset() { taskStack.Clear(); currentTask = null; }
public void beginIdle() { currentTask = null; }
public void AddTemporaryTask(AIUnitTask task) { Debug.Log("Adding ai unit task " + task.ToString()); taskStack.AddFirst(task); IsReassignable = true; }
public override void PauseUntilTask(AIUnitTask otherTask) { base.PauseUntilTask(otherTask); StopMining(); }