-
Notifications
You must be signed in to change notification settings - Fork 21
/
screen.js
36 lines (29 loc) · 1.1 KB
/
screen.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
var gamejs = require('gamejs');
var Hud = function (size) {
this.health = 100;
this.score = 0;
this.level = 0;
this.font = new gamejs.font.Font('20px ubuntu, sans-serif');
this.healthIcon = gamejs.image.load('images/health-hud.png');
this.size = size;
var cachedScore = -1;
var scoreSurface = null;
var cachedLevel = -1;
var levelSurface = null;
this.draw = function (surface) {
if (this.score != cachedScore) {
cachedScore = this.score;
scoreSurface = this.font.render(this.score, "#fff");
}
if (this.level != cachedLevel) {
cachedLevel = this.level;
levelSurface = this.font.render("Level " + (this.level + 1), "#fff");
}
surface.blit(this.healthIcon, [8, 8]);
gamejs.draw.rect(surface, "#0c0", new gamejs.Rect([26 + 16, 19, Math.max(0, this.health), 4]), 0);
surface.blit(scoreSurface, [this.size[0] - scoreSurface.getSize()[0] - 8, 8]);
surface.blit(levelSurface, [this.size[0] - levelSurface.getSize()[0] - 8, scoreSurface.getSize()[1] + 8]);
};
return this;
};
exports.Hud = Hud