forked from regularkid/ohflip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.min.js
15 lines (15 loc) · 6.77 KB
/
main.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var b=document.getElementById("canvas"),c=b.getContext("2d",{alpha:!1}),e=b.width,aa=b.height,ba,ca=-1,da=-1,h=0,k=0,l=0,m=0,p=1E3,q=0,r=!1,t=0,u=3,ea=.5,v=!1,w=0,fa=!1,x=0,y=0,z=0,ha=0,A=!1,B=0,C=0,D=!1,E=0,F=0,G=0,H=.7,I=0,J=0,K=!1,ia=0,ja=0,L=!0,M=!1,ka=[],N=[],O=parseInt(localStorage.getItem("ohflip.goalIdx"))||0;N.push({text:"Do a flip",b:P,a:1});N.push({text:"Land 2 flips in a row",b:Q,a:2});N.push({text:"Land perfectly",b:la,a:1});N.push({text:"Reach a height of 20 ft",b:R,a:20});
N.push({text:"Do a double flip",b:P,a:2});N.push({text:"Land 3 flips in a row",b:Q,a:3});N.push({text:"Land on your head",b:ma,a:1});N.push({text:"Do a triple flip",b:P,a:3});N.push({text:"Land perfectly 2 times in a row",b:na,a:2});N.push({text:"Reach a height of 50 ft",b:R,a:5});N.push({text:"Land 4 flips in a row",b:Q,a:4});N.push({text:"Do a quad flip",b:P,a:4});N.push({text:"Land 5 flips in a row",b:Q,a:5});N.push({text:"Land perfectly 3 times in a row",b:na,a:3});
N.push({text:"Reach a height of 100 ft",b:R,a:10});N.push({text:"Do a x5 flip",b:P,a:5});N.push({text:"Land 10 flips in a row",b:Q,a:10});N.push({text:"Reach a height of 250 ft",b:R,a:250});N.push({text:"Land perfectly 5 times in a row",b:na,a:5});N.push({text:"Do a x10 flip",b:P,a:7});N.push({text:"Reach a height of 500 ft",b:R,a:500});var S=0;document.addEventListener("mousedown",function(a){K=!0;T(a)},!1);document.addEventListener("mouseup",function(a){K=!1;T(a)},!1);
document.addEventListener("touchstart",function(a){K=!0;T(a);a.preventDefault()},!1);document.addEventListener("touchend",function(a){K=!1;T(a);a.preventDefault()},!1);document.addEventListener("touchcancel",function(a){K=!1;T(a);a.preventDefault()},!1);document.addEventListener("keydown",function(a){a.altKey&&"KeyR"===a.code&&(localStorage.setItem("ohflip.maxHeightFt",0),localStorage.setItem("ohflip.maxTotalFlips",0),localStorage.setItem("ohflip.goalIdx",0),O=0)});
function T(a){ia=a.pageX-b.offsetLeft;ja=a.pageY-b.offsetTop}function pa(){k=h=0;l=p=1E3;q=m=0;r=!1;G=F=t=0;H=.7;I=0;v=!1;ha=S=z=y=x=0;A=!1;C=B=0;D=!1;E=0}
function qa(a){var d=Math.min((a-(ba||a))/1E3,.2);ba=a;a=b.width/b.height;var f=window.innerWidth,g=window.innerWidth/a;g>window.innerHeight&&(g=window.innerHeight,f=g*a);if(f!==ca||g!==da)b.style.width=f+"px",b.style.height=g+"px",ca=f,da=g;window.scrollTo(0,0);ra(d);a=K&&!M;v?(a=w/1,h=400*Math.cos(a*Math.PI*.5)*(fa?-1:1)*p*.001,k=200*Math.sin(a*Math.PI)*p*.001,m+=800*d*(fa?-1:1),w-=d,0>=w&&pa()):(a&&100<k?(r=!1,q+=.1*(720-q)):(r&&(m*=.8,.01>Math.abs(m)&&(r=!1)),q*=.7),a=m,m+=q*d,t+=m-a,a=y,y=Math.floor((t+
90)/360),y>a&&(U(.5*b.width+100,b.height-200,"x"+y,"#D37CFF"),0<l&&ha++),180<=m?m-=360:-180>m&&(m+=360),l+=-1400*d,k+=l*d,E=Math.max(k,E),0>=k&&(F=16,G=0,30<Math.abs(m)?(v=!0,w=1,fa=.5>Math.random(),U(.5*b.width+100,b.height-100,"miss","#F42"),145<Math.abs(m)&&(D=!0)):(a=270<=t,A=6.5>Math.abs(m),p=a?p+(A?180:120)*(1+y/5*.5):Math.max(p-120,1E3),a&&A&&!L&&(J=.025),a?(z=y,x+=y,B++,A&&C++,A?U(.5*b.width+100,b.height-100,"perfect!","#FF0"):U(.5*b.width+100,b.height-100,"good","#0F4")):C=B=0),O<N.length&&
N[O].b(N[O])&&(U(b.width-100,120,"complete!","#FF0",!0),S=1,C=B=0),k=0,l=p,r=!0,ha=y=z=t=0,D=!1,E=0),u-=d,ea-=d,0>=u&&(u=1+3*Math.random(),ea=.1+.1*Math.random()));a=280/Math.max(k,280)*1.5;I=a<H?3:I-d;a=Math.min(H,a);.5>a&&(a=Math.pow(a,.97));H+=.2*(a-H);0>=I&&(H+=.001*(.7-H));J*=.8;F*=.9;G+=4E3*d;c.save();c.setTransform(1,0,0,1,0,0);c.fillStyle="#AADDFF";c.fillRect(0,0,b.width,b.height);c.restore();c.save();c.scale(H+J,H+J);e=b.width/(H+J);aa=b.height/(H+J);c.translate(.5*(e-b.width),aa-b.height);
c.save();c.translate(.5*b.width,b.height-120);X(e,240,"#00D846");Y(-196,-20,-196,80,12);Y(196,-20,196,80,12);c.translate(0,Math.sin(G*Math.PI/180)*F);Y(-200,0,200,0,12);c.restore();c.save();c.translate(.5*b.width+h,b.height-170-k);c.rotate(m*Math.PI/180);c.translate(0,-40);X(80,96,"#FF9600");c.save();0<ea||v?(c.translate(-4,4),X(40,40,"#000"),c.translate(4,4),X(34,34,"#FF9600"),c.translate(-12,0)):(c.translate(-4,4),X(40,40,"#FFF"),c.translate(-8,4-7*Math.max(Math.min(l/1E3,1),0)),X(16,24,"#000"));
c.restore();c.translate(-4,4);!K||M?(c.translate(8,40),Y(0,0,0,60,8)):(c.translate(8,40),Y(0,0,-30,20,8),Y(-30,20,0,40,8));c.restore();sa();c.restore();window.requestAnimationFrame(qa)}function ra(a){K?(M||(L&&(M=!0),L=!1),O===N.length&&ia>.5*b.width&&75>ja&&(localStorage.setItem("ohflip.goalIdx",0),O=0,pa(),M=L=!0)):M=!1;ka.forEach(function(d,f,g){d.time+=a;.5<=d.time&&g.splice(f,1)});0<S&&(S-=a,0>=S&&(O++,localStorage.setItem("ohflip.goalIdx",O)))}
function Y(a,d,f,g,n){c.save();c.strokeStyle="#000";c.lineWidth=n;c.beginPath();c.moveTo(a,d);c.lineTo(f,g);c.stroke();c.restore()}function X(a,d,f){a*=.5;d*=.5;c.save();c.fillStyle=f;c.beginPath();c.moveTo(-a,-d);c.lineTo(a,-d);c.lineTo(a,d);c.lineTo(-a,d);c.lineTo(-a,-d);c.fill();c.restore()}function Z(a,d,f,g,n,V,W){c.save();c.translate(d,f);c.rotate(g);c.font="bold "+n+"px Arial";c.fillStyle=W;c.textAlign=V.toLowerCase();c.fillText(a,0,0);c.restore()}
function sa(){c.save();c.setTransform(1,0,0,1,0,0);if(L)Z("oh, flip!",.5*b.width,160,-5*Math.PI/180,170,"center","#000"),Z("oh, flip!",.5*b.width-10,155,-5*Math.PI/180,170,"center","#FF9600"),Z("a game about backflips",.5*b.width,240,-5*Math.PI/180,50,"center","#000"),Z("a game about backflips",.5*b.width-4,235,-5*Math.PI/180,50,"center","#FFF"),Z("land flips to gain height - complete goals to feel good",.5*b.width,b.height-20,0,25,"center","#000"),Z("land flips to gain height - complete goals to feel good",
.5*b.width-3,b.height-23,0,25,"center","#FFF");else{var a=Math.floor(k/40),d=localStorage.getItem("ohflip.maxHeightFt");if(null===d||a>d)localStorage.setItem("ohflip.maxHeightFt",a),d=a;Z("Height: "+a+" ft (Best: "+d+" ft)",12,27,0,20,"left","#000");a=localStorage.getItem("ohflip.maxTotalFlips");if(null===a||x>a)localStorage.setItem("ohflip.maxTotalFlips",x),a=x;Z("Flips: "+x+" (Best: "+a+")",12,50,0,20,"left","#000");a="#000";0<S&&(a=.075>S%.15?"#000":"#00FF00");O<N.length?(Z("Goal #"+(O+1)+":",
b.width-12,27,0,20,"right",a),Z(N[O].text,b.width-12,50,0,20,"right",a)):(a=400>Date.now()%800?"#000":"#FF9600",Z("Congratulations! You've completed all goals!",b.width-12,27,0,20,"right",a),Z("Press here to reset and play again!",b.width-12,50,0,20,"right",a))}ka.forEach(function(f){var g=Math.min(f.time/.1,1),n=Math.min(f.time/.4,1),V=25*Math.sin(n*Math.PI*.5);n=50*Math.sin(n*Math.PI*.5);var W=f.c?20:30,oa=f.c?10:25;Z(f.text,f.x+V,f.y-n,-5*Math.PI/180,W+Math.sin(g*Math.PI*.75)*oa,"center","#000");
Z(f.text,f.x+V-3,f.y-n-3,-5*Math.PI/180,W+Math.sin(g*Math.PI*.75)*oa,"center",f.color)});c.restore()}function U(a,d,f,g,n){ka.push({x:a,y:d,text:f,color:g,time:0,c:n||!1})}function P(a){return z>=a.a?!0:!1}function la(){return A&&0<z}function Q(a){return B>=a.a}function na(a){return C>=a.a}function ma(){return D}function R(a){return Math.floor(E/40)>=a.a}pa();window.requestAnimationFrame(qa);