diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/BaseTask.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/BaseTask.cs index 94165869..385467ee 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/BaseTask.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/BaseTask.cs @@ -39,6 +39,15 @@ public void StopAsFailed() StopExecution(); } + /// + /// Can be used to abort the task outside of its Update method + /// + public void StopAsAborted() + { + State = TaskState.Aborted; + StopExecution(); + } + /// /// Can be used to let the task succseed outside of its Update method /// diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentAnimationTask.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentAnimationTask.cs index 037ab712..b7cf543e 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentAnimationTask.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentAnimationTask.cs @@ -140,5 +140,15 @@ public void Deserialize(SerializationDataContainer serializer) playTime = serializer.GetSerializedFloat("Play Time"); } + /// + /// Aborts the animation task and sets its state to aborted + /// + public override void Abort() + { + Debug.Log("Abort in AnimationTask"); + StopExecution(); + State = TaskState.Aborted; + } + } } \ No newline at end of file diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentBaseTask.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentBaseTask.cs index 556340c1..e576e521 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentBaseTask.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentBaseTask.cs @@ -1,6 +1,7 @@ using i5.Toolkit.Core.Utilities; using System; using System.Collections.Generic; +using UnityEngine; namespace i5.VirtualAgents.AgentTasks { @@ -89,5 +90,18 @@ public void WaitFor(params AgentBaseTask[] otherTasks) } } } + + /// + /// Aborts the task and sets its state to aborted + /// + public virtual void Abort() + { + Debug.Log("Abort in BaseTask"); + if (State == TaskState.Running) + { + StopAsAborted(); + State = TaskState.Aborted; + } + } } } \ No newline at end of file diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentMovementTask.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentMovementTask.cs index 51c6f38b..94ce64d2 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentMovementTask.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentMovementTask.cs @@ -202,5 +202,18 @@ public void Deserialize(SerializationDataContainer serializer) TargetSpeed = serializer.GetSerializedFloat("TargetSpeed"); //FollowingGameObject = serializer.GetSerializedBool("FollowingGameObject"); } + + /// + /// Aborts the movement task and sets its state to aborted + /// + public override void Abort() + { + Debug.Log("Abort in MovementTask"); + if (navMeshAgent != null) + { + StopExecution(); + } + State = TaskState.Aborted; + } } } \ No newline at end of file diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentWaitTask.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentWaitTask.cs index 2514435b..028ea6dd 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentWaitTask.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/AgentWaitTask.cs @@ -14,6 +14,10 @@ public class AgentWaitTask : AgentBaseTask, ISerializable /// public float WaitTimeInSeconds { get; set; } + private Coroutine waitCoroutine; + + private Agent agent; + public AgentWaitTask() { } /// @@ -32,6 +36,7 @@ public AgentWaitTask(float timeInSeconds) /// The agent which executes this task public override void StartExecution(Agent agent) { + this.agent = agent; base.StartExecution(agent); if (WaitTimeInSeconds <= 0) @@ -47,8 +52,7 @@ public override void StartExecution(Agent agent) FinishTaskAsFailed(); return; } - - agent.StartCoroutine(Wait(WaitTimeInSeconds)); + waitCoroutine = agent.StartCoroutine(Wait(WaitTimeInSeconds)); } // wait for the given time and then finish the task @@ -58,14 +62,28 @@ private IEnumerator Wait(float timeInSeconds) FinishTask(); } - public void Serialize(SerializationDataContainer serializer) + public void Serialize(SerializationDataContainer serializer) { serializer.AddSerializedData("Wait time", WaitTimeInSeconds); } - public void Deserialize(SerializationDataContainer serializer) + public void Deserialize(SerializationDataContainer serializer) { WaitTimeInSeconds = serializer.GetSerializedFloat("Wait time"); } + + /// + /// Aborts the wait task + /// + public override void Abort() + { + Debug.Log("Abort in WaitTask"); + if (waitCoroutine != null) + { + agent.StopCoroutine(waitCoroutine); + waitCoroutine = null; + } + State = TaskState.Aborted; + } } } diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/IAgentTask.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/IAgentTask.cs index 547e59a5..e0e6839c 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/IAgentTask.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/FundamentalAgentTasks/IAgentTask.cs @@ -14,5 +14,10 @@ public interface IAgentTask : ITask /// False if there are unfulfilled conditions that block the execution /// bool CanStart { get; } + + /// + /// Aborts the task + /// + public void Abort() {} } } \ No newline at end of file diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/ITask.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/ITask.cs index 5d682b34..f4ac798c 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/ITask.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/ITask.cs @@ -9,7 +9,8 @@ public enum TaskState Waiting, // Task created, but never executed Running, // Task currently running Failure, // Task has finished executing and failed - Success // Task has finished executing and succeeded + Success, // Task has finished executing and succeeded + Aborted // Task has been aborted } /// diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/ScheduleBasedExecution/AgentTaskManager.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/ScheduleBasedExecution/AgentTaskManager.cs index 8f733ec5..545435b3 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/ScheduleBasedExecution/AgentTaskManager.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/ScheduleBasedExecution/AgentTaskManager.cs @@ -1,6 +1,7 @@ using i5.Toolkit.Core.Utilities; using System; using i5.VirtualAgents.AgentTasks; +using UnityEngine; namespace i5.VirtualAgents.ScheduleBasedExecution { @@ -189,5 +190,50 @@ public IAgentTask PeekNextTask() { return queue.PeekNextTask(); } + + /// + /// Removes all tasks from the queue and sets the state of the task manager to idle + /// + /// Determines whether the current task should be aborted as well + public void Clear(bool clearCurrentTask) + { + queue.Clear(); + if (clearCurrentTask) + { + CurrentTask.Abort(); + CurrentTask = null; + CurrentState = TaskManagerState.idle; + } + // TODO add after merging with TaskBundles: + // lastTask = null; + } + + /// + /// Removes a task from the task queue + /// + /// The task to be removed + public void RemoveTask(IAgentTask task) + { + queue.RemoveTask(task); + if (CurrentTask == task) + { + Abort(); + } + } + + /// + /// Aborts the current task + /// - /// Contains a task, its corresponding priority and the task bundle it belongs to if it is part of one + /// Contains a task and its corresponding priority /// private struct TaskEntry { public IAgentTask task; public int priority; } + + /// + /// Removes all tasks from the queue + /// + public void Clear() + { + taskQueue.Clear(); + } + + /// + /// Removes a task from the task queue + /// + /// Task to be removed + public void RemoveTask(IAgentTask task) + { + TaskEntry taskEntry = new TaskEntry(); + taskQueue.Remove(taskEntry); + } } diff --git a/Assets/Virtual Agents Framework/Runtime/Scripts/ScheduleBasedExecution/ScheduleBasedTaskSystem.cs b/Assets/Virtual Agents Framework/Runtime/Scripts/ScheduleBasedExecution/ScheduleBasedTaskSystem.cs index bb6d0712..847a255c 100644 --- a/Assets/Virtual Agents Framework/Runtime/Scripts/ScheduleBasedExecution/ScheduleBasedTaskSystem.cs +++ b/Assets/Virtual Agents Framework/Runtime/Scripts/ScheduleBasedExecution/ScheduleBasedTaskSystem.cs @@ -50,5 +50,25 @@ public void ScheduleTask(IAgentTask task, int priority = 0, string layer = "Base { taskManagers[layer].ScheduleTask(task, priority); } + + /// + /// Aborts the current task on the specified layer + /// + /// Name of the layer on which the task should be aborted + public void Abort(string layer = "Base Layer") + { + taskManagers[layer].Abort(); + } + + /// + /// Aborts the current tasks on all layers + /// + public void AbortAllLayers() + { + foreach (var layer in taskManagers) + { + taskManagers[layer.Key].Abort(); + } + } } }