Add: show heart_full, heart_empty
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 6.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 4.5 KiB |
+39
-13
@@ -2,21 +2,47 @@ class HeartGauge {
|
|||||||
|
|
||||||
constructor(heartManager) {
|
constructor(heartManager) {
|
||||||
this.heartManager = heartManager;
|
this.heartManager = heartManager;
|
||||||
|
this.hearts = [];
|
||||||
|
|
||||||
this.heartManager.addOnChangeHeartListener(
|
this.heartManager.addOnChangeCountListener(
|
||||||
() => {
|
() => { this.onChangeCountListener(); }
|
||||||
console.log(this.heartManager.getHearts());
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HeartGauge.DEFAULT_TEXT_FONT = {
|
|
||||||
font: "30px Arial",
|
|
||||||
boundsAlignH: "center", // left, center. right
|
|
||||||
boundsAlignV: "middle", // top, middle, bottom
|
|
||||||
fill: "#fff"
|
|
||||||
};
|
|
||||||
|
|
||||||
HeartGauge.MOVE_UP_AMOUNT_PX = 50;
|
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
class HeartManager {
|
class HeartManager {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.countHearts = HeartManager.DEFAULT_MAX_COUNT;
|
this.countHearts = HeartManager.DEFAULT_COUNT;
|
||||||
this.countMaxHearts = HeartManager.DEFAULT_MAX_COUNT;
|
this.countMaxHearts = HeartManager.DEFAULT_COUNT;
|
||||||
|
|
||||||
this.onChangeHeartListeners = [];
|
this.onChangeCountListeners = [];
|
||||||
|
this.onChangeMaxCountListeners = [];
|
||||||
this.onChangeGameOverListeners = [];
|
this.onChangeGameOverListeners = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,14 +17,24 @@ class HeartManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addOnChangeHeartListener(onChangeFunction) {
|
addOnChangeCountListener(onChangeFunction) {
|
||||||
this.onChangeHeartListeners.push(onChangeFunction);
|
this.onChangeCountListeners.push(onChangeFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
removeOnChangeHeartListener(onChangeFunction) {
|
removeOnChangeCountListener(onChangeFunction) {
|
||||||
let itemIndex = this.onChangeHeartListeners.indexOf(onChangeFunction);
|
let itemIndex = this.onChangeCountListeners.indexOf(onChangeFunction);
|
||||||
if(itemIndex > -1)
|
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) {
|
addOnChangeGameOverListener(onChangeGameOverFunction) {
|
||||||
@@ -37,12 +48,13 @@ class HeartManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
removeAllListeners() {
|
removeAllListeners() {
|
||||||
this.onChangeHeartListeners = [];
|
this.onChangeCountListeners = [];
|
||||||
|
this.onChangeMaxCountListeners = [];
|
||||||
this.onChangeGameOverListeners = [];
|
this.onChangeGameOverListeners = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
setHearts(amount) {
|
setCount(amount) {
|
||||||
let beforeCountHearts = this.countHearts;
|
let beforeCountHearts = this.countHearts;
|
||||||
this.countHearts = amount;
|
this.countHearts = amount;
|
||||||
if(this.countHearts < 0)
|
if(this.countHearts < 0)
|
||||||
@@ -51,25 +63,35 @@ class HeartManager {
|
|||||||
this.countHearts = this.countMaxHearts;
|
this.countHearts = this.countMaxHearts;
|
||||||
|
|
||||||
if(beforeCountHearts !== this.countHearts)
|
if(beforeCountHearts !== this.countHearts)
|
||||||
this.callListener(this.onChangeHeartListeners);
|
this.callListener(this.onChangeCountListeners);
|
||||||
|
|
||||||
if(this.countHearts === 0)
|
if(this.countHearts === 0)
|
||||||
this.callListener(this.onChangeGameOverListeners);
|
this.callListener(this.onChangeGameOverListeners);
|
||||||
}
|
}
|
||||||
|
|
||||||
setMaxHearts(amount) {
|
getCount() {
|
||||||
this.countMaxHearts = amount;
|
|
||||||
|
|
||||||
if(this.countHearts > this.countMaxHearts)
|
|
||||||
this.countHearts = this.countMaxHearts;
|
|
||||||
}
|
|
||||||
|
|
||||||
getHearts() {
|
|
||||||
return this.countHearts;
|
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) {
|
addHearts(amount) {
|
||||||
this.setHearts(this.countHearts + amount);
|
this.setCount(this.countHearts + amount);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,19 +99,7 @@ class HeartManager {
|
|||||||
this.addHearts(-1 * amount);
|
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() {
|
create() {
|
||||||
this.game.stage.backgroundColor = '#4d4d4d';
|
this.game.stage.backgroundColor = '#4d4d4d';
|
||||||
|
|
||||||
|
this.initListeners();
|
||||||
|
|
||||||
// top
|
// top
|
||||||
let backButton = new BackButton( () => {
|
let backButton = new BackButton( () => {
|
||||||
location.href = '../../web/client/menu_app.html';
|
location.href = '../../web/client/menu_app.html';
|
||||||
@@ -13,11 +15,7 @@ class Game {
|
|||||||
|
|
||||||
let scoreBoard = new ScoreBoard();
|
let scoreBoard = new ScoreBoard();
|
||||||
let heartGauge = new HeartGauge(heartManager);
|
let heartGauge = new HeartGauge(heartManager);
|
||||||
heartManager.addOnChangeGameOverListener(
|
heartGauge.start();
|
||||||
() => {
|
|
||||||
this.gameOver();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
let fullscreenButton = new FullscreenButton(this.game);
|
let fullscreenButton = new FullscreenButton(this.game);
|
||||||
|
|
||||||
@@ -51,6 +49,11 @@ class Game {
|
|||||||
screenBottom.printBottomRightText(sessionStorageManager.playerName);
|
screenBottom.printBottomRightText(sessionStorageManager.playerName);
|
||||||
|
|
||||||
|
|
||||||
|
this.startGame();
|
||||||
|
// this.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
initListeners() {
|
||||||
scoreManager.addOnChangeScoreListener( score => {
|
scoreManager.addOnChangeScoreListener( score => {
|
||||||
console.log("onChangeScore : " + score);
|
console.log("onChangeScore : " + score);
|
||||||
scoreBoard.printScore(NumberUtil.numberWithCommas(score));
|
scoreBoard.printScore(NumberUtil.numberWithCommas(score));
|
||||||
@@ -60,8 +63,9 @@ class Game {
|
|||||||
screenBottom.printBottomLeftText("최고 기록 : " + NumberUtil.numberWithCommas(highScore));
|
screenBottom.printBottomLeftText("최고 기록 : " + NumberUtil.numberWithCommas(highScore));
|
||||||
});
|
});
|
||||||
|
|
||||||
this.startGame();
|
heartManager.addOnChangeGameOverListener(
|
||||||
// this.countDown();
|
() => { this.gameOver(); }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -118,7 +122,6 @@ class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onAlienEscaped() {
|
onAlienEscaped() {
|
||||||
console.log("onAlienEscaped");
|
|
||||||
heartManager.breakHearts(1);
|
heartManager.breakHearts(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ class Loading {
|
|||||||
startLoading() {
|
startLoading() {
|
||||||
this.game.load.image('icon_fullscreen', '../../../resources/image/icon/fullscreen_white.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('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('a', '../../../resources/image/icon/fullscreen_white.png');
|
||||||
// this.game.load.image('b', '../../../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');
|
// this.game.load.image('c', '../../../resources/image/icon/fullscreen_white.png');
|
||||||
|
|||||||
+29
-29
@@ -1,63 +1,63 @@
|
|||||||
let heartManager = new HeartManager();
|
let heartManager = new HeartManager();
|
||||||
|
|
||||||
QUnit.test( "MaxCount", function( assert ) {
|
QUnit.test( "MaxCount", function( assert ) {
|
||||||
heartManager.setHearts(3);
|
heartManager.setCount(3);
|
||||||
assert.equal(heartManager.getHearts(), 3);
|
assert.equal(heartManager.getCount(), 3);
|
||||||
|
|
||||||
heartManager.setMaxHearts(5);
|
heartManager.setMaxCount(5);
|
||||||
assert.equal(heartManager.getHearts(), 3);
|
assert.equal(heartManager.getCount(), 3);
|
||||||
|
|
||||||
heartManager.setMaxHearts(3);
|
heartManager.setMaxCount(3);
|
||||||
assert.equal(heartManager.getHearts(), 3);
|
assert.equal(heartManager.getCount(), 3);
|
||||||
|
|
||||||
heartManager.setMaxHearts(2);
|
heartManager.setMaxCount(2);
|
||||||
assert.equal(heartManager.getHearts(), 2);
|
assert.equal(heartManager.getCount(), 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
QUnit.test( "setHearts", function( assert ) {
|
QUnit.test( "setCount", function( assert ) {
|
||||||
heartManager.setMaxHearts(3);
|
heartManager.setMaxCount(3);
|
||||||
|
|
||||||
heartManager.setHearts(3);
|
heartManager.setCount(3);
|
||||||
assert.equal(heartManager.getHearts(), 3);
|
assert.equal(heartManager.getCount(), 3);
|
||||||
|
|
||||||
heartManager.setHearts(2);
|
heartManager.setCount(2);
|
||||||
assert.equal(heartManager.getHearts(), 2);
|
assert.equal(heartManager.getCount(), 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
QUnit.test( "addHearts", function( assert ) {
|
QUnit.test( "addHearts", function( assert ) {
|
||||||
heartManager.setMaxHearts(3);
|
heartManager.setMaxCount(3);
|
||||||
|
|
||||||
heartManager.setHearts(1);
|
heartManager.setCount(1);
|
||||||
assert.equal(heartManager.getHearts(), 1);
|
assert.equal(heartManager.getCount(), 1);
|
||||||
|
|
||||||
heartManager.addHearts(1);
|
heartManager.addHearts(1);
|
||||||
assert.equal(heartManager.getHearts(), 2);
|
assert.equal(heartManager.getCount(), 2);
|
||||||
|
|
||||||
heartManager.setHearts(1);
|
heartManager.setCount(1);
|
||||||
assert.equal(heartManager.getHearts(), 1);
|
assert.equal(heartManager.getCount(), 1);
|
||||||
|
|
||||||
heartManager.addHearts(2);
|
heartManager.addHearts(2);
|
||||||
assert.equal(heartManager.getHearts(), 3);
|
assert.equal(heartManager.getCount(), 3);
|
||||||
|
|
||||||
heartManager.setHearts(1);
|
heartManager.setCount(1);
|
||||||
assert.equal(heartManager.getHearts(), 1);
|
assert.equal(heartManager.getCount(), 1);
|
||||||
|
|
||||||
heartManager.addHearts(3);
|
heartManager.addHearts(3);
|
||||||
assert.equal(heartManager.getHearts(), 3);
|
assert.equal(heartManager.getCount(), 3);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
QUnit.test( "breakHearts", function( assert ) {
|
QUnit.test( "breakHearts", function( assert ) {
|
||||||
heartManager.setMaxHearts(3);
|
heartManager.setMaxCount(3);
|
||||||
|
|
||||||
heartManager.setHearts(3);
|
heartManager.setCount(3);
|
||||||
assert.equal(heartManager.getHearts(), 3);
|
assert.equal(heartManager.getCount(), 3);
|
||||||
|
|
||||||
heartManager.breakHearts(1);
|
heartManager.breakHearts(1);
|
||||||
assert.equal(heartManager.getHearts(), 2);
|
assert.equal(heartManager.getCount(), 2);
|
||||||
|
|
||||||
heartManager.breakHearts(2);
|
heartManager.breakHearts(2);
|
||||||
assert.equal(heartManager.getHearts(), 0);
|
assert.equal(heartManager.getCount(), 0);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user