var Game = new function() { var boards = []; // Game Initialization this.initialize = function(canvasElementId,sprite_data,callback) { this.canvas= document.createElement("canvas"); this.canvas.style.cssText="idtkscale:ScaleAspectFit;"; // CocoonJS extension this.canvas.width= 320; this.canvas.height= 480; document.body.appendChild(this.canvas); this.playerOffset = 10; this.canvasMultiplier= 1; this.mobile = true; this.width = this.canvas.width; this.height= this.canvas.height; this.ctx = this.canvas.getContext && this.canvas.getContext('2d'); if(!this.ctx) { return alert("Please upgrade your browser to play"); } this.setupInput(); this.loop(); if(this.mobile) { this.setBoard(4,new TouchControls()); } SpriteSheet.load(sprite_data,callback); }; // Handle Input var KEY_CODES = { 37:'left', 39:'right', 32 :'fire' }; this.keys = {}; this.setupInput = function() { window.addEventListener('keydown',function(e) { if(KEY_CODES[event.keyCode]) { Game.keys[KEY_CODES[event.keyCode]] = true; e.preventDefault(); } },false); window.addEventListener('keyup',function(e) { if(KEY_CODES[event.keyCode]) { Game.keys[KEY_CODES[event.keyCode]] = false; e.preventDefault(); } },false); }; var lastTime = new Date().getTime(); var maxTime = 1/30; // Game Loop this.loop = function() { var curTime = new Date().getTime(); webkitRequestAnimationFrame(Game.loop); var dt = (curTime - lastTime)/1000; if(dt > maxTime) { dt = maxTime; } for(var i=0,len = boards.length;io2.y+o2.h-1) || (o1.x+o1.w-1o2.x+o2.w-1)); }; // Find the first object that collides with obj // match against an optional type this.collide = function(obj,type) { return this.detect(function() { if(obj != this) { var col = (!type || this.type & type) && board.overlap(obj,this); return col ? this : false; } }); }; }; var Sprite = function() { }; Sprite.prototype.setup = function(sprite,props) { this.sprite = sprite; this.merge(props); this.frame = this.frame || 0; this.w = SpriteSheet.map[sprite].w; this.h = SpriteSheet.map[sprite].h; }; Sprite.prototype.merge = function(props) { if(props) { for (var prop in props) { this[prop] = props[prop]; } } }; Sprite.prototype.draw = function(ctx) { SpriteSheet.draw(ctx,this.sprite,this.x,this.y,this.frame); }; Sprite.prototype.hit = function(damage) { this.board.remove(this); }; var Level = function(levelData,callback) { this.levelData = []; for(var i =0; i curShip[1]) { remove.push(curShip); } else if(curShip[0] < this.t) { // Get the enemy definition blueprint var enemy = enemies[curShip[3]], override = curShip[4]; // Add a new enemy with the blueprint and override this.board.add(new Enemy(enemy,override)); // Increment the start time by the gap curShip[0] += curShip[2]; } idx++; } // Remove any objects from the levelData that have passed for(var i=0,len=remove.length;i unitWidth && x < 2*unitWidth) { Game.keys['right'] = true; } } if(e.type == 'touchstart' || e.type == 'touchend') { for(i=0;i 4 * unitWidth) { Game.keys['fire'] = (e.type == 'touchstart'); } } } }; Game.canvas.addEventListener('touchstart',this.trackTouch,false); Game.canvas.addEventListener('touchmove',this.trackTouch,false); Game.canvas.addEventListener('touchend',this.trackTouch,false); Game.canvas.addEventListener('touchcancel',this.trackTouch,false); Game.playerOffset = unitWidth + 20; }; var GamePoints = function() { Game.points = 0; var pointsLength = 8; this.draw = function(ctx) { ctx.save(); ctx.font = "bold 18px arial"; ctx.fillStyle= "#FFFFFF"; var txt = "" + Game.points; var i = pointsLength - txt.length, zeros = ""; while(i-- > 0) { zeros += "0"; } ctx.fillText(zeros + txt,10,20); ctx.restore(); }; this.step = function(dt) { }; };