Enemy NPC Shoot Interaction - UQcsse3200/2024-studio-2 GitHub Wiki

Overview

View ShootTask Implementation

The ShootTask class is a component of the game's AI system, responsible for allowing an entity to wait for a specified duration before shooting a projectile at a target. As part of the priority task system, it determines when to shoot based on the distance to the target and the time elapsed since the last shot. This behaviour enables dynamic interaction in combat scenarios, ensuring that entities engage with their targets at appropriate distances and intervals.

Inheritance

ShootTask extends the DefaultTask class and implements the PriorityTask interface.

Dependencies

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

Logging: Utilizes SLF4J for logging events. Game Time: Interacts with the GameTime service for tracking shot intervals. Service Locator: Accesses game services through the ServiceLocator to get instances of GameTime.

Constructor

public ShootTask(float waitTime, Entity target, float range)

Parameters

waitTime: (type float) The time in seconds to wait between shots. target: (type Entity) The target entity to shoot at. range: (type float) The distance within which the entity will stop chasing and start shooting.

Methods

getPriority(): Determines the priority of the task based on the current status (active or inactive) and the distance to the target. update(): Updates the state of the task. If the wait time has elapsed or no shots have been fired, it initiates shooting. getDistanceToTarget(): Calculates the distance between the entity and the target. getActivePriority(): Retrieves the priority when the task is active based on the distance to the target and the time since the last shot. getInactivePriority(): Retrieves the priority when the task is inactive based on the distance to the target and the time since the last shot. startWaiting(): Initiates the waiting state before shooting, triggering any relevant events. startShooting(): Executes the logic for firing a projectile at the target, updating the time of the last shot and the number of shots fired.

Example Usage

ShootTask shootTask = new ShootTask(waitTime, targetEntity, shootingRange);
shootTask.start();

This example creates a new instance of ShootTask, setting its wait time, target entity, and shooting range. It then starts the task, allowing the entity to shoot at the specified target after waiting the defined duration.

Sequence Diagram