-
Notifications
You must be signed in to change notification settings - Fork 4
/
progress-bar.js
94 lines (73 loc) · 1.54 KB
/
progress-bar.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
import Phaser from 'phaser';
export default class ProgressBar extends Phaser.Graphics {
constructor(game, x, y, text, color) {
super(game, x, y);
this.color = color;
this.progress = 1;
this.fixedToCamera = true;
this.alpha = 0;
this.addLabel(text);
this.draw(this.progress);
}
addLabel(text) {
const style = {
fill: Phaser.Color.getWebRGB(this.color),
font: '21px Open Sans'
};
const label = new Phaser.Text(this.game, 0, -30, text, style);
this.addChild(label);
}
draw(progress) {
this.clear();
const width = ProgressBar.WIDTH;
const height = 5;
this.lineStyle(2, this.color, 0.6);
this.drawRect(0, 0, width, height);
this.beginFill(this.color);
this.drawRect(0, 0, progress * width, height);
this.endFill();
}
get value() {
return this.progress;
}
set value(val) {
this.progress = val;
this.draw(this.progress);
}
animate(time) {
this.value = 1;
this.show();
if (this.tween && this.tween.isRunning) {
this.tween.stop();
}
this.tween = this.game.add
.tween(this)
.to(
{
value: 0
},
time
)
.start();
this.tween.onComplete.add(this.hide, this);
}
show() {
this.animateAlpha(1);
}
hide() {
this.animateAlpha(0);
}
animateAlpha(alpha) {
const animationTime = 150;
this.game.add
.tween(this)
.to(
{
alpha
},
animationTime
)
.start();
}
}
ProgressBar.WIDTH = 500;