window.requestAnimFrame=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(b){window.setTimeout(b,1E3/60)}}();
var ArcadeFont={alphabet:{A:"28,54,99,99,127,99,99",B:"63,99,99,63,99,99,63",C:"62,99,3,3,3,99,62",D:"31,51,99,99,99,51,31",E:"127,3,3,63,3,3,127",F:"63,3,3,31,3,3,3,0",G:"62,99,3,115,99,99,62",H:"99,99,99,127,99,99,99,0",I:"30,12,12,12,12,12,30,0",J:"96,96,96,96,96,99,62",K:"99,51,27,15,27,51,99",L:"3,3,3,3,3,3,127",M:"99,119,127,107,99,99,99",N:"99,103,111,127,123,115,99",O:"62,99,99,99,99,99,62",P:"63,99,99,99,63,3,3",Q:"62,99,99,99,123,51,94",R:"63,99,99,63,27,51,99",S:"62,99,3,127,96,99,62",
T:"63,12,12,12,12,12,12",U:"99,99,99,99,99,99,62",V:"99,99,99,99,54,28,8",W:"99,99,99,107,127,119,99",X:"99,119,62,28,62,119,99",Y:"51,51,51,30,12,12,12",Z:"127,112,56,28,14,7,127"," ":"0,0,0,0,0,0,0",1:"12,14,12,12,12,12,63",2:"62,99,112,60,6,3,127",3:"126,48,24,60,96,99,62",4:"56,60,54,51,127,48,48",5:"127,3,63,96,96,99,62",6:"62,99,3,63,99,99,62",7:"127,96,48,24,12,12,12",8:"62,99,99,62,99,99,62",9:"62,99,99,126,96,99,62","0":"62,99,99,99,99,99,62"},gutter:2,blueprint:function(b){var a=[],c,d,
g,j,h,b=b.toUpperCase();for(g in b){c=this.alphabet[b[g]].split(",");for(h=0,j=c.length;h<j;++h){d=EightBit.decodeNumber(c[h],7);a[h]||(a[h]="");a[h]+=d;for(d=this.gutter;d--;)a[h]+="0"}}return a.join("\n")}},EightBit={encode:function(b){var a=[],b=b.split("\n"),c,d;for(c=0,d=b.length;c<d;++c)a.push(this.encodeLine(b[c]));return a.join(",")},decode:function(b,a){var c=[],d=b.split(","),g,j;for(g=0,j=d.length;g<j;++g)c.push(this.decodeNumber(d[g],a));return c.join("\n")},encodeLine:function(b){var a=
0,c;for(c in b)a+="1"===b[c]?Math.pow(2,c):0;return a},decodeNumber:function(b,a){var c="",d,g;for(d=a;d--;)g=Math.pow(2,d),g<=b?(b-=g,c="1"+c):c="0"+c;return c}},redraw=function(b){var a=this.element.getContext("2d"),c=this.blueprint.split("\n"),d,g;a.fillStyle="rgba("+this.color+", 1)";for(var j=0,h=c.length;j<=h;++j){d=c[j]||"";for(var n=0,o=d.length;n<=o;++n)if(g=d[n],"1"===g&&(a.fillRect(Math.round(this.x+this.pixelSize*n),Math.round(this.y+this.pixelSize*j),this.pixelSize,this.pixelSize),b))this.boundingBox.width=
Math.max(this.boundingBox.width,this.pixelSize*n+this.pixelSize);b&&(this.boundingBox.height+=this.pixelSize)}},isHit=function(b){var a=this.boundingBox,c=b.boundingBox;return this.y+a.height<b.y?!1:this.y>b.y+c.height?!1:this.x+a.width<b.x?!1:this.x>b.x+c.width?!1:!0},explode=function(b){for(var a,c=this.blueprint.split("\n"),d,g=0,j=c.length;g<=j;++g){a=c[g]||"";for(var h=0,n=a.length;h<=n;++h)if(d=a[h],"1"===d){d=this.color.split(",");for(var o=0;o<d.length;++o)d[o]=Math.round(parseInt(d[o],10)+
80*Math.random()-40),d[o]=Math.min(255,d[o]),d[o]=Math.max(0,d[o]);b.push(new Particle(this.element,{color:d.join(","),pixelSize:this.pixelSize,x:Math.round(this.x+this.pixelSize*h),y:Math.round(this.y+this.pixelSize*g),velX:380*Math.random()-200,velY:280*-Math.random(),gravity:100,drag:0.96,fade:0.01+0.01*Math.random()}))}}},Text=function(b,a,c){c=c||{};$.extend(this,c);this.element=b;this.boundingBox={width:0,height:0};this.setText(a)};Text.prototype.redraw=redraw;Text.prototype.explode=explode;
Text.prototype.setText=function(b){this.blueprint=ArcadeFont.blueprint(b.toString());this.redraw(!0)};var Canon=function(b,a){a=a||{};$.extend(this,a);this.blueprint=EightBit.decode(this.blueprint,this.base);this.element=b;this.boundingBox={width:0,height:0};this.redraw(!0)};Canon.prototype.redraw=redraw;Canon.prototype.isHit=isHit;Canon.prototype.explode=explode;
var Invader=function(b,a){var c=document.createElement("canvas"),a=a||{};this.points=1;$.extend(this,a);c.width=this.maxWidth*this.pixelSize;c.height=this.maxHeight*this.pixelSize;this.element=b;this.boundingBox={width:0,height:0};this.redraw(!0)};Invader.prototype.redraw=redraw;Invader.prototype.shoot=function(b,a){return new Bullet(this.element,{speed:b,color:"255, 255, 255",pixelSize:a,x:this.x+this.boundingBox.width/2,y:this.y+this.boundingBox.height})};Invader.prototype.explode=explode;
Invader.prototype.isHit=isHit;var Bullet=function(b,a){this.canvas=b;this.ctx=b.getContext("2d");$.extend(this,a);this.boundingBox={width:this.pixelSize,height:this.pixelSize}};Bullet.prototype.update=function(b){this.y+=this.speed*b;this.ctx.fillStyle="rgba("+this.color+", 1)";this.ctx.fillRect(Math.round(this.x),Math.round(this.y),this.pixelSize,this.pixelSize);return this.y>this.canvas.height||this.y<-this.pixelSize?!1:!0};Bullet.prototype.isHit=isHit;
Bullet.prototype.explode=function(b){for(var a=this.pixelSize;a--;)for(var c=this.pixelSize;c--;){colors=[255,255,255];for(var d=3;d--;)colors[d]=Math.round(255,25*-Math.random());b.push(new Particle(this.canvas,{color:colors.join(","),pixelSize:this.pixelSize,x:Math.round(this.x+a),y:Math.round(this.y+c),velX:380*Math.random()-200,velY:280*-Math.random(),gravity:100,drag:0.95,fade:0.01+0.01*Math.random()}))}};
var Particle=function(b,a){var c=b.getContext("2d");this.size=a.pixelSize;this.boundingBox={width:this.size,height:this.size};this.alpha=1;this.update=function(b){a.velY*=a.drag;a.velX*=a.drag;a.y+=(a.velY+a.gravity)*b;a.x+=a.velX*b;this.alpha-=a.fade||0;this.size*=a.shrink||1;c.fillStyle="rgba("+a.color+", "+this.alpha+")";c.fillRect(Math.round(a.x),Math.round(a.y),this.size,this.size);return 0.01<=this.alpha}},Game=function(b,a){function c(){var c="255,151,94 255,182,240 195,255,232 182,255,252 197,255,182 255,242,117".split(" "),
k,g=0,e=0,h=0,i;$.each(a.invaders,function(d,f){k=[];if(!(++h>v+2)){i=c[Math.floor(Math.random()*c.length)];for(var j=0;j<a.numPerRow;++j)e=t+j*(a.gutter+G*a.pixelSize),k.push(new Invader(b,{blueprint:EightBit.decode(f,a.invaderBase),color:i,pixelSize:a.pixelSize,x:e,y:a.offset+g*G*a.pixelSize})),H=Math.max(e,H),++w;l.push(k);++g}});f||(f=new Canon(b,{blueprint:"32,112,1022,2047,2047,2047",base:11,color:"255,82,75",pixelSize:a.pixelSize,x:b.width/2,y:b.height-30}),$(window).bind("keydown keyup",d));
5<v&&(z+=M,A+=N)}function d(c){if(f){var d="keydown"===c.type,c=c.which;37===c||65===c?f.left=d:39===c||68===c?f.right=d:80===c&&d?(B=!B)?(C=new Date,D(),$("#end-screen").hide(),E.explode(q),d=$.inArray(E,r),-1<d&&r.splice(d,1)):E=n("PAUSE"):32===c&&d&&(d=new Date,O<d-I&&(m.push(new Bullet(b,{speed:-P,color:"255, 255, 255",pixelSize:a.bulletSize,x:f.x+f.boundingBox.width/2,y:f.y})),I=d))}}function g(a){for(var b=l.length;b--;)for(var c=l[b].length;c--;)!1===a(l[b][c])&&(l[b].splice(c,1),l[b].length||
l.splice(b,1))}function j(){var b=Math.floor(Math.random()*l.length);return l[b][Math.floor(Math.random()*l[b].length)]}function h(){var b=n("GAME OVER");x=!0;setTimeout(function(){b.explode(q);var a=$.inArray(b,r);-1<a&&r.splice(a,1);x=!0},5E3)}function n(a){a=new Text(b,a,{color:"205,212,75",pixelSize:2,x:b.width/2-50,y:b.height-30});a.x=(b.width-a.boundingBox.width)/2;a.y=(b.height-a.boundingBox.height)/2;r.push(a);return a}function o(a){y+=a;s.setText(y);s.x=b.width-s.boundingBox.width-5}function D(){Q.clearRect(0,
0,b.width,b.height);var d=new Date,k=(d-C)/1E3,n=!1,k=Math.min(50,k);if(!(100<k)){if(f){if(f.right)f.x+=J*k,f.x=Math.min(f.x,b.width-t-f.boundingBox.width);else if(f.left)f.x-=J*k,f.x=Math.max(f.x,t);f.redraw()}g(function(a){a.x+=(u?1:-1)*A*k;a.redraw();if(u&&a.x+a.boundingBox.width>b.width-t||!u&&a.x<t)n=!0});n&&(f&&g(function(b){b.y+=a.invaderVelY}),u=!u);for(var e=m.length;e--;)m[e].update(k)||(m.splice(e,1),++R);for(e=p.length;e--;)p[e].update(k)||p.splice(e,1);for(e=q.length;e--;)q[e].update(k)||
q.splice(e,1);g(function(a){for(var b=!0,c=m.length;c--;)b&&a.isHit(m[c])&&(o(a.points),a.explode(q),b=!1,m.splice(c,1),--w);return b});for(e=p.length;e--;)for(var l=m.length;l--;)p[e]&&p[e].isHit(m[l])&&(m.splice(l,1),p[e].explode(q),p.splice(e,1));for(e=p.length;e--;)f&&f.isHit(p[e])&&(f.explode(q),f=null,p.splice(e,1));f&&w&&Math.random()<z&&p.push(j().shoot(S,a.bulletSize));!w&&!q.length&&(++v,c());!x&&!f&&!q.length&&!m.length&&h();if(i){i.x+=(F?1:-1)*a.mothership.speed*k;i.redraw();for(e=m.length;e--;)i.isHit(m[e])&&
(o(i.points),i.explode(q),i=null,m.splice(e,1),K=d);if(i&&(i.x>b.width+20||i.x<i.boundingBox.width-20))i=null}else if((L<=v||x)&&K<d-1E3*a.mothership.minTime&&Math.random()<a.mothership.probability)F=0.5>Math.random(),i=new Invader(b,{blueprint:EightBit.decode(a.mothership.blueprint,a.mothership.base),color:a.mothership.color,pixelSize:a.pixelSize,x:b.width,y:6,points:10}),i.x=F?-i.boundingBox.width:b.width,++L;for(e=r.length;e--;)r[e].redraw()}C=d;B&&window.requestAnimFrame(D)}var G=12,l=[],m=[],
p=[],q=[],y=0,s,R=0,v=1,w=0,u=!0,A=40,N=20,J=130,O=500,I=0,P=100,S=100,i,L=0,K=new Date,F,C=new Date,Q=b.getContext("2d"),H=0,t=a.gutter,z=0.075,M=0.05,B=!0,x=!1,r=[],E,f;s=new Text(b,y,{color:"205,212,75",pixelSize:2,x:b.width-50,y:5});s.x=b.width-s.boundingBox.width-5;r.push(s);c();D()};
$(document).ready(function(){var b=document.getElementById("game");b.width=500;b.height=400;new Game(b,{pixelSize:3,bulletSize:3,numPerRow:6,invaders:{"#invader8":"260,136,508,886,2047,1533,1285,216,","#invader2":"240,1020,4095,3687,4095,4095,360,660,1122,0","#invader3":"144,2553,2925,3069,4095,240,504,612,1026","#invader6":"240,2046,4095,3687,4095,504,876,3075","#invader1":"264,144,1020,1020,3951,3069,3069,144,264,0","#invader7":"260,1161,1533,1911,1022,1022,260,514"},invaderVelY:10,invaderBase:12,
gutter:20,offset:60,mothership:{blueprint:"48,252,1023,819,510",base:10,color:"232,36,16",offset:10,minTime:15,probability:0.004,speed:60,points:20}})});
