forked from wololoa/js13k2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_all_minified.html
31 lines (27 loc) · 37.6 KB
/
index_all_minified.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html><html>
<style type="text/css">
.body {
color: black;
}
.text {
position:absolute;
width: 100%;
z-index: 1;
font-size: 200%;
font-family: consolas;
color: orange;
text-align:center;
}
</style>
<div id="upper" class="text" style="bottom:5%;"></div>
<body>
<script>
"use strict";function rnd(t,e){return e||(e=t,t=0),Math.random()*(e-t)+t}function rndi(t,e){return parseInt(rnd(t,e))}function rndf(t,e){return Math.floor(rnd(t,e))}function rndc(){return[rnd(0,1),rnd(0,1),rnd(0,1),1]}function rndc256(){return[rnd(0,255),rnd(0,255),rnd(0,255),1]}function rnda(t){return t[Math.floor(Math.random()*t.length)]}function rgba(t,e,i,s){return"rgba("+t+","+e+","+i+","+(s||1)+")"}function argba(t){return 1==t.length?rgba(t[0],t[0],t[0],1):rgba(t[0],t[1],t[2],t[3]||1)}function clamp(t,e,i){return Math.min(Math.max(t,e),i)}function lerp(t,e,i){return t+i*(e-t)}function lerpc(t,e,i){return[lerp(t[0],e[0],i),lerp(t[1],e[1],i),lerp(t[2],e[2],i),1]}var GL=function(){var t,e=305397760;class i{constructor(){var t=Array.prototype.concat.apply([],arguments);t.length||(t=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]),this.m=new Float32Array(t)}multiply(t){return i.multiply(this,t,new i)}}i.multiply=function(t,e,s){s=s||new i;var r=t.m,o=e.m,n=s.m;return n[0]=r[0]*o[0]+r[1]*o[4]+r[2]*o[8]+r[3]*o[12],n[1]=r[0]*o[1]+r[1]*o[5]+r[2]*o[9]+r[3]*o[13],n[2]=r[0]*o[2]+r[1]*o[6]+r[2]*o[10]+r[3]*o[14],n[3]=r[0]*o[3]+r[1]*o[7]+r[2]*o[11]+r[3]*o[15],n[4]=r[4]*o[0]+r[5]*o[4]+r[6]*o[8]+r[7]*o[12],n[5]=r[4]*o[1]+r[5]*o[5]+r[6]*o[9]+r[7]*o[13],n[6]=r[4]*o[2]+r[5]*o[6]+r[6]*o[10]+r[7]*o[14],n[7]=r[4]*o[3]+r[5]*o[7]+r[6]*o[11]+r[7]*o[15],n[8]=r[8]*o[0]+r[9]*o[4]+r[10]*o[8]+r[11]*o[12],n[9]=r[8]*o[1]+r[9]*o[5]+r[10]*o[9]+r[11]*o[13],n[10]=r[8]*o[2]+r[9]*o[6]+r[10]*o[10]+r[11]*o[14],n[11]=r[8]*o[3]+r[9]*o[7]+r[10]*o[11]+r[11]*o[15],n[12]=r[12]*o[0]+r[13]*o[4]+r[14]*o[8]+r[15]*o[12],n[13]=r[12]*o[1]+r[13]*o[5]+r[14]*o[9]+r[15]*o[13],n[14]=r[12]*o[2]+r[13]*o[6]+r[14]*o[10]+r[15]*o[14],n[15]=r[12]*o[3]+r[13]*o[7]+r[14]*o[11]+r[15]*o[15],s},i.identity=function(t){var e=(t=t||new i).m;return e[0]=e[5]=e[10]=e[15]=1,e[1]=e[2]=e[3]=e[4]=e[6]=e[7]=e[8]=e[9]=e[11]=e[12]=e[13]=e[14]=0,t},i.perspective=function(t,e,s,r,o){var n=Math.tan(t*Math.PI/360)*s,a=n*e;return i.frustum(-a,a,-n,n,s,r,o)},i.frustum=function(t,e,s,r,o,n,a){var h=(a=a||new i).m;return h[0]=2*o/(e-t),h[1]=0,h[2]=(e+t)/(e-t),h[3]=0,h[4]=0,h[5]=2*o/(r-s),h[6]=(r+s)/(r-s),h[7]=0,h[8]=0,h[9]=0,h[10]=-(n+o)/(n-o),h[11]=-2*n*o/(n-o),h[12]=0,h[13]=0,h[14]=-1,h[15]=0,a},i.scale=function(t,e,s,r){var o=(r=r||new i).m;return o[0]=t,o[1]=0,o[2]=0,o[3]=0,o[4]=0,o[5]=e,o[6]=0,o[7]=0,o[8]=0,o[9]=0,o[10]=s,o[11]=0,o[12]=0,o[13]=0,o[14]=0,o[15]=1,r},i.translate=function(t,e,s,r){var o=(r=r||new i).m;return o[0]=1,o[1]=0,o[2]=0,o[3]=t,o[4]=0,o[5]=1,o[6]=0,o[7]=e,o[8]=0,o[9]=0,o[10]=1,o[11]=s,o[12]=0,o[13]=0,o[14]=0,o[15]=1,r},i.rotate=function(t,e,s,r,o){if(!t||!e&&!s&&!r)return i.identity(o);var n=(o=o||new i).m,a=Math.sqrt(e*e+s*s+r*r);t*=Math.PI/180,e/=a,s/=a,r/=a;var h=Math.cos(t),l=Math.sin(t),c=1-h;return n[0]=e*e*c+h,n[1]=e*s*c-r*l,n[2]=e*r*c+s*l,n[3]=0,n[4]=s*e*c+r*l,n[5]=s*s*c+h,n[6]=s*r*c-e*l,n[7]=0,n[8]=r*e*c-s*l,n[9]=r*s*c+e*l,n[10]=r*r*c+h,n[11]=0,n[12]=0,n[13]=0,n[14]=0,n[15]=1,o};class s{constructor(t,e){this.buffer=null,this.target=t,this.type=e,this.data=[]}compile(e){for(var i=[],s=0;s<this.data.length;s+=1e4)i=Array.prototype.concat.apply(i,this.data.slice(s,s+1e4));var r=this.data.length?i.length/this.data.length:0;if(r!=Math.round(r))throw new Error("buffer elements not of consistent size, average size is "+r);this.buffer=this.buffer||t.createBuffer(),this.buffer.length=i.length,this.buffer.spacing=r,t.bindBuffer(this.target,this.buffer),t.bufferData(this.target,new this.type(i),e||35044)}}class r{constructor(t){t=t||{},this.vertexBuffers={},this.indexBuffers={},this.addVertexBuffer("vertices","gl_Vertex"),t.coords&&this.addVertexBuffer("coords","gl_TexCoord"),t.normals&&this.addVertexBuffer("normals","gl_Normal"),t.colors&&this.addVertexBuffer("colors","gl_Color"),"triangles"in t&&!t.triangles||this.addIndexBuffer("triangles"),t.lines&&this.addIndexBuffer("lines")}addVertexBuffer(t,e){(this.vertexBuffers[e]=new s(34962,Float32Array)).name=t,this[t]=[]}addIndexBuffer(t){this.indexBuffers[t]=new s(34963,Uint16Array);this[t]=[]}compile(){for(var t in this.vertexBuffers){(i=this.vertexBuffers[t]).data=this[i.name],i.compile()}for(var e in this.indexBuffers){var i;(i=this.indexBuffers[e]).data=this[e],i.compile()}}computeNormals(){this.normals||this.addVertexBuffer("normals","gl_Normal");for(var t=0;t<this.vertices.length;t++)this.normals[t]=new u;for(t=0;t<this.triangles.length;t++){var e=this.triangles[t],i=u.fromArray(this.vertices[e[0]]),s=u.fromArray(this.vertices[e[1]]),r=u.fromArray(this.vertices[e[2]]),o=s.subtract(i).cross(r.subtract(i)).unit();this.normals[e[0]]=this.normals[e[0]].add(o),this.normals[e[1]]=this.normals[e[1]].add(o),this.normals[e[2]]=this.normals[e[2]].add(o)}for(t=0;t<this.vertices.length;t++)this.normals[t]=this.normals[t].unit().toArray();return this.compile(),this}}function o(t,e,i){for(var s;null!=(s=t.exec(e));)i(s)}function n(t){var e=Object.prototype.toString.call(t);return"[object Number]"==e||"[object Boolean]"==e}r.cube2=function(t){let e=new r({coords:!0,normals:!0,colors:!0});e.vertices=[],e.coords=[],e.triangles=[],e.colors=[],e.normals=[];let i=0,s=1-.125*(7-i),o=.0625,n=.5625;i=0,s=1-o*(15-i);function a(i,r){1==t&&(i=0,r=2),0==i?(s=1-o*(15-r),e.coords.push([s,n],[s-o,n],[s,.5],[s-o,.5])):1==i&&(s=1-(.125*(7-r)+o),e.coords.push([s+o,.625],[s-o,.625],[s+o,n],[s-o,n]))}e.vertices.push([-1,-1,-1],[1,-1,-1],[-1,-1,1],[1,-1,1]),a(1,0),e.vertices.push([-1,1,-1],[1,1,-1],[-1,1,1],[1,1,1]),a(1,0),e.vertices.push([-1,-1,-1],[1,-1,-1],[-1,1,-1],[1,1,-1]),a(1,0),e.vertices.push([-1,-1,1],[1,-1,1],[-1,1,1],[1,1,1]),a(1,0),e.vertices.push([-1,-1,-1],[-1,-1,1],[-1,1,-1],[-1,1,1]),a(0,0),e.vertices.push([1,-1,-1],[1,-1,1],[1,1,-1],[1,1,1]),a(0,0);for(var h=0,l=0;l<6;l++){h=4*l,e.triangles.push([0+h,1+h,2+h],[2+h,1+h,3+h]);for(let t=0;t<4;t++)e.colors.push([.5,.5,.5,1])}return e.computeNormals(),e.compile(),e};var a,h,l="LIGHTGL";class c{constructor(e,i,s){s=s||{},this.id=t.createTexture(),this.width=e,this.height=i,this.format=s.format||6408,this.type=s.type||5121;var r=s.filter||s.magFilter||9729,o=s.filter||s.minFilter||9729;t.bindTexture(3553,this.id),t.pixelStorei(37440,1),t.texParameteri(3553,10240,r),t.texParameteri(3553,10241,o),t.texParameteri(3553,10242,s.wrap||s.wrapS||33071),t.texParameteri(3553,10243,s.wrap||s.wrapT||33071),t.texImage2D(3553,0,this.format,e,i,0,this.format,this.type,s.data||null)}bind(e){t.activeTexture(33984+(e||0)),t.bindTexture(3553,this.id)}unbind(e){t.activeTexture(33984+(e||0)),t.bindTexture(3553,null)}canDrawTo(){a=a||t.createFramebuffer(),t.bindFramebuffer(36160,a),t.framebufferTexture2D(36160,36064,3553,this.id,0);var e=36053==t.checkFramebufferStatus(36160);return t.bindFramebuffer(36160,null),e}drawTo(e){var i=t.getParameter(2978);if(a=a||t.createFramebuffer(),h=h||t.createRenderbuffer(),t.bindFramebuffer(36160,a),t.bindRenderbuffer(36161,h),this.width==h.width&&this.height==h.height||(h.width=this.width,h.height=this.height,t.renderbufferStorage(36161,33189,this.width,this.height)),t.framebufferTexture2D(36160,36064,3553,this.id,0),t.framebufferRenderbuffer(36160,36096,36161,h),36053!=t.checkFramebufferStatus(36160))throw new Error("Rendering to this texture is not supported (incomplete framebuffer)");t.viewport(0,0,this.width,this.height),e(),t.bindFramebuffer(36160,null),t.bindRenderbuffer(36161,null),t.viewport(i[0],i[1],i[2],i[3])}swapWith(t){var e;e=t.id,t.id=this.id,this.id=e,e=t.width,t.width=this.width,this.width=e,e=t.height,t.height=this.height,this.height=e}}c.fromImage=function(e,i){i=i||{};var s=new c(e.width,e.height,i);try{t.texImage2D(3553,0,s.format,s.format,s.type,e)}catch(t){console.log("no texture")}return s};class u{constructor(t,e,i){this.x=t||0,this.y=e||0,this.z=i||0}set(t,e,i){return this.x=t,this.y=e,this.z=i,this}negative(){return new u(-this.x,-this.y,-this.z)}add(t){return t instanceof u?new u(this.x+t.x,this.y+t.y,this.z+t.z):new u(this.x+t,this.y+t,this.z+t)}subtract(t){return t instanceof u?new u(this.x-t.x,this.y-t.y,this.z-t.z):new u(this.x-t,this.y-t,this.z-t)}multiply(t){return t instanceof u?new u(this.x*t.x,this.y*t.y,this.z*t.z):new u(this.x*t,this.y*t,this.z*t)}divide(t){return t instanceof u?new u(this.x/t.x,this.y/t.y,this.z/t.z):new u(this.x/t,this.y/t,this.z/t)}dot(t){return this.x*t.x+this.y*t.y+this.z*t.z}cross(t){return new u(this.y*t.z-this.z*t.y,this.z*t.x-this.x*t.z,this.x*t.y-this.y*t.x)}length(){return Math.sqrt(this.dot(this))}unit(){return this.divide(this.length())}angleTo(t){return Math.acos(this.dot(t)/(this.length()*t.length()))}toArray(t){return[this.x,this.y,this.z].slice(0,t||3)}clone(){return new u(this.x,this.y,this.z)}}u.fromAngles=function(t,e){return new u(Math.cos(t)*Math.cos(e),Math.sin(e),Math.sin(t)*Math.cos(e))},u.randomDirection=function(){return u.fromAngles(Math.random()*Math.PI*2,Math.asin(2*Math.random()-1))},u.fromArray=function(t){return new u(t[0],t[1],t[2])};var d={keys:{},Matrix:i,Buffer:s,Mesh:r,Shader:class{constructor(e,i){var s=e,r=i,n=" uniform mat4 gl_ModelViewMatrix; uniform mat4 gl_ModelViewProjectionMatrix; ",a=n+" attribute vec4 gl_Vertex; attribute vec4 gl_TexCoord; attribute vec3 gl_Normal; attribute vec4 gl_Color; vec4 ftransform() { return gl_ModelViewProjectionMatrix * gl_Vertex; } ",h=" precision highp float; "+n,c=s+r,u={};function d(t,e){var i={},s=/^((\s*\/\/.*\n|\s*#extension.*\n)+)[^]*$/.exec(e);return e=s?s[1]+t+e.substr(s[1].length):t+e,o(/\bgl_\w+\b/g,t,(function(t){t in i||(e=e.replace(new RegExp("\\b"+t+"\\b","g"),l+t),i[t]=!0)})),e}function m(e,i){var s=t.createShader(e);if(t.shaderSource(s,i),t.compileShader(s),!t.getShaderParameter(s,t.COMPILE_STATUS))throw console.log("ERROR COMPILING "+(35633==e?"VERTEX":"FRAGMENT")+" SHADER: ",i),new Error("compile error: "+t.getShaderInfoLog(s));return s}if(o(/\b(gl_[^;]*)\b;/g,n,(function(t){var e=t[1];if(-1!=c.indexOf(e)){var i=e.replace(/[a-z_]/g,"");u[i]=l+e}})),-1!=c.indexOf("ftransform")&&(u.MVPM=l+"gl_ModelViewProjectionMatrix"),this.usedMatrices=u,s=d(a,s),r=d(h,r),this.program=t.createProgram(),t.attachShader(this.program,m(t.VERTEX_SHADER,s)),t.attachShader(this.program,m(t.FRAGMENT_SHADER,r)),t.linkProgram(this.program),!t.getProgramParameter(this.program,t.LINK_STATUS))throw new Error("link error: "+t.getProgramInfoLog(this.program));this.attributes={},this.uniformLocations={};var f={};o(/uniform\s+sampler(1D|2D|3D|Cube)\s+(\w+)\s*;/g,s+r,(function(t){f[t[2]]=1})),this.isSampler=f}uniforms(e){for(var s in t.useProgram(this.program),e){var r=this.uniformLocations[s]||t.getUniformLocation(this.program,s);if(r){this.uniformLocations[s]=r;var o=e[s];if(o instanceof u?o=[o.x,o.y,o.z]:o instanceof i&&(o=o.m),a=o,h=void 0,"[object Array]"==(h=Object.prototype.toString.call(a))||"[object Float32Array]"==h)switch(o.length){case 1:t.uniform1fv(r,new Float32Array(o));break;case 2:t.uniform2fv(r,new Float32Array(o));break;case 3:t.uniform3fv(r,new Float32Array(o));break;case 4:t.uniform4fv(r,new Float32Array(o));break;case 9:t.uniformMatrix3fv(r,!1,new Float32Array([o[0],o[3],o[6],o[1],o[4],o[7],o[2],o[5],o[8]]));break;case 16:t.uniformMatrix4fv(r,!1,new Float32Array([o[0],o[4],o[8],o[12],o[1],o[5],o[9],o[13],o[2],o[6],o[10],o[14],o[3],o[7],o[11],o[15]]));break;default:throw new Error("don't know how to load uniform \""+s+'" of length '+o.length)}else{if(!n(o))throw new Error('attempted to set uniform "'+s+'" to invalid value '+o);(this.isSampler[s]?t.uniform1i:t.uniform1f).call(t,r,o)}}}var a,h;return this}draw(t,e){this.drawBuffers(t.vertexBuffers,t.indexBuffers.triangles,4)}drawBuffers(e,i,s){var r=this.usedMatrices,o=t.modelviewMatrix,n=t.projectionMatrix,a=r.MVPM?n.multiply(o):null,h={};r.MVPM&&(h[r.MVPM]=a),r.MVM&&(h[r.MVM]=o),this.uniforms(h);var c=0;for(var u in e){var d=e[u],m=this.attributes[u]||t.getAttribLocation(this.program,u.replace(/^(gl_.*)$/,l+"$1"));-1!=m&&d.buffer&&(this.attributes[u]=m,t.bindBuffer(t.ARRAY_BUFFER,d.buffer),t.enableVertexAttribArray(m),t.vertexAttribPointer(m,d.buffer.spacing,t.FLOAT,!1,0,0),c=d.buffer.length/d.buffer.spacing)}for(var u in this.attributes)u in e||t.disableVertexAttribArray(this.attributes[u]);return!c||i&&!i.buffer||(i?(t.bindBuffer(34963,i.buffer),t.drawElements(s,i.buffer.length,5123,0)):t.drawArrays(s,0,c)),this}},Texture:c,Vector:u,create:function(s){s=s||{};var r=document.createElement("canvas");r.width=800,r.height=600,"alpha"in s||(s.alpha=!1);try{t=r.getContext("webgl",s)}catch(t){}try{t=t||r.getContext("experimental-webgl",s)}catch(t){}if(!t)throw new Error("WebGL not supported");return r.addEventListener("click",(async()=>{await r.requestPointerLock()})),function(){t.MODELVIEW=1|e,t.PROJECTION=2|e;var s=new i,r=new i;t.modelviewMatrix=new i,t.projectionMatrix=new i;var o,n,a=[],h=[];t.matrixMode=function(e){switch(e){case t.MODELVIEW:o="modelviewMatrix",n=a;break;case t.PROJECTION:o="projectionMatrix",n=h;break;default:throw new Error("invalid matrix mode "+e)}},t.loadIdentity=function(){i.identity(t[o])},t.loadMatrix=function(e){for(var i=e.m,s=t[o].m,r=0;r<16;r++)s[r]=i[r]},t.multMatrix=function(e){t.loadMatrix(i.multiply(t[o],e,r))},t.perspective=function(e,r,o,n){t.multMatrix(i.perspective(e,r,o,n,s))},t.scale=function(e,r,o){t.multMatrix(i.scale(e,r,o,s))},t.translate=function(e,r,o){t.multMatrix(i.translate(e,r,o,s))},t.rotate=function(e,r,o,n){t.multMatrix(i.rotate(e,r,o,n,s))},t.pushMatrix=function(){n.push(Array.prototype.slice.call(t[o].m))},t.popMatrix=function(){var e=n.pop();t[o].m=new Float32Array(e)},t.matrixMode(t.MODELVIEW)}(),function(){var e=t,i=0,s=0,r={},o=!1,n=Object.prototype.hasOwnProperty;function a(){for(var t in r)if(n.call(r,t)&&r[t])return!0;return!1}function h(e){var r={};for(var n in e)"function"==typeof e[n]?r[n]=function(t){return function(){t.apply(e,arguments)}}(e[n]):r[n]=e[n];r.original=e,r.x=r.pageX,r.y=r.pageY,r.mx=r.movementX,r.my=r.movementY;for(var h=t.canvas;h;h=h.offsetParent)r.x-=h.offsetLeft,r.y-=h.offsetTop;return o?(r.deltaX=r.x-i,r.deltaY=r.y-s):(r.deltaX=0,r.deltaY=0,o=!0),i=r.x,s=r.y,r.dragging=a(),r.preventDefault=function(){r.original.preventDefault()},r.stopPropagation=function(){r.original.stopPropagation()},r}function l(i){t=e,a()||(f(document,"mousemove",c),f(document,"mouseup",u),x(t.canvas,"mousemove",c),x(t.canvas,"mouseup",u)),r[i.which]=!0,i=h(i),t.onmousedown&&t.onmousedown(i),i.preventDefault()}function c(i){t=e,i=h(i),t.onmousemove&&t.onmousemove(i),i.preventDefault()}function u(i){t=e,r[i.which]=!1,a()||(x(document,"mousemove",c),x(document,"mouseup",u),f(t.canvas,"mousemove",c),f(t.canvas,"mouseup",u)),i=h(i),t.onmouseup&&t.onmouseup(i),i.preventDefault()}function d(i){t=e,i=h(i),t.onmousewheel&&t.onmousewheel(i),i.preventDefault()}function m(){o=!1}function g(){r={},o=!1}f(t.canvas,"mousedown",l),f(t.canvas,"mousemove",c),f(t.canvas,"mouseup",u),f(t.canvas,"mouseover",m),f(t.canvas,"mouseout",m),f(t.canvas,"wheel",d),f(document,"contextmenu",g)}(),t.animate=function(){var e=(new Date).getTime(),i=t;function s(){t=i;var r=(new Date).getTime();t.onupdate&&t.onupdate((r-e)/1e3),t.ondraw&&t.ondraw(),window.requestAnimationFrame(s),e=r}s()},t.fullscreen=function(e){function i(){t.canvas.width=window.innerWidth,t.canvas.height=window.innerHeight,t.viewport(0,0,t.canvas.width,t.canvas.height),!e.camera&&"camera"in e||(t.matrixMode(t.PROJECTION),t.loadIdentity(),t.perspective(e.fov||45,t.canvas.width/t.canvas.height,e.near||.1,e.far||1e3),t.matrixMode(t.MODELVIEW)),t.ondraw&&t.ondraw()}e=e||{},document.body.appendChild(t.canvas),document.body.style.overflow="hidden",t.canvas.style.position="absolute",t.canvas.style.left="0px",t.canvas.style.top="0px",f(window,"resize",i),i()},t}};function m(t){return{8:"BACKSPACE",9:"TAB",13:"ENTER",16:"SHIFT",27:"ESCAPE",32:"SPACE",37:"LEFT",38:"UP",39:"RIGHT",40:"DOWN"}[t]||(t>=65&&t<=90?String.fromCharCode(t):null)}function f(t,e,i){t.addEventListener(e,i)}function x(t,e,i){t.removeEventListener(e,i)}return f(document,"keydown",(function(t){if(!t.altKey&&!t.ctrlKey&&!t.metaKey){var e=m(t.keyCode);e&&(d.keys[e]=!0),d.keys[t.keyCode]=!0}})),f(document,"keyup",(function(t){if(!t.altKey&&!t.ctrlKey&&!t.metaKey){var e=m(t.keyCode);e&&(d.keys[e]=!1),d.keys[t.keyCode]=!1}})),d}();
/**
* quadtree-js
* @version 1.2.6
* @license MIT
* @author Timo Hausmann
*/class Quadtree{constructor(t,e,i,s){this.mo=e||10,this.ml=i||4,this.lv=s||0,this.b=t,this.o=[],this.n=[]}split(){var t=this.lv+1,e=this.b.width/2,i=this.b.height/2,s=this.b.x,r=this.b.y;this.n[0]=new Quadtree({x:s+e,y:r,width:e,height:i},this.mo,this.ml,t),this.n[1]=new Quadtree({x:s,y:r,width:e,height:i},this.mo,this.ml,t),this.n[2]=new Quadtree({x:s,y:r+i,width:e,height:i},this.mo,this.ml,t),this.n[3]=new Quadtree({x:s+e,y:r+i,width:e,height:i},this.mo,this.ml,t)}getIndex(t){var e=[],i=this.b.x+this.b.width/2,s=this.b.y+this.b.height/2,r=t.position.x-t.extents.x,o=t.position.z-t.extents.z,n=o<s,a=r<i,h=r+2*t.extents.x>i,l=o+2*t.extents.z>s;return n&&h&&e.push(0),a&&n&&e.push(1),a&&l&&e.push(2),h&&l&&e.push(3),e}insert(t){var e,i=0;if(this.n.length)for(e=this.getIndex(t),i=0;i<e.length;i++)this.n[e[i]].insert(t);else if(this.o.push(t),this.o.length>this.mo&&this.lv<this.ml){for(this.n.length||this.split(),i=0;i<this.o.length;i++){e=this.getIndex(this.o[i]);for(var s=0;s<e.length;s++)this.n[e[s]].insert(this.o[i])}this.o=[]}}retrieve(t){var e=this.getIndex(t),i=this.o;if(this.n.length)for(var s=0;s<e.length;s++)i=i.concat(this.n[e[s]].retrieve(t));return 0===this.lv?Array.from(new Set(i)):i}clear(){this.o=[];for(var t=0;t<this.n.length;t++)this.n.length&&this.n[t].clear();this.n=[]}}class Body{constructor(t,e,i){this.tag=i,this.position=t,this.extents=e,this.velocity=new GL.Vector(0,0,0),this.mass=1,this.elasticity=.235,this.friction=.1,this.dynamic=!0,this.solid=!0,this.skipList=[],this.hits=[],this._N=new GL.Vector(0,0,0),this._xVel=new GL.Vector(0,0,0),this._Vn=new GL.Vector(0,0,0),this._Vt=new GL.Vector(0,0,0),SIM.addBody(this)}skip(t){this.skipList.push(t)}canSkip(t){return-1!==this.skipList.indexOf(t)}intersects(t){return SIM.testAABB(this,t)}processCollision(t,e){this._N.set(e.otherObj.x-e.thisObj.x,e.otherObj.y-e.thisObj.y,e.otherObj.z-e.thisObj.z);let i=this.calculateMassRatio(t,!0);if(!i.result)return;let s=i.thisObj,r=i.otherObj;this.position=this.position.add(this._N.multiply(s)),t.position=t.position.subtract(this._N.multiply(r)),this._xVel=this.velocity.subtract(t.velocity);let o=this._N.dot(this._xVel);if(o>0)return;let n=this._N.dot(this._N);i=this.calculateMassRatio(t,!1),s=i.thisObj,r=i.otherObj,this._Vn=this._N.multiply(o/n),this._Vt=this._Vt.clone(this._xVel.negative()),this.velocity=this.velocity.subtract(this._Vn.multiply((1+this.elasticity)*s).add(this._Vt.multiply(this.friction)))}calculateMassRatio(t,e){let i={result:!1,thisObj:0,otherObj:0},s=0,r=0,o=this.mass+t.mass;return o<1e-6||(t.mass<1e-7||!1===t.dynamic?(s=1,r=0):this.mass<1e-7||!1===this.dynamic?(s=0,r=1):e?(s=.5,r=1-s):(s=t.mass/o,r=1-s),i.result=!0,i.thisObj=s,i.otherObj=r),i}impulse(t){this.dynamic&&(this.velocity=this.velocity.add(t.divide(this.mass)))}isCol2(t,e){let i=!1,s=!1;for(let r=0;r<this.hits.length;r++)this.hits[r].tag==t&&(i=!0),this.hits[r].tag==e&&(s=!0);return i&&s}isCol3(t,e,i){var s=!1,r=!1,o=!1;for(let n=0;n<this.hits.length;n++)this.hits[n].tag==t&&(s=!0),this.hits[n].tag==e&&(r=!0),this.hits[n].tag==i&&(o=!0);return s&&r&&o}update(t){this.dynamic&&(this.position=this.position.add(this.velocity.multiply(t)),this.velocity=this.velocity.multiply(this.friction))}}class SimImpl{constructor(t){this.bodies=[],this.callbacks={},this._xMin0=new GL.Vector(0,0,0),this._xMax0=new GL.Vector(0,0,0),this._xMin1=new GL.Vector(0,0,0),this._xMax1=new GL.Vector(0,0,0),this._N=new GL.Vector(0,0,0),this._pDist=new GL.Vector(0,0,0),this.quadtree=new Quadtree(t,4,4,0)}onCollision(t,e,i){this.callbacks[t+e]={a:t,b:e,c:i}}doCallbacks(t,e){for(let i in this.callbacks){let i=t.tag+e.tag;if(this.callbacks[i])return this.callbacks[i].c(t,e),!0}return!1}addBody(t){this.bodies.push(t)}addStatic(t,e){for(let i=0;i<t.length;i++){let s=t[i].w/2,r=t[i].h/2,o=t[i].x+s,n=t[i].y+r,a=new Body(new GL.Vector(o,0,n),new GL.Vector(s,4,r),e);a.dynamic=!1,a.solid=!0,a.mass=0,this.addBody(a)}}axisIntersect(t,e,i,s,r){var o=s-t,n=e-i;return o<0||n<0?null:o<n?o:-n}impulse(t){for(let e=0;e<this.bodies.length;e++)this.bodies[e].dynamic&&(this.bodies[e].velocity=this.bodies[e].velocity.add(t.divide(this.bodies[e].mass)))}testAABB(t,e){var i={result:!1,thisObj:{},otherObj:{}};this._xMin0=t.position.subtract(t.extents),this._xMax0=t.position.add(t.extents),this._xMin1=e.position.subtract(e.extents),this._xMax1=e.position.add(e.extents),this._N.set(0,0,0);let s=this.axisIntersect(this._xMin0.x,this._xMax0.x,this._xMin1.x,this._xMax1.x,this._N.x),r=this.axisIntersect(this._xMin0.y,this._xMax0.y,this._xMin1.y,this._xMax1.y,this._N.y),o=this.axisIntersect(this._xMin0.z,this._xMax0.z,this._xMin1.z,this._xMax1.z,this._N.z);if(!s||!r||!o)return i;this._N.set(s,r,o);let n=Math.abs(this._N.x);return Math.abs(this._N.y)<n?(n=Math.abs(this._N.y),this._N.x=0):this._N.y=0,Math.abs(this._N.z)<n?(n=Math.abs(this._N.z),this._N.x=this._N.y=0):this._N.z=0,i.result=!0,i.thisObj={x:0,y:0,z:0},i.otherObj=this._N,i}updateQuadtree(t){let e=null;for(let e=0;e<this.bodies.length;e++)this.bodies[e].update(t),this.bodies[e].hits=[];this.quadtree.clear();for(var i=0;i<this.bodies.length;i++)this.quadtree.insert(this.bodies[i]);for(i=0;i<this.bodies.length;i++){var s=this.bodies[i];if(s.dynamic)for(var r=this.quadtree.retrieve(s),o=0;o<r.length;o++){0;var n=r[o];if(s===n)continue;let t=s,i=n;if(!t.canSkip(i.tag)&&!i.canSkip(t.tag)&&(e=t.intersects(i),!0===e.result)){t.solid&&i.solid&&t.processCollision(i,e),this.doCallbacks(t,i)||this.doCallbacks(i,t),t.hits.push(i),i.hits.push(t)}}}}}const SIM=new SimImpl({x:-10,y:-10,width:20,height:20});var MAP=null,NO_LIGHT=[.01,.01,.02,1];class Cell{constructor(t,e,i,s){this.c=t,this.r=e,this.x=i,this.y=s,this.e=!1,this.t=" ",this.w=0,this.l=NO_LIGHT}carve(t,e,i){for(let t=0;t<MAP.rooms.length;t++)this.c>=MAP.rooms[t].y/20&&this.c<MAP.rooms[t].y/20+MAP.rooms[t].h/20&&this.r>=MAP.rooms[t].x/20&&this.r<MAP.rooms[t].x/20+MAP.rooms[t].w/20&&(this.e=!0,this.t="R")}ccH(t,e,i){this.r>=e&&this.r<e+t&&this.c<i+1&&this.c>i-1&&(this.e=!0,this.t="C")}ccV(t,e,i){this.c>=i&&this.c<i+t&&this.r<e+1&&this.r>e-1&&(this.e=!0,this.t="C")}}class Room{constructor(t,e,i,s,r){this.x=20*(t-1),this.y=20*(e-1),this.w=20*i,this.h=20*s,this.i=r,this.x%2!=0&&this.x++,this.y%2!=0&&this.y++,this.center=[Math.floor(this.x/20+i/2),Math.floor(this.y/20+s/2)]}}const C=1.25,FW=0,BW=1,LW=2,RW=3,GT=4,CT=5,TG=0,TW=1,TC=2,TCL=3;class MapImpl{constructor(){this.rows=this.cols=50,this.collide=!1,this.amount=13,this.size=[6,9],this.disX=this.disY=0,this.grid=this.rooms=this.matrix=null}it(t,e){for(let i=1;i<this.rows-1;i++)for(let s=1;s<this.cols-1;s++)e(t,i,s,t[i][s])}generate(){const t=960;var e=0,i=0;this.grid=[];for(let t=0;t<this.rows;t++)for(let e=0;e<this.cols;e++){let i=new Cell(e,t,20*t,20*e);this.grid.push(i)}for(this.rooms=[],e=0;e<this.amount;e++){let i=new Room(rndf(3,this.rows-3),rndf(3,this.cols-3),rndf(this.size[0],this.size[1]),rndf(this.size[0],this.size[1]),e);if(e>0){if(this.rooms[0].x+this.rooms[0].w>=t||this.rooms[0].x<=0||this.rooms[0].y+this.rooms[0].h>=t||this.rooms[0].y<=0){this.generate();break}for(let s=0;s<this.rooms.length;s++){if(this.collide=!1,i.x<=this.rooms[s].x+this.rooms[s].w&&i.x+i.w>=this.rooms[s].x&&i.y<=this.rooms[s].y+this.rooms[s].h&&i.y+i.h>=this.rooms[s].y){this.collide=!0,e--;break}if(i.x+i.w>=t||i.x<=0||i.y+i.h-1>=t||i.y<=0){this.collide=!0,e--;break}}}this.collide||(this.rooms.push(i),e>0&&(this.hC(this.rooms[e-1].center[0],i.center[0],this.rooms[e-1].center[1],i.center[1]),this.vC(this.rooms[e-1].center[0],i.center[0],this.rooms[e-1].center[1],i.center[1])))}for(e=0;e<this.grid.length;e++)this.grid[e].carve();for(this.matrix=[],e=0;e<this.cols;e++)this.matrix[e]=[];for(e=0;e<this.rows;e++)for(i=0;i<this.cols;i++)this.matrix[e][i]=this.grid[e*this.rows+i];this.it(this.matrix,(function(t,e,i,s){let r=0,o=t[e][i-1],n=t[e][i+1],a=t[e-1][i],h=t[e+1][i];" "!=t[e][i].t&&(o&&" "!=o.t&&(r+=1),n&&" "!=n.t&&(r+=4),a&&" "!=a.t&&(r+=8),h&&" "!=h.t&&(r+=2)),t[e][i].w=r})),this.it(this.matrix,(function(t,e,i,s){t[e][i].l=NO_LIGHT}));let s=this.rooms[0].center;for(this.matrix[s[0]-2][s[1]].t="W",this.matrix[s[0]-2][s[1]].l=[1,.98,.32,1],this.it(this.matrix,(function(t,e,i,s){let r=t[e][i],o=t[e][i-1],n=t[e][i+1],a=t[e-1][i],h=t[e+1][i],l=rnd(0,9)>6.5,c=rnd(0,9)>7;l&&15==r.w&&15==a.w&&15==h.w&&15==o.w&&15==n.w&&(t[e][i].t="W",t[e][i].l=[.05,.7,.9,1]),c&&(10==r.w&&10==a.w&&10==h.w||5==r.w&&5==o.w&&5==n.w)&&(t[e][i].t="W",t[e][i].l=[.9,.5,.05,1])})),e=1;e<this.rows-1;e++)for(i=1;i<this.cols-1;i++){let t=this.matrix[e][i],s=this.matrix[e][i-1],r=this.matrix[e][i+1],o=this.matrix[e-1][i],n=this.matrix[e+1][i],a=0,h=0,l=0,c=t.l;0!=t.w&&0!=o.w&&(a=(s.l[0]+r.l[0]+o.l[0]+n.l[0])/4,h=(s.l[1]+r.l[1]+o.l[1]+n.l[1])/4,l=(s.l[2]+r.l[2]+o.l[2]+n.l[2])/4,this.matrix[e][i].l=[c[0]+a,c[1]+h,c[2]+l,1])}}gV(t,e,i){let s=null;return 0==t?s=[[e,0,i],[e+1,0,i],[e,C,i],[e+1,C,i]]:1==t?s=[[e+1,0,i+1],[e,0,i+1],[e+1,C,i+1],[e,C,i+1]]:2==t?s=[[e,0,i+1],[e,0,i],[e,C,i+1],[e,C,i]]:3==t?s=[[e+1,0,i],[e+1,0,i+1],[e+1,C,i],[e+1,C,i+1]]:4==t?s=[[e,0,i+1],[e+1,0,i+1],[e,0,i],[e+1,0,i]]:5==t&&(s=[[e,C,i+1],[e+1,C,i+1],[e,C,i],[e+1,C,i]]),s}toMesh(){let t=new GL.Mesh({coords:!0,normals:!0,colors:!0});function e(e){t.vertices.push.apply(t.vertices,e)}function i(e,i,s,r){i?t.colors.push(e,i,s,r):t.colors.push(e,e,e,e)}function s(e,i){let s=0,r=0,o=.875;2==e&&(s=1,i&&"W"==i&&(s=2),e=0),r=1-.125*(7-s),0==e?t.coords.push([r,1],[r-.125,1],[r,.875],[r-.125,.875]):1==e&&t.coords.push([r,o],[r-.125,o],[r,.6875],[r-.125,.6875])}t.vertices=[],t.coords=[],t.triangles=[],t.colors=[];for(let r=1;r<this.rows-1;r++)for(let o=1;o<this.cols-1;o++){let n=this.matrix[o][r].w,a=this.matrix[o][r].l,h=this.matrix[o][r+1].l,l=this.matrix[o+1][r+1].l,c=this.matrix[o][r].l,u=this.matrix[o+1][r].l;if(0!=n){e(this.gV(4,r,o)),s(0),i(u,l,c,h),e(this.gV(5,r,o)),s(2,this.matrix[o][r].t);let d=.95;t.colors.push([u[0]*d,u[1]*d,u[2]*d,1],[l[0]*d,l[1]*d,l[2]*d,1],[c[0]*d,c[1]*d,c[2]*d,1],[h[0]*d,h[1]*d,h[2]*d,1]),3==n&&(e(this.gV(0,r,o)),s(1),i(a),e(this.gV(3,r,o)),s(1),i(a)),6==n&&(e(this.gV(2,r,o)),s(1),i(a),e(this.gV(0,r,o)),s(1),i(a)),12==n&&(e(this.gV(2,r,o)),s(1),i(a),e(this.gV(1,r,o)),s(1),i(a)),9==n&&(e(this.gV(3,r,o)),s(1),i(a),e(this.gV(1,r,o)),s(1),i(a)),7==n&&(e(this.gV(0,r,o)),s(1),i(a)),13==n&&(e(this.gV(1,r,o)),s(1),i(a)),11==n&&(e(this.gV(3,r,o)),s(1),i(a)),14==n&&(e(this.gV(2,r,o)),s(1),i(a)),5==n&&(e(this.gV(0,r,o)),s(1),i(a),e(this.gV(1,r,o)),s(1),i(a)),10==n&&(e(this.gV(2,r,o)),s(1),i(a),e(this.gV(3,r,o)),s(1),i(a))}}let r=0;for(let e=0;e<t.vertices.length/4;e++)r=4*e,t.triangles.push([0+r,1+r,2+r],[2+r,1+r,3+r]);return t.compile(),t.computeNormals(),t}hC(t,e,i,s){if(t>e){this.disX=t-e+1;for(let t=0;t<this.grid.length;t++)this.grid[t].ccH(this.disX,e,s)}else{this.disX=e-t+1;for(let e=0;e<this.grid.length;e++)this.grid[e].ccH(this.disX,t,i)}}vC(t,e,i,s){let r=0;if(i>s){this.disY=i-s+1,r=e+(this.disX-1)>t+(this.disX-1)?e:e+(this.disX-1);for(let t=0;t<this.grid.length;t++)this.grid[t].ccV(this.disY,r,s)}else{this.disY=s-i+1,r=t+(this.disX-1)>e+(this.disX-1)?t:t+(this.disX-1);for(let t=0;t<this.grid.length;t++)this.grid[t].ccV(this.disY,r,i)}}calculateBoxes(){let t=[],e=0,i=0;function s(s,r,o,n){t.push({x:r,y:s,w:n,h:o}),e=0,i=s}for(let t=0;t<this.cols;t++){i=0;for(let r=0;r<this.rows;r++){let o=this.matrix[t][r].w;0==o?e++:(6!=o&&14!=o&&12!=o&&10!=o||s(t,i,1,e),i=r+1)}s(t,i,1,e)}t.sort((function(t,e){return parseFloat(t.x)-parseFloat(e.x)}));let r=t[0],o=[],n=0,a=r.y;for(let e=0;e<t.length;e++)t[e].x==r.x&&t[e].w==r.w&&1==Math.abs(t[e].y-a)?(n++,a=t[e].y):(o.push({x:r.x,y:r.y,w:r.w,h:n}),a=t[e].y,r=t[e],n=1);o.push({x:r.x,y:r.y,w:r.w,h:n}),o.splice(0,1),this.boxes=o}}class ATexture{constructor(t,e,i){let s=document.createElement("canvas");return s.id=t,s.style.display="none",i=i||e,this.w=s.width=e,this.h=s.height=i,document.body.appendChild(s),this.c=s,this.ctx=s.getContext("2d"),this}blur(t,e){return this.ctx.shadowBlur=t*this.w,this.ctx.shadowColor=argba(e),this}box(t,e,i,s,r,o){return this.ctx.fillStyle=argba(r),this.ctx.strokeStyle=argba(r),this.ctx.globalAlpha=r[3]||1,this.ctx.beginPath(),o?(this.ctx.rect(t*this.w,e*this.h,i*this.w,s*this.h),this.ctx.lineWidth=""+o*this.w,this.ctx.stroke()):this.ctx.fillRect(t*this.w,e*this.h,i*this.w,s*this.h),this.ctx.globalAlpha=1,this}text(t,e,i,s,r){return r=(r||.1)*this.w,this.ctx.fillStyle=argba(s),this.ctx.font=r+"px Arial",this.ctx.textAlign="center",this.ctx.fillText(i,t*this.w,e*this.h),this}t2(t,e,i,s,r,o,n,a,h){return this.text(t,e,r,o,a),this.text(t+i,e+s,r,n,h),this}gradient(t,e,i,s,r,o){let n;n=this.ctx.createLinearGradient((t+i)/2*this.w,e*this.h,(t+i)/2*this.w,(e+s)*this.h);let a=1;return n.addColorStop(0,argba(r[0])),3==r.length&&(a=2,n.addColorStop(.5,argba(r[1]))),n.addColorStop(1,argba(r[a])),this.ctx.fillStyle=n,this.ctx.fillRect(t*this.w,e*this.h,i*this.w,s*this.h),this}noise(t,e,i){var s=this.ctx.getImageData(0,0,this.c.width,this.c.height),r=s.data,o=0;for(i=i||function(t){return rnd(0,255)},t=t||[1,1,1],e=e||1;o<r.length;)if(rnd(0,1)<=e){let e=i(o);r[o++]+=t[0]*e,r[o++]+=t[1]*e,r[o++]+=t[2]*e,r[o++]+=255}else r[o++]+=1,r[o++]+=1,r[o++]+=1,r[o++]+=255;return this.ctx.putImageData(s,0,0),this}perlin(t,e,i,s){this.ctx.save(),i=i||1,s=s||1;for(var r=e=e||4;r<=t.c.width;r*=2){var o=Math.random()*(t.c.width-r)|0,n=Math.random()*(t.c.width-r)|0;this.ctx.globalAlpha=e/r,this.ctx.drawImage(t.c,o,n,r,r,0,0,this.c.width*i,this.c.height*s)}return this.ctx.restore(),this}image(t,e,i,s,r,o){return e=e||0,i=i||0,s=s||1,r=r||1,this.ctx.globalAlpha=o||1,this.ctx.drawImage(t.c,e*this.w,i*this.h,s*this.w,r*this.h),this}}const GREY_L=0,GREY_M=1,GREY_D=2,RUST_L=3,RUST_M=4,RUST_D=5,GREEN_L=6,GREEN_M=7,GREEN_D=8,RED_L=9,RED_M=10,RED_D=11;var P=[[132,134,131],[108,104,104],[64,63,56],[76,56,8],[62,58,44],[26,25,16],[138,138,108],[66,74,53],[29,51,22],[175,71,33],[101,55,19],[67,30,10]];function Pa(t,e){return e=e||1,[P[t][0],P[t][1],P[t][2],e]}let rn=new ATexture("rn",512);rn.noise([1,2,1],.81),rn.box(0,0,1,1,Pa(0,.9));let tp=new ATexture("tp",512);tp.noise(),tp.perlin(tp,8);let pr=new ATexture("pr",256);pr.noise(Pa(8),.9),pr.perlin(pr,64,1,16);let tg=new ATexture("gbase",256);tg.box(0,0,1,1,Pa(5)).box(.025,.025,.95,.95,Pa(5,.9)).box(.025,.025,.95,.95,Pa(4,.9),.05).text(.5,1,"▢",Pa(7,.6),1.5).gradient(0,0,1,1,[Pa(5,.5),Pa(1,.18),Pa(5,.7)]);let tc=new ATexture("tc",256);tc.image(tg,0,0,1,1).image(tp,0,0,1,1,.1);let tcl=new ATexture("tcl",256);tcl.image(tg,0,0,1,1).image(tp,0,0,1,1,.1).blur(.05,Pa(5)).box(.15,.15,.3,.3,[255,255,255,.7]).box(.55,.15,.3,.3,[255,255,255,.5]).box(.15,.55,.3,.3,[255,255,255,.8]).box(.55,.55,.3,.3,[255,255,255,.9]);for(let t=0;t<12;t++)for(let e=0;e<12;e++){let i=0;e%2==0&&(i=.05),tg.t2(i+.1*t,0+.1*e,.005,.015,"⛬",Pa(0,rnd(.3,.7)),Pa(5,rnd(.5,.7)),.1,.12)}tg.image(tp,0,0,1,1,.1);let yy=[255,255,0,1],tw=new ATexture("wbase",256,384);tw.box(0,0,1,1,Pa(1,.3));let ss=256,l=56,s=ss/l,b=rnd(15,35);tw.box(0,0,1,1,Pa(7)).blur(.015,[5,5,5]);let i=l;for(;i>0;)i--,rnd(0,9)>5||(b=rnd(88,126),tw.box(0,i/l,1,i/l,[b,b,b,1]));b=rnd(22,44);let lpt=[255,255,0,1];tw.image(pr,0,0,1,.922,.1).box(.05,.025,.9,.95,Pa(0,.88),.1).box(.05,.025,.9,.95,[b,b,b,1],.035).gradient(.1,.25,.8,.17,[Pa(2,.7),Pa(0,.6),Pa(1,.7)]),tw.ctx.globalAlpha=1,tw.text(.15,.35,"◈",yy,.05).text(.85,.35,"◈",yy,.05).gradient(0,0,1,1,[[12,12,12,.75],Pa(0,.05),Pa(5,.75)]);let tcbr=new ATexture("tcbr",256,128);for(ss=256,l=18,s=ss/l,b=rnd(115,185),tcbr.box(0,0,1,1,Pa(2)).blur(.015,[5,5,5]),i=l;i>0;)i--,b=rnd(188,216),tcbr.box(0,.1+i/l,1,i/l,Pa(5,rnd(.2,.7)));tcbr.image(pr,0,0,1,.922,.1),tcbr.ctx.globalAlpha=1;let tcbs=new ATexture("tcbs",256,128);tcbs.image(tcbr,0,0,1,1),tcbr.text(.05,.2,"◜",yy),tcbr.text(.95,.85,"◞",yy),tcbr.text(.95,.2,"◝",yy),tcbr.text(.05,.85,"◟",yy);let tln=new ATexture("tln",256);for(i=0;i<20;i++)tln.box(0,.05*i,1,.0125,yy);let perlin=new ATexture("perlin",128);perlin.noise([1,1,1]),perlin.perlin(perlin,32);let atlas=new ATexture("tileset",2048);for(let t=0;t<8;t++)atlas.image(tg,0+.125*t,0,.1255,.1255),atlas.image(tw,0+.125*t,.125,.125,.1875);atlas.image(tg,0,0,.125,.125),atlas.image(tc,.125,0,.125,.125),atlas.image(tcl,.25,0,.125,.125),atlas.image(tcbr,0,.375,.125,.0625),atlas.image(tcbs,0,.4375,.125,.0625),atlas.image(tln,.125,.375,.125,.125);var PLAYER_SPEED=10,gl=GL.create(),camera={position:new GL.Vector(0,.5,3),angle:new GL.Vector(0,0,0)},mouseSensitivity=3.7,lastDelta=.016,totalTime=0,timeSinceLastP=10,level=null,center=null,boxes=null,player=null,sensor=null,clicked=!1,ground=null,ceiling=null,des=0,lit=0,texture=GL.Texture.fromImage(tileset),shader=new GL.Shader(" varying vec2 coord; varying vec4 pos; varying vec4 color; varying vec3 normal; void main() { coord = gl_TexCoord.xy; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; pos = gl_Position; color = gl_Color; normal = gl_Normal; } "," varying vec2 coord; varying vec4 pos; varying vec4 color; varying vec3 normal; uniform sampler2D texture; uniform float lit; void main() { vec4 td = texture2D(texture, coord); if(td.a < 0.9) discard; gl_FragColor = td * color * 4.0 * lit; if(td.r >= 0.5 && td.g >= 0.5 && td.b < 0.5) gl_FragColor = td * lit; } ");function text(t){document.getElementById("upper").innerHTML=t}function init(){text("[click to play]"),(MAP=new MapImpl).generate(),MAP.calculateBoxes(),level=MAP.toMesh(),center=MAP.rooms[0].center,boxes=[];for(let t=0;t<100;t++){let e=parseInt(t%10)+1;center=MAP.rooms[e].center;let i=new Body(new GL.Vector(rnd(center[1]-2,center[1]+2),.5,rnd(center[0]-2,center[0]+2)),new GL.Vector(rnd(.125,.3),.125,.125),"box");i.friction=.999,i.elasticity=.999,i.impulse(GL.Vector.randomDirection().multiply(rnd(1,2))),i.mass=rnd(1,30),i.mesh=new GL.Mesh.cube2,boxes.push(i)}center=MAP.rooms[0].center,(player=new Body(new GL.Vector(center[1]+.5,.75,center[0]),new GL.Vector(.25,.2,.25),"player")).friction=.93,player.elasticity=.1,(sensor=new Body(new GL.Vector(center[1]+.5,.55,center[0]-2.5),new GL.Vector(.4,.75,.4),"sensor")).solid=!1,sensor.dynamic=!1,sensor.mesh=new GL.Mesh.cube2(1),SIM.addStatic(MAP.boxes,"map"),(ground=new Body(new GL.Vector(25,-.15,25),new GL.Vector(25,.15,25),"ground")).mass=0,ground.dynamic=!1,(ceiling=new Body(new GL.Vector(25,1.4,25),new GL.Vector(25,.15,25),"ceiling")).mass=0,ceiling.dynamic=!1}SIM.onCollision("player","box",(function(t,e){"box"==e.tag&&e.dynamic&&e.impulse(t.velocity.multiply(.5))})),SIM.onCollision("player","sensor",(function(t,e){player.ouch=!0})),SIM.onCollision("box","sensor",(function(t,e){"box"==t.tag&&t.impulse(new GL.Vector(0,10,0))})),gl.onmousewheel=function(t){mouseSensitivity+=Math.sign(t.wheelDelta)*lastDelta*20,console.log(mouseSensitivity)},gl.onmousedown=function(t){clicked=!0,text("")},gl.onmousemove=function(t){camera.angle.y-=t.mx*lastDelta*mouseSensitivity,camera.angle.x=Math.max(-90,Math.min(90,camera.angle.x-t.my*lastDelta*mouseSensitivity))},gl.onupdate=function(t){if(!clicked)return;lastDelta=t,totalTime+=t,lit<1&&(lit+=t),lit>1&&(lit-=8*t),Math.abs(1-lit)<.01&&(lit=1);let e=t*PLAYER_SPEED,i=GL.keys.W|GL.keys.UP,s=GL.keys.S|GL.keys.DOWN,r=GL.Vector.fromAngles((90-camera.angle.y)*Math.PI/180,(180-camera.angle.x)*Math.PI/180);camera.position=camera.position.add(r.multiply(e*(i-s)));let o=GL.keys.A|GL.keys.LEFT,n=GL.keys.D|GL.keys.RIGHT,a=GL.Vector.fromAngles(-camera.angle.y*Math.PI/180,0);camera.position=camera.position.add(a.multiply(e*(n-o))),player.ouch&&(e*=-100,player.ouch=!1),player.velocity=player.velocity.add(r.multiply(e*(i-s))),player.velocity=player.velocity.add(a.multiply(e*(n-o))),camera.position.x=player.position.x,camera.position.y=player.position.y,camera.position.z=player.position.z,SIM.updateQuadtree(t);for(let e=0;e<boxes.length;e++){let i=boxes[e].position;if((i.x<1||i.x>48||i.z<1||i.z>48)&&(boxes[e].solid=!1),boxes[e].isCol2("ceiling","sensor")&&(boxes[e].position.set(200,200,200),boxes[e].dynamic=!1,boxes[e].solid=!1,lit=16,des++),!boxes[e].solid)continue;let s=boxes[e].mesh,r=boxes[e].position;boxes[e].extents;for(let e=0;e<s.colors.length;e++){let i=Math.floor(r.z+.5),o=Math.floor(r.x+.5),n=MAP.matrix[i][o].l,a=MAP.matrix[i+1][o],h=MAP.matrix[i-1][o],l=MAP.matrix[i][o+1],c=MAP.matrix[i][o+1];a=0!=a.w?a.l:n,h=0!=h.w?h.l:n,l=0!=l.w?l.l:n,c=0!=c.w?c.l:n,s.normals[e][0]>0?s.colors[e]=lerpc(s.colors[e],l,2*t):s.normals[e][0]<0&&(s.colors[e]=lerpc(s.colors[e],c,2*t)),0!=s.normals[e][1]&&(s.colors[e]=lerpc(s.colors[e],n,2*t)),s.normals[e][2]>0?s.colors[e]=lerpc(s.colors[e],a,2*t):s.normals[e][2]<0&&(s.colors[e]=lerpc(s.colors[e],h,2*t))}boxes[e].mesh.compile()}},gl.ondraw=function(){if(gl.clear(16640),clicked){des>0&&text(""+des),gl.loadIdentity(),texture.bind(0),gl.rotate(-camera.angle.x,1,0,0),gl.rotate(-camera.angle.y,0,1,0),gl.translate(-camera.position.x,-camera.position.y,-camera.position.z),gl.pushMatrix(),shader.uniforms({texture:0,lit:lit}),shader.draw(level);for(var t=0;t<boxes.length;t++)gl.pushMatrix(),gl.translate(boxes[t].position.x,boxes[t].position.y,boxes[t].position.z),gl.scale(boxes[t].extents.x,boxes[t].extents.y,boxes[t].extents.z),shader.draw(boxes[t].mesh),gl.popMatrix();gl.translate(sensor.position.x,sensor.position.y,sensor.position.z),gl.scale(sensor.extents.x+.1*Math.sin(10*totalTime),sensor.extents.y,sensor.extents.z+.1*Math.cos(10*totalTime)),shader.draw(sensor.mesh),gl.popMatrix()}},init(),gl.clearColor(.01,.01,.01,1),gl.enable(2929),gl.fullscreen({far:50}),gl.animate();
</script>
</body></html>