Add: HeartManager, HeartGauge
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user