Skip to content

MobAttackTask

meganroxburgh edited this page Sep 11, 2023 · 10 revisions

Introduction

The MobAttackTask is used to allow the NPC general mobs to interact with the towers and engineers, by either shooting projectiles or performing a melee attack. The method determines how far it is from a tower/engineer in its lane and based on this, performs an attack action. If it has not yet reached the tower, it will stop and shoot a projectile, before continuing to move forward. If it has reached its target, it will stop shooting projectiles and instead perform a melee (close-ranged) attack.

Usage

The 'MobAttackTask' is added to all NPC entities in the 'NPCFactory'. It requires two arguments; int priority, which denotes the task priority when targets are detected (handled by the PriorityTask class) and; float maxRange, which denotes the maximum effective range of the weapon mob and determines the detection distance of targets. All general mobs are created in the NPCFactory using the createBaseNPC, and after adding the AIComponent the MobAttackTask can be added. An example is shown below for create a Xeno Grunt entity and adding the task.

Entity xenoGrunt = createBaseNPC(target);
AITaskComponent ai= new AITaskComponent();
xenoGrunt.addComponent(ai);
xenoGrunt.getComponent(AITaskComponent.class).addTask(new MobAttackTask(priority, maxRange));

Sequence Diagram

MobAttackTask_Sequence

Methods

Below explains the key methods found in this task that implement the functionality of detecting targets and performing the attack action.

updateMobState()

updateMobState has been updated to include a check that the target is attackable as well as visible to fire and deploy, it will also toggle mob movement. It has also been expanded to attack a target with melee or projectile based on the distance from the target.

If the target is attackable and visible, then the CombatStatsComponent of the mob will determine which weapon is used to attack the target. If the weapon is of type Melee, using TouchAttackComponent.onCollisionStart the mob will apply damage and kickback to the enemy. If the returned weapon is of type Projectile, a projectile will be produced.

When the entity enters state DEPLOY, if it can see a target and it is attackable the method will toggle the Movement of the mob to false, so they will stop moving to attack. When FIRING status is complete, the mob movement is toggled to true and the mob will begin moving again.

isTargetVisible()

The 'isTargetVisible' method returns a Boolean value depending on whether the mob can see a tower or engineer entity in its path. This will now detect all those in the HUMAN layer. If an object is Infront of the target but is not HUMAN, it is considered there is no target.

meleeOrProjectile()

This method will return a Weapon that can be used to attack a target. If the target does not have a CombatStatsComponent and therefore cannot be attacked, the method will return null. If the mob holds at least one Weapon (as defined in the CombatStatsComponent), it will return the Weapon to be used to attack the target. For more on the selection of the Weapon see CombatStatsComponent.getWeapon(Entity target).

setTarget()

For each triggering of the MobAttackTask, the entity will have a target. This method will assign the target to the private target variable to be referenced to in updateMobState and meleeOrProjectile.

update()

This function will trigger updateMobState, then if the state is STOW the state is triggered and this task’s status is updated to FINISHED.

stop()

This function will now check with state MobAttackTask in and will not let it stop unless the mobState is finished (IDLE or STOW) this is to avoid animation triggering. If it can be finished, it is triggered to stop.

getActivePriority()/getInactivePriority()

This function now takes into consideration:

  • If the delay attack time has been reached, if not, -1
  • If there is a visible target
  • If a weapon is returned to attack the target

If there is no target returned to attack the target this indicates that either the mob cannot attack, or the target is immune (they don’t have CombatStatsComponent) and can’t take damage.

Clone this wiki locally