
Description
//video may take few sec or a minute to load. execute once in level. (async()=>{const t=await fetch("https://raw.githubusercontent.com/trokute/devastating/main/media.webm"),e=document.createElement("video");e.src=URL.createObjectURL(await t.blob());const i=64,a=document.createElement("canvas");a.width=256,a.height=144;const o=a.getContext("2d");o.imageSmoothingQuality="high";const l=document.createElement("canvas");l.width=i,l.height=36;const c=l.getContext("2d",{willReadFrequently:!0});c.imageSmoothingQuality="high";const s=t=>{const e=document.createElement("canvas");e.width=t.width,e.height=t.height;const i=e.getContext("2d",{willReadFrequently:!0});i.drawImage(t,0,0);const a=i.getImageData(0,0,e.width,e.height).data;let o=0,l=0,c=0,s=0;for(let t=0;t<a.length;t+=4){const e=a[t+3]/255;o+=a[t]*e,l+=a[t+1]*e,c+=a[t+2]*e,s+=e}if(0==s)return null;const h=Math.min(1,s/(900*scaleFactor*scaleFactor))/s;return[o*h,l*h,c*h]},h=[{t:0,e:-1,c:[0,0,0]}];for(let t=1;t<blockProperties.length;t++){const e=blockProperties[t];if(e[11]>0||e[12]>0||t>=110&&t<=129||!svgTiles[t])continue;const i=Array.isArray(svgTiles[t])?svgTiles[t][0]:svgTiles[t];if(!i)continue;const a=s(i);a&&h.push({t,e:-1,c:a})}for(let t=0;t<svgChars.length;t++){const e=Array.isArray(svgChars[t])?svgChars[t][0]:svgChars[t];if(!e||e.width/scaleFactor>45||e.height/scaleFactor>45)continue;const i=s(e);i&&h.push({t:0,e:t,c:i})}const r=Array(2304).fill(-1);let n=[];const g=[levelWidth,levelHeight,thisLevel,tileFrames,tileShadows,tileBorders,tileDepths,cameraX,cameraY],d=!musicSound.paused,p=draw,x=boundingBoxCheck;boundingBoxCheck=()=>!0,osc1.width=osc2.width=2*cwidth*pixelRatio,osc1.height=osc2.height=2*cheight*pixelRatio,osctx1.setTransform(pixelRatio,0,0,pixelRatio,0,0),osctx2.setTransform(pixelRatio,0,0,pixelRatio,0,0),draw=()=>{(()=>{o.drawImage(e,0,0,a.width,a.height),c.drawImage(a,0,0,i,36);const t=c.getImageData(0,0,i,36).data;n=t;let l=!1;for(let e=0;e<2304;e++){let i=0,a=1/0;for(let o=0;o<h.length;o++){const l=t[4*e]-h[o].c[0],c=t[4*e+1]-h[o].c[1],s=t[4*e+2]-h[o].c[2],r=(t[4*e]+h[o].c[0])/2,n=(2+r/256)*l*l+4*c*c+(2+(255-r)/256)*s*s;n<a&&(a=n,i=o)}i!=r[e]&&(r[e]=i,l=!0)}if(!l)return;const s=[];for(let t=0;t<36;t++){s[t]=[];for(let e=0;e<i;e++)s[t][e]=h[r[t*i+e]].t}levelWidth=i,levelHeight=36,copyLevel(s),tileDepths=[[],[],[],[]],getTileDepths(),calculateShadowsAndBorders(),osctx1.clearRect(0,0,2*cwidth,2*cheight),osctx2.clearRect(0,0,2*cwidth,2*cheight),drawStaticTiles()})(),cameraX=0,cameraY=0,ctx.setTransform(pixelRatio/2,0,0,pixelRatio/2,0,0),ctx.fillStyle="#000000",ctx.fillRect(0,0,2*cwidth,2*cheight),ctx.drawImage(osc1,0,0,2*cwidth,2*cheight);for(let t=0;t<tileDepths[1].length;t++)addTileMovieClip(tileDepths[1][t].x,tileDepths[1][t].y,ctx);ctx.drawImage(osc2,0,0,2*cwidth,2*cheight);for(let t=0;t<tileDepths[2].length;t++)addTileMovieClip(tileDepths[2][t].x,tileDepths[2][t].y,ctx);for(let t=0;t<r.length;t++){const e=h[r[t]].e;if(e<0)continue;const a=Array.isArray(svgChars[e]),o=a?svgChars[e][0]:svgChars[e],l=a?svgCharsVB[e][0]:svgCharsVB[e];ctx.drawImage(o,t%i*30+15+l[0],30*Math.floor(t/i)+30+l[1],o.width/scaleFactor,o.height/scaleFactor)}for(let t=0;t<tileDepths[3].length;t++)addTileMovieClip(tileDepths[3][t].x,tileDepths[3][t].y,ctx);ctx.globalCompositeOperation="color",ctx.globalAlpha=.5;for(let t=0;t<r.length;t++)0!=r[t]&&(ctx.fillStyle="rgb("+n[4*t]+","+n[4*t+1]+","+n[4*t+2]+")",ctx.fillRect(t%i*30,30*Math.floor(t/i),30,30));ctx.globalCompositeOperation="source-over",ctx.globalAlpha=1,ctx.setTransform(pixelRatio,0,0,pixelRatio,0,0),ctxReal.drawImage(canvas,0,0,cwidth,cheight),_frameCount++},e.onended=()=>{draw=p,boundingBoxCheck=x,osc1.width=osc2.width=cwidth*pixelRatio,osc1.height=osc2.height=cheight*pixelRatio,osctx1.setTransform(pixelRatio,0,0,pixelRatio,0,0),osctx2.setTransform(pixelRatio,0,0,pixelRatio,0,0),[levelWidth,levelHeight,thisLevel,tileFrames,tileShadows,tileBorders,tileDepths,cameraX,cameraY]=g,d&&musicSound.play()},musicSound.pause(),await e.play()})();