Projectile Attack - UQcsse3200/2024-studio-1 GitHub Wiki

ProjectileAttackComponent

ProjectileAttackComponent manages the lifecycle of a projectile in the game. It handles the creation, shooting, collision detection, and damage application of projectiles. This component integrates several other essential components to ensure the projectile behaves as expected during its lifespan.

Class Description

The ProjectileAttackComponent is responsible for the lifecycle management of projectiles. It initializes itself by shooting the projectile, listens for collision events, attempts to apply damage to any colliding entities, and finally cleans up after the projectile's lifecycle ends. It requires other components such as ProjectileActions, CombatStatsComponent, and HitboxComponent for full functionality.

Key Methods:

  • ProjectileAttackComponent(short layer, Vector2 direction, Vector2 speed):

    • Description: Constructs a new ProjectileAttackComponent, initializing key variables for the projectile's behavior.
    • Parameters:
      • short layer: The physics layer the projectile interacts with, provided by the ProjectileConfig.
      • Vector2 direction: The direction in which the projectile is shot. For example, Vector2Utils.LEFT for leftward movement.
      • Vector2 speed: The speed of the projectile set in the projectile configuration (e.g., Vector2(3,3) representing 3 meters per second).
  • void create():

    • Description: Initializes the component, setting up the necessary references to other components and starting the projectile's shooting action. It also registers a collision listener for detecting impacts with other entities.
  • private void onCollisionStart(Fixture me, Fixture other):

    • Description: Handles the collision event triggered when the projectile collides with another entity. It checks whether the collision is valid (against the right fixture and target layer) and attempts to apply damage to the target entity if possible.
    • Parameters:
      • Fixture me: The fixture belonging to this projectile entity.
      • Fixture other: The fixture belonging to the hit entity.

Lifecycle Actions:

  • Shooting: The projectile is shot in a specified direction with a defined speed upon creation, enabling it to move through the game world.
  • Collision Handling: Upon collision with a valid target, the component applies damage using the combat stats associated with both the projectile and the target entity.
  • Disposal: After a collision, the projectile cleans up its resources by disposing of its texture and unregistering itself from the entity service.