Add: HeartManager, HeartGauge

This commit is contained in:
2018-05-15 09:07:17 +09:00
parent b89490facd
commit 407f95f26f
9 changed files with 240 additions and 34 deletions
-1
View File
@@ -33,6 +33,5 @@ let sessionStorageManager = new SessionStorageManager();
console.log("playingAppName : " + sessionStorageManager.playingAppName);
}
let appInfoManager = new AppInfoManager();
let scoreManager = new ScoreManager();
let textStyleBasic = { font: "bold 32px Arial", fill: "#fff", align: "center", boundsAlignH: "center", boundsAlignV: "middle" };
+22
View File
@@ -0,0 +1,22 @@
class HeartGauge {
constructor(heartManager) {
this.heartManager = heartManager;
this.heartManager.addOnChangeHeartListener(
() => {
console.log(this.heartManager.getHearts());
}
);
};
}
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;
+95
View File
@@ -0,0 +1,95 @@
class HeartManager {
constructor() {
this.countHearts = HeartManager.DEFAULT_MAX_COUNT;
this.countMaxHearts = HeartManager.DEFAULT_MAX_COUNT;
this.onChangeHeartListeners = [];
this.onChangeGameOverListeners = [];
}
callListener(functionArray) {
if(functionArray.length > 0) {
for(let i = 0; i < functionArray.length; i++) {
functionArray[i](this.score);
}
}
}
addOnChangeHeartListener(onChangeFunction) {
this.onChangeHeartListeners.push(onChangeFunction);
}
removeOnChangeHeartListener(onChangeFunction) {
let itemIndex = this.onChangeHeartListeners.indexOf(onChangeFunction);
if(itemIndex > -1)
this.onChangeHeartListeners.splice(itemIndex, 1);
}
addOnChangeGameOverListener(onChangeGameOverFunction) {
this.onChangeGameOverListeners.push(onChangeGameOverFunction);
}
removeOnChangeHighScoreListener(onChangeGameOverFunction) {
let itemIndex = this.onChangeGameOverListeners.indexOf(onChangeGameOverFunction);
if(itemIndex > -1)
this.onChangeGameOverListeners.splice(itemIndex, 1);
}
removeAllListeners() {
this.onChangeHeartListeners = [];
this.onChangeGameOverListeners = [];
}
setHearts(amount) {
let beforeCountHearts = this.countHearts;
this.countHearts = amount;
if(this.countHearts < 0)
this.countHearts = 0;
else if(this.countHearts > this.countMaxHearts)
this.countHearts = this.countMaxHearts;
if(beforeCountHearts !== this.countHearts)
this.callListener(this.onChangeHeartListeners);
if(this.countHearts === 0)
this.callListener(this.onChangeGameOverListeners);
}
setMaxHearts(amount) {
this.countMaxHearts = amount;
if(this.countHearts > this.countMaxHearts)
this.countHearts = this.countMaxHearts;
}
getHearts() {
return this.countHearts;
}
addHearts(amount) {
this.setHearts(this.countHearts + amount);
}
breakHearts(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;
+27 -31
View File
@@ -4,33 +4,41 @@ class ScoreManager {
this.score = 0;
this.highScore = 0;
this.onChangeScoreFunctions = [];
this.onChangeHighScoreFunctions = [];
this.onChangeScoreListeners = [];
this.onChangeHighScoreListeners = [];
}
addOnChangeScoreFunctions(onChangeFunction) {
this.onChangeScoreFunctions.push(onChangeFunction);
callListener(functionArray) {
if(functionArray.length > 0) {
for(let i = 0; i < functionArray.length; i++) {
functionArray[i](this.score);
}
}
}
removeOnChangeScoreFunctions(onChangeFunction) {
let itemIndex = this.onChangeScoreFunctions.indexOf(onChangeFunction);
addOnChangeScoreListener(onChangeFunction) {
this.onChangeScoreListeners.push(onChangeFunction);
}
removeOnChangeScoreListener(onChangeFunction) {
let itemIndex = this.onChangeScoreListeners.indexOf(onChangeFunction);
if(itemIndex > -1)
this.onChangeScoreFunctions.splice(itemIndex, 1);
this.onChangeScoreListeners.splice(itemIndex, 1);
}
addOnChangeHighScoreFunctions(onChangeHighScoreFunction) {
this.onChangeHighScoreFunctions.push(onChangeHighScoreFunction);
addOnChangeHighScoreListener(onChangeHighScoreFunction) {
this.onChangeHighScoreListeners.push(onChangeHighScoreFunction);
}
removeOnChangeHighScoreFunctions(onChangeHighScoreFunction) {
let itemIndex = this.onChangeHighScoreFunctions.indexOf(onChangeHighScoreFunction);
removeOnChangeHighScoreListener(onChangeHighScoreFunction) {
let itemIndex = this.onChangeHighScoreListeners.indexOf(onChangeHighScoreFunction);
if(itemIndex > -1)
this.onChangeHighScoreFunctions.splice(itemIndex, 1);
this.onChangeHighScoreListeners.splice(itemIndex, 1);
}
removeAllFunctions() {
this.onChangeScoreFunctions = [];
this.onChangeHighScoreFunctions = [];
removeAllListeners() {
this.onChangeScoreListeners = [];
this.onChangeHighScoreListeners = [];
}
@@ -56,24 +64,12 @@ class ScoreManager {
this.score = value;
// update score
if(beforeScore !== this.score) {
if(this.onChangeScoreFunctions.length > 0) {
for(let i = 0; i < this.onChangeScoreFunctions.length; i++) {
this.onChangeScoreFunctions[i](this.score);
}
}
}
if(beforeScore !== this.score)
this.callListener(this.onChangeScoreListeners);
// update high score
if(this.score > this.highScore) {
this.highScore = this.score;
if(this.onChangeHighScoreFunctions.length > 0) {
for(let i = 0; i < this.onChangeHighScoreFunctions.length; i++) {
this.onChangeHighScoreFunctions[i](this.highScore);
}
}
}
if(this.score > this.highScore)
this.callListener(this.onChangeHighScoreListeners);
}
}
@@ -0,0 +1,2 @@
let scoreManager = new ScoreManager();
let heartManager = new HeartManager();
+13 -2
View File
@@ -12,6 +12,12 @@ class Game {
});
let scoreBoard = new ScoreBoard();
let heartGauge = new HeartGauge(heartManager);
heartManager.addOnChangeGameOverListener(
() => {
this.gameOver();
}
);
let fullscreenButton = new FullscreenButton(this.game);
@@ -45,11 +51,11 @@ class Game {
screenBottom.printBottomRightText(sessionStorageManager.playerName);
scoreManager.addOnChangeScoreFunctions( score => {
scoreManager.addOnChangeScoreListener( score => {
console.log("onChangeScore : " + score);
scoreBoard.printScore(NumberUtil.numberWithCommas(score));
});
scoreManager.addOnChangeHighScoreFunctions( highScore => {
scoreManager.addOnChangeHighScoreListener( highScore => {
console.log("onChangeHighScore : " + highScore);
screenBottom.printBottomLeftText("최고 기록 : " + NumberUtil.numberWithCommas(highScore));
});
@@ -94,6 +100,10 @@ class Game {
this.activateNextAlien();
}
gameOver() {
console.log("gameOver");
}
activateNextAlien() {
if(this.activatedAlienIndex === this.aliens.length)
return;
@@ -109,6 +119,7 @@ class Game {
onAlienEscaped() {
console.log("onAlienEscaped");
heartManager.breakHearts(1);
}
}