Skip to content

Commit

Permalink
Add methods to abort the current task and remove tasks from the task …
Browse files Browse the repository at this point in the history
…queue
  • Loading branch information
lenalinke committed Jun 5, 2024
1 parent 6333627 commit 5c6bea9
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Assets/Virtual Agents Framework/Runtime/Scripts/BaseTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public void StopAsFailed()
StopExecution();
}

/// <summary>
/// Can be used to abort the task outside of its Update method
/// </summary>
public void StopAsAborted()
{
State = TaskState.Aborted;
StopExecution();
}

/// <summary>
/// Can be used to let the task succseed outside of its Update method
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,15 @@ public void Deserialize(SerializationDataContainer serializer)
playTime = serializer.GetSerializedFloat("Play Time");
}

/// <summary>
/// Aborts the animation task and sets its state to aborted
/// </summary>
public override void Abort()
{
Debug.Log("Abort in AnimationTask");
StopExecution();
State = TaskState.Aborted;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using i5.Toolkit.Core.Utilities;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace i5.VirtualAgents.AgentTasks
{
Expand Down Expand Up @@ -89,5 +90,18 @@ public void WaitFor(params AgentBaseTask[] otherTasks)
}
}
}

/// <summary>
/// Aborts the task and sets its state to aborted
/// </summary>
public virtual void Abort()
{
Debug.Log("Abort in BaseTask");
if (State == TaskState.Running)
{
StopAsAborted();
State = TaskState.Aborted;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,18 @@ public void Deserialize(SerializationDataContainer serializer)
TargetSpeed = serializer.GetSerializedFloat("TargetSpeed");
//FollowingGameObject = serializer.GetSerializedBool("FollowingGameObject");
}

/// <summary>
/// Aborts the movement task and sets its state to aborted
/// </summary>
public override void Abort()
{
Debug.Log("Abort in MovementTask");
if (navMeshAgent != null)
{
StopExecution();
}
State = TaskState.Aborted;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class AgentWaitTask : AgentBaseTask, ISerializable
/// </summary>
public float WaitTimeInSeconds { get; set; }

private Coroutine waitCoroutine;

private Agent agent;

public AgentWaitTask() { }

/// <summary>
Expand All @@ -32,6 +36,7 @@ public AgentWaitTask(float timeInSeconds)
/// <param name="agent">The agent which executes this task</param>
public override void StartExecution(Agent agent)
{
this.agent = agent;
base.StartExecution(agent);

if (WaitTimeInSeconds <= 0)
Expand All @@ -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
Expand All @@ -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");
}

/// <summary>
/// Aborts the wait task
/// </summary>
public override void Abort()
{
Debug.Log("Abort in WaitTask");
if (waitCoroutine != null)
{
agent.StopCoroutine(waitCoroutine);
waitCoroutine = null;
}
State = TaskState.Aborted;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public interface IAgentTask : ITask
/// False if there are unfulfilled conditions that block the execution
/// </summary>
bool CanStart { get; }

/// <summary>
/// Aborts the task
/// </summary>
public void Abort() {}
}
}
3 changes: 2 additions & 1 deletion Assets/Virtual Agents Framework/Runtime/Scripts/ITask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using i5.Toolkit.Core.Utilities;
using System;
using i5.VirtualAgents.AgentTasks;
using UnityEngine;

namespace i5.VirtualAgents.ScheduleBasedExecution
{
Expand Down Expand Up @@ -189,5 +190,50 @@ public IAgentTask PeekNextTask()
{
return queue.PeekNextTask();
}

/// <summary>
/// Removes all tasks from the queue and sets the state of the task manager to idle
/// </summary>
/// <param name="clearCurrentTask">Determines whether the current task should be aborted as well</param>
public void Clear(bool clearCurrentTask)
{
queue.Clear();
if (clearCurrentTask)
{
CurrentTask.Abort();
CurrentTask = null;
CurrentState = TaskManagerState.idle;
}
// TODO add after merging with TaskBundles:
// lastTask = null;
}

/// <summary>
/// Removes a task from the task queue
/// </summary>
/// <param name="task">The task to be removed</param>
public void RemoveTask(IAgentTask task)
{
queue.RemoveTask(task);
if (CurrentTask == task)
{
Abort();
}
}

/// <summary>
/// Aborts the current task
/// </summary
public void Abort()
{
Debug.Log("Abort called in AgentTaskManager:");
if (CurrentTask != null)
{
CurrentTask.Abort();
CurrentTask = null;
CurrentState = TaskManagerState.idle;
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,31 @@ public void AddTask(IAgentTask task, int priority = 0)
}

/// <summary>
/// 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
/// </summary>
private struct TaskEntry
{
public IAgentTask task;
public int priority;
}

/// <summary>
/// Removes all tasks from the queue
/// </summary>
public void Clear()
{
taskQueue.Clear();
}

/// <summary>
/// Removes a task from the task queue
/// </summary>
/// <param name="task">Task to be removed</param>
public void RemoveTask(IAgentTask task)
{
TaskEntry taskEntry = new TaskEntry();
taskQueue.Remove(taskEntry);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,25 @@ public void ScheduleTask(IAgentTask task, int priority = 0, string layer = "Base
{
taskManagers[layer].ScheduleTask(task, priority);
}

/// <summary>
/// Aborts the current task on the specified layer
/// </summary>
/// <param name="layer">Name of the layer on which the task should be aborted</param>
public void Abort(string layer = "Base Layer")
{
taskManagers[layer].Abort();
}

/// <summary>
/// Aborts the current tasks on all layers
/// </summary>
public void AbortAllLayers()
{
foreach (var layer in taskManagers)
{
taskManagers[layer.Key].Abort();
}
}
}
}

0 comments on commit 5c6bea9

Please sign in to comment.