Enemy NPC Pull Interaciton - UQcsse3200/2024-studio-2 GitHub Wiki

Overview

The PullTask class is a component of the game's AI system, responsible for creating a behaviour where an enemy can pull a player character towards itself. As a part of the priority task system, it determines its execution order based on the proximity and visibility of the target. This behaviour is implemented through a task that checks the distance and visibility between the enemy and the player, applying a pulling force if the player is within a specified range. The use of priority tasks allows for dynamic and responsive AI behaviour, ensuring that the most relevant actions are executed in an efficient manner.

This special enemy npc interaction is used by enemy NPCs:

  • Frog
  • Octopus

Inheritance

PullTask extends the DefaultTask class and implements the PriorityTask interface

Dependencies

The PullTask class relies on several external components and services, including:

  • Logging: Utilizes SLF4J for logging events.
  • Game Physics: Interacts with the PhysicsEngine for raycasting and physics-related operations.
  • Rendering: Uses DebugRenderer for visual debugging.
  • Service Locator: Accesses game services through the ServiceLocator to get instances of PhysicsEngine and DebugRenderer.

Constructor

public PullTask(int priority, Entity target, float viewDistance, float pullDistance)

Parameters

  • priority: (type int) The priority of the task.
  • target: (type Entity) The player entity that will be pulled.
  • viewDistance: (type float) The maximum distance at which the enemy can detect the player.
  • pullDistance : (type float) The distance within which the enemy can pull the player.

Methods

  • getPriority(): Determines the priority of the task based on the distance to the target and its visibility.
  • update(): Updates the state of the task. If the target is within the specified pull distance, it calls the pullToPlayer() method.
  • pullToPlayer(): Handles the logic for pulling the player towards the enemy. It calculates the direction from the player to the enemy, applies a pulling force, and triggers appropriate animations based on the relative positions.
  • getDistanceToTarget(): Calculates the distance between the enemy and the target player.
  • isTargetVisible(): Checks whether the target player is visible to the enemy by performing a raycast from the enemy to the player.

Example Usage

PullTask pullTask = new PullTask(priority, targetEntity, viewDistance, pullDistance);
pullTask.start();

This example creates a new instance of PullTask, setting its priority, target player, view distance, and pull distance.

Sequence Diagram