-
Notifications
You must be signed in to change notification settings - Fork 0
/
bar.js
60 lines (51 loc) · 1.69 KB
/
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
class bar {
constructor(scene, sprite) {
this.scene = scene;
this.sprite = sprite;
this.npc = sprite.npc;
this.border_width = 2;
this.bar_border = this.scene.add.graphics();
this.bar_border.fillStyle(0xffffff, 1);
this.bar_border.fillRect(
-20 - this.border_width,
-this.npc.sprite.height - this.border_width,
this.npc.sprite.width - 5 + this.border_width * 2,
5 + this.border_width * 2,
);
this.bar = this.scene.add.graphics();
}
update() {
this.bar_border.x = this.sprite.x;
this.bar_border.y = this.sprite.y;
const hunger_percent = Math.min(Math.max(this.npc.hunger / 150, 0.0), 1.0);
this.bar.clear();
this.bar.fillStyle(interpolate("#ff0000", "#00ff00", hunger_percent), 1);
this.bar.fillRect(
-20,
-this.sprite.height,
(this.sprite.width - 5) * hunger_percent,
5,
);
this.bar.x = this.sprite.x;
this.bar.y = this.sprite.y;
}
destroy() {
this.bar_border.destroy();
this.bar.destroy();
}
}
function interpolate(color1, color2, percent) {
// Convert the hex colors to RGB values
const r1 = parseInt(color1.substring(1, 3), 16);
const g1 = parseInt(color1.substring(3, 5), 16);
const b1 = parseInt(color1.substring(5, 7), 16);
const r2 = parseInt(color2.substring(1, 3), 16);
const g2 = parseInt(color2.substring(3, 5), 16);
const b2 = parseInt(color2.substring(5, 7), 16);
// Interpolate the RGB values
const r = Math.round(r1 + (r2 - r1) * percent);
const g = Math.round(g1 + (g2 - g1) * percent);
const b = Math.round(b1 + (b2 - b1) * percent);
// Convert the interpolated RGB values back to a hex color
return (1 << 24) + (r << 16) + (g << 8) + b;
}