Worm Attack (Green macaw NPC) - UQcsse3200/2024-studio-2 GitHub Wiki

Overview

All green macaws have the ability to throw worm in the MainGame. This happens when the player enters a radius (currently set to 5 in game units) of the macaw entity.

The behavior of the worm is controlled by the projectile components and tasks, detailed here: (https://github.com/UQcsse3200/2024-studio-2/wiki/Projectile-Entities), and is spawned in via an entity event attached to a monkey entity. This event calls the spawnWorm() method in the ForestGameArea class:

  public static Entity createWorm(Entity target) {
    String path = configs.worm.getSpritePath();
    TextureAtlas atlas = ServiceLocator.getResourceService().getAsset(path, TextureAtlas.class);

    AnimationRenderComponent animator = new AnimationRenderComponent(atlas);
    animator.addAnimation("fire", 0.25f, Animation.PlayMode.LOOP);

    Entity worm = createBaseProjectile(target, configs.worm, 0.5f, animator, new BananaAnimationController());
    worm.setEnemyType(Entity.EnemyType.WORM);
    return worm;
  }

ShootTask Management

The ShootTask handles the logic for shooting projectiles, such as worm, toward the player. It allows the macaw entity to wait for a set amount of time and then fire a banana projectile at the player. The task calculates when to shoot based on a predefined wait time and the distance between the monkey and the player.

Key Features of ShootTask:

  • Wait Time Management: The ShootTask incorporates a waitTime parameter, which defines the time interval between consecutive banana shots. This ensures a balanced gameplay experience by preventing the monkey from continuously firing without pause.
  • Target Detection and Range Control: The task uses a range parameter to determine the distance within which the macaw will start shooting bananas. If the player is outside this range, the monkey will not engage in shooting.
  • Event-Based Shooting: When the conditions are met (e.g., the player is within range and the wait time has elapsed), the task triggers an event (FireWorm) to spawn a banana projectile, which is then managed by the ProjectileFactory and associated components.

startShooting() Method Example

Here is a snippet of how the ShootTask manages the shooting process:

private void startShooting() {
    logger.debug("Shooting at target");
    // Update the time of the last shot
    lastShotTime = timer.getTime();
    // Increase the shot count
    numShots++;
    // Trigger the event to fire a banana
    owner.getEntity().getEvents().trigger("FireBanana", owner.getEntity());
}

The worm is currently a still image, with future plans of adding an animation.

worm