-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscore.js
executable file
·79 lines (71 loc) · 2.39 KB
/
score.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
BonusImg = new Image();
BonusImg.src = 'images/bonus.png';
function Score(game){
this.Game = game
this.canvasCtx = null
this.canvas = null
this.scoreFlashDuration = 0;
this.scoreFlashValue = 0;
this.SCORE_FLASH_FRAMES = 240;
this.SCORE_FLASH_SPEED = 30;
this.score = 0;
this.highScore = 0;
this.bonus = 0;
this.addBonus = false;
this.addBonusDuration = 0;
this.init()
}
Score.prototype = {
init: function(){
this.canvasCtx = this.Game.canvasCtx
this.canvas = this.canvasCtx.canvas
this.score = 0;
this.bonus = 0;
},
draw: function(frameCount){
this.canvasCtx.font = "20px Monospace";
this.canvasCtx.fillStyle = "black";
this.canvasCtx.textAlign = "left";
if (this.Game.gameOver) {
this.canvasCtx.fillText(this.score, 10, 20);
} else {
this.score = this.bonus + Math.floor(frameCount/10);
if (this.score == 100 || this.score == 250 || this.score == 500 || this.score == 750 || this.score == 1000 || this.score == 1500 || (this.score % 1000 == 0 && this.score > 0)) {
this.scoreFlashDuration = this.SCORE_FLASH_FRAMES;
this.scoreFlashValue = this.score;
}
if (this.scoreFlashDuration > 0) {
this.scoreFlashDuration--;
if (Math.floor(this.scoreFlashDuration / this.SCORE_FLASH_SPEED) % 2 == 1) {
this.canvasCtx.fillText(this.scoreFlashValue, 10, 20);
}
} else {
this.canvasCtx.fillText(this.score, 10, 20);
}
}
this.canvasCtx.textAlign = "right";
this.canvasCtx.fillText("HI " + this.highScore, canvas.width - 10, 20)
if(this.addBonus){
this.addBonusDuration = 80;
this.addBonus = false;
}
if(this.addBonusDuration > 0) {
this.displayBonus(this.Game.Player.charX-5, this.Game.Player.charY-18);
this.addBonusDuration--;
}
},
update: function(frameCount){
this.draw(frameCount)
},
reset: function(){
this.init()
},
updateHighScore: function() {
if (this.score > this.highScore) {
this.highScore = this.score;
}
},
displayBonus: function(x, y){
this.canvasCtx.drawImage(BonusImg, x, y, 40, 14);
}
}