Sprint 1 Main Character Movement and Animation - UQdeco2800/2021-ext-studio-2 GitHub Wiki

Contents

Movement

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 Implemented
Walk Default movement Implemented
Run Right D / RIGHT Implemented
Run Left A / LEFT Deprecated
Crouch To be determined Future Sprints
Attack To be determined Future Sprints
Item Pick Up To be determined Future Sprints
Use Item To be determined Future Sprints
Switch Item To be determined Future Sprints

Movement Implementation

The movement and action controls are implemented by using the KeyboardPlayerInputComponent class, while using functions to detect key presses and releases. Each key press triggers a certain event and is handled using trigger/listener pairs.

Implementation in KeyboardPlayerInputComponent.java:

/**
   * Triggers player events on specific keycodes.
   *
   * @return whether the input was processed
   * @see InputProcessor#keyDown(int)
   */
  @Override
  public boolean keyDown(int keycode) {
    switch (keycode) {

      case Keys.S:
      case Keys.DOWN:
        walkDirection.add(Vector2Utils.DOWN);
        triggerWalkEvent();
        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;
    }
  }
  /**
   * Triggers player events on specific keycodes.
   *
   * @return whether the input was processed
   * @see InputProcessor#keyUp(int)
   */
  @Override
  public boolean keyUp(int keycode) {
    switch (keycode) {
      case Keys.S:
      case Keys.DOWN:
        walkDirection.sub(Vector2Utils.DOWN);
        triggerWalkEvent();
        return true;
      case Keys.D:
      case Keys.RIGHT:
        walkDirection.sub(Vector2Utils.RIGHT);
        entity.getEvents().trigger(("stopWalkRight"));
        triggerWalkEvent();
        return true;
      case Keys.W:
      case Keys.UP:
        entity.getEvents().trigger("stopJump");
      default:
        return false;
    }
  }

Implementation of Jump

A vertical vector impulse is applied directly to the body of the main player entity to simulate jumping in the game environment.

Implementation in 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);

Relevant Files

KeyboardPlayerInputComponent.java

PlayerActions.java

Animations

The main player character starts off as a static texture until the user presses the key for the right movement. Once the movement is triggered, the texture is removed from the game area and replaced with an AnimationRenderComponent. As movement is triggered, the default movement animation is walking. Once the move right key is pressed, the animation switches to running and all the previous animations are stopped for the entity player.

Animation Implementation

An AnimationRenderComponent is used to define the animation of the character, itโ€™s movement and the FPS:

PlayerFactory.java


    AnimationRenderComponent animator =
            new AnimationRenderComponent(
                    ServiceLocator.getResourceService()
                            .getAsset("images/mpcMovement.atlas",
                                    TextureAtlas.class));
    animator.addAnimation("main_player_run", 0.1f, Animation.PlayMode.LOOP);
    animator.addAnimation("main_player_walk", 0.5f, Animation.PlayMode.LOOP);
    animator.addAnimation("mpc_front", 1f, Animation.PlayMode.LOOP);

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

public void create() {
    animator = this.entity.getComponent(AnimationRenderComponent.class);
    physicsComponent = entity.getComponent(PhysicsComponent.class);
    entity.getEvents().addListener("walk", this::walk);
    entity.getEvents().addListener("walkStop", this::stopWalking);
    entity.getEvents().addListener("walkRight", this::walkRight);
    entity.getEvents().addListener("stopWalkRight", this::stopWalkRight);
    entity.getEvents().addListener("walkLeft", this::walkLeft);
    entity.getEvents().addListener("attack", this::attack);
    entity.getEvents().addListener("jump", this::jump);
  }

The below functions are triggered on key press events and are responsible for switching between the running and walking animation - PlayerActions.java:

/**
   * Updates the player sprite to run right
   */

  void walkRight() {
    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");

  }

Relevant Files

PlayerFactory.java

PlayerActions.java

mpcMovement.atlas

mpcMovement.png

PlayerAnimationController

In-game Animations Demonstration

Walking

Walking

Running

Running

Texture atlas

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

  1. Standing (facing right, static)
  2. Walking (facing right, animated)
  3. Running (facing right, animated)
  4. Standing (facing forward, static)

Generated Texture pack

mpcMovement.png

Generated Texture atlas


mpcMovement.png
size: 4096, 1024
format: RGBA8888
filter: Nearest, Nearest
repeat: none
main_player_right
  rotate: false
  xy: 2717, 2
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: -1
main_player_run
  rotate: false
  xy: 545, 2
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 2
main_player_run
  rotate: false
  xy: 2174, 2
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 1
main_player_walk
  rotate: false
  xy: 2, 2
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 2
main_player_walk
  rotate: false
  xy: 1088, 2
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: 1
mpc_front
  rotate: false
  xy: 1631, 2
  size: 541, 541
  orig: 541, 541
  offset: 0, 0
  index: -1

Relevant Files

mpcMovement.atlas

mpcMovement.png

UML Diagrams

Class Diagrams

PlayerActions

PlayerActions

PlayerAnimationController

PlayerAnimationController

Sequence Diagrams

animateJump

animateJump

animateAttack

animateAttack

animateCrouch

animateCrouch

stopAnimateAll

stopAnimateAll

preAnimationCleanUp

preAnimationCleanUp

PlayerAnimationController

PlayerAnimationController