Skip to content

Engineer Combat Task

Praneet Dhoolia edited this page Sep 10, 2023 · 46 revisions

Introduction

The EngineerCombatTask implements the entire shooting functionality of Engineers and enables them to detect and attack enemy mobs. It scans for mobs every second and takes a high priority if a mob is detected.

It is ran by the HumanWanderTask and works in coordination with HumanWaitTask and HumanMovementTask to give the Engineers a fighting chance as a last straw against enemies.

Responsibilities of this task include switching through multiple Engineer states, playing the required animations and sounds, and spawning Projectiles in the enemy's direction.

Usage

This task is instantiated by HumanWanderTask through the EngineerCombatTask(float maxRange) constructor, which takes the maximum shooting range of the Engineer entity as a parameter.

Task Actions

The following task actions are called based on the game area's current state inside the overridden update() method, which is handled by the task runner implemented in AITaskComponent.

Helper Methods

The following methods are used to optimize the primary methods and remove code redundancy.

Retrieving the closest target: fetchTarget()

This method is responsible for returning the nearest target's position from the list of detected targets. To implement this logic, it uses a placeholder integer to store the distance between the Engineer and Mob lanes, and iterates through the stored positions to find the one closest to the Engineer.

Getting the task priority: getPriority()

This method returns the task's priority, thus deciding if it should be run before any other game tasks or not. It returns the default priority (3) if any enemies are visible, otherwise 0.


Detecting Enemy Mobs: isTargetVisible()

This method is able to detect any mobs within 5 lanes of the Engineer on either end of the map.

To detect a mob in a certain lane, the inbuilt physics.raycast() method is used between two vector positions on the map to detect any entities within a certain layer.

If detected, the position of the enemy mob is stored in a list. After the detection sequence finishes running, the method returns true or false depending on whether one or more targets have been found.

Code:

Vector2 position = owner.getEntity().getCenterPosition();

for (int i = 5; i > -5; i--) {
    if (physics.raycast(position, new Vector2(position.x + maxRange, position.y + i), TARGET, hit)) {
        hits.add(hit);
        targets.add(new Vector2(position.x + maxRange, position.y + i));
    }
}
return !hits.isEmpty();
Clone this wiki locally