Add: show heart_full, heart_empty
This commit is contained in:
+38
-12
@@ -2,21 +2,47 @@ class HeartGauge {
|
||||
|
||||
constructor(heartManager) {
|
||||
this.heartManager = heartManager;
|
||||
this.hearts = [];
|
||||
|
||||
this.heartManager.addOnChangeHeartListener(
|
||||
() => {
|
||||
console.log(this.heartManager.getHearts());
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
HeartGauge.DEFAULT_TEXT_FONT = {
|
||||
font: "30px Arial",
|
||||
boundsAlignH: "center", // left, center. right
|
||||
boundsAlignV: "middle", // top, middle, bottom
|
||||
fill: "#fff"
|
||||
};
|
||||
onChangeMaxCountListener() {
|
||||
this.updateHearts();
|
||||
}
|
||||
|
||||
HeartGauge.MOVE_UP_AMOUNT_PX = 50;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
class HeartManager {
|
||||
|
||||
constructor() {
|
||||
this.countHearts = HeartManager.DEFAULT_MAX_COUNT;
|
||||
this.countMaxHearts = HeartManager.DEFAULT_MAX_COUNT;
|
||||
this.countHearts = HeartManager.DEFAULT_COUNT;
|
||||
this.countMaxHearts = HeartManager.DEFAULT_COUNT;
|
||||
|
||||
this.onChangeHeartListeners = [];
|
||||
this.onChangeCountListeners = [];
|
||||
this.onChangeMaxCountListeners = [];
|
||||
this.onChangeGameOverListeners = [];
|
||||
}
|
||||
|
||||
@@ -16,14 +17,24 @@ class HeartManager {
|
||||
}
|
||||
}
|
||||
|
||||
addOnChangeHeartListener(onChangeFunction) {
|
||||
this.onChangeHeartListeners.push(onChangeFunction);
|
||||
addOnChangeCountListener(onChangeFunction) {
|
||||
this.onChangeCountListeners.push(onChangeFunction);
|
||||
}
|
||||
|
||||
removeOnChangeHeartListener(onChangeFunction) {
|
||||
let itemIndex = this.onChangeHeartListeners.indexOf(onChangeFunction);
|
||||
removeOnChangeCountListener(onChangeFunction) {
|
||||
let itemIndex = this.onChangeCountListeners.indexOf(onChangeFunction);
|
||||
if(itemIndex > -1)
|
||||
this.onChangeHeartListeners.splice(itemIndex, 1);
|
||||
this.onChangeCountListeners.splice(itemIndex, 1);
|
||||
}
|
||||
|
||||
addOnChangeMaxCountListener(onChangeMaxFunction) {
|
||||
this.onChangeMaxCountListeners.push(onChangeMaxFunction);
|
||||
}
|
||||
|
||||
removeOnChangeMaxCountListener(onChangeMaxFunction) {
|
||||
let itemIndex = this.onChangeMaxCountListeners.indexOf(onChangeMaxFunction);
|
||||
if(itemIndex > -1)
|
||||
this.onChangeMaxCountListeners.splice(itemIndex, 1);
|
||||
}
|
||||
|
||||
addOnChangeGameOverListener(onChangeGameOverFunction) {
|
||||
@@ -37,12 +48,13 @@ class HeartManager {
|
||||
}
|
||||
|
||||
removeAllListeners() {
|
||||
this.onChangeHeartListeners = [];
|
||||
this.onChangeCountListeners = [];
|
||||
this.onChangeMaxCountListeners = [];
|
||||
this.onChangeGameOverListeners = [];
|
||||
}
|
||||
|
||||
|
||||
setHearts(amount) {
|
||||
setCount(amount) {
|
||||
let beforeCountHearts = this.countHearts;
|
||||
this.countHearts = amount;
|
||||
if(this.countHearts < 0)
|
||||
@@ -51,25 +63,35 @@ class HeartManager {
|
||||
this.countHearts = this.countMaxHearts;
|
||||
|
||||
if(beforeCountHearts !== this.countHearts)
|
||||
this.callListener(this.onChangeHeartListeners);
|
||||
this.callListener(this.onChangeCountListeners);
|
||||
|
||||
if(this.countHearts === 0)
|
||||
this.callListener(this.onChangeGameOverListeners);
|
||||
}
|
||||
|
||||
setMaxHearts(amount) {
|
||||
this.countMaxHearts = amount;
|
||||
|
||||
if(this.countHearts > this.countMaxHearts)
|
||||
this.countHearts = this.countMaxHearts;
|
||||
}
|
||||
|
||||
getHearts() {
|
||||
getCount() {
|
||||
return this.countHearts;
|
||||
}
|
||||
|
||||
setMaxCount(amount) {
|
||||
let beforeCountHearts = this.countMaxHearts;
|
||||
this.countMaxHearts = amount;
|
||||
if(this.countMaxHearts > HeartManager.HEART_MAX_COUNT)
|
||||
this.countMaxHearts = HeartManager.HEART_MAX_COUNT;
|
||||
|
||||
if(beforeCountHearts !== this.countMaxHearts)
|
||||
this.callListener(this.onChangeMaxCountListeners);
|
||||
|
||||
if(this.countHearts > this.countMaxHearts)
|
||||
this.setCount(this.countMaxHearts);
|
||||
}
|
||||
|
||||
getMaxCount() {
|
||||
return this.countMaxHearts;
|
||||
}
|
||||
|
||||
addHearts(amount) {
|
||||
this.setHearts(this.countHearts + amount);
|
||||
this.setCount(this.countHearts + amount);
|
||||
|
||||
}
|
||||
|
||||
@@ -77,19 +99,7 @@ class HeartManager {
|
||||
this.addHearts(-1 * amount);
|
||||
}
|
||||
|
||||
setScore(value) {
|
||||
let beforeScore = this.score;
|
||||
this.score = value;
|
||||
|
||||
// update score
|
||||
if(beforeScore !== this.score)
|
||||
this.callListener(this.onChangeHeartListeners);
|
||||
|
||||
// update high score
|
||||
if(this.score > this.highScore)
|
||||
this.callListener(this.onChangeGameOverListeners);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
HeartManager.DEFAULT_MAX_COUNT = 3;
|
||||
HeartManager.DEFAULT_COUNT = 3;
|
||||
HeartManager.HEART_MAX_COUNT = 5;
|
||||
Reference in New Issue
Block a user