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:

  1. Create an instance of DroidCombatTask, specifying the necessary parameters such as priority and maxRange.
  2. Add it to an AiTaskComponent instance 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_UP and SHOOT_UP events and moves to the DOWN state; otherwise, triggers the IDLE event.
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 to IDLE. It triggers GO_UP either way.
DOWN
  • Description: Tower is ready to shoot downwards.
  • Behavior: Checks for target visibility.
  • Transition: If a target is seen, goes to SHOOT_DOWN and triggers GO_DOWN; otherwise, returns to IDLE and triggers IDLE.
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_UP and SHOOT_UP events and goes to DOWN; otherwise, returns to IDLE and triggers IDLE.
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_DOWN and SHOOT_DOWN events and goes to UP; otherwise, transitions to UP and trigger GO_UP.
WALK
  • Description: The initial state when the task starts.
  • Behavior: triggers WALK then automatically transitions to IDLE.
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