-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHpBar.js
More file actions
66 lines (53 loc) · 1.38 KB
/
HpBar.js
File metadata and controls
66 lines (53 loc) · 1.38 KB
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
import Phaser from "phaser";
import { clamp } from "../utils/math";
export default class HpBar extends Phaser.GameObjects.Graphics {
static WIDTH = 80;
static HEIGHT = 12;
static BORDER = 2;
constructor(scene, player, maxHp) {
super(scene);
this.m_x = player.x - HpBar.WIDTH / 2;
this.m_y = player.y + 30;
this.m_maxHp = maxHp;
this.m_currentHp = maxHp;
this.draw();
this.setScrollFactor(0);
scene.add.existing(this);
}
increase(amount) {
this.m_currentHp = clamp(this.m_currentHp + amount, 0, this.m_maxHp);
this.draw();
}
decrease(amount) {
this.m_currentHp = clamp(this.m_currentHp - amount, 0, this.m_maxHp);
this.draw();
}
draw() {
this.clear();
// BG
this.fillStyle(0x000000);
this.fillRect(this.m_x, this.m_y, HpBar.WIDTH, HpBar.HEIGHT);
// Health
this.fillStyle(0xffffff);
this.fillRect(
this.m_x + HpBar.BORDER,
this.m_y + HpBar.BORDER,
HpBar.WIDTH - 2 * HpBar.BORDER,
HpBar.HEIGHT - 2 * HpBar.BORDER
);
if (this.m_currentHp < 30) {
this.fillStyle(0xff0000);
} else {
this.fillStyle(0x00ff00);
}
let d = Math.floor(
((HpBar.WIDTH - 2 * HpBar.BORDER) / this.m_maxHp) * this.m_currentHp
);
this.fillRect(
this.m_x + HpBar.BORDER,
this.m_y + HpBar.BORDER,
d,
HpBar.HEIGHT - 2 * HpBar.BORDER
);
}
}