Sprint 2 Main Character Interaction and Movement Animations - UQdeco2800/2021-ext-studio-2 GitHub Wiki
- Animation Implementation and Controls
- Refactoring Player Animation Controller
- Attack
- Jump
- Crouch
- Item Pickup
- Texture Atlas
- Testing
- UML Diagrams
- Relevant Files
An AnimationRenderComponent is used to define all the animations of the character, their playmode, and FPS.
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.
Action | Key Control | Comment |
---|---|---|
Jump | W / UP | Implemeted |
Walk | Default movement | Implemented |
Run Right | D / RIGHT | Implemeted |
Deprecated | ||
Crouch | S / DOWN | Implemeted |
Attack | SPACE | Implemeted |
Item PickUp | X | Implemeted |
Use Item | E | Future Sprints |
Switch Item | To be determined | Future Sprints |
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();
}
}
The MPC attack interaction is mapped to the spacebar
. Once the attack action is triggered, the attack animation is run.
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");
}
To initiate the jump movement and animation of the player, 'W' or Arrow Key 'UP'
is used to trigger the movement and the animation.
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);
...
/**
* 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");
}
The MPC Crouch interaction is mapped to the S/Arrow `Down` keys
. Once the crouch action is triggered, the crouch animation is run.
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");
}
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.
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");
}
The atlas that was used for creating the animations was generated using a texture packer. The atlas has player representations in the following states:
- Standing (facing right, static)
- Standing (facing forward, static)
- Walking (facing right, animated)
- Running (facing right, animated)
- Attack (facing right, animated)
- Crouch (facing right, animated)
- Jump (facing right, animated)
- Item PickUp (facing right, animated)
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"));
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
Player Factory + PlayerActions + PlayerAnimationController + KeyboardPlayerInputComponent
animateAttack
animateCrouch
animateWalk
animateRight
animateJump
animatePickUp
PlayerActions - create()
PlayerAnimationController - create()
PlayerAnimationController.java