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;
|
||||
@@ -6,6 +6,8 @@ class Game {
|
||||
create() {
|
||||
this.game.stage.backgroundColor = '#4d4d4d';
|
||||
|
||||
this.initListeners();
|
||||
|
||||
// top
|
||||
let backButton = new BackButton( () => {
|
||||
location.href = '../../web/client/menu_app.html';
|
||||
@@ -13,11 +15,7 @@ class Game {
|
||||
|
||||
let scoreBoard = new ScoreBoard();
|
||||
let heartGauge = new HeartGauge(heartManager);
|
||||
heartManager.addOnChangeGameOverListener(
|
||||
() => {
|
||||
this.gameOver();
|
||||
}
|
||||
);
|
||||
heartGauge.start();
|
||||
|
||||
let fullscreenButton = new FullscreenButton(this.game);
|
||||
|
||||
@@ -51,6 +49,11 @@ class Game {
|
||||
screenBottom.printBottomRightText(sessionStorageManager.playerName);
|
||||
|
||||
|
||||
this.startGame();
|
||||
// this.countDown();
|
||||
}
|
||||
|
||||
initListeners() {
|
||||
scoreManager.addOnChangeScoreListener( score => {
|
||||
console.log("onChangeScore : " + score);
|
||||
scoreBoard.printScore(NumberUtil.numberWithCommas(score));
|
||||
@@ -60,8 +63,9 @@ class Game {
|
||||
screenBottom.printBottomLeftText("최고 기록 : " + NumberUtil.numberWithCommas(highScore));
|
||||
});
|
||||
|
||||
this.startGame();
|
||||
// this.countDown();
|
||||
heartManager.addOnChangeGameOverListener(
|
||||
() => { this.gameOver(); }
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -118,7 +122,6 @@ class Game {
|
||||
}
|
||||
|
||||
onAlienEscaped() {
|
||||
console.log("onAlienEscaped");
|
||||
heartManager.breakHearts(1);
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,10 @@ class Loading {
|
||||
}
|
||||
|
||||
startLoading() {
|
||||
this.game.load.image('icon_fullscreen', '../../../resources/image/icon/fullscreen_white.png');
|
||||
this.game.load.image('alien_normal', '../../../resources/image/character/alien_normal.png');
|
||||
this.game.load.image('icon_fullscreen', '../../../resources/image/icon/fullscreen_white.png');
|
||||
this.game.load.image('alien_normal', '../../../resources/image/character/alien_normal.png');
|
||||
this.game.load.image('heart_full', '../../../resources/image/ui/heart_full.png');
|
||||
this.game.load.image('heart_empty', '../../../resources/image/ui/heart_empty.png');
|
||||
// this.game.load.image('a', '../../../resources/image/icon/fullscreen_white.png');
|
||||
// this.game.load.image('b', '../../../resources/image/icon/fullscreen_white.png');
|
||||
// this.game.load.image('c', '../../../resources/image/icon/fullscreen_white.png');
|
||||
|
||||
Reference in New Issue
Block a user