Skip to content

Sprint 2 Main Character Interaction and Movement Animations

Rohith Palakirti edited this page Sep 13, 2021 · 16 revisions

Contents

Animation Implementation and Controls

An AnimationRenderComponent is used to define all the animations of the character, their playmode, and FPS.

PlayerFactory.java

    AnimationRenderComponent mpcAnimator = createAnimationComponent("images/mpc/mpcAnimation.atlas");
        mpcAnimator.addAnimation("main_player_run", 0.1f, Animation.PlayMode.LOOP);
        mpcAnimator.addAnimation("main_player_walk", 0.5f, Animation.PlayMode.LOOP);
        mpcAnimator.addAnimation("main_player_front", 1f, Animation.PlayMode.LOOP);
        mpcAnimator.addAnimation("main_player_jump", 2.5f, Animation.PlayMode.LOOP);
        mpcAnimator.addAnimation("main_player_attack", 1f, Animation.PlayMode.LOOP);
        mpcAnimator.addAnimation("main_player_crouch", 1f, Animation.PlayMode.LOOP);
        mpcAnimator.addAnimation("main_player_right", 1f, Animation.PlayMode.LOOP);
        mpcAnimator.addAnimation("main_player_pickup",0.125f,Animation.PlayMode.LOOP);

Key event triggers are defined in KeyboardPlayerInputComponent.java

public boolean keyDown(int keycode) {
    switch (keycode) {

      case Keys.S:
      case Keys.DOWN:
        entity.getEvents().trigger(("crouch"));
        return true;
      case Keys.D:
      case Keys.RIGHT:
        walkDirection.add(Vector2Utils.RIGHT);
        triggerWalkEvent();
        entity.getEvents().trigger(("walkRight"));
        return true;
      case Keys.SPACE:
        entity.getEvents().trigger("attack");
        return true;
      case Keys.W:
      case Keys.UP:
        entity.getEvents().trigger(("jump"));
        return true;
      default:
        return false;
    }
  }

The listeners implemented here lookout for trigger events and run the corresponding functions in PlayerAnimationController.java. These functions are discussed in detail in the following sections of the wiki.

public void create() {
        super.create();
        animator = this.entity.getComponent(AnimationRenderComponent.class);
        entity.getEvents().addListener("walkRight", this::animateRight);
        entity.getEvents().addListener("crouch", this::animateCrouch);
        entity.getEvents().addListener("stopCrouch", this::animateWalk);
        entity.getEvents().addListener("attack", this::animateAttack);
        entity.getEvents().addListener("stopAttack", this::animateWalk);
        entity.getEvents().addListener("jump", this::animateJump);
        entity.getEvents().addListener("stopJump", this::animateWalk);
        entity.getEvents().addListener("itemPickUp", this::animatePickUp);
        entity.getEvents().addListener("stopPickUp", this::animateWalk);
        entity.getEvents().addListener("startMPCAnimation", this::animateWalk);
        entity.getEvents().addListener("stopMPCAnimation", this::preAnimationCleanUp);
    }

Each time an animation event is triggered, a helper function is used to clear any existing textures or stop an already running animation.

/**
     * Helper function to stop all animations and trigger walking animation
     */

    private void animateWalk() {
        preAnimationCleanUp();
        animator.startAnimation("main_player_walk");
    }

/**
     * Helper function to dispose texture (if it exists) and stop all prior running animations.
     */

    private void preAnimationCleanUp() {
        if(texturePresent) {
            animator.getEntity().getComponent(TextureRenderComponent.class).dispose();
            texturePresent = false;
        }
        animator.stopAnimation();
    }

The main player character can be controlled using the WASD keys or the arrow keys. Below is a table including descriptions and controls of how to manipulate the main character.

Key Map

Action Key Control Comment
Jump W / UP Implemeted
Walk Default movement Implemented
Run Right D / RIGHT Implemeted
Run Left A / LEFT Deprecated
Crouch S / DOWN Implemeted
Attack SPACE Implemeted
Item PickUp X Implemeted
Use Item E Future Sprints
Switch Item To be determined Future Sprints

Refactoring Implementation to Controller Pattern

In sprint 1, the animations were handled by methods in PlayerActions.java. For sprint 2, the animations are entirely implemented in PlayerAnimationController.java using the Controller Pattern. Each animation is handled by its respective method, which is run once a event trigger is picked up by the attached listeners.

Existing Implementation (Sprint 1 - PlayerActions.java)

/**
   * Updates the player sprite to turn right
   */
  boolean walkLeft;

  void walkRight() {
    if(walkLeft) {
      walkLeft = false;
      Sound turnSound = ServiceLocator.getResourceService().getAsset("sounds/turnDirection.ogg", Sound.class);
      turnSound.play();
    }
    animator.getEntity().setRemoveTexture();
    animator.stopAnimation();
    animator.startAnimation("main_player_run");
    Sound turnSound = ServiceLocator.getResourceService().getAsset("sounds/turnDirection.ogg", Sound.class);
    turnSound.play();
  }

  void stopWalkRight() {
    animator.stopAnimation();
    animator.startAnimation("main_player_walk");

  }

Refactored Implementation (Sprint 2 - PlayerAnimationController.java)

public class PlayerAnimationController extends Component {
    AnimationRenderComponent animator;
    private boolean texturePresent = true;


    @Override
    public void create() {
        super.create();
        animator = this.entity.getComponent(AnimationRenderComponent.class);
        entity.getEvents().addListener("walkRight", this::animateRight);
        entity.getEvents().addListener("crouch", this::animateCrouch);
        entity.getEvents().addListener("stopCrouch", this::animateWalk);
        entity.getEvents().addListener("attack", this::animateAttack);
        entity.getEvents().addListener("stopAttack", this::animateWalk);
        entity.getEvents().addListener("jump", this::animateJump);
        entity.getEvents().addListener("stopJump", this::animateWalk);
        entity.getEvents().addListener("itemPickUp", this::animatePickUp);
        entity.getEvents().addListener("stopPickUp", this::animateWalk);
        entity.getEvents().addListener("startMPCAnimation", this::animateWalk);
        entity.getEvents().addListener("stopMPCAnimation", this::preAnimationCleanUp);
    }

    /**
     *  Makes the player pickup items
     */
    private void animatePickUp() {
        preAnimationCleanUp();
        animator.startAnimation("main_player_pickup");
    }
    /**
     * Makes the player crouch.
     */

    private void animateCrouch() {
        preAnimationCleanUp();
        animator.startAnimation("main_player_crouch");

    }

    /**
     * Makes the player run to the right.
     */

    private void animateRight() {
        preAnimationCleanUp();
        animator.startAnimation("main_player_run");
    }

    /**
     * Makes the player attack.
     */

    private void animateAttack() {
        preAnimationCleanUp();
        animator.startAnimation("main_player_attack");
    }

    /**
     * Makes the player jump.
     */

    private void animateJump() {
         preAnimationCleanUp();
         animator.startAnimation("main_player_jump");
    }

    /**
     * Helper function to stop all animations and trigger walking animation
     */

    private void animateWalk() {
        preAnimationCleanUp();
        animator.startAnimation("main_player_walk");
    }

    /**
     * Helper function to dispose texture (if it exists) and stop all prior running animations.
     */

    private void preAnimationCleanUp() {
        if(texturePresent) {
            animator.getEntity().getComponent(TextureRenderComponent.class).dispose();
            texturePresent = false;
        }
        animator.stopAnimation();
    }


}

Attack Animation

The MPC attack interaction is mapped to the spacebar. Once the attack action is triggered, the attack animation is run.

PlayerFactory.java

    AnimationRenderComponent mpcAnimator = createAnimationComponent("images/mpc/mpcAnimation.atlas");
        ...
        mpcAnimator.addAnimation("main_player_attack", 1f, Animation.PlayMode.LOOP);
        ...
        

The listeners implemented here lookout for trigger events and run the corresponding functions in PlayerAnimationController.java

public void create() {
        super.create();
        animator = this.entity.getComponent(AnimationRenderComponent.class);
        ...
        entity.getEvents().addListener("attack", this::animateRight);
        entity.getEvents().addListener("stopAttack", this::animateWalk);
        ...

PlayerAnimationController.java

/**
     * Makes the player attack.
     */

    private void animateAttack() {
        preAnimationCleanUp();
        animator.startAnimation("main_player_attack");
    }

Jump Animation

Jumping

To initiate the jump movement and animation of the player, 'W' or Arrow Key 'UP' is used to trigger the movement and the animation.

PlayerFactory.java

    AnimationRenderComponent mpcAnimator = createAnimationComponent("images/mpc/mpcAnimation.atlas");
        ...
        mpcAnimator.addAnimation("main_player_jump", 2.5f, Animation.PlayMode.LOOP);
        ...

The listeners implemented here lookout for trigger events and run the corresponding functions in PlayerAnimationController.java

public void create() {
        super.create();
        animator = this.entity.getComponent(AnimationRenderComponent.class);
        ...
        entity.getEvents().addListener("jump", this::animateJump);
        entity.getEvents().addListener("stopJump", this::animateWalk);
        ...

PlayerActions.java

/**
   * Makes the player jump
   */

  void jump() {
    Sound jumpSound = ServiceLocator.getResourceService().getAsset("sounds/jump.ogg", Sound.class);
    jumpSound.play();

    Body body = physicsComponent.getBody();
    body.applyForceToCenter(0, 400f, true);
  }

PlayerAnimationController.java

/**
     * Makes the player jump.
     */

    private void animateJump() {
         preAnimationCleanUp();
         animator.startAnimation("main_player_jump");
    }

Crouch Animation

The MPC Crouch interaction is mapped to the S/Arrow `Down` keys. Once the crouch action is triggered, the crouch animation is run.

PlayerFactory.java

    AnimationRenderComponent mpcAnimator = createAnimationComponent("images/mpc/mpcAnimation.atlas");
        ...
        mpcAnimator.addAnimation("main_player_crouch", 1f, Animation.PlayMode.LOOP);
        ...
        

The listeners implemented here lookout for trigger events and run the corresponding functions in PlayerAnimationController.java

public void create() {
        super.create();
        animator = this.entity.getComponent(AnimationRenderComponent.class);
        ...
        entity.getEvents().addListener("crouch", this::animateCrouch);
        entity.getEvents().addListener("stopCrouch", this::animateWalk);
        ...

PlayerAnimationController.java

/**
     * Makes the player crouch.
     */

    private void animateCrouch() {
        preAnimationCleanUp();
        animator.startAnimation("main_player_crouch");
        
    }

Item Pick Up

ItemPickUp

According to the game map, the Item Pick Up animation is configured to be triggered using the KeyBoard Key 'X'. Once the animation is triggered, the static texture (if not removed) and all the previous animations are removed from the game area and replaced with an AnimationRenderComponent that triggers item pickup animation. As a result, the particular item texture is removed from the game.

PlayerFactory.java

    AnimationRenderComponent mpcAnimator = createAnimationComponent("images/mpc/mpcAnimation.atlas");
        ...
        mpcAnimator.addAnimation("main_player_crouch", 1f, Animation.PlayMode.LOOP);
        ...
        

The listeners implemented here lookout for trigger events and run the corresponding functions in PlayerAnimationController.java

public void create() {
        super.create();
        animator = this.entity.getComponent(AnimationRenderComponent.class);
        ...
        entity.getEvents().addListener("itemPickUp", this::animatePickUp);
        entity.getEvents().addListener("stopPickUp", this::animateWalk);
        ...

PlayerAnimationController.java

    /**
     *  Makes the player pick up items
     */
    private void animatePickUp() {
        preAnimationCleanUp();
        animator.startAnimation("main_player_pickup");
    }

Texture Atlases

The atlas that was used for creating the animations was generated using a texture packer. The atlas has player representations in the following states:

  1. Standing (facing right, static)
  2. Standing (facing forward, static)
  3. Walking (facing right, animated)
  4. Running (facing right, animated)
  5. Attack (facing right, animated)
  6. Crouch (facing right, animated)
  7. Jump (facing right, animated)
  8. Item PickUp (facing right, animated)

Generated Texture pack

mpcAnimation.png mpcAnimation.png

Testing

Considering it is hard to test whether an animation plays because of it's visual nature,unit tests were written to verify and validate the generated texture atlas and animation packs instead. The tests check that all the expected animations actually exist in the atlas, in order to catch bugs when editing/renaming/deleting animations. PlayerAnimationAtlasTest.java

@ExtendWith(GameExtension.class)
class PlayerAnimationAtlasTest {


    @Test
    void shouldLoadTextureAtlases() {
        String asset1 = "test/files/mpcAnimation.atlas";
        String asset2 = "test/files/test2.atlas";
        String[] textures = {asset1, asset2};


        AssetManager assetManager = new AssetManager();
        ResourceService resourceService = new ResourceService(assetManager);
        resourceService.loadTextureAtlases(textures);

        assetManager.load(asset1, TextureAtlas.class);
        assetManager.load(asset2, TextureAtlas.class);

        TextureAtlas atlas = new TextureAtlas(Gdx.files.internal(asset1));
        AnimationRenderComponent testAnimator = new AnimationRenderComponent(atlas);

        ObjectSet<Texture> texture = atlas.getTextures();
        String tex = texture.toString();
        assertEquals("{test/files/mpcAnimation.png}",tex);

        testAnimator.addAnimation("main_player_run", 1f);
        assertTrue(testAnimator.hasAnimation("main_player_run"));
        ...
        assertNotNull(atlas.findRegion("main_player_run"));
        assertTrue(testAnimator.hasAnimation("main_player_run"));

Generated Texture atlas

mpcAnimation.atlas

mpcAnimation.png
size: 4096, 2048
format: RGBA8888
filter: Nearest, Nearest
repeat: none
main_player_attack
  rotate: false
  xy: 2, 1089
  size: 542, 542
  orig: 542, 542
  offset: 0, 0
  index: 1
main_player_attack
  rotate: false
  xy: 2, 545
  size: 542, 542
  orig: 542, 542
  offset: 0, 0
  index: 3
main_player_attack
  rotate: false
  xy: 546, 1089
  size: 542, 542
  orig: 542, 542
  offset: 0, 0
  index: 2
main_player_crouch
  rotate: false
  xy: 3262, 1090
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 2
main_player_crouch
  rotate: false
  xy: 3261, 547
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 1
main_player_front
  rotate: false
  xy: 1088, 3
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: -1
main_player_jump
  rotate: false
  xy: 546, 546
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 2
main_player_jump
  rotate: false
  xy: 2176, 1090
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 1
main_player_jump
  rotate: false
  xy: 1632, 547
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 3
main_player_pickup
  rotate: false
  xy: 2, 2
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 1
main_player_pickup
  rotate: false
  xy: 1633, 1090
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 3
main_player_pickup
  rotate: false
  xy: 1089, 546
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 2
main_player_pickup
  rotate: false
  xy: 2718, 547
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 4
main_player_right
  rotate: false
  xy: 1631, 3
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: -1
main_player_run
  rotate: false
  xy: 2719, 1090
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 2
main_player_run
  rotate: false
  xy: 2175, 547
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 1
main_player_walk
  rotate: false
  xy: 1090, 1090
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 2
main_player_walk
  rotate: false
  xy: 545, 2
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 1

UML Diagrams

Class Diagram

Player Factory + PlayerActions + PlayerAnimationController + KeyboardPlayerInputComponent

**Player Animation Manager Classes **

Sequence Diagrams

animateAttack

animateCrouch

animateWalk

animateRight

animateJump

animatePickUp

PlayerActions - create()

PlayerAnimationController - create()

Relevant Files

mpcAnimation.atlas

mpcAnimation.png

PlayerFactory.java

PlayerActions.java

PlayerAnimationController.java

KeyboardPlayerInputComponent.java

PlayerAnimationAtlasTest.java

Gameplay

Home

Main Character

πŸ‘Ύ Obstacle/Enemy

Food & Water

Pickable Items

Game achievements

Game Design

Emotional Goals

Game Story

Influences

Style

Pixel Grid Resolution

Camera Angle and The Player's Perspective

Features Design

Achievements Screen

Achievements Trophies and Cards

Music Selection GUI

πŸ‘Ύ Obstacle/Enemy

 Monster Manual
 Obstacles/Enemies
  - Alien Plants
  - Variation thorns
  - Falling Meteorites
  - FaceHugger
  - AlienMonkey
 Spaceship & Map Entry
 Particle effect

Buffs

Debuffs

Buffs and Debuffs manual

Game Instruction

[code for debuff animations](code for debuff animations)

Infinite loop game system

Main Menu Screen

New Setting Screen

Hunger and Thirst

Goals and Objectives

HUD User Interface

Inventory System

Item Bar System

Scoring System

Props store

BGM design

Sound Effect design

Main game interface

Invisible ceiling

New terrain sprint 4

New game over screen sprint 4

Code Guidelines

Main Character Movement, Interactions and Animations - Code Guidelines

Item Pickup

ItemBar & Recycle system

Main Menu Button Code

Game Instructions Code

πŸ‘Ύ Obstacle/Enemy

 Obstacle/Enemy
 Monster Manual
 Spaceship Boss
 Particle effects
 Other Related Code
 UML & Sequence diagram of enemies/obstacles

Scoring System Implementation Explanation

Music Implementation

Buff and Debuff Implementation

Score History Display

code improvement explanation

Infinite generating terrains Implementation Explanation

Game Over Screen and functions explaination

Buffer timer before game start

Scrolling background

Multiple Maps

Invisible ceiling

Rocks and woods layout optimization

Magma and nails code implementation

Background Music Selection

Chooser GUI Implementation

Chooser GUI Logic Persistence

Guide: Adding Background music for a particular screen

Achievements Ecosystem - Code Guidelines

Achievements System

Achievements Screen

Adding Achievements (Guide)

Game Records

DateTimeUtils

History Scoreboard - Score Details

Listening for important events in the Achievements ecosystem

Food and Water System

Food System Water System

Hunger and Thirst icon code guidelines

Asset Creation

In Game Background Music

User Testing

Hunger and Thirst User Testing

Main Character Testing

Buffs and Debuffs Testing

Buff and Debuff Manual User Testing

Game Instruction User Testing

The Main Menu User Test

The New Button User Test in Setting Page

The Main Menu Buttons User Testing

Hunger and Thirst User Test

Infinite loop game and Terrain Testing

Item Bar System Testing

Randomised Item Drops

Recycle System Testing

Scoring System Testing

Music User test

https://github.com/UQdeco2800/2021-ext-studio-2.wiki.git

πŸ‘Ύ Obstacle/Enemy

 Obstacle testing
  - Alien Plants & Variation Thorns
  - Falling Meteorites
 Enemy testing
  - Alien Monkeys & Facehugger
  - Spaceship Boss
 Monster Manual
 Particle-effect
 Player attack testing
  - Player Attack

Inventory system UI layout

Props store user testing

Achievements User Testing

Sprint 1

Sprint 2

Sprint 3

Sprint 4

Items testing

Player Status testing

Changeable background & Buffer time testing

Main game interface test

Invisible ceiling test

Game over screen test sprint 4

New terrain textures on bonus map test sprint 4

Buying Props User Testing

Testing

Hunger and Thirst Testing

Main Character Player

Achievements System, Game Records and Unlockable Chapters

DateTimeUtils Testing

Scoring System Testing Plan

Distance Display Testing Plan

Musics Implementation Testing plan

History Board Testing plan

Rocks and woods testing plan

Sprint 4 terrain tests

Items

Item Bar System Testing Plan

Recycle System Testing Plan

Game Engine

Game Engine Help

Getting Started

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