const e=1920,l=1080,n={running:0,numPlayers:1,rAF:-1,lastRender:-12345,ticksRender:0,ticksPhysics:0,music:new Audio("sfx/dudu.opus"),bg:new Image,players:[{a:"🤖",ix:760,h:["",""],p:{b:0,s:0}},{a:"🤡",ix:1160,h:["",""],p:{b:0,s:0}}],ufo:{x:960,v:-2},falling:[],start:e=>{n.running||(n.numPlayers=e,n.running=1,a(),n.start=()=>{})},click:()=>{n.running=1},move:l=>{n.players[0].ix=l.x/(i.clientWidth/e)}};let i,t;export function part(e){n.exit=e.next,onclick=()=>n.start(1),onkeydown=e=>{if(-1!==["Space","Enter","NumpadEnter","ControlLeft","ControlRight","ShiftLeft","ShiftRight"].indexOf(e.code)&&n.start(2),!n.running)return;-1!==["ArrowLeft","KeyA"].indexOf(e.code)&&(n.players[1].ix=n.players[1].ix-50),-1!==["ArrowRight","KeyD"].indexOf(e.code)&&(n.players[1].ix=n.players[1].ix+50)},n.rAF=requestAnimationFrame(f)}function a(){i=document.createElement("canvas"),i.width=e,i.height=l,i.style.cssText="width:100vw;height:100vh;cursor:none;",t=i.getContext("2d",{alpha:!1}),document.body.innerHTML="",document.body.appendChild(i),n.music.onended=()=>{n.running=0,setTimeout((()=>{n.exit()}),4e3)},n.music.loop=!1,n.bg.onload=()=>{n.music.play(),onmousemove=n.move,"undefined"!=typeof ontouchstart&&(ontouchstart=n.click,ontouchmove=n.move)},n.bg.src="gfx/Givry2.png"}function r(){const e="🌭,🍺".split(",");n.falling.push({i:e[o(0,e.length-1)],x:n.ufo.x+100,vx:Math.min(.5,Math.max(-.5,n.ufo.v/3)),y:200,vy:Math.max(1,5*Math.random())})}function s(){n.ticksPhysics++;const e=performance.now();n.lastRender;if(n.lastRender=e,!n.running)return;const i=n.music.currentTime/n.music.duration;if((n.ufo.x<300||n.ufo.x>1200)&&(n.ufo.v=-1*Math.sign(n.ufo.v)*o(3,8),n.ufo.x<300&&(n.ufo.x+=20),n.ufo.x>1200&&(n.ufo.x-=20)),n.ufo.x+=n.ufo.v,n.ticksPhysics%10==0&&i<.9){Math.random();let e=n.falling.length;0==e?r():e<2?Math.random()<.5&&r():e<4?Math.random()<.25&&r():e<6&&Math.random()<.125&&r()}for(let e=n.falling.length-1;e>-1;e--)if(n.falling[e].x+=.8*n.falling[e].vx,n.falling[e].y+=1.3*n.falling[e].vy,n.falling[e].y>l)n.falling.splice(e,1);else if(n.falling[e].y>950)for(let l=0;l<n.numPlayers;l++)if(n.falling[e].x+60>n.players[l].x&&n.falling[e].x<n.players[l].x+60){if(""!=n.players[l].h[0]&&""!=n.players[l].h[1])continue;if(""==n.players[l].h[0]&&""==n.players[l].h[1]){n.falling[e].x<n.players[l].x?n.players[l].h[0]=n.falling[e].i:n.players[l].h[1]=n.falling[e].i,n.falling.splice(e,1);break}if(""==n.players[l].h[0]){n.players[l].h[0]=n.falling[e].i,n.falling.splice(e,1);break}if(""==n.players[l].h[1]){n.players[l].h[1]=n.falling[e].i,n.falling.splice(e,1);break}}for(let e=0;e<n.numPlayers;e++)n.players[e].ix<100&&(n.players[e].ix=100,"🍺"==n.players[e].h[0]&&(n.players[e].h[0]="",n.players[e].p.b++),"🍺"==n.players[e].h[1]&&(n.players[e].h[1]="",n.players[e].p.b++)),n.players[e].ix>1680&&(n.players[e].ix=1680,"🌭"==n.players[e].h[0]&&(n.players[e].h[0]="",n.players[e].p.s++),"🌭"==n.players[e].h[1]&&(n.players[e].h[1]="",n.players[e].p.s++)),n.players[e].x=n.players[e].ix}function f(){if(n.rAF=requestAnimationFrame(f),u(),n.running){s(),n.ticksRender++,t.clearRect(0,0,e,l),t.drawImage(n.bg,0,0,e,l),t.font="50px sans-serif",t.fillText("🤖",10,50),t.fillText("🍺",10,110),t.fillText("🌭",10,170),t.fillStyle="#000",t.fillText(n.players[0].p.b,80,110),t.fillText(n.players[0].p.s,80,170),n.numPlayers>1&&(t.fillText("🤡",1850,50),t.fillText("🍺",1850,110),t.fillText("🌭",1850,170),t.fillStyle="#fff",t.fillText(n.players[1].p.b,1805,110),t.fillText(n.players[1].p.s,1805,170)),t.font="300px sans-serif",t.fillText("🛸",n.ufo.x,220),t.fillStyle="blue",t.fillRect(10,960,100,110),t.fillRect(1800,960,110,110),t.font="50px sans-serif",t.fillText("🍺",25,1010),t.fillText("🍺",10,1050),t.fillText("🍺",40,1050),t.fillText("🌭",1800,1050),t.fillText("🌭",1840,1050),t.fillText("🌭",1825,1010);for(let e=0;e<n.falling.length;e++)t.font="100px sans-serif",t.fillText(n.falling[e].i,n.falling[e].x,n.falling[e].y);for(let e=n.numPlayers-1;e>-1;e--)t.font="100px sans-serif",t.fillText(n.players[e].a,n.players[e].x,1050),t.font="50px sans-serif",t.fillText(n.players[e].h[0],n.players[e].x-40,1050),t.fillText(n.players[e].h[1],n.players[e].x+110,1050)}}function o(e,l){return e=Math.ceil(e),l=Math.floor(l),Math.floor(Math.random()*(l-e+1))+e}function p(){navigator.vibrate&&navigator.vibrate(10)}function x(){navigator.vibrate&&navigator.vibrate(100)}function u(){const e=navigator.getGamepads();let l=1;for(let i=0;i<e.length;i++){if(!n.running)for(let l=0;l<e[i]?.buttons.length;l++)e[i].buttons[l].pressed&&n.start(2);if(!e[i]?.axes[0])continue;if(l<0)return;if(!n.running)continue;const t=50,a=e[i]?.buttons[4]?.pressed||e[i]?.buttons[14]?.pressed,r=e[i]?.buttons[5]?.pressed||e[i]?.buttons[15]?.pressed;a&&!r&&(n.players[l].ix-=t),r&&!a&&(n.players[l].ix+=t),l--}}