Banana Attack (Monkey NPC) - UQcsse3200/2024-studio-2 GitHub Wiki
Overview
All monkeys have the ability to throw bananas in the MainGame. This happens when the player enters a radius (currently set to 5 ingame units) of the monkey entity.
The behavior of the banana 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 spawnBanana() method in the ForestGameArea class:
private void spawnBanana(Entity enemy) {
Entity banana = ProjectileFactory.createBanana(player);
// Calculate bananaX and bananaY based on target's relative position
float bananaX = (enemy.getPosition().x - player.getPosition().x) > 0 ? -1 : 1;
float bananaY = (enemy.getPosition().y - player.getPosition().y) > 0 ? 1 : -1;
// Calculate the new position using Vector2
Vector2 pos = new Vector2(enemy.getPosition().x + bananaX, enemy.getPosition().y + bananaY);
//spawns
spawnEntityAtVector(banana, pos);
}
ShootTask
Management
The ShootTask
handles the logic for shooting projectiles, such as bananas, toward the player. It allows the monkey 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.
ShootTask
:
Key Features of - Wait Time Management: The
ShootTask
incorporates awaitTime
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 monkey 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 (
FireBanana
) to spawn a banana projectile, which is then managed by theProjectileFactory
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 banana is currently a still image, with future plans of adding an animation.