52 lines
1.1 KiB
JavaScript
52 lines
1.1 KiB
JavaScript
class HeartGauge {
|
|
|
|
constructor(heartManager) {
|
|
this.heartManager = heartManager;
|
|
this.hearts = [];
|
|
|
|
let self = this;
|
|
this.heartManager.addOnChangeCountListener(
|
|
function() { self.onChangeCountListener(); }
|
|
);
|
|
|
|
this.heartManager.addOnChangeMaxCountListener(
|
|
function() { self.onChangeMaxCountListener(); }
|
|
);
|
|
|
|
let startPosX = GAME_SCREEN_SIZE.x - HeartGauge.GAP * 4;
|
|
let posY = 30;
|
|
for(let i = 0; i < HeartManager.HEART_MAX_COUNT; i++) {
|
|
let heart = game.add.sprite(startPosX - i * HeartGauge.GAP, posY, "heart_full");
|
|
heart.anchor.set(0.5);
|
|
heart.scale.set(HeartGauge.HEART_SCALE);
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
HeartGauge.GAP = 35;
|
|
HeartGauge.HEART_SCALE = 0.6; |