1 /**
  2  * Application controller
  3  * @constructor
  4  */
  5 function BB_App() {
  6 	var self = this;
  7 	this.storage = new BB_Storage(BB_Game.maps.length);
  8 	this.game = this.storage.getLevel();
  9 	this.sound = new BB_Sound(function() {
 10 		switch (this.index) {
 11 			case -1:
 12 			case 3:
 13 				self.scene.on('load');
 14 				break;
 15 			case 4:
 16 				self.scene.on('music');
 17 				break;
 18 		}
 19 	});
 20 	this.sound.disabled = !this.storage.getSound();
 21 	this.drawer = new BB_Drawer();
 22 	this.sprite = new BB_Sprite();
 23 	this.init();
 24 	document.body.appendChild(this.drawer.canvas);
 25 	document.body.appendChild(this.canvas);
 26 	this.bind();
 27 }
 28 
 29 /**
 30  * Extends CP_Canvas
 31  */
 32 BB_App.prototype = new CP_Canvas(480, 320);
 33 
 34 /**
 35  * Init and resize app
 36  */
 37 BB_App.prototype.init = function() {
 38 	this.resize();
 39 	this.drawer.resize();
 40 	this.sprite.render(this.ctx, this.scale * this.ratio);
 41 	this.scene = new BB_Main(this);
 42 };
 43 
 44 /**
 45  * Event handler
 46  * @param e
 47  */
 48 BB_App.prototype.on = function(e) {
 49 	this.scene.on(e, this.x, this.y, this.touch);
 50 };
 51 
 52 /**
 53  * Draw the canvas
 54  */
 55 BB_App.prototype.paint = function() {
 56 	this.scene.paint(this.ctx);
 57 	this.scene.run();
 58 };
 59 
 60 /**
 61  * Start new level
 62  * @param {Number} add
 63  */
 64 BB_App.prototype.start = function(add) {
 65 	if (add) {
 66 		this.game += add;
 67 	}
 68 	delete this.scene;
 69 	this.scene = new BB_Game(this, this.game);
 70 };
 71 
 72 /**
 73  * Animation thread
 74  */
 75 BB_App.prototype.run = function() {
 76 	var self = this;
 77 	self.paint();
 78 	requestAnimFrame(function() {
 79 		self.run();
 80 	});
 81 };
 82 
 83 window.requestAnimFrame =
 84 	window.requestAnimationFrame       ||
 85 	window.webkitRequestAnimationFrame ||
 86 	window.mozRequestAnimationFrame    ||
 87 	function(callback) { window.setTimeout(callback, 1000 / 60); };
 88 
 89 window.onload = function() {
 90 	var app = new BB_App(),
 91 		timer = null;
 92 	window.setTimeout(function() { app.run(); }, 500);
 93 	window.onresize = function() {
 94 		if (timer) window.clearTimeout(timer);
 95 		timer = window.setTimeout(function() { app.init(); }, 500);
 96 	};
 97 };
 98 
 99