-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHud.js
123 lines (106 loc) · 3.79 KB
/
Hud.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function Hud(descr) {
// Common inherited setup logic from Entity
this.setup(descr);
this.killCount = 0;
};
Hud.prototype = new Entity();
Hud.prototype.score = 0;
Hud.prototype.life = 2;
Hud.prototype.gameOver = false;
Hud.prototype.KEY_FIRE = ' '.charCodeAt(0);
Hud.prototype.charge = 0;
Hud.prototype.highscore = localStorage.getItem("highscore");
Hud.prototype.godModeEnabled = false;
Hud.prototype.incrementScore = function (scoreIncr) {
this.score += scoreIncr;
this.killCount++;
};
Hud.prototype.decrementLife = function () {
this.life -= 1;
if (this.life < 1) {
this.gameOver = true;
}
}
Hud.prototype.updateHighscore = function () {
// Saves highscore using localStorage
if (entityManager._ships[0] !== undefined){
if (entityManager._ships[0].godMode)
this.godModeEnabled = true;
}
if (this.highscore !== null) {
// If there's a defined highscore and the current score is higher;
// set highscore to current score
if (this.score > this.highscore && !this.godModeEnabled) {
localStorage.setItem("highscore", this.score);
}
}
else {
// If there's no defined highscore, set current score as highscore
localStorage.setItem("highscore", this.score);
}
this.highscore = localStorage.getItem("highscore");
}
Hud.prototype.render = function () {
g_ctx.font = "30px Courier New";
// Text in HUD
g_ctx.save();
g_ctx.fillStyle = "white"
g_ctx.shadowColor = "deepskyblue";
g_ctx.shadowOffsetX = 3;
g_ctx.shadowOffsetY = 3;
g_ctx.textAlign = "center";
g_ctx.fillText(this.score, g_canvas.width / 3 + 50, g_canvas.height - 8);
g_ctx.fillText("BEAM", g_canvas.width / 3 + 50, g_canvas.height - 35);
this.updateHighscore();
g_ctx.fillText("Highscore: ", g_canvas.width / 3 + 200, g_canvas.height - 8);
g_ctx.fillText(this.highscore, g_canvas.width / 3 + 330, g_canvas.height - 8);
// Power up timer
if (entityManager._ships[0] !== undefined) { // Check if ship exists
var powerUpTime = entityManager._ships[0].powerUpTime;
powerUpTime = Math.ceil(powerUpTime / 100)
if (entityManager._ships[0].multiGun && entityManager._ships[0].superGun) {
g_ctx.fillText("Super Multigun", 155, 20)
g_ctx.fillText(powerUpTime, 305, 20);
}
else if (entityManager._ships[0].multiGun) {
g_ctx.fillText("Multigun", 100, 20)
g_ctx.fillText(powerUpTime, 190, 20);
}
else if (entityManager._ships[0].superGun) {
g_ctx.fillText("Supergun", 100, 20)
g_ctx.fillText(powerUpTime, 190, 20);
}
}
g_ctx.restore();
// Load texture for the beam bar
g_sprites.beamBar.drawCentredAt(g_ctx,
g_canvas.width / 2.2 + g_sprites.beamBar.width / 4 - 10,
g_canvas.height - 50 + g_sprites.beamBar.height / 4);
// Beam based on this.charge
util.fillBox(ctx, g_canvas.width / 2.2, g_canvas.height - 46, this.charge, 10, 'blue');
// Grey lines on beam bar to indicate where bullet type changes
util.fillBox(ctx, g_canvas.width / 2.2 + 50, g_canvas.height - 50, 3, 14, 'grey');
util.fillBox(ctx, g_canvas.width / 2.2 + 110, g_canvas.height - 50, 3, 14, 'grey');
util.fillBox(ctx, g_canvas.width / 2.2 + 180, g_canvas.height - 50, 3, 14, 'grey');
util.fillBox(ctx, g_canvas.width / 2.2 + 240, g_canvas.height - 50, 3, 14, 'grey');
this.drawLives(g_ctx);
}
Hud.prototype.update = function () {
}
Hud.prototype.incrementBeam = function (du) {
// Gets called in Ship when the player holds down the space bar
if (this.charge < 250) // Max charge
this.charge += 2 * du;
}
Hud.prototype.resetBeam = function () {
// When the player releases the space bar the charge gets reset (or if the player dies)
this.charge = 0;
}
Hud.prototype.drawLives = function (ctx) {
if (g_lives > 0)
g_sprites.ship[2].drawCentredAt(ctx, 30, 700, this.rotation);
if (g_lives > 1)
g_sprites.ship[2].drawCentredAt(ctx, 70, 700, this.rotation);
if (g_lives > 2)
g_sprites.ship[2].drawCentredAt(ctx, 110, 700, this.rotation);
}