Add: ScoreManager, ScoreBoard

This commit is contained in:
2018-05-13 16:15:46 +09:00
parent b720f940ec
commit b89490facd
9 changed files with 196 additions and 27 deletions
+79
View File
@@ -0,0 +1,79 @@
class ScoreManager {
constructor() {
this.score = 0;
this.highScore = 0;
this.onChangeScoreFunctions = [];
this.onChangeHighScoreFunctions = [];
}
addOnChangeScoreFunctions(onChangeFunction) {
this.onChangeScoreFunctions.push(onChangeFunction);
}
removeOnChangeScoreFunctions(onChangeFunction) {
let itemIndex = this.onChangeScoreFunctions.indexOf(onChangeFunction);
if(itemIndex > -1)
this.onChangeScoreFunctions.splice(itemIndex, 1);
}
addOnChangeHighScoreFunctions(onChangeHighScoreFunction) {
this.onChangeHighScoreFunctions.push(onChangeHighScoreFunction);
}
removeOnChangeHighScoreFunctions(onChangeHighScoreFunction) {
let itemIndex = this.onChangeHighScoreFunctions.indexOf(onChangeHighScoreFunction);
if(itemIndex > -1)
this.onChangeHighScoreFunctions.splice(itemIndex, 1);
}
removeAllFunctions() {
this.onChangeScoreFunctions = [];
this.onChangeHighScoreFunctions = [];
}
getScore() {
return this.score;
}
getHighScore() {
return this.highScore;
}
plusScore(amount) {
this.setScore(this.score + amount);
}
minusScore(amount) {
this.plusScore(-1 * amount);
}
setScore(value) {
let beforeScore = this.score;
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);
}
}
}
// 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);
}
}
}
}
}