Skip to content

Commit

Permalink
Show replay message with score commentary and reload the page when cl…
Browse files Browse the repository at this point in the history
…icked. Fix #29
  • Loading branch information
MattSurabian committed Sep 28, 2017
1 parent d21b644 commit d78de60
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
53 changes: 48 additions & 5 deletions src/modules/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,21 +343,64 @@ class Game {
win() {
sound.play('champ');
this.gameStatus = 'You Win!';
this.showReplay(this.getScoreMessage());
}

loss() {
sound.play('loserSound');
this.gameStatus = 'You Lose!';
this.showReplay(this.getScoreMessage());
}

getScoreMessage() {
let scoreMessage;

if (this.score === 9400) {
scoreMessage = 'Flawless victory.';
}

if (this.score < 9400) {
scoreMessage = 'Close to perfection.';
}

if (this.score <= 9000) {
scoreMessage = 'Truly impressive score.';
}

if (this.score <= 8000) {
scoreMessage = 'Solid score.'
}

if (this.score <= 6000) {
scoreMessage = 'Yikes.';
}

return scoreMessage;
}

showReplay(replayText) {
this.stage.hud.createTextBox('replayButton', {
location: Stage.replayButtonLocation()
});
this.stage.hud.replayButton = replayText + ' Play Again?';
this.bindInteractions();

}

handleClick(event) {
if (!this.outOfAmmo()) {
sound.play('gunSound');
this.bullets -= 1;
this.updateScore(this.stage.shotsFired({
let clickPoint = {
x: event.data.global.x,
y: event.data.global.y
}));
};

if (!this.stage.hud.replayButton && !this.outOfAmmo()) {
sound.play('gunSound');
this.bullets -= 1;
this.updateScore(this.stage.shotsFired(clickPoint));
}

if (this.stage.hud.replayButton && this.stage.clickedReplay(clickPoint)) {
window.location.reload();
}
}

Expand Down
19 changes: 16 additions & 3 deletions src/modules/Stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const HUD_LOCATIONS = {
SCORE: new Point(MAX_X - 10, 10),
WAVE_STATUS: new Point(MAX_X - 10, MAX_Y - 20),
GAME_STATUS: new Point(MAX_X / 2, MAX_Y * 0.45),
REPLAY_BUTTON: new Point(MAX_X / 2, MAX_Y * 0.56),
BULLET_STATUS: new Point(10, 10),
DEAD_DUCK_STATUS: new Point(10, MAX_Y * 0.91),
MISSED_DUCK_STATUS: new Point(10, MAX_Y * 0.95)
Expand Down Expand Up @@ -75,6 +76,10 @@ class Stage extends Container {
return HUD_LOCATIONS.GAME_STATUS;
}

static replayButtonLocation() {
return HUD_LOCATIONS.REPLAY_BUTTON;
}

static bulletStatusBoxLocation() {
return HUD_LOCATIONS.BULLET_STATUS;
}
Expand Down Expand Up @@ -185,12 +190,10 @@ class Stage extends Container {
this.flashScreen.visible = false;
}, FLASH_MS);

clickPoint.x /= this.scale.x;
clickPoint.y /= this.scale.y;
let ducksShot = 0;
for (let i = 0; i < this.ducks.length; i++) {
const duck = this.ducks[i];
if (duck.alive && Utils.pointDistance(duck.position, clickPoint) < 60) {
if (duck.alive && Utils.pointDistance(duck.position, this.getScaledClickLocation(clickPoint)) < 60) {
ducksShot++;
duck.shot();
duck.timeline.add(() => {
Expand All @@ -201,6 +204,16 @@ class Stage extends Container {
return ducksShot;
}

clickedReplay(clickPoint) {
return Utils.pointDistance(this.getScaledClickLocation(clickPoint), HUD_LOCATIONS.REPLAY_BUTTON) < 200;
}

getScaledClickLocation(clickPoint) {
return {
x: clickPoint.x / this.scale.x,
y: clickPoint.y / this.scale.y
};
}
/**
* flyAway
* Helper method that causes the sky to change color and the ducks to fly away
Expand Down

0 comments on commit d78de60

Please sign in to comment.