-
Notifications
You must be signed in to change notification settings - Fork 4
Engineer Combat Task
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.
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.
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
.
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.
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();
The following methods are used to optimize the primary methods and remove code redundancy.
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.