DroidCombatTask - UQcsse3200/2023-studio-3 GitHub Wiki
DroidCombatTask Class
Overview
The DroidCombatTask class is responsible for executing the AI logic for the DroidTower entity within the game. This class handles the scanning for targets in a straight line from its center, and triggers actions based on its internal Finite State Machine (FSM). The class is designed to be added to an AiTaskComponent, which should be attached to a DroidTower entity.
Usage
To utilize DroidCombatTask, execute the following steps:
- Create an instance of
DroidCombatTask, specifying the necessary parameters such as priority andmaxRange. - Add it to an
AiTaskComponentinstance that is connected to a DroidTower entity.
// Create an AiTaskComponent instance
AITaskComponent aiTaskComponent = new AITaskComponent();
// Add the DroidCombatTask to the AiTaskComponent
aiTaskComponent.addTask(new DroidCombatTask(10, 7.0f));
// Add AiTaskComponent to Droid entity
droid.addComponent(aiTaskComponent);
Key Points
- Priority: An integer representing the task's priority relative to other tasks.
- MaxRange: A float value that establishes the detection range for potential targets.
State Management: updateTowerState()
The updateTowerState() method is at the heart of the Droid Tower's AI logic, acting as the Finite State Machine (FSM) to manage the tower's states and transitions. The FSM toggles between multiple states described by the STATE enum: IDLE, UP, DOWN, SHOOT_UP, SHOOT_DOWN, WALK, and DIE.
Variables to be taken into consideration:
public static final String GO_UP = "goUpStart";
public static final String GO_DOWN = "goDownStart";
public static final String ATTACK_UP = "attackUpStart";
public static final String ATTACK_DOWN = "attackDownStart";
public static final String WALK = "walkStart";
public static final String DEATH = "deathStart";
public static final String IDLE = "idleStart";
public static final String SHOOT_UP = "ShootUp";
public static final String SHOOT_DOWN = "ShootDown";
State Descriptions
IDLE
- Description: Default state, where the Droid Tower is idle and not detecting any targets.
- Behavior: Calls
isTargetVisible()to look for enemies. - Transition: If a target is found, triggers the
ATTACK_UPandSHOOT_UPevents and moves to theDOWNstate; otherwise, triggers theIDLEevent.
UP
- Description: Tower is ready to shoot upwards.
- Behavior: Checks for target visibility.
- Transition: If a target is seen, goes to
SHOOT_UP; otherwise, returns toIDLE. It triggersGO_UPeither way.
DOWN
- Description: Tower is ready to shoot downwards.
- Behavior: Checks for target visibility.
- Transition: If a target is seen, goes to
SHOOT_DOWNand triggersGO_DOWN; otherwise, returns toIDLEand triggersIDLE.
SHOOT_UP
- Description: Tower is in the process of shooting upwards.
- Behavior: Checks for target visibility.
- Transition: If a target is seen, triggers the
ATTACK_UPandSHOOT_UPevents and goes toDOWN; otherwise, returns toIDLEand triggersIDLE.
SHOOT_DOWN
- Description: Tower is in the process of shooting downwards.
- Behavior: Checks for target visibility.
- Transition: If a target is seen, it triggers
ATTACK_DOWNandSHOOT_DOWNevents and goes toUP; otherwise, transitions toUPand triggerGO_UP.
WALK
- Description: The initial state when the task starts.
- Behavior: triggers
WALKthen automatically transitions toIDLE.
DIE
- Description: This state is activated when the tower's health drops to zero.
- Behavior: Flags the tower for deletion once the death animation is finished.
NOTE: All the events triggered in this class are captured by event listeners in DroidAnimationController
Identifying the Target: isTargetVisible()
Refer to TNTTowerCombatTask for a detailed explanation.
Why Finite State Machine?
Refer to TNTTowerCombatTask
Test Plan
DroidCombatTask has been tested according to DroidCombatTask Test Plan