/
Sprite_Timer.js
60 lines (50 loc) · 1.58 KB
/
Sprite_Timer.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
//-----------------------------------------------------------------------------
// Sprite_Timer
//
// The sprite for displaying the timer.
function Sprite_Timer() {
this.initialize.apply(this, arguments);
}
Sprite_Timer.prototype = Object.create(Sprite.prototype);
Sprite_Timer.prototype.constructor = Sprite_Timer;
Sprite_Timer.prototype.initialize = function() {
Sprite.prototype.initialize.call(this);
this._seconds = 0;
this.createBitmap();
this.update();
};
Sprite_Timer.prototype.createBitmap = function() {
this.bitmap = new Bitmap(96, 48);
this.bitmap.fontSize = 32;
};
Sprite_Timer.prototype.update = function() {
Sprite.prototype.update.call(this);
this.updateBitmap();
this.updatePosition();
this.updateVisibility();
};
Sprite_Timer.prototype.updateBitmap = function() {
if (this._seconds !== $gameTimer.seconds()) {
this._seconds = $gameTimer.seconds();
this.redraw();
}
};
Sprite_Timer.prototype.redraw = function() {
var text = this.timerText();
var width = this.bitmap.width;
var height = this.bitmap.height;
this.bitmap.clear();
this.bitmap.drawText(text, 0, 0, width, height, 'center');
};
Sprite_Timer.prototype.timerText = function() {
var min = Math.floor(this._seconds / 60) % 60;
var sec = this._seconds % 60;
return min.padZero(2) + ':' + sec.padZero(2);
};
Sprite_Timer.prototype.updatePosition = function() {
this.x = Graphics.width - this.bitmap.width;
this.y = 0;
};
Sprite_Timer.prototype.updateVisibility = function() {
this.visible = $gameTimer.isWorking();
};