AI Task System - UQcsse3200/2024-studio-1 GitHub Wiki
The AI task system controls NPC behaviors in the game using a priority-based approach to decide which task an NPC should execute at any given time.
The AITaskComponent
is the core of the AI system. It manages a list of PriorityTask
s and selects which task to execute based on their priorities.
public class AITaskComponent extends Component {
private final List<PriorityTask> priorityTasks = new ArrayList<>();
private PriorityTask currentTask;
// ...
}
Key functionalities:
- Adding Tasks: Tasks are added to the AI component, each with its own priority.
- Task Selection: On each update, the AI component selects the task with the highest priority.
- Task Switching: If a new task has a higher priority than the current one, the AI component switches to the new task.
All tasks implement the PriorityTask
interface:
public interface PriorityTask {
int getPriority();
void start();
void update();
void stop();
}
- Dynamic Priorities: Task priorities can change based on game conditions.
- Negative Priority: A negative priority indicates that the task should not be run.
The AITaskComponent
uses the following logic:
-
Task Selection:
- Get the priority of each task
- Select the task with the highest priority
-
Task Execution:
- If the selected task is different from the current task:
- Stop the current task
- Start the new task
- Update the current task
- If the selected task is different from the current task:
classDiagram
class AITaskComponent {
-List~PriorityTask~ priorityTasks
-PriorityTask currentTask
+addTask(PriorityTask)
+update()
-getHighestPriorityTask()
-changeTask(PriorityTask)
}
class PriorityTask {
<<interface>>
+getPriority() int
+start()
+update()
+stop()
}
class ConcreteTask {
-int priority
+getPriority() int
+start()
+update()
+stop()
}
AITaskComponent o-- PriorityTask
PriorityTask <|.. ConcreteTask
This diagram shows the main components of the AI Task System:
-
AITaskComponent
: Manages and executes tasks -
PriorityTask
: Interface for all tasks -
ConcreteTask
: An example of a specific task implementation
sequenceDiagram
participant AITaskComponent
participant PriorityTask
AITaskComponent->>AITaskComponent: update()
AITaskComponent->>AITaskComponent: getHighestPriorityTask()
loop for each task
AITaskComponent->>PriorityTask: getPriority()
PriorityTask-->>AITaskComponent: priority
end
Note over AITaskComponent: Select highest priority task
This diagram illustrates how the AITaskComponent
selects the highest priority task.
sequenceDiagram
participant AITaskComponent
participant CurrentTask
participant NewTask
alt new task != current task
AITaskComponent->>CurrentTask: stop()
AITaskComponent->>AITaskComponent: changeTask(newTask)
AITaskComponent->>NewTask: start()
end
AITaskComponent->>NewTask: update()
This diagram shows how the AITaskComponent
switches to and executes a new task.
For a detailed overview of all available tasks, refer to the Animal Tasks page.