Skip to content

TNTTowerCombatTask

Mohamad Dabboussi edited this page Sep 11, 2023 · 6 revisions

Documentation for TNTTowerCombatTask

TNTTowerCombatTask Class

Overview

The TNTTowerCombatTask class is responsible for executing the AI logic for the TNTTower entity in the game. It scans for targets in a straight line emanating from the center of the TNTTower entity and initiates actions based on its internal Finite State Machine (FSM). The class is meant to be attached to an AiTaskComponent that is associated with a TNTTower entity.

Usage

To use TNTTowerCombatTask, do the following:

  1. Create an instance of TNTTowerCombatTask, passing in the necessary parameters such as priority and maxRange.
  2. Attach it to an AiTaskComponent instance that is linked to a TNTTower entity.

Here's an example of how you could instantiate and add the TNTTowerCombatTask to an AiTaskComponent:

// Create an AiTaskComponent instance
AITaskComponent aiTaskComponent = new AITaskComponent();

// Add the TNTTowerCombatTask to the AiTaskComponent
aiTaskComponent.addTask(new TNTTowerCombatTask(10, 5.0f));

//Add AiTaskComponent to TNT entity
tnt.addComponent(aiTaskComponent);

Key Points

  • Priority: An integer that specifies the priority of this task relative to other tasks.
  • MaxRange: Float value defining the detection range for potential target entities.
  • State Machine: The internal FSM toggles between three primary states: IDLE, EXPLODE, and REMOVE, triggering appropriate events for each transition.

State Management: updateTowerState()

The updateTowerState() method operates on the principles of a Finite State Machine (FSM) to manage the behavior of the TNTTower entity. The method alternates between three core states: IDLE, EXPLODE, and REMOVE, executing actions and triggering events that correspond to each state.

State Descriptions

IDLE

  • Description: This is the initial or default state when the TNTTower entity has no targets within its range.
  • Behavior: In this state, the isTargetVisible() method is invoked to check for potential targets within a specified range.
  • Transition: If a target is detected, the tower triggers a dig animation then the state machine transitions to the EXPLODE state.

EXPLODE

  • Description: This state is activated when a target comes into range.
  • Behavior: Upon entering this state, the tower triggers an explosion animation and applies damage to the nearby target(s) by triggering an event found in TNTDamageComponent.
  • Transition: After completing the explosion and damage action, the state machine transitions to the REMOVE state.

REMOVE

  • Description: This state flags the TNTTower entity for deletion.
  • Behavior: In this state, the tower is flagged for deletion.
  • Transition: After flagging, the tower is effectively removed from the game world, completing its lifecycle.

NOTE: All the events triggered in this class were captured by event listeners in TNTAnimationController


Identifying the Target: isTargetVisible()

This method scans linearly from the current position of the tower (towerPosition) to the furthest point in its range (maxRangePosition) to look for enemies. It returns true if an enemy is detected and false otherwise, courtesy of the built-in physics.raycast() function.

// Using physics.raycast to identify enemies
return physics.raycast(towerPosition, maxRangePosition, TARGET, hit);   

Why Use Finite State Machine ?

Using Finite State Machines (FSMs) in the TNTTowerCombatTask class simplifies complex logic by clearly defining states and transitions. This modularity enables easy debugging, testing, and future extensions. FSMs also facilitate a clean separation of concerns between combat and animation logic, making the codebase more maintainable and scalable.

Future Implementation

In the future, the EXPLODE state will feature more elaborate and visually impactful animations to make the explosion feel more realistic.

Test Plan

TNTTowerCombatTask has been tested according to TNTTowerCombatTask Test Plan

Clone this wiki locally