Skip to content

Player Animation Functionalities

SirAardvark edited this page Sep 13, 2021 · 1 revision

Purpose

The purpose of player animation is to provide a more visual representation of the current state of the player.

Usage

Adding an animation for the player, the operation of which can consist of five parts:

  1. In ForestGameArea.java add the images of the animations that will be used in the atlas file into the forestTextures String[].
private static final String[] forestTextures = {
    "images/Walking.png",
    "images/Sprint.png",
}
  1. In PlayerMovementAnimations.atlas add the atlas of the animation underneath all other animations
IdleCharacters.png
size: 1015, 462
format: RGBA8888
filter: Linear,Linear
repeat: none
normal-stationary
  rotate: false
  xy: 214, 1
  size: 265, 431
  orig: 400, 518
  offset: 74, 50
  index: 0
  1. In PlayerFactory.java add the animation with the same name as in the atlas file
//Defining and adding player related animation here
    AnimationRenderComponent animator =
            new AnimationRenderComponent(
                    ServiceLocator.getResourceService().getAsset("images/PlayerMovementAnimations.atlas", 
                            TextureAtlas.class));
    // Example animations added
    animator.addAnimation("normal-stationary", 0.1f, Animation.PlayMode.LOOP);
    animator.addAnimation("normal-walk", 0.2f, Animation.PlayMode.LOOP);
    animator.addAnimation("normal-sprint", 0.1f, Animation.PlayMode.LOOP);
  1. In PlayerAnimationController if you are using an animation that is not in the PlayerStateComponent.java add an eventListener and a new function to call entity.startAnimation. If the animation is in the PlayerStateComponent.java then the updatePlayerStatusAnimation function will handle it for you
  @Override
  public void create() {
    super.create();
    animator = this.entity.getComponent(AnimationRenderComponent.class);
    entity.getEvents().addListener("playerStatusAnimation", this::updatePlayerStatusAnimation);
  }

  void updatePlayerStatusAnimation() {
    // Updates the health value in PlayerStateComponent
    int health = this.entity.getComponent(CombatStatsComponent.class).getHealth();
    if (health <= 90 && health > 50) {
      this.entity.getComponent(PlayerStateComponent.class).updateHealth(Health.ROUGH);
    } else if (health <= 50 && health > 10) {
      this.entity.getComponent(PlayerStateComponent.class).updateHealth(Health.DAMAGED);
    } else if (health == 0){
      this.entity.getComponent(PlayerStateComponent.class).updateHealth(Health.DEAD);
    }

    // Applies the correct animation
    animator.startAnimation(entity.getComponent(PlayerStateComponent.class).getStateAnimation());
  }
  1. In the KeyboardPlayerInputComponent class, when the corresponding button is pressed or released, the corresponding event (i.e. animation) is triggered.
//Function to trigger the event
private void triggerMovementEvent() {
  entity.getEvents().trigger("walk", walkDirection);
  entity.getEvents().trigger("playerStatusAnimation");
}

//Call the animation on pressing the 'A' Key
public boolean keyDown(int keycode) {
    case Keys.A:
        walkDirection.add(Vector2Utils.LEFT);
        triggerMovementEvent();
        return true;
}

UML

UML for PlayerAnimationController

Table of Contents

Home

Introduction

Main Menu

Main Game Screen

Gameplay

Player Movement

Character Animations

Enemy Monster Design and Animations

Game basic functionalities

User Testing

GitHub Wiki Tutorial

Game Engine

Getting Started

Documentation

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally