-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandomAttack.min.js
6 lines (6 loc) · 14.4 KB
/
RandomAttack.min.js
1
2
3
4
5
6
/*
RandomAttack Game by Abdullah Altahery
Licensed under the MIT license - http://opensource.org/licenses/MIT
*/
function color(s,e,o,t){this.r=s,this.g=e,this.b=o,this.a=t,this.getString=function(){return"rgba("+this.r+","+this.g+","+this.b+","+this.a+")"},this.opacity=function(s){return this.a=s,this},this.convertToColor=function(s){for(temp="",rgba=[0,0,0,0],rgba_index=0,i=4;i<s.length;i++)","!=s[i]&&"]"!=s[i]?temp+=s[i]:(i++,rgba[rgba_index]=parseInt(temp),rgba_index++,temp="");this.r=rgba[0],this.g=rgba[1],this.b=rgba[2],this.a=rgba[3]}}function getSecs(s){return s?(new Date).getTime():(new Date).getTime()/1e3}function playSound(s){if("kill"==s||"shotBig"==s||"shotBigP"==s){if(0==sounds[s].play)sounds[s].play=getSecs(!0);else if(sounds[s].play+500<getSecs(!0)){var e=new Audio(s+".mp3");e.play(),sounds[s].play=getSecs(!0)}}else if("shot1"==s||"shot2"==s||"shotBigP"==s){if(0==sounds[s].play)sounds[s].play=getSecs(!0);else if(sounds[s].play+100<getSecs(!0)){var e=new Audio(s+".mp3");e.play(),sounds[s].play=getSecs(!0)}}else if("shot1E"==s||"shot2E"==s)if(0==sounds[s].play)sounds[s].play=getSecs(!0);else if(sounds[s].play+300<getSecs(!0)){var e=new Audio(s+".mp3");e.play(),sounds[s].play=getSecs(!0)}}function randNumber(s,e){return Math.floor(Math.random()*s)+e}var TanksAry=[],ShotsAry=[],NUM_DEAD=0,NUM_SHOTS=0;sounds={shot1:{play:0},shot1E:{play:0},shot2:{play:0},shot2E:{play:0},shotBig:{play:0},shotBigP:{play:0},starUp:{play:0},kill:{play:0}};var mouseXY={mX:0,mY:0};document.onmousemove=function(s){mouseXY={mX:s.pageX,mY:s.pageY}},function(){function s(s,e,o,t,i){x.fillStyle=o.getString(),x.beginPath(),x.moveTo(s,e),x.lineTo(s+t,e),x.lineTo(s+t,e+i),x.lineTo(s,e+i),x.lineTo(s,e),x.closePath(),x.fill()}function e(s,e,o,t){x.beginPath(),x.fillStyle=o.getString(),x.arc(s,e,t,0,2*Math.PI),x.fill()}function o(s,e,o,t){x.beginPath(),x.lineWidth=1,x.strokeStyle=o.getString(),x.arc(s,e,t,0,2*Math.PI),x.stroke()}function t(s,e,o,t,i){x.font=s+"px sans-serif",x.fillStyle=i.getString(),x.fillText(e,o,t)}function h(s,e,o){-1!=z[e]&&("undefined"==typeof z[e]?z[e]=getSecs()+s:getSecs()-z[e]>0&&(o(),z[e]=-1))}function a(){this.is_mouse_in=0,this.is_mouseout=function(s,e){if(0!=this.is_mouse_in){this.is_mouse_in=0;var o=e.shape_pos.X,t=e.shape_pos.X+e.shape_size.sizeX,i=e.shape_pos.Y+e.shape_size.sizeY,h=e.shape_pos.Y;if(s.mX<o||s.mX>t||s.mY<h||s.mY>i)return!0}else{var o=e.shape_pos.X,t=e.shape_pos.X+e.shape_size.sizeX,i=e.shape_pos.Y+e.shape_size.sizeY,h=e.shape_pos.Y;s.mX<o||s.mX>t||s.mY<h||s.mY>i?this.is_mouse_in=0:this.is_mouse_in=1}return!1},this.is_hover=function(s,e){var o=e.shape_pos.X+e.shape_size.sizeX,t=e.shape_pos.X,i=e.shape_pos.Y+e.shape_size.sizeY,h=e.shape_pos.Y;return s.mX>t&&s.mX<o&&s.mY>h&&s.mY<i?(this.is_mouse_in=1,!0):!1},this.type=[{name:"hover",callback:function(){}},{name:"mouseout",callback:function(){}}],this.FireCallback=function(s,e,o){for(i=0;i<this.type.length;i++)if(this.type[i].name==s){this.type[i].callback(e,o);break}},this.addListner=function(s,e){for(i=0;i<this.type.length;i++)this.type[i].name==s&&(this.type[i].callback=e)}}function n(s){setTimeout(function(){setInterval(function(){S&&(setTimeout(function(){r(s,randNumber(4,0))},randNumber(2e3,609)),setTimeout(function(){r(s,randNumber(4,0))},randNumber(2e3,609)),setTimeout(function(){s.Health>0&&p(s),"normalShot"==s.weapon?playSound("shot1E"):(playSound("shot2E"),setTimeout(function(){playSound("shotBig")},1e3))},randNumber(2e3,609)))},1e3)},100)}function c(s,e,t,i,h,a,n,c){var p=s.X,r=s.Y;if("normalShot"==a){var l=new T;l.setBox({shape_size:{sizeX:e.X,sizeY:e.Y},type:"box",damage:n,boxAccXz:1.7,WeaponType:a,boxColor:i,shooterID:c.ID,afterFinishAni:function(s){s.IsOutRang||(s.boxColor.a-=.009,s.boxColor.a<1e-5&&(s.IsOutRang=!0))}})}else if("BigBoom"==a){var l=new T,u=1.3;l.setBox({shape_size:{sizeX:e.X,sizeY:e.Y},type:"box",boxAccXm:.1,boxAccYm:.1,boxAccXz:u,damage:n,WeaponType:a,shooterID:c.ID,boxColor:new color(255,220,20,.8),afterFinishAni:function(s){s.IsOutRang||(s.boxColor.a-=.009,s.boxColor.a<1e-4&&(s.IsOutRang=!0),o(s.shape_pos.X,s.shape_pos.Y,new color(200,0,0,1),2),o(s.shape_pos.X+s.shape_size.sizeX,s.shape_pos.Y+s.shape_size.sizeY,new color(200,0,0,1),2),o(s.shape_pos.X+s.shape_size.sizeX,s.shape_pos.Y,new color(200,0,0,1),2),o(s.shape_pos.X,s.shape_pos.Y+s.shape_size.sizeY,new color(200,0,0,1),2),s.shape_pos.X-=s.boxAccXz/4,s.shape_pos.Y-=s.boxAccXz/4,s.shape_size.sizeX+=s.boxAccXz,s.shape_size.sizeY+=s.boxAccXz,o(s.shape_pos.XT,s.shape_pos.YT,s.boxColor,s.shape_size.sizeX),s.after(.4,2,function(){"undefined"==typeof s.shape_size.s1?s.shape_size.s1=0:s.shape_size.s1+=s.boxAccXz*u,o(s.shape_pos.XT,s.shape_pos.YT,s.boxColor,s.shape_size.s1)}),s.after(.8,1,function(){"undefined"==typeof s.shape_size.s2?s.shape_size.s2=0:s.shape_size.s2+=s.boxAccXz*u,o(s.shape_pos.XT,s.shape_pos.YT,s.boxColor,s.shape_size.s2)}))}})}switch(h){case 3:l.boxAccX=t,l.shape_pos.X=p+30,l.shape_pos.Y=r+10;break;case 2:l.boxAccX=-1*t,l.shape_pos.X=p-15,l.shape_pos.Y=r+10;break;case 0:l.boxAccY=-1*t,l.shape_pos.X=p+8,l.shape_pos.Y=r-15;break;case 1:l.shape_pos.X=p+8,l.shape_pos.Y=r+25,l.boxAccY=t}return l}function p(s){var e=s.boxColor,o=s.weapon,t=[],i=s.ShotStrong;t.push(c({X:s.shape_pos.X,Y:s.shape_pos.Y},{X:7,Y:7},i,e,s.look,o,s.damage,s));for(var h=0;h<t.length;h++)ShotsAry.push(t[h])}function r(e,o){switch(e.boxAccX=0,e.boxAccY=0,acval=e.speed,o){case 1:e.addAcc(0,acval),e.look=1,e.drawHolePos=function(e){s(e.X+7,e.Y+20,this.boxColor,5,10)};break;case 0:e.boxAccX=0,e.boxAccY=0,e.addAcc(0,-1*acval),e.look=0,e.drawHolePos=function(e){s(e.X+13,e.Y,this.boxColor,-5,-10)};break;case 3:e.boxAccX=0,e.boxAccY=0,e.addAcc(acval,0),e.look=3,e.drawHolePos=function(e){s(e.X+20,e.Y+7,this.boxColor,10,5)};break;case 2:e.boxAccX=0,e.boxAccY=0,e.addAcc(-1*acval,0),e.look=2,e.drawHolePos=function(e){s(e.X,e.Y+7,this.boxColor,-10,5)}}}function l(s,e){var o=s.shape_size.sizeX+s.shape_pos.X,t=s.shape_pos.X,i=s.shape_size.sizeY+s.shape_pos.Y,h=s.shape_pos.Y,a=e.shape_pos.X,n=e.shape_pos.X+e.shape_size.sizeX,c=e.shape_pos.Y,p=e.shape_pos.Y+e.shape_size.sizeY;(a>t&&o>a||n>t&&o>n)&&(c>h&&i>c||p>h&&i>p)&&s.ID!=e.shooterID&&(s.Shild>0?s.Shild<10?(s.Shild--,e.damage-s.Shild>0?s.Health-=e.damage-s.Shild:(s.Health-=10,s.Shild-=2)):s.Shild<20?(s.Shild--,e.damage-s.Shild>0?s.Health-=e.damage-s.Shild:(s.Health-=10,s.Shild-=2)):(s.Health-=5,s.Shild-=2):s.Health-=e.damage,s.Health<=0&&playSound("kill"),"BigBoom"!=e.WeaponType&&(e.IsOutRang=!0,e.damage=0))}function u(){I.setBox({shape_pos:{X:600,Y:300},shape_size:{sizeX:20,sizeY:20},boxAccX:0,boxAccY:0,boxAccXm:.04,boxAccYm:.04,ShotStrong:6.2,damage:40,Shild:10,ID:"Team1",weapon:"normalShot",boxColor:new color(255,20,20,.8),look:1,drawShape:function(e){this.Health>=60?s(e.X,e.Y-10,new color(0,200,0,1),.2*this.Health,5):this.Health>=40?s(e.X,e.Y-10,new color(200,100,0,1),.2*this.Health,5):this.Health<40&&s(e.X,e.Y-10,new color(255,0,0,1),.2*this.Health,5),s(e.X,e.Y,this.boxColor,20,20),this.drawHolePos(e)},drawHolePos:function(e){s(e.X+7,e.Y+20,this.boxColor,5,10)}}),TanksAry.push(I);var e=0,o=4;setInterval(function(){S&&o>e&&(m(1,"normalShot",10,new color(200,200,0,.8),5,"Team2"),e++)},600),setInterval(function(){S&&o>e&&(m(1,"normalShot",10,new color(200,200,0,.8),5,"Team2"),e++)},1e3),setInterval(function(){S&&m(2,"normalShot",30,new color(20,20,20,1),50,"Team2")},2e4),setInterval(function(){S&&m(1,"normalShot",30,new color(255,20,20,1),50,"Team1")},5e3),setInterval(function(){S&&(m(5,"normalShot",10,new color(200,20,200,1),10,"Team3"),m(5,"BigBoom",10,new color(200,20,200,1),10,"Team3"))},5e4),S=!0,document.getElementById("scors").style.opacity=1}function m(e,o,t,i,h,a){for(var c=0;e>c;c++){var p=new v;p.setBox({shape_pos:{X:randNumber(800,109)-20,Y:randNumber(300,109)},shape_size:{sizeX:randNumber(20,10),sizeY:randNumber(20,10)},boxAccX:0,boxAccY:0,boxAccXm:.04,boxAccYm:.04,ShotStrong:5.2,damage:t,weapon:o,boxColor:i,Shild:h,ID:a,look:randNumber(4,0),drawShape:function(e){this.Health>=60?s(e.X,e.Y-10,new color(0,200,0,1),.2*this.Health,5):this.Health>=40?s(e.X,e.Y-10,new color(200,100,0,1),.2*this.Health,5):this.Health<40&&s(e.X,e.Y-10,new color(255,0,0,1),.2*this.Health,5),s(e.X,e.Y,this.boxColor,20,20),this.drawHolePos(e)},drawHolePos:function(e){s(e.X+7,e.Y+20,this.boxColor,5,10)}}),TanksAry.push(p),n(p)}}function b(s,e){for(var o=0;o<s.length;o++)for(var t=0;t<e.length;t++)l(s[o],e[t])}function d(){if(window.requestAnimationFrame(d),Y=Date.now(),f=Y-w,f>y){x.clearRect(0,0,X.width,X.height),w=Y-f%y,document.getElementById("score").innerHTML=NUM_DEAD,document.getElementById("shots").innerHTML=NUM_SHOTS,document.getElementById("health").innerHTML=I.Health,h(1,1,function(){document.getElementById("GameIntro1").style.opacity=1}),h(2,4,function(){document.getElementById("GameIntro4").style.opacity=1});for(var s=0;s<ShotsAry.length;s++)ShotsAry[s].draw(X.width,X.height,mouseXY);for(var s=0;s<TanksAry.length;s++)t(10,TanksAry[s].ID,TanksAry[s].shape_pos.X,TanksAry[s].shape_pos.Y-15,TanksAry[s].boxColor),TanksAry[s].draw(X.width,X.height,mouseXY);S&&b(TanksAry,ShotsAry),I.Health<=0&&(console.log("gameOver"),S=!1)}}function A(s){for(var e=[],o=0;o<s.length;o++)s[o].IsOutRang||e.push(s[o]);return e}function _(s){for(var e=[],o=0;o<s.length;o++)s[o].Health>0?e.push(s[o]):(m(1,"normalShot",10,new color(230,200,230,1),15,"Team2"),NUM_DEAD>3?(I.weapon="BigBoom",playSound("starUp")):NUM_DEAD>20?m(1,"BigBoom",20,new color(230,200,0,1),5,"Team2"):NUM_DEAD>50&&m(1,"BigBoom",50,new color(20,20,0,1),5,"Team2"),NUM_DEAD++);return e}var X=document.getElementById("game"),x=X.getContext("2d");X.width=window.innerWidth,X.height=window.innerHeight;var Y,f,g=34,w=Date.now(),y=1e3/g,S=!1,z=[],T=function(){this.damage=2,this.shape_pos={X:0,Y:0,XT:0,YT:0},this.shape_size={sizeX:0,sizeY:0},this.boxAccX=0,this.boxAccY=0,this.boxAccXm=0,this.boxAccYm=0,this.IsOutRang=!1,this.boxColor=new color(200,200,200,.6),this.drawShape=0,this.boxAccXz=0,this.WeaponType="normalShot",this.afterFinishAni=function(){},this.isFinshedAnimation=!1,this.time_started=[],this.after=function(s,e,o){"undefined"==typeof this.time_started[e]?this.time_started[e]=(new Date).getTime()/1e3+s:(new Date).getTime()/1e3-this.time_started[e]>0&&o()},this.addAcc=function(s,e){this.boxAccX<10&&(this.boxAccX+=s),this.boxAccY<10&&(this.boxAccY+=e)},this.drawHolePos=function(){},this.setBox=function(s){if(null!==s&&"object"==typeof s)for(var e in s)s.hasOwnProperty(e)&&(this[e]=s[e])},this.calcAcc=function(){if(0!=this.boxAccY||0!=this.boxAccX){if(this.boxAccY>=.01&&this.boxAccY<=1&&this.boxAccX>=.01&&this.boxAccX<=1)return this.boxAccY=0,this.boxAccX=0,this.boxAccYm=0,this.boxAccXm=0,this.isFinshedAnimation=!0,this.shape_pos.XT=this.shape_pos.X,void(this.shape_pos.YT=this.shape_pos.Y);if(this.boxAccY>=-.01&&this.boxAccY<=.01&&this.boxAccX>=-.01&&this.boxAccX<=.01)return this.boxAccY=0,this.boxAccX=0,this.boxAccYm=0,this.boxAccXm=0,this.isFinshedAnimation=!0,this.shape_pos.XT=this.shape_pos.X,void(this.shape_pos.YT=this.shape_pos.Y);this.shape_pos.X+=this.boxAccX,this.shape_pos.Y+=this.boxAccY,this.boxAccX>0?this.boxAccX-=this.boxAccXm:this.boxAccX+=this.boxAccXm,this.boxAccY>0?this.boxAccY-=this.boxAccYm:this.boxAccY+=this.boxAccYm}},this.draw=function(o,t,i){this.IsOutRang||(S&&this.calcAcc(),this.shape_pos.X>o||this.shape_pos.Y>t||this.shape_pos.X<0||this.shape_pos.Y<0?this.IsOutRang=!0:this.isFinshedAnimation?this.afterFinishAni(this):"box"==this.type?s(this.shape_pos.X,this.shape_pos.Y,this.boxColor,this.shape_size.sizeX,this.shape_size.sizeY):"cir"==this.type&&e(this.shape_pos.X,this.shape_pos.Y,this.boxColor,this.shape_size.sizeY))}},v=function(){this.shape_pos={X:0,Y:0},this.shape_size={sizeX:0,sizeY:0},this.boxAccX=0,this.boxAccY=0,this.boxAccXm=0,this.boxAccYm=0,this.IsOutRang=!1,this.boxColor=new color(200,200,200,.6),this.drawShape=0,this.ShotStrong=3.2,this.Health=100,this.speed=3,this.weapon="normalShot",this.damage=1,this.type="box",this.ID="Tank",this.Shild=0,this.addAcc=function(s,e){this.boxAccX<10&&(this.boxAccX+=s),this.boxAccY<10&&(this.boxAccY+=e)},this.drawHolePos=function(){},this.setBox=function(s){if(null!==s&&"object"==typeof s)for(var e in s)s.hasOwnProperty(e)&&(this[e]=s[e])},this.calcAcc=function(){(0!=this.boxAccY||0!=this.boxAccX)&&(this.boxAccY>=.1&&this.boxAccY<=2&&this.boxAccX>=.1&&this.boxAccX<=2?(this.boxAccY=0,this.boxAccX=0,this.boxAccYm=0,this.boxAccXm=0):(this.shape_pos.X+=this.boxAccX,this.shape_pos.Y+=this.boxAccY,this.boxAccX>0?this.boxAccX-=this.boxAccXm:this.boxAccX+=this.boxAccXm,this.boxAccY>0?this.boxAccY-=this.boxAccYm:this.boxAccY+=this.boxAccYm)),this.shape_pos.Y>X.height-40&&(this.shape_pos.Y=X.height-40,this.boxAccY=0),this.shape_pos.Y<100&&(this.shape_pos.Y=100,this.boxAccY=0),this.shape_pos.X<10&&(this.shape_pos.X=10,this.boxAccX=0),this.shape_pos.X>X.width-40&&(this.shape_pos.X=X.width-40,this.boxAccX=0)},this.draw=function(o,t,i){this.Health>0&&(S&&this.calcAcc(),this.process_events(this,i),this.shape_pos.X>o||this.shape_pos.Y>t?this.IsOutRang=!0:0==this.drawShape?"box"==this.type?s(this.shape_pos.X,this.shape_pos.Y,this.boxColor,this.shape_size.sizeX,this.shape_size.sizeY):"cir"==this.type&&e(this.shape_pos.X,this.shape_pos.Y,this.boxColor,this.shape_size.sizeY):this.drawShape({X:this.shape_pos.X,Y:this.shape_pos.Y}))},this.events=new a,this.process_events=function(s,e){s.events.is_mouseout(e,s)&&s.events.FireCallback("mouseout",e,s),s.events.is_hover(e,s)&&s.events.FireCallback("hover",e,s)}},I=new v;document.onkeydown=function(s){switch(s.which){case 40:r(I,1);break;case 38:r(I,0);break;case 39:r(I,3);break;case 37:r(I,2);break;case 32:"normalShot"==I.weapon?playSound("shot1"):(playSound("shot2"),setTimeout(function(){playSound("shotBigP")},2e3)),p(I),NUM_SHOTS++}},document.getElementById("start_game").onclick=function(){u(),document.getElementById("GameIntro4").style.opacity=0,setTimeout(function(){document.getElementById("GameIntro1").style.padding=0,document.getElementById("GameIntro4").style.display="none"},1e3)},setInterval(function(){S&&(ShotsAry=A(ShotsAry))},1e4),setInterval(function(){S&&(TanksAry=_(TanksAry))},800),function(){for(var s=0,e=["ms","moz","webkit","o"],o=0;o<e.length&&!window.requestAnimationFrame;++o)window.requestAnimationFrame=window[e[o]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[e[o]+"CancelAnimationFrame"]||window[e[o]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(e,o){var t=(new Date).getTime(),i=Math.max(0,16-(t-s)),h=window.setTimeout(function(){e(t+i)},i);return s=t+i,h}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(s){clearTimeout(s)})}(),window.requestAnimationFrame(d)}();