[Add] New animation type.

This commit is contained in:
Ritchie Cunningham 2022-03-09 22:41:34 +00:00
parent 4f08f51ae3
commit 35468417a4
9 changed files with 42 additions and 27 deletions

Binary file not shown.

BIN
bin/tfg/AnimationType.class Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -22,10 +22,7 @@ public class AnimatedSprite {
* Keeps track of time between frame changes. * Keeps track of time between frame changes.
*/ */
private int frameCounter = 0; private int frameCounter = 0;
/**
* Whether or not the sprite is currently animated.
*/
private boolean animating = false;
/** /**
* The position at which the sprite is rendered. * The position at which the sprite is rendered.
*/ */
@ -43,6 +40,11 @@ public class AnimatedSprite {
*/ */
private final Vector2i spriteSize = new Vector2i(32, 48); private final Vector2i spriteSize = new Vector2i(32, 48);
/**
* The current animation to play.
*/
private AnimationType currentAnimationType = AnimationType.NONE;
/** /**
* Create a new animated sprite. * Create a new animated sprite.
* @param s The sprite to animate. * @param s The sprite to animate.
@ -53,21 +55,14 @@ public class AnimatedSprite {
entityLoc = l; entityLoc = l;
} }
/**
* Start the animation.
*/
public void startWalkingAnimation() {
startAnimation();
}
/** /**
* Update animation. * Update animation.
*/ */
public void animate() { public void animate() {
if(animating) { /* Then update animation. */ if(currentAnimationType != AnimationType.NONE) {
if(frameCounter >= animationSpeed) { /* If 15Hz has passed. */ if(frameCounter >= animationSpeed) { /* If 15Hz has passed. */
stopAnimation(); stopAnimation();
} else { } else if(currentAnimationType == AnimationType.WALKING) {
/* Get the position between old and new. */ /* Get the position between old and new. */
spritePosition = entityLoc.interpolate(animationSpeed, frameCounter); spritePosition = entityLoc.interpolate(animationSpeed, frameCounter);
Vector2i entityPos = entityLoc.getPosition(); /* Get the entity position. */ Vector2i entityPos = entityLoc.getPosition(); /* Get the entity position. */
@ -86,6 +81,17 @@ public class AnimatedSprite {
} else if(change >= .75f && change <= 1.0f) { } else if(change >= .75f && change <= 1.0f) {
animationFrame = 3; animationFrame = 3;
} }
} else if(currentAnimationType== AnimationType.STATIONARY_WALK) {
float steps = animationSpeed / 4;
if(frameCounter >= 0.f && frameCounter < steps) {
animationFrame = 0;
} else if(frameCounter >= steps && frameCounter < (2*steps)) {
animationFrame = 1;
} else if(frameCounter >= (2*steps)&&frameCounter<(3*steps)) {
animationFrame = 2;
} else if(frameCounter >= (3*steps)&&frameCounter<=(4*steps)) {
animationFrame = 3;
}
} }
frameCounter++; /* Increment on each update to keep track of time. */ frameCounter++; /* Increment on each update to keep track of time. */
} }
@ -96,7 +102,7 @@ public class AnimatedSprite {
*/ */
private void stopAnimation() { private void stopAnimation() {
/* Reset the following. */ /* Reset the following. */
animating = false; currentAnimationType = AnimationType.NONE;
frameCounter = 0; frameCounter = 0;
animationFrame = 0; animationFrame = 0;
} }
@ -104,8 +110,8 @@ public class AnimatedSprite {
/** /**
* Start the animation. * Start the animation.
*/ */
private void startAnimation() { public void startAnimation(AnimationType t) {
animating = true; currentAnimationType = t;
} }
/** /**
@ -113,7 +119,7 @@ public class AnimatedSprite {
* @return If the animation finished. * @return If the animation finished.
*/ */
public boolean finishedAnimating() { public boolean finishedAnimating() {
return !animating; return currentAnimationType == AnimationType.NONE;
} }
/** /**

View File

@ -0,0 +1,7 @@
package tfg;
public enum AnimationType {
NONE,
WALKING,
STATIONARY_WALK
};

View File

@ -66,6 +66,7 @@ public class Game {
windowTitle); windowTitle);
player.changeMap(new Map(10, 10, Tile.SAND)); player.changeMap(new Map(10, 10, Tile.SAND));
camera = new Camera(window); camera = new Camera(window);
// window.setFramerateLimit(60);
} }
/** /**

View File

@ -17,10 +17,6 @@ import java.nio.file.Paths;
* @author Ritchie Cunningham * @author Ritchie Cunningham
*/ */
public class Player extends Entity implements Drawable { public class Player extends Entity implements Drawable {
/**
* The players location.
*/
private Location playerLoc;
/** /**
* The texture for the sprite. * The texture for the sprite.
*/ */
@ -104,9 +100,14 @@ public class Player extends Entity implements Drawable {
entityLoc.setDirection(d); entityLoc.setDirection(d);
if(currentMap.isValidLocation(newLoc)) { if(currentMap.isValidLocation(newLoc)) {
currentAction = PlayerAction.MOVING; currentAction = PlayerAction.MOVING;
entityLoc.setDirection(d); playerSprite.startAnimation(AnimationType.WALKING);
playerSprite.startWalkingAnimation();
} else if(cannotMove.getStatus() == SoundSource.Status.STOPPED) { } else if(cannotMove.getStatus() == SoundSource.Status.STOPPED) {
/*
* Play the stationary walk animation.
* It appears like the player is trying to move, but can't.
*/
playerSprite.startAnimation(AnimationType.STATIONARY_WALK);
/* Play an annoying sound. */
cannotMove.play(); cannotMove.play();
} }
} }
@ -124,10 +125,10 @@ public class Player extends Entity implements Drawable {
entityLoc = entityLoc.getRelativeLocation(entityLoc.getDirection()); entityLoc = entityLoc.getRelativeLocation(entityLoc.getDirection());
/* Update the sprite with new location. */ /* Update the sprite with new location. */
playerSprite.updatePosition(entityLoc); playerSprite.updatePosition(entityLoc);
} else {
playerSprite.animate(); /* Proceed with the animation. */
} }
} }
/* Update the animation, if there is no current animation, ignore. */
playerSprite.animate();
} }
/** /**

View File

@ -20,7 +20,7 @@ public enum Tile {
/** /**
* Get the coords of a specific tile from the tile-set image. * Get the coords of a specific tile from the tile-set image.
* @param t The tole to get the coordinates of. * @param t The tile to get the coordinates of.
* @return Coords of the tile image in the tile-set. * @return Coords of the tile image in the tile-set.
*/ */
public static Vector2f getTextureCoords(Tile t) { public static Vector2f getTextureCoords(Tile t) {