1 /**
  2  * Game data storage
  3  * @param {Number} levels
  4  * @constructor
  5  */
  6 function BB_Storage(levels) {
  7 	this.levels = levels;
  8 	this.data = {
  9 		level: 0,
 10 		balls: [],
 11 		score: [],
 12 		sound: !window.orientation
 13 	};
 14 	if (localStorage.data) {
 15 		this.data = JSON.parse(localStorage.data);
 16 	}
 17 }
 18 
 19 /**
 20  * Save data to local storage
 21  */
 22 BB_Storage.prototype.save = function() {
 23 	localStorage.data = JSON.stringify(this.data);
 24 };
 25 
 26 /**
 27  * Set score data and check new highscore
 28  * @param {Number} level
 29  * @param {BB_Score} score
 30  * @param {Boolean} level completed
 31  */
 32 BB_Storage.prototype.setScore = function(level, score, done) {
 33 	if (level <= this.data.level) {
 34 		score.highscore = this.data.score[level] || 0;
 35 		score.high = score.score > score.highscore;
 36 		if (score.high) {
 37 			this.data.score[level] = score.score;
 38 			this.data.balls[level] = score.shot;
 39 		}
 40 		if (
 41 			done && 
 42 			level < this.levels-1 && 
 43 			level == this.data.level
 44 		) {
 45 			this.data.level++;
 46 		}
 47 		this.save();
 48 		score.balls = this.data.balls[level] || 0;
 49 		score.next = level < this.data.level;
 50 	}
 51 };
 52 
 53 /**
 54  * Set sound enabled
 55  * @param {Boolean} enabled
 56  */
 57 BB_Storage.prototype.setSound = function(enabled) {
 58 	this.data.sound = enabled;
 59 	this.save();
 60 };
 61 
 62 /**
 63  * Get sound enabled
 64  * @returns {Boolean}
 65  */
 66 BB_Storage.prototype.getSound = function() {
 67 	return this.data.sound;
 68 };
 69 
 70 /**
 71  * Get maximun level number
 72  * @returns {Number}
 73  */
 74 BB_Storage.prototype.getLevel = function() {
 75 	return this.data.level;
 76 };
 77