Skip to content

Commit

Permalink
feat(rhino): rhino celebrating
Browse files Browse the repository at this point in the history
  • Loading branch information
programad committed Aug 30, 2020
1 parent 5b5e68f commit e2c3be1
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 40 deletions.
26 changes: 16 additions & 10 deletions src/Core/AnimationController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gameManager } from '../Core/GameManager';

export class AnimationController {
animationName = '';
playing = false;
loop = false;
assetNames = [];
Expand All @@ -15,25 +16,30 @@ export class AnimationController {
let currentFrame = gameManager.getCurrentFrame();
if (currentFrame > this.lastFrame) {
this.lastFrame = currentFrame;

this.currentFrameIndex++;

if (this.loop) {
this.currentFrameIndex = (this.currentFrameIndex % this.assetNames.length + this.assetNames.length) % this.assetNames.length;
} else {
if (this.currentFrameIndex >= this.assetNames.length) {
this.currentFrameIndex = this.assetNames.length - 1;
this.playing = false;
if (this.assetNames.length > 0) {
if (this.loop) {
this.currentFrameIndex = (this.currentFrameIndex % this.assetNames.length + this.assetNames.length) % this.assetNames.length;
} else {
if (this.currentFrameIndex >= this.assetNames.length) {
this.currentFrameIndex = this.assetNames.length - 1;
this.playing = false;
this.animationName = '';
this.assetNames = [];
}
}
}
}
}

play(assets, loop) {
if (assets) {
play(newAnimationName, newAssets, loop) {
if (newAssets && (this.animationName !== newAnimationName || !loop)) {
this.animationName = newAnimationName
this.loop = loop ? loop : false;
this.playing = true;
this.assetNames = assets;
this.assetNames = newAssets;
this.currentFrameIndex = 0;
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Core/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ export class Game {
if (this.gameState != Constants.GAME_STATE.PAUSED) {
gameManager.updateTimer();

this.skier.update();
if (this.gameState === Constants.GAME_STATE.RUNNING) {
this.skier.update();

this.obstacleManager.placeNewObstacle(this.gameWindow, previousGameWindow);

this.skier.checkIfSkierHitObstacle(this.obstacleManager, this.assetManager);
this.obstacleManager.placeNewObstacle(this.gameWindow, previousGameWindow);

this.skier.checkIfSkierHitObstacle(this.obstacleManager, this.assetManager);
}

this.checkRhinoSpawn();
if (this.rhino) {
Expand Down
2 changes: 1 addition & 1 deletion src/Core/GameManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class GameManager {
gameState = Constants.GAME_STATE.RUNNING;
lastCalledTime = performance.now();
currentFrame = 0;
animationFrameDuration = 8;
animationFrameDuration = 10;
fps = 0;
score = 0;
speedModifier = 1;
Expand Down
8 changes: 5 additions & 3 deletions src/Entities/Character.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ export class Character extends Entity {
this.direction = direction;
}

animate(){
this.animationController.update();
}

update() {
if (this.canMove) {
this.move();
}

this.animationController.update();

this.isJumping = this.animationController.playing;
this.animate();

this.updateAsset();
}
Expand Down
33 changes: 25 additions & 8 deletions src/Entities/Rhino.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export class Rhino extends Character {
runLeftAnimation = [Constants.RHINO_RUN_LEFT, Constants.RHINO_RUN_LEFT_2];
runRightAnimation = [Constants.RHINO_RUN_RIGHT, Constants.RHINO_RUN_RIGHT_2];
eatingAnimation = [Constants.RHINO_LIFT, Constants.RHINO_LIFT_MOUTH_OPEN, Constants.RHINO_LIFT_EAT_1, Constants.RHINO_LIFT_EAT_2, Constants.RHINO_LIFT_EAT_3, Constants.RHINO_LIFT_EAT_4];
celebratingAnimation = [Constants.RHINO_LIFT_EAT_3, Constants.RHINO_LIFT_EAT_4];

defaultAssetName = Constants.RHINO_DEFAULT;
assetName = Constants.RHINO_DEFAULT;

direction = Constants.CHARACTER_DIRECTIONS.DOWN;
speedX = Constants.RHINO_STARTING_SPEED;
speedX = Constants.RHINO_STARTING_SPEED;
originalSpeedX = Constants.RHINO_STARTING_SPEED;
speedY = Constants.RHINO_STARTING_SPEED;
isEating = false;
Expand All @@ -26,11 +28,26 @@ export class Rhino extends Character {

this.animationController = new AnimationController(this.name);

this.animationController.play(this.runLeftAnimation, true);
this.animationController.play('runleft', this.runLeftAnimation, true);
}

updateAsset() {
this.assetName = this.animationController.getCurrentAssetName();

if (!this.assetName) {
this.assetName = this.defaultAssetName;
}
}

animate() {
if (gameManager.getGameState() == Constants.GAME_STATE.OVER) {
this.isEating = this.animationController.playing && this.animationController.animationName === 'eating';
if (!this.isEating) {
this.animationController.play('celebrate', this.celebratingAnimation, true);
}
}

this.animationController.update();
}

move() {
Expand All @@ -40,8 +57,10 @@ export class Rhino extends Character {
if (this.x === this.target.x) {
this.turnDown();
} else if (this.target.x < this.x) {
this.animationController.play('runleft', this.runLeftAnimation, true);
this.turnLeft();
} else if (this.target.x > this.x) {
this.animationController.play('runright', this.runRightAnimation, true);
this.turnRight();
}

Expand All @@ -52,14 +71,12 @@ export class Rhino extends Character {
switchDirection() {
switch (this.direction) {
case Constants.CHARACTER_DIRECTIONS.LEFT_DOWN:
this.animationController.play(this.runLeftAnimation, true);
this.moveLeftDown();
break;
case Constants.CHARACTER_DIRECTIONS.DOWN:
this.moveDown();
break;
case Constants.CHARACTER_DIRECTIONS.RIGHT_DOWN:
this.animationController.play(this.runRightAnimation, true);
this.moveRightDown();
break;
}
Expand All @@ -76,7 +93,7 @@ export class Rhino extends Character {

moveRightDown() {
let amountToMoveX = this.setAmountToMove();

if (this.canMove) {
this.x += amountToMoveX;
this.y += this.speedY * this.diagonalFactor;
Expand Down Expand Up @@ -121,8 +138,8 @@ export class Rhino extends Character {

eat() {
if (!this.isEating) {
this.animationController.play('eating', this.eatingAnimation, false);
this.isEating = true;
this.animationController.play(this.eatingAnimation, false);

this.updateAsset();
}
Expand All @@ -138,10 +155,10 @@ export class Rhino extends Character {

const collision = intersectTwoRects(this.myBounds, targetBounds);

if (collision) {
if (collision && gameManager.getGameState() !== Constants.GAME_STATE.OVER) {
this.stop();
this.target.stop();

this.eat();

gameManager.gameOver();
Expand Down
15 changes: 11 additions & 4 deletions src/Entities/Skier.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class Skier extends Character {
frontFlipAnimation = [Constants.SKIER_JUMP_START, Constants.SKIER_JUMP_CLIMAX, Constants.SKIER_JUMP_ROLL, Constants.SKIER_JUMP_ALMOST, Constants.SKIER_JUMP_LANDING];
backFlipAnimation = [Constants.SKIER_JUMP_START, Constants.SKIER_JUMP_ALMOST, Constants.SKIER_JUMP_ROLL, Constants.SKIER_JUMP_CLIMAX, Constants.SKIER_JUMP_LANDING];

defaultAssetName = Constants.SKIER_DOWN;
assetName = Constants.SKIER_DOWN;

direction = Constants.CHARACTER_DIRECTIONS.DOWN;
Expand All @@ -31,9 +32,15 @@ export class Skier extends Character {
} else {
this.assetName = Constants.SKIER_DIRECTION_ASSET[this.direction];
}

if (!this.assetName) {
this.assetName = this.defaultAssetName;
}
}

move() {
move() {
this.isJumping = this.animationController.playing;

this.calculateSpeed();

switch (this.direction) {
Expand Down Expand Up @@ -105,7 +112,7 @@ export class Skier extends Character {

if (this.direction !== Constants.CHARACTER_DIRECTIONS.CRASH) {
this.isJumping = true;
this.animationController.play(this.jumpAnimation);
this.animationController.play('jump', this.jumpAnimation);

this.updateAsset();
}
Expand All @@ -122,9 +129,9 @@ export class Skier extends Character {

let flipChance = this.getFlipChance();
if (flipChance > 7) {
this.animationController.play(this.backFlipAnimation);
this.animationController.play('backflip', this.backFlipAnimation);
} else {
this.animationController.play(this.frontFlipAnimation);
this.animationController.play('frontflip', this.frontFlipAnimation);
}

this.updateAsset();
Expand Down
18 changes: 9 additions & 9 deletions src/Test/AnimationController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('no loop animation controller tests', () => {
});

test('the animation should be played', () => {
animationController.play(longAnimation);
animationController.play('animation1', longAnimation);

expect(animationController.playing).not.toBeFalsy();
});
Expand All @@ -36,7 +36,7 @@ describe('no loop animation controller tests', () => {
});

test('should return the first frame', () => {
animationController.play(longAnimation);
animationController.play('animation1', longAnimation);

let firstFrame = longAnimation[0];
let frame = animationController.getCurrentAssetName();
Expand All @@ -46,7 +46,7 @@ describe('no loop animation controller tests', () => {
});

test('should return the second frame', () => {
animationController.play(longAnimation);
animationController.play('animation1', longAnimation);

gameManager.timer = animationController.animationInterval;
gameManager.getCurrentFrame = jest.fn();
Expand All @@ -62,7 +62,7 @@ describe('no loop animation controller tests', () => {
});

test('should return the last frame', () => {
animationController.play(longAnimation);
animationController.play('animation1', longAnimation);

gameManager.timer = animationController.animationInterval;
animationController.currentFrameIndex = longAnimation.length - 1;
Expand All @@ -76,7 +76,7 @@ describe('no loop animation controller tests', () => {
});

test('should return the single frame', () => {
animationController.play(singleFrameAnimation);
animationController.play('animation1', singleFrameAnimation);

gameManager.timer = animationController.animationInterval;
animationController.currentFrameIndex = singleFrameAnimation.length - 1;
Expand All @@ -90,7 +90,7 @@ describe('no loop animation controller tests', () => {
});

test('should return the last frame', () => {
animationController.play(longAnimation);
animationController.play('animation1', longAnimation);

animationController.currentFrameIndex = longAnimation.length - 1;
gameManager.timer = animationController.animationInterval;
Expand All @@ -107,7 +107,7 @@ describe('no loop animation controller tests', () => {
});

test('should not return a frame', () => {
animationController.play(longAnimation);
animationController.play('animation1', longAnimation);

animationController.currentFrameIndex = longAnimation.length;
animationController.update();
Expand All @@ -131,7 +131,7 @@ describe('when looping animations', () => {
gameManager.getCurrentFrame = jest.fn();
gameManager.getCurrentFrame.mockReturnValueOnce(1);

animationController.play(runLeftAnimation, true);
animationController.play('animation1', runLeftAnimation, true);

let firstFrame = runLeftAnimation[0];
let frame = animationController.getCurrentAssetName();
Expand All @@ -145,7 +145,7 @@ describe('when looping animations', () => {
gameManager.getCurrentFrame = jest.fn();
gameManager.getCurrentFrame.mockReturnValueOnce(2);

animationController.play(runLeftAnimation, true);
animationController.play('animation1', runLeftAnimation, true);

animationController.update();

Expand Down
2 changes: 1 addition & 1 deletion src/Test/Skier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ describe("Testing Skier", () => {

skier.flip();

expect(skier.animationController.play).toHaveBeenCalledWith(skier.backFlipAnimation);
expect(skier.animationController.play).toHaveBeenCalledWith('backflip', skier.backFlipAnimation);
});
});

Expand Down

0 comments on commit e2c3be1

Please sign in to comment.