Created
December 19, 2022 19:26
-
-
Save smdooley/eaa25052117d48d8e03145afb16e0806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/photonstorm/phaser3-examples/blob/master/public/src/game%20objects/graphics/health%20bars%20demo.js | |
import Phaser from "phaser"; | |
export default class HealthBar extends Phaser.GameObjects.Container { | |
constructor(scene, x, y) { | |
super(scene, x, y); | |
this.bar = new Phaser.GameObjects.Graphics(scene); | |
this.x = x; | |
this.y = y; | |
this.value = 100; | |
this.p = 76 / 100; | |
this.draw(); | |
scene.add.existing(this.bar); | |
} | |
decrease(amount) { | |
this.value -= amount; | |
if (this.value < 0) { | |
this.value = 0; | |
} | |
this.draw(); | |
return (this.value === 0); | |
} | |
draw() { | |
this.bar.clear(); | |
// Background | |
this.bar.fillStyle(0x000000); | |
this.bar.fillRect(this.x, this.y, 80, 16); | |
// Health | |
this.bar.fillStyle(0xffffff); | |
this.bar.fillRect(this.x + 2, this.y + 2, 76, 12); | |
if (this.value < 30) { | |
this.bar.fillStyle(0xff0000); | |
} | |
else { | |
this.bar.fillStyle(0x00ff00); | |
} | |
var d = Math.floor(this.p * this.value); | |
this.bar.fillRect(this.x + 2, this.y + 2, d, 12); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment