Skip to content

MobWanderTask

meganroxburgh edited this page Sep 11, 2023 · 3 revisions

Introduction

The MobWanderTask is used to allow the general mobs to move across the lanes of the map, and also incorporates the dying and disposing of the entity when it reaches zero health. The mobs will die and drop a currency for the player to collect

Usage

This task is attached to an entity, for example the Xeno Grunt, in NPCFactory. It takes on two positional arguments; Vector2 wanderRange, distance in X and Y the entity can move from its position when start() is called and, float waitTime, the time in seconds to wait before wandering.

Entity xenoGrunt = createBaseNPC(target);
AITaskComponent ai= new AITaskComponent();
xenoGrunt.addComponent(ai);
xenoGrunt.getComponent(AITaskComponent.class).addTask(new MobWanderTask(wanderRange, waitTime));

Sequence Diagram

MobWanderTask_Sequence

Methods

Below outlines the key methods of this class.

update()

The update method will repeatedly check for the mob to be dead, calling the isDead() method from CombatStatsComponent, which checks if the mob's health equals zero. The update method will also repeatedly update the mobs x & y position and store it in a private Vector2 mobPosition.

isDead()

The MobWanderTask contains a boolean isDead() which will be set on update, if the mob has died according to the isDead() method. Then:

  1. The dieStart animation will trigger & play
  2. The current task (MobWanderTask) will stop
  3. The isDead flag will be set to true

Once the animation has stopped, using the AnimationRenderComponent isFinished() method, a currency will be dropped in place. Firstly currency drop is initialised with owner.getEntity().getComponent(CombatStatsComponent.class).drop(). Next the drop’s position is set with the setPosition() method from the Entity class & using the mob last x & y position. Finally the drop entity is registered with ServiceLocator.getEntityService().register(drop).

Finally the mob is disposed off and deleted from the game area with owner.getEntity().setFlagForDelete(true).

Clone this wiki locally