81 lines
2.1 KiB
JavaScript
81 lines
2.1 KiB
JavaScript
function ScoreManager() {
|
|
this.onChangeScoreListeners = [];
|
|
this.onChangeHighScoreListeners = [];
|
|
|
|
this.resetScore();
|
|
}
|
|
|
|
ScoreManager.prototype.resetScore = function() {
|
|
// this.score = Number(sessionStorageManager.record);
|
|
this.score = 0;
|
|
sessionStorageManager.record = this.score;
|
|
|
|
if(sessionStorageManager.bestRecord === null)
|
|
this.highScore = 0;
|
|
else
|
|
this.highScore = Number(sessionStorageManager.bestRecord);
|
|
}
|
|
|
|
ScoreManager.prototype.callListener = function(functionArray) {
|
|
if(functionArray.length > 0) {
|
|
for(var i = 0; i < functionArray.length; i++) {
|
|
functionArray[i](this.score);
|
|
}
|
|
}
|
|
}
|
|
|
|
ScoreManager.prototype.addOnChangeScoreListener = function(onChangeFunction) {
|
|
this.onChangeScoreListeners.push(onChangeFunction);
|
|
}
|
|
|
|
ScoreManager.prototype.removeOnChangeScoreListener = function(onChangeFunction) {
|
|
var itemIndex = this.onChangeScoreListeners.indexOf(onChangeFunction);
|
|
if(itemIndex > -1)
|
|
this.onChangeScoreListeners.splice(itemIndex, 1);
|
|
}
|
|
|
|
ScoreManager.prototype.addOnChangeHighScoreListener = function(onChangeHighScoreFunction) {
|
|
this.onChangeHighScoreListeners.push(onChangeHighScoreFunction);
|
|
}
|
|
|
|
ScoreManager.prototype.removeOnChangeHighScoreListener = function(onChangeHighScoreFunction) {
|
|
var itemIndex = this.onChangeHighScoreListeners.indexOf(onChangeHighScoreFunction);
|
|
if(itemIndex > -1)
|
|
this.onChangeHighScoreListeners.splice(itemIndex, 1);
|
|
}
|
|
|
|
ScoreManager.prototype.removeAllListeners = function() {
|
|
this.onChangeScoreListeners = [];
|
|
this.onChangeHighScoreListeners = [];
|
|
}
|
|
|
|
|
|
ScoreManager.prototype.getScore = function() {
|
|
return this.score;
|
|
}
|
|
|
|
ScoreManager.prototype.getHighScore = function() {
|
|
return this.highScore;
|
|
}
|
|
|
|
|
|
ScoreManager.prototype.plusScore = function(amount) {
|
|
this.setScore(this.score + amount);
|
|
}
|
|
|
|
ScoreManager.prototype.minusScore = function(amount) {
|
|
this.plusScore(-1 * amount);
|
|
}
|
|
|
|
ScoreManager.prototype.setScore = function(value) {
|
|
var beforeScore = this.score;
|
|
this.score = value;
|
|
|
|
// update score
|
|
if(beforeScore !== this.score)
|
|
this.callListener(this.onChangeScoreListeners);
|
|
|
|
// update high score
|
|
if(this.score > this.highScore)
|
|
this.callListener(this.onChangeHighScoreListeners);
|
|
} |