summaryrefslogtreecommitdiff
path: root/src/entities/hp_bar.js
blob: a5e5f50a62f2932b44f6094a18a8eb09cc9ed947 (plain)
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
import { WHITE } from "../assets.js"
import { Vector } from "../engine/vector.js"
import { Engine } from "../engine.js"
import { Entity } from "./entity.js"
import { enemy } from "../main.js"

export class HPBar extends Entity {
    constructor() {
        let size = new Vector(Engine.screen.viewport.x, 20)
        super({
            size: new Vector(Engine.screen.viewport.x, 20),
            position: new Vector(size.x / 2, size.y / 2),
            sprite: WHITE,
            pixelated: true,
        })

        this.span.style.zIndex = 1
        this.lasthp = enemy.hp
    }

    update() {
        if (enemy.hp != this.lasthp) {
            this.lasthp = enemy.hp

            let hpScale = enemy.hp / enemy.max_hp
            let viewport = Engine.screen.viewport

            this.size.x = hpScale * viewport.x
            this.position.x = (viewport.x / 2) - (1 - hpScale) / 2 * viewport.x
        }
    }
}