PlayerAnimationController - UQcsse3200/2024-studio-3 GitHub Wiki

Introduction

The PlayerAnimationController class is responsible for controlling the player's animations in the game by responding to movement-related events. This class ensures that the player's character displays appropriate animations for various movement directions, making the gameplay more engaging and responsive. It dynamically adjusts the character's appearance based on directional movement and ensures smooth transitions between animations.

Key Features

  • Event-Driven Animation: The component listens for specific movement-related events (e.g., walking in different directions) and triggers the corresponding animations.
  • Comprehensive Directional Support: Animations are available for all major directions, including diagonal movements (up-left, up-right, down-left, down-right).
  • Idle Animation: The character can smoothly transition to standing animations based on the last movement direction, making the gameplay visually coherent.
  • Dynamic Control: The animations are started dynamically based on player actions, ensuring flexibility and scalability for additional animations.

Code Breakdown

The PlayerAnimationController class extends the Component class and is part of the com.csse3200.game.components.player package. It leverages an AnimationRenderComponent to handle the rendering and starting of different animations.

Constructor & Intialisation

In the create() method, the controller retrieves the AnimationRenderComponent and registers event listeners for various movement events such as walking in different directions or stopping. The event system ensures that the player's animations are updated in real time as the player moves.

@Override
public void create() {
    super.create();
    // Retrieve the AnimationRenderComponent to manage animations
    animator = this.entity.getComponent(AnimationRenderComponent.class);

    // Register event listeners for movement and stop animations
    entity.getEvents().addListener("walkLeft", this::animateLeft);
    entity.getEvents().addListener("walkRight", this::animateRight);
    entity.getEvents().addListener("walkUp", this::animateUp);
    entity.getEvents().addListener("walkDown", this::animateDown);
    entity.getEvents().addListener("walkUpLeft", this::animateUpLeft);
    entity.getEvents().addListener("walkUpRight", this::animateUpRight);
    entity.getEvents().addListener("walkDownLeft", this::animateDownLeft);
    entity.getEvents().addListener("walkDownRight", this::animateDownRight);
    entity.getEvents().addListener("walkStopAnimation", this::animateStop);
}

Event-Based Animation Handling

Each movement event triggers its respective animation. For example, the animateLeft() method starts the animation for walking left, and similarly, other methods handle different directions. Diagonal movements are also supported, providing a smooth animation experience when the player moves in diagonal directions.

void animateLeft() {
    animator.startAnimation("Character_Left");
}

void animateRight() {
    animator.startAnimation("Character_Right");
}

void animateUp() {
    animator.startAnimation("Character_Up");
}

void animateDown() {
    animator.startAnimation("Character_Down");
}

// Handle diagonal movement animations
void animateUpLeft() {
    animator.startAnimation("Character_UpLeft");
}

void animateUpRight() {
    animator.startAnimation("Character_UpRight");
}

void animateDownLeft() {
    animator.startAnimation("Character_DownLeft");
}

void animateDownRight() {
    animator.startAnimation("Character_DownRight");
}

Handling Idle/Stop Animation

When the player stops moving, the animateStop() method is triggered. It determines the last direction the player was facing based on their last movement, and the corresponding standing animation is triggered. This ensures that the character does not remain in a walking animation when idle.

void animateStop(Vector2 lastDirection) {
    if (lastDirection.x < -0.1) {
        animator.startAnimation("Character_StandLeft");
    } else if (lastDirection.x > 0.1) {
        animator.startAnimation("Character_StandRight");
    } else if (lastDirection.y < -0.1) {
        animator.startAnimation("Character_StandDown");
    } else if (lastDirection.y > 0.1) {
        animator.startAnimation("Character_StandUp");
    }
}