Fix: restructing session manager, ES6 -> ES5
This commit is contained in:
@@ -1,85 +1,81 @@
|
||||
class ScoreManager {
|
||||
function ScoreManager() {
|
||||
this.onChangeScoreListeners = [];
|
||||
this.onChangeHighScoreListeners = [];
|
||||
|
||||
constructor() {
|
||||
this.onChangeScoreListeners = [];
|
||||
this.onChangeHighScoreListeners = [];
|
||||
this.resetScore();
|
||||
}
|
||||
|
||||
this.resetScore();
|
||||
}
|
||||
ScoreManager.prototype.resetScore = function() {
|
||||
// this.score = Number(sessionStorageManager.record);
|
||||
this.score = 0;
|
||||
sessionStorageManager.record = this.score;
|
||||
|
||||
resetScore() {
|
||||
// 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);
|
||||
}
|
||||
|
||||
if(sessionStorageManager.bestRecord === null)
|
||||
this.highScore = 0;
|
||||
else
|
||||
this.highScore = Number(sessionStorageManager.bestRecord);
|
||||
}
|
||||
|
||||
callListener(functionArray) {
|
||||
if(functionArray.length > 0) {
|
||||
for(let i = 0; i < functionArray.length; i++) {
|
||||
functionArray[i](this.score);
|
||||
}
|
||||
ScoreManager.prototype.callListener = function(functionArray) {
|
||||
if(functionArray.length > 0) {
|
||||
for(var i = 0; i < functionArray.length; i++) {
|
||||
functionArray[i](this.score);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addOnChangeScoreListener(onChangeFunction) {
|
||||
this.onChangeScoreListeners.push(onChangeFunction);
|
||||
}
|
||||
ScoreManager.prototype.addOnChangeScoreListener = function(onChangeFunction) {
|
||||
this.onChangeScoreListeners.push(onChangeFunction);
|
||||
}
|
||||
|
||||
removeOnChangeScoreListener(onChangeFunction) {
|
||||
let itemIndex = this.onChangeScoreListeners.indexOf(onChangeFunction);
|
||||
if(itemIndex > -1)
|
||||
this.onChangeScoreListeners.splice(itemIndex, 1);
|
||||
}
|
||||
ScoreManager.prototype.removeOnChangeScoreListener = function(onChangeFunction) {
|
||||
var itemIndex = this.onChangeScoreListeners.indexOf(onChangeFunction);
|
||||
if(itemIndex > -1)
|
||||
this.onChangeScoreListeners.splice(itemIndex, 1);
|
||||
}
|
||||
|
||||
addOnChangeHighScoreListener(onChangeHighScoreFunction) {
|
||||
this.onChangeHighScoreListeners.push(onChangeHighScoreFunction);
|
||||
}
|
||||
ScoreManager.prototype.addOnChangeHighScoreListener = function(onChangeHighScoreFunction) {
|
||||
this.onChangeHighScoreListeners.push(onChangeHighScoreFunction);
|
||||
}
|
||||
|
||||
removeOnChangeHighScoreListener(onChangeHighScoreFunction) {
|
||||
let itemIndex = this.onChangeHighScoreListeners.indexOf(onChangeHighScoreFunction);
|
||||
if(itemIndex > -1)
|
||||
this.onChangeHighScoreListeners.splice(itemIndex, 1);
|
||||
}
|
||||
ScoreManager.prototype.removeOnChangeHighScoreListener = function(onChangeHighScoreFunction) {
|
||||
var itemIndex = this.onChangeHighScoreListeners.indexOf(onChangeHighScoreFunction);
|
||||
if(itemIndex > -1)
|
||||
this.onChangeHighScoreListeners.splice(itemIndex, 1);
|
||||
}
|
||||
|
||||
removeAllListeners() {
|
||||
this.onChangeScoreListeners = [];
|
||||
this.onChangeHighScoreListeners = [];
|
||||
}
|
||||
ScoreManager.prototype.removeAllListeners = function() {
|
||||
this.onChangeScoreListeners = [];
|
||||
this.onChangeHighScoreListeners = [];
|
||||
}
|
||||
|
||||
|
||||
getScore() {
|
||||
return this.score;
|
||||
}
|
||||
ScoreManager.prototype.getScore = function() {
|
||||
return this.score;
|
||||
}
|
||||
|
||||
getHighScore() {
|
||||
return this.highScore;
|
||||
}
|
||||
ScoreManager.prototype.getHighScore = function() {
|
||||
return this.highScore;
|
||||
}
|
||||
|
||||
|
||||
plusScore(amount) {
|
||||
this.setScore(this.score + amount);
|
||||
}
|
||||
ScoreManager.prototype.plusScore = function(amount) {
|
||||
this.setScore(this.score + amount);
|
||||
}
|
||||
|
||||
minusScore(amount) {
|
||||
this.plusScore(-1 * amount);
|
||||
}
|
||||
ScoreManager.prototype.minusScore = function(amount) {
|
||||
this.plusScore(-1 * amount);
|
||||
}
|
||||
|
||||
setScore(value) {
|
||||
let beforeScore = this.score;
|
||||
this.score = value;
|
||||
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);
|
||||
}
|
||||
// update score
|
||||
if(beforeScore !== this.score)
|
||||
this.callListener(this.onChangeScoreListeners);
|
||||
|
||||
// update high score
|
||||
if(this.score > this.highScore)
|
||||
this.callListener(this.onChangeHighScoreListeners);
|
||||
}
|
||||
Reference in New Issue
Block a user