diff --git a/engine.js b/engine.js index deff7d3..8853903 100644 --- a/engine.js +++ b/engine.js @@ -3,28 +3,28 @@ var vendors = ['ms', 'moz', 'webkit', 'o']; for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; - window.cancelAnimationFrame = + window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; } - + if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); - var id = window.setTimeout(function() { callback(currTime + timeToCall); }, + var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; - + if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); }; }()); - -var Game = new function() { + +var Game = new function() { var boards = []; // Game Initialization @@ -43,7 +43,7 @@ var Game = new function() { this.setupInput(); - this.loop(); + this.loop(); if(this.mobile) { this.setBoard(4,new TouchControls()); @@ -51,7 +51,7 @@ var Game = new function() { SpriteSheet.load(sprite_data,callback); }; - + // Handle Input var KEY_CODES = { 37:'left', 39:'right', 32 :'fire' }; @@ -67,7 +67,7 @@ var Game = new function() { window.addEventListener('keyup',function(e) { if(KEY_CODES[e.keyCode]) { - Game.keys[KEY_CODES[e.keyCode]] = false; + Game.keys[KEY_CODES[e.keyCode]] = false; e.preventDefault(); } },false); @@ -77,21 +77,21 @@ var Game = new function() { var lastTime = new Date().getTime(); var maxTime = 1/30; // Game Loop - this.loop = function() { + this.loop = function() { var curTime = new Date().getTime(); requestAnimationFrame(Game.loop); var dt = (curTime - lastTime)/1000; if(dt > maxTime) { dt = maxTime; } for(var i=0,len = boards.length;i= 1280 || !hasTouch) { return false; } @@ -139,9 +139,9 @@ var Game = new function() { var SpriteSheet = new function() { - this.map = { }; + this.map = { }; - this.load = function(spriteData,callback) { + this.load = function(spriteData,callback) { this.map = spriteData; this.image = new Image(); this.image.onload = callback; @@ -152,9 +152,9 @@ var SpriteSheet = new function() { var s = this.map[sprite]; if(!frame) frame = 0; ctx.drawImage(this.image, - s.sx + frame * s.w, - s.sy, - s.w, s.h, + s.sx + frame * s.w, + s.sy, + s.w, s.h, Math.floor(x), Math.floor(y), s.w, s.h); }; @@ -173,7 +173,7 @@ var TitleScreen = function TitleScreen(title,subtitle,callback) { ctx.fillStyle = "#FFFFFF"; ctx.font = "bold 40px bangers"; - var measure = ctx.measureText(title); + var measure = ctx.measureText(title); ctx.fillText(title,Game.width/2 - measure.width/2,Game.height/2); ctx.font = "bold 20px bangers"; @@ -191,18 +191,18 @@ var GameBoard = function() { this.cnt = {}; // Add a new object to the object list - this.add = function(obj) { - obj.board=this; - this.objects.push(obj); + this.add = function(obj) { + obj.board=this; + this.objects.push(obj); this.cnt[obj.type] = (this.cnt[obj.type] || 0) + 1; - return obj; + return obj; }; // Mark an object for removal - this.remove = function(obj) { + this.remove = function(obj) { var idx = this.removed.indexOf(obj); if(idx == -1) { - this.removed.push(obj); + this.removed.push(obj); return true; } else { return false; @@ -223,7 +223,7 @@ var GameBoard = function() { } }; - // Call the same method on all current objects + // Call the same method on all current objects this.iterate = function(funcName) { var args = Array.prototype.slice.call(arguments,1); for(var i=0,len=this.objects.length;io2.y+o2.h-1) || @@ -318,9 +318,9 @@ Level.prototype.step = function(dt) { // Start, End, Gap, Type, Override // [ 0, 4000, 500, 'step', { x: 100 } ] - while((curShip = this.levelData[idx]) && + while((curShip = this.levelData[idx]) && (curShip[0] < this.t + 2000)) { - // Check if we've passed the end time + // Check if we've passed the end time if(this.t > curShip[1]) { remove.push(curShip); } else if(curShip[0] < this.t) { @@ -343,7 +343,7 @@ Level.prototype.step = function(dt) { if(remIdx != -1) this.levelData.splice(remIdx,1); } - // If there are no more enemies on the board or in + // If there are no more enemies on the board or in // levelData, this level is done if(this.levelData.length === 0 && this.board.cnt[OBJECT_ENEMY] === 0) { if(this.callback) this.callback(); @@ -371,8 +371,8 @@ var TouchControls = function() { var txtSize = ctx.measureText(txt); - ctx.fillText(txt, - x+blockWidth/2-txtSize.width/2, + ctx.fillText(txt, + x+blockWidth/2-txtSize.width/2, y+3*blockWidth/4+5); }; @@ -400,10 +400,10 @@ var TouchControls = function() { x = touch.pageX / Game.canvasMultiplier - Game.canvas.offsetLeft; if(x < unitWidth) { Game.keys['left'] = true; - } + } if(x > unitWidth && x < 2*unitWidth) { Game.keys['right'] = true; - } + } } if(e.type == 'touchstart' || e.type == 'touchend') { diff --git a/game.js b/game.js index 0f18bba..4eee6b3 100644 --- a/game.js +++ b/game.js @@ -10,13 +10,13 @@ var sprites = { }; var enemies = { - straight: { x: 0, y: -50, sprite: 'enemy_ship', health: 10, + straight: { x: 0, y: -50, sprite: 'enemy_ship', health: 10, E: 100 }, - ltr: { x: 0, y: -100, sprite: 'enemy_purple', health: 10, + ltr: { x: 0, y: -100, sprite: 'enemy_purple', health: 10, B: 75, C: 1, E: 100, missiles: 2 }, - circle: { x: 250, y: -50, sprite: 'enemy_circle', health: 10, + circle: { x: 250, y: -50, sprite: 'enemy_circle', health: 10, A: 0, B: -100, C: 1, E: 20, F: 100, G: 1, H: Math.PI/2 }, - wiggle: { x: 100, y: -50, sprite: 'enemy_bee', health: 20, + wiggle: { x: 100, y: -50, sprite: 'enemy_bee', health: 20, B: 50, C: 4, E: 100, firePercentage: 0.001, missiles: 2 }, step: { x: 0, y: -50, sprite: 'enemy_circle', health: 10, B: 150, C: 1.2, E: 75 } @@ -38,8 +38,8 @@ var startGame = function() { Game.setBoard(0,new Starfield(20,0.4,100,true)); Game.setBoard(1,new Starfield(50,0.6,100)); Game.setBoard(2,new Starfield(100,1.0,50)); - } - Game.setBoard(3,new TitleScreen("Alien Invasion", + } + Game.setBoard(3,new TitleScreen("Alien Invasion", "Press fire to start playing", playGame)); }; @@ -67,13 +67,13 @@ var playGame = function() { }; var winGame = function() { - Game.setBoard(3,new TitleScreen("You win!", + Game.setBoard(3,new TitleScreen("You win!", "Press fire to play again", playGame)); }; var loseGame = function() { - Game.setBoard(3,new TitleScreen("You lose!", + Game.setBoard(3,new TitleScreen("You lose!", "Press fire to play again", playGame)); }; @@ -82,13 +82,13 @@ var Starfield = function(speed,opacity,numStars,clear) { // Set up the offscreen canvas var stars = document.createElement("canvas"); - stars.width = Game.width; + stars.width = Game.width; stars.height = Game.height; var starCtx = stars.getContext("2d"); var offset = 0; - // If the clear option is set, + // If the clear option is set, // make the background black instead of transparent if(clear) { starCtx.fillStyle = "#000"; @@ -139,7 +139,7 @@ var Starfield = function(speed,opacity,numStars,clear) { }; }; -var PlayerShip = function() { +var PlayerShip = function() { this.setup('ship', { vx: 0, reloadTime: 0.25, maxVel: 200 }); this.reload = this.reloadTime; @@ -154,7 +154,7 @@ var PlayerShip = function() { this.x += this.vx * dt; if(this.x < 0) { this.x = 0; } - else if(this.x > Game.width - this.w) { + else if(this.x > Game.width - this.w) { this.x = Game.width - this.w; } @@ -182,7 +182,7 @@ PlayerShip.prototype.hit = function(damage) { var PlayerMissile = function(x,y) { this.setup('missile',{ vy: -700, damage: 10 }); this.x = x - this.w/2; - this.y = y - this.h; + this.y = y - this.h; }; PlayerMissile.prototype = new Sprite(); @@ -194,8 +194,8 @@ PlayerMissile.prototype.step = function(dt) { if(collision) { collision.hit(this.damage); this.board.remove(this); - } else if(this.y < -this.h) { - this.board.remove(this); + } else if(this.y < -this.h) { + this.board.remove(this); } }; @@ -209,9 +209,9 @@ var Enemy = function(blueprint,override) { Enemy.prototype = new Sprite(); Enemy.prototype.type = OBJECT_ENEMY; -Enemy.prototype.baseParameters = { A: 0, B: 0, C: 0, D: 0, +Enemy.prototype.baseParameters = { A: 0, B: 0, C: 0, D: 0, E: 0, F: 0, G: 0, H: 0, - t: 0, reloadTime: 0.75, + t: 0, reloadTime: 0.75, reload: 0 }; Enemy.prototype.step = function(dt) { @@ -253,7 +253,7 @@ Enemy.prototype.hit = function(damage) { if(this.health <=0) { if(this.board.remove(this)) { Game.points += this.points || 100; - this.board.add(new Explosion(this.x + this.w/2, + this.board.add(new Explosion(this.x + this.w/2, this.y + this.h/2)); } } @@ -275,7 +275,7 @@ EnemyMissile.prototype.step = function(dt) { collision.hit(this.damage); this.board.remove(this); } else if(this.y > Game.height) { - this.board.remove(this); + this.board.remove(this); } }; @@ -299,5 +299,3 @@ Explosion.prototype.step = function(dt) { window.addEventListener("load", function() { Game.initialize("game",sprites,startGame); }); - -