Skip to content

PlayerAnimationController

Manpreetingh edited this page Sep 10, 2024 · 1 revision

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");
    }
}

Table of Contents

Home

Team Design Document

Game Features

Inventory System
Scoring System
Food Recipes
Level System
Player Actions
Ordering System
Stations
Items
Map Design
Customers
Pause Menu
Upgrades
End of Day Display
Day Night Cycle
Moral System
Debug Terminal
Game Interactions Tutorial
Backstory Cutscenes

Game

Getting Started

Entities and Components

World Backstory

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Map Design

Test Plans

Sensor Component

Customer Sensor Component

Interaction Component

Inventory Component

Inventory Display

Station Meal Component

Station Progress Display

Keyboard Input Component

Fire Extinguisher Handler Component

Score System

HoverBox Component

MainGameActions Create Docket Triggers

End Day Display Component

Cutscene Area

Docket

Docket Line Display

Docket Meal Display

Main Game Order Button Display

Order Actions

Recipe

Ticket Details Component

BackstoryCutscene Test Plan

BackstoryCutsceneDisplay Test Plan

Test Plan for Tutorial

Keybinds

Keybinds Test Plan

Test Plan for MainGameOrderTicketDisplay

Test Plan for MainGameOrderBtnDisplay

Test Plan for Docket

Test Plan for DocketLineDisplay

Test Plan for OrderActions

Ticket Details

Test plan for RandomComboService

Test plan for LoanUpgrade

Test plan for UpgradesDisplay

Test plan for RageUpgrade

Test plan for SpeedBoostUpgrade

Test plan for DancePartyUpgrade

Test plan for ExtortionUpgrade

Troubleshooting

MacOS Setup Guide

Clone this wiki locally