48 lines
985 B
JavaScript
48 lines
985 B
JavaScript
class HeartGauge {
|
|
|
|
constructor(heartManager) {
|
|
this.heartManager = heartManager;
|
|
this.hearts = [];
|
|
|
|
this.heartManager.addOnChangeCountListener(
|
|
() => { this.onChangeCountListener(); }
|
|
);
|
|
|
|
this.heartManager.addOnChangeMaxCountListener(
|
|
() => { this.onChangeMaxCountListener(); }
|
|
);
|
|
|
|
let startPosX = 950;
|
|
let posY = 30;
|
|
for(let i = 0; i < HeartManager.HEART_MAX_COUNT; i++) {
|
|
let heart = game.add.sprite(startPosX - i * 30, posY, "heart_full");
|
|
heart.anchor.set(0.5);
|
|
heart.scale.set(1);
|
|
heart.alpha = 0;
|
|
this.hearts.push(heart);
|
|
}
|
|
};
|
|
|
|
onChangeCountListener() {
|
|
this.updateHearts();
|
|
}
|
|
|
|
onChangeMaxCountListener() {
|
|
this.updateHearts();
|
|
}
|
|
|
|
start() {
|
|
this.updateHearts();
|
|
}
|
|
|
|
updateHearts() {
|
|
for(let i = 0; i < this.heartManager.getMaxCount(); i++) {
|
|
if(i < this.heartManager.getCount())
|
|
this.hearts[i].loadTexture("heart_full");
|
|
else
|
|
this.hearts[i].loadTexture("heart_empty");
|
|
|
|
this.hearts[i].alpha = 1;
|
|
}
|
|
}
|
|
} |