Add: HeartManager, HeartGauge
This commit is contained in:
@@ -33,6 +33,5 @@ let sessionStorageManager = new SessionStorageManager();
|
|||||||
console.log("playingAppName : " + sessionStorageManager.playingAppName);
|
console.log("playingAppName : " + sessionStorageManager.playingAppName);
|
||||||
}
|
}
|
||||||
let appInfoManager = new AppInfoManager();
|
let appInfoManager = new AppInfoManager();
|
||||||
let scoreManager = new ScoreManager();
|
|
||||||
|
|
||||||
let textStyleBasic = { font: "bold 32px Arial", fill: "#fff", align: "center", boundsAlignH: "center", boundsAlignV: "middle" };
|
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.score = 0;
|
||||||
this.highScore = 0;
|
this.highScore = 0;
|
||||||
|
|
||||||
this.onChangeScoreFunctions = [];
|
this.onChangeScoreListeners = [];
|
||||||
this.onChangeHighScoreFunctions = [];
|
this.onChangeHighScoreListeners = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
addOnChangeScoreFunctions(onChangeFunction) {
|
callListener(functionArray) {
|
||||||
this.onChangeScoreFunctions.push(onChangeFunction);
|
if(functionArray.length > 0) {
|
||||||
|
for(let i = 0; i < functionArray.length; i++) {
|
||||||
|
functionArray[i](this.score);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeOnChangeScoreFunctions(onChangeFunction) {
|
addOnChangeScoreListener(onChangeFunction) {
|
||||||
let itemIndex = this.onChangeScoreFunctions.indexOf(onChangeFunction);
|
this.onChangeScoreListeners.push(onChangeFunction);
|
||||||
|
}
|
||||||
|
|
||||||
|
removeOnChangeScoreListener(onChangeFunction) {
|
||||||
|
let itemIndex = this.onChangeScoreListeners.indexOf(onChangeFunction);
|
||||||
if(itemIndex > -1)
|
if(itemIndex > -1)
|
||||||
this.onChangeScoreFunctions.splice(itemIndex, 1);
|
this.onChangeScoreListeners.splice(itemIndex, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
addOnChangeHighScoreFunctions(onChangeHighScoreFunction) {
|
addOnChangeHighScoreListener(onChangeHighScoreFunction) {
|
||||||
this.onChangeHighScoreFunctions.push(onChangeHighScoreFunction);
|
this.onChangeHighScoreListeners.push(onChangeHighScoreFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
removeOnChangeHighScoreFunctions(onChangeHighScoreFunction) {
|
removeOnChangeHighScoreListener(onChangeHighScoreFunction) {
|
||||||
let itemIndex = this.onChangeHighScoreFunctions.indexOf(onChangeHighScoreFunction);
|
let itemIndex = this.onChangeHighScoreListeners.indexOf(onChangeHighScoreFunction);
|
||||||
if(itemIndex > -1)
|
if(itemIndex > -1)
|
||||||
this.onChangeHighScoreFunctions.splice(itemIndex, 1);
|
this.onChangeHighScoreListeners.splice(itemIndex, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
removeAllFunctions() {
|
removeAllListeners() {
|
||||||
this.onChangeScoreFunctions = [];
|
this.onChangeScoreListeners = [];
|
||||||
this.onChangeHighScoreFunctions = [];
|
this.onChangeHighScoreListeners = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -56,24 +64,12 @@ class ScoreManager {
|
|||||||
this.score = value;
|
this.score = value;
|
||||||
|
|
||||||
// update score
|
// update score
|
||||||
if(beforeScore !== this.score) {
|
if(beforeScore !== this.score)
|
||||||
if(this.onChangeScoreFunctions.length > 0) {
|
this.callListener(this.onChangeScoreListeners);
|
||||||
for(let i = 0; i < this.onChangeScoreFunctions.length; i++) {
|
|
||||||
this.onChangeScoreFunctions[i](this.score);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// update high score
|
// update high score
|
||||||
if(this.score > this.highScore) {
|
if(this.score > this.highScore)
|
||||||
this.highScore = this.score;
|
this.callListener(this.onChangeHighScoreListeners);
|
||||||
|
|
||||||
if(this.onChangeHighScoreFunctions.length > 0) {
|
|
||||||
for(let i = 0; i < this.onChangeHighScoreFunctions.length; i++) {
|
|
||||||
this.onChangeHighScoreFunctions[i](this.highScore);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
let scoreManager = new ScoreManager();
|
||||||
|
let heartManager = new HeartManager();
|
||||||
@@ -12,6 +12,12 @@ class Game {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let scoreBoard = new ScoreBoard();
|
let scoreBoard = new ScoreBoard();
|
||||||
|
let heartGauge = new HeartGauge(heartManager);
|
||||||
|
heartManager.addOnChangeGameOverListener(
|
||||||
|
() => {
|
||||||
|
this.gameOver();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
let fullscreenButton = new FullscreenButton(this.game);
|
let fullscreenButton = new FullscreenButton(this.game);
|
||||||
|
|
||||||
@@ -45,11 +51,11 @@ class Game {
|
|||||||
screenBottom.printBottomRightText(sessionStorageManager.playerName);
|
screenBottom.printBottomRightText(sessionStorageManager.playerName);
|
||||||
|
|
||||||
|
|
||||||
scoreManager.addOnChangeScoreFunctions( score => {
|
scoreManager.addOnChangeScoreListener( score => {
|
||||||
console.log("onChangeScore : " + score);
|
console.log("onChangeScore : " + score);
|
||||||
scoreBoard.printScore(NumberUtil.numberWithCommas(score));
|
scoreBoard.printScore(NumberUtil.numberWithCommas(score));
|
||||||
});
|
});
|
||||||
scoreManager.addOnChangeHighScoreFunctions( highScore => {
|
scoreManager.addOnChangeHighScoreListener( highScore => {
|
||||||
console.log("onChangeHighScore : " + highScore);
|
console.log("onChangeHighScore : " + highScore);
|
||||||
screenBottom.printBottomLeftText("최고 기록 : " + NumberUtil.numberWithCommas(highScore));
|
screenBottom.printBottomLeftText("최고 기록 : " + NumberUtil.numberWithCommas(highScore));
|
||||||
});
|
});
|
||||||
@@ -94,6 +100,10 @@ class Game {
|
|||||||
this.activateNextAlien();
|
this.activateNextAlien();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gameOver() {
|
||||||
|
console.log("gameOver");
|
||||||
|
}
|
||||||
|
|
||||||
activateNextAlien() {
|
activateNextAlien() {
|
||||||
if(this.activatedAlienIndex === this.aliens.length)
|
if(this.activatedAlienIndex === this.aliens.length)
|
||||||
return;
|
return;
|
||||||
@@ -109,6 +119,7 @@ class Game {
|
|||||||
|
|
||||||
onAlienEscaped() {
|
onAlienEscaped() {
|
||||||
console.log("onAlienEscaped");
|
console.log("onAlienEscaped");
|
||||||
|
heartManager.breakHearts(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,7 @@
|
|||||||
<script src="../../game/lib/app_info_manager.js"></script>
|
<script src="../../game/lib/app_info_manager.js"></script>
|
||||||
<script src="../../game/lib/session_storage_manager.js"></script>
|
<script src="../../game/lib/session_storage_manager.js"></script>
|
||||||
<script src="../../game/lib/score_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>
|
<script src="../../game/global/global_variables.js"></script>
|
||||||
|
|
||||||
<!-- library source files -->
|
<!-- library source files -->
|
||||||
@@ -21,6 +22,7 @@
|
|||||||
<script src="../../game/lib/input_type_text.js"></script>
|
<script src="../../game/lib/input_type_text.js"></script>
|
||||||
<script src="../../game/lib/score_board.js"></script>
|
<script src="../../game/lib/score_board.js"></script>
|
||||||
<script src="../../game/lib/score_text.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/round_rect_button.js"></script>
|
||||||
<script src="../../game/lib/back_button.js"></script>
|
<script src="../../game/lib/back_button.js"></script>
|
||||||
<script src="../../game/lib/fullscreen_button.js"></script>
|
<script src="../../game/lib/fullscreen_button.js"></script>
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width">
|
||||||
|
<title>QUnit Example</title>
|
||||||
|
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.6.0.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="qunit"></div>
|
||||||
|
<div id="qunit-fixture"></div>
|
||||||
|
<script src="https://code.jquery.com/qunit/qunit-2.6.0.js"></script>
|
||||||
|
<script src="../src/game/lib/heart_manager.js"></script>
|
||||||
|
<script src="test_heart_manager.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
let heartManager = new HeartManager();
|
||||||
|
|
||||||
|
QUnit.test( "MaxCount", function( assert ) {
|
||||||
|
heartManager.setHearts(3);
|
||||||
|
assert.equal(heartManager.getHearts(), 3);
|
||||||
|
|
||||||
|
heartManager.setMaxHearts(5);
|
||||||
|
assert.equal(heartManager.getHearts(), 3);
|
||||||
|
|
||||||
|
heartManager.setMaxHearts(3);
|
||||||
|
assert.equal(heartManager.getHearts(), 3);
|
||||||
|
|
||||||
|
heartManager.setMaxHearts(2);
|
||||||
|
assert.equal(heartManager.getHearts(), 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
QUnit.test( "setHearts", function( assert ) {
|
||||||
|
heartManager.setMaxHearts(3);
|
||||||
|
|
||||||
|
heartManager.setHearts(3);
|
||||||
|
assert.equal(heartManager.getHearts(), 3);
|
||||||
|
|
||||||
|
heartManager.setHearts(2);
|
||||||
|
assert.equal(heartManager.getHearts(), 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
QUnit.test( "addHearts", function( assert ) {
|
||||||
|
heartManager.setMaxHearts(3);
|
||||||
|
|
||||||
|
heartManager.setHearts(1);
|
||||||
|
assert.equal(heartManager.getHearts(), 1);
|
||||||
|
|
||||||
|
heartManager.addHearts(1);
|
||||||
|
assert.equal(heartManager.getHearts(), 2);
|
||||||
|
|
||||||
|
heartManager.setHearts(1);
|
||||||
|
assert.equal(heartManager.getHearts(), 1);
|
||||||
|
|
||||||
|
heartManager.addHearts(2);
|
||||||
|
assert.equal(heartManager.getHearts(), 3);
|
||||||
|
|
||||||
|
heartManager.setHearts(1);
|
||||||
|
assert.equal(heartManager.getHearts(), 1);
|
||||||
|
|
||||||
|
heartManager.addHearts(3);
|
||||||
|
assert.equal(heartManager.getHearts(), 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
QUnit.test( "breakHearts", function( assert ) {
|
||||||
|
heartManager.setMaxHearts(3);
|
||||||
|
|
||||||
|
heartManager.setHearts(3);
|
||||||
|
assert.equal(heartManager.getHearts(), 3);
|
||||||
|
|
||||||
|
heartManager.breakHearts(1);
|
||||||
|
assert.equal(heartManager.getHearts(), 2);
|
||||||
|
|
||||||
|
heartManager.breakHearts(2);
|
||||||
|
assert.equal(heartManager.getHearts(), 0);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user