Add: HeartManager, HeartGauge
This commit is contained in:
@@ -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" };
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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();
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
<script src="../../game/lib/app_info_manager.js"></script>
|
||||
<script src="../../game/lib/session_storage_manager.js"></script>
|
||||
<script src="../../game/lib/score_manager.js"></script>
|
||||
<script src="../../game/lib/heart_manager.js"></script>
|
||||
<script src="../../game/global/global_variables.js"></script>
|
||||
|
||||
<!-- library source files -->
|
||||
@@ -21,6 +22,7 @@
|
||||
<script src="../../game/lib/input_type_text.js"></script>
|
||||
<script src="../../game/lib/score_board.js"></script>
|
||||
<script src="../../game/lib/score_text.js"></script>
|
||||
<script src="../../game/lib/heart_gauge.js"></script>
|
||||
<script src="../../game/lib/round_rect_button.js"></script>
|
||||
<script src="../../game/lib/back_button.js"></script>
|
||||
<script src="../../game/lib/fullscreen_button.js"></script>
|
||||
|
||||
Reference in New Issue
Block a user