text: !function(){"use strict";const t="noscale";class e extends HTMLElement{static get[Symbol.for("cuttleTemplate")](){let t=document.createElement("template");return t.innerHTML='<div class="container">\n    <label class="hidden" id="title">display-port</label>\n    <label class="hidden" id="fps">00</label>\n    <label class="hidden" id="dimension">0x0</label>\n    <canvas></canvas>\n    <slot></slot>\n</div>',Object.defineProperty(this,Symbol.for("cuttleTemplate"),{value:t}),t}static get[Symbol.for("cuttleStyle")](){let t=document.createElement("style");return t.innerHTML=":host{display:inline-block;color:#555}.container{display:flex;position:relative;width:100%;height:100%}canvas{background:#000;margin:auto;-ms-interpolation-mode:nearest-neighbor;image-rendering:-moz-crisp-edges;image-rendering:pixelated}label{font-family:monospace;color:currentColor;position:absolute}#title{left:.5rem;top:.5rem}#fps{right:.5rem;top:.5rem}#dimension{left:.5rem;bottom:.5rem}.hidden{display:none}:host([debug]) .container{outline:6px dashed rgba(0,0,0,.1);outline-offset:-4px;background-color:rgba(0,0,0,.1)}:host([mode=noscale]) canvas{margin:0;top:0;left:0}:host([mode=center]),:host([mode=fit]),:host([mode=stretch]){width:100%;height:100%}:host([full]){width:100vw!important;height:100vh!important}:host([disabled]){display:none}slot{display:flex;flex-direction:column;align-items:center;justify-content:center;position:absolute;width:100%;height:100%;top:0;left:0;pointer-events:none}::slotted(*){pointer-events:auto}",Object.defineProperty(this,Symbol.for("cuttleStyle"),{value:t}),t}static get observedAttributes(){return["onframe","width","height","disabled","debug","id","class"]}static get properties(){return{width:Number,height:Number,disabled:Boolean,debug:Boolean,mode:{type:String,value:"fit",observed:!1}}}get mode(){return this.getAttribute("mode")}set mode(t){this.setAttribute("mode",t)}get debug(){return this._debug}set debug(t){this.toggleAttribute("debug",t)}get disabled(){return this._disabled}set disabled(t){this.toggleAttribute("disabled",t)}get height(){return this._height}set height(t){this.setAttribute("height",String(t))}get width(){return this._width}set width(t){this.setAttribute("width",String(t))}static get customEvents(){return["frame"]}get onframe(){return this._onframe}set onframe(t){this._onframe&&this.removeEventListener("frame",this._onframe),this._onframe=t,this._onframe&&this.addEventListener("frame",t)}constructor(){super(),this.attachShadow({mode:"open"}),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleTemplate")].content.cloneNode(!0)),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleStyle")].cloneNode(!0)),this._canvasElement=this.shadowRoot.querySelector("canvas"),this._titleElement=this.shadowRoot.querySelector("#title"),this._fpsElement=this.shadowRoot.querySelector("#fps"),this._dimensionElement=this.shadowRoot.querySelector("#dimension"),this._animationRequestHandle=0,this._prevAnimationFrameTime=0,this._width=300,this._height=150,this.update=this.update.bind(this)}get canvas(){return this._canvasElement}connectedCallback(){if(Object.prototype.hasOwnProperty.call(this,"onframe")){let t=this.onframe;delete this.onframe,this.onframe=t}if(Object.prototype.hasOwnProperty.call(this,"width")){let t=this.width;delete this.width,this.width=t}if(Object.prototype.hasOwnProperty.call(this,"height")){let t=this.height;delete this.height,this.height=t}if(Object.prototype.hasOwnProperty.call(this,"disabled")){let t=this.disabled;delete this.disabled,this.disabled=t}if(Object.prototype.hasOwnProperty.call(this,"debug")){let t=this.debug;delete this.debug,this.debug=t}if(Object.prototype.hasOwnProperty.call(this,"mode")){let t=this.mode;delete this.mode,this.mode=t}this.hasAttribute("mode")||this.setAttribute("mode","fit"),this.hasAttribute("tabindex")||this.setAttribute("tabindex",0),this.updateCanvasSize(),this.resume()}disconnectedCallback(){this.pause()}attributeChangedCallback(t,e,i){switch(t){case"width":this._width=Number(i);break;case"height":this._height=Number(i);break;case"disabled":this._disabled=null!==i;break;case"debug":this._debug=null!==i;break;case"onframe":this.onframe=new Function("event","with(document){with(this){"+i+"}}").bind(this)}((t,e,i)=>{switch(t){case"disabled":i?(this.update(0),this.pause()):this.resume();break;case"id":case"class":this._titleElement.innerHTML=`display-port${this.className?"."+this.className:""}${this.hasAttribute("id")?"#"+this.getAttribute("id"):""}`;break;case"debug":this._titleElement.classList.toggle("hidden",i),this._fpsElement.classList.toggle("hidden",i),this._dimensionElement.classList.toggle("hidden",i)}})(t,0,i)}pause(){cancelAnimationFrame(this._animationRequestHandle)}resume(){this._animationRequestHandle=requestAnimationFrame(this.update)}update(e){this._animationRequestHandle=requestAnimationFrame(this.update),this.updateCanvasSize();const i=e-this._prevAnimationFrameTime;if(this._prevAnimationFrameTime=e,this.debug){const e=i<=0?"--":String(Math.round(1e3/i)).padStart(2,"0");if(this._fpsElement.innerText!==e&&(this._fpsElement.innerText=e),this.mode===t){let t=`${this._width}x${this._height}`;this._dimensionElement.innerText!==t&&(this._dimensionElement.innerText=t)}else{let t=`${this._width}x${this._height}|${this.shadowRoot.host.clientWidth}x${this.shadowRoot.host.clientHeight}`;this._dimensionElement.innerText!==t&&(this._dimensionElement.innerText=t)}}this.dispatchEvent(new CustomEvent("frame",{detail:{now:e,prevTime:this._prevAnimationFrameTime,deltaTime:i,canvas:this._canvasElement},bubbles:!1,composed:!0}))}updateCanvasSize(){let e=this.shadowRoot.host.getBoundingClientRect();const i=e.width,s=e.height;let n=this._canvasElement,o=this._width,r=this._height;const a=this.mode;if("stretch"===a)o=i,r=s;else if(a!==t){if(i<o||s<r||"fit"===a){let t=i/o,e=s/r;t<e?(o=i,r*=t):(o*=e,r=s)}}o=Math.floor(o),r=Math.floor(r),n.clientWidth===o&&n.clientHeight===r||(n.width=this._width,n.height=this._height,n.style=`width: ${o}px; height: ${r}px`,this.dispatchEvent(new CustomEvent("resize",{detail:{width:o,height:r},bubbles:!1,composed:!0})))}}function i(t,e,i){return Math.min(i,Math.max(e,t))}window.customElements.define("display-port",e);class s{next(){return Math.random()}}let n;class o{constructor(t=new s){this.generator=t}static next(){return n.next()}next(){return this.generator.next()}static choose(t){return n.choose(t)}choose(t){return t[Math.floor(this.generator.next()*t.length)]}static range(t,e){return n.range(t,e)}range(t,e){return(e-t)*this.generator.next()+t}static sign(){return n.sign()}sign(){return this.generator.next()<.5?-1:1}}n=new o;class r{constructor(t,e,i=new o){this.width=t,this.height=e,this.length=t*e,this.rand=i,this.dangerCount=.2*this.length,this.data={tiles:new Array(length),overlay:new Array(length),regions:new Array(length),solids:new Array(length),marks:new Array(length)},a(this,this.dangerCount,this.rand)}dig(t,e){const i=this.width,s=this.height;let n=this.data,o=t+e*i;if(n.solids[o]<=0)return!0;if(n.marks[o]>0)return!0;if(n.tiles[o]>0)return n.solids[o]=0,!1;let r=new Set,a=[];for(a.push(o);a.length>0;){let t=a.pop();r.add(t);let e=t%i,o=Math.floor(t/i);if(n.solids[t]=0,n.overlay[t]<=0){let t=h(e,o,i,s);for(let e of t)!r.has(e)&&n.marks[e]<=0&&a.push(e)}}return!0}mark(t,e){const i=this.width;let s=this.data,n=t+e*i;return!(s.solids[n]<=0)&&(s.marks[n]>0?s.marks[n]=0:s.marks[n]=1,!0)}checkWinCondition(){const t=this.data;for(let e=0;e<t.tiles.length;++e)if(t.solids[e]>0&&t.tiles[e]<=0)return!1;return!0}reset(){a(this,this.dangerCount,this.rand)}clear(){let t=this.data;for(let e=0;e<this.length;++e)t.tiles[e]=0,t.overlay[e]=0,t.solids[e]=1,t.regions[e]=0,t.marks[e]=0}}function a(t,e,i){t.clear();const s=t.width,n=t.height,o=t.rand;let r=t.data;for(let t=0;t<e;++t){let e=Math.floor(i.range(0,s)),o=Math.floor(i.range(0,n)),a=e+o*s,l=1;if(0===r.tiles[a]){r.tiles[a]+=l,r.overlay[a]=1/0;for(let t of h(e,o,s,n))r.overlay[t]+=l}else--t}const a=Array.from(function(t,e,i,s){const n=t.tiles.length;let o=new Map,r=new Map,a=1;for(let h=0;h<n;++h)if(s(h))t.regions[h]=-1;else{let n=l(t,e,i,h,a++,s);if(n){if(r.has(n.tileIndex)){const t=r.get(n.tileIndex);o.delete(t),r.delete(n.tileIndex)}r.set(n.tileIndex,n.regionIndex),o.set(n.regionIndex,n)}}return o}(r,s,n,(t=>r.overlay[t]>0)).values()).sort(((t,e)=>t.count-e.count)),c=[[a[0]]];let d=0;for(let t of a)c[d][0].count!==t.count?c[++d]=[t]:c[d].push(t);let u=c[Math.floor(c.length/2)],p=o.choose(u).tileIndex,m=p%s,f=Math.floor(p/s);t.dig(m,f)}function l(t,e,i,s,n,o){let r=1/0,a=new Set,l=[];for(l.push(s);l.length>0;){let s=l.pop();if(o(s))continue;a.add(s);let c=s%e,d=Math.floor(s/e);t.regions[s]=n,s<r&&(r=s);let u=h(c,d,e,i);for(let t of u)a.has(t)||l.push(t)}return r<1/0?{tileIndex:r,regionIndex:n,count:a.size}:null}function h(t,e,i,s){let n=[],o=t+e*i;return t>0&&n.push(o-1),t<i-1&&n.push(o+1),e>0&&n.push(o-i),e<s-1&&n.push(o+i),t>0&&e>0&&n.push(o-1-i),t<i-1&&e>0&&n.push(o+1-i),t>0&&e<s-1&&n.push(o-1+i),t<i-1&&e<s-1&&n.push(o+1+i),n}const c=1,d=2,u=3,p={NULL:0,DOWN:1,UP:2,MOVE:3,parse(t){if("string"!=typeof t)return p.NULL;switch(t.toLowerCase()){case"down":return p.DOWN;case"up":return p.UP;case"move":return p.MOVE;default:return p.NULL}}},m="*";class f{static isAxis(t){return!1}static isButton(t){return!1}constructor(t,e){this.deviceName=t,this.eventTarget=e,this.listeners={}}destroy(){this.listeners={}}addInputListener(t,e){let i=this.listeners[t];i?i.push(e):(i=[e],this.listeners[t]=i)}removeInputListener(t,e){let i=this.listeners[t];i&&(i.indexOf(e),i.splice(e,1))}dispatchInput(t){const{keyCode:e}=t,i=this.listeners[e];let s=!1;if(i){for(let e of i)s|=e(t);return s}for(let e of this.listeners["*"])s|=e(t);return s}}class g{static createAdapter(t,e,i,s,n,o){return{target:t,adapterId:e,deviceName:i,keyCode:s,scale:n,eventCode:o}}constructor(){this.adapters={"*":v()}}add(t){for(let e of t){const{deviceName:t,keyCode:i}=e;let s;t in this.adapters?s=this.adapters[t]:(s=v(),this.adapters[t]=s),i in s?s[i].push(e):s[i]=[e]}}delete(t){for(let e of t){const{deviceName:t,keyCode:i}=e;if(t in this.adapters){let s=this.adapters[t];if(i in s){let t=s[i],n=t.indexOf(e);n>=0&&t.splice(n,1)}}}}clear(){for(let t in this.adapters)this.adapters[t]=v()}poll(t,e,i){const s=this.findAdapters(t,e);for(let t of s){const e=t.eventCode;if(e===p.NULL){const{target:e,scale:s}=t,n=i.value*s;e.poll(n,t)}else{const{target:s,scale:n}=t,o=i.getEvent(e)*n;s.poll(o,t)}}return s.length>0}update(t,e,i){let s=!1;for(let n of this.findAdapters(t,e)){const t=n.eventCode;if(t!==p.NULL){const{target:e,scale:o}=n,r=i.getEvent(t)*o;e.update(r,n),s=!0}}return s}reset(t,e,i){let s=!1;for(let i of this.findAdapters(t,e))i.target.reset(),s=!0;return s}findAdapters(t,e){let i=[];if(t in this.adapters){let s=this.adapters[t];e in s&&i.push(...s[e]),i.push(...s["*"])}let s=this.adapters["*"];return e in s&&i.push(...s[e]),i.push(...s["*"]),i}}function v(){return{[m]:[]}}class b{constructor(){this.value=0,this.prev=0}update(t){this.value=t}poll(){this.prev=this.value,this.value=0}getEvent(t){return 0}getState(){return this.value}getPrevState(){return this.prev}}class y extends b{constructor(){super(),this.delta=0,this.next={delta:0}}update(t,e){this.value=t,this.next.delta+=e}poll(){this.prev=this.value,this.delta=this.next.delta,this.next.delta=0}getEvent(t){switch(t){case p.MOVE:return this.delta;default:return super.getEvent(t)}}}class w extends b{constructor(){super(),this.down=!1,this.up=!1,this.next={down:!1,up:!1}}update(t){t?this.next.down=!0:this.next.up=!0}poll(){this.prev=this.value;const{up:t,down:e}=this.next;this.value?this.up&&!t&&(this.value=0):e&&(this.value=1),this.down=e,this.up=t,this.next.down=!1,this.next.up=!1}getEvent(t){switch(t){case p.DOWN:return 1&this.down;case p.UP:return 1&this.up;default:return super.getEvent(t)}}}const E=1,_=2;class x{constructor(t=[]){this.onInputEvent=this.onInputEvent.bind(this),this.onAnimationFrame=this.onAnimationFrame.bind(this);let e={},i={};for(let s of t){const t=s.deviceName;if(t in e)throw new Error(`Another device with name '${t}' already exists.`);e[t]=s,i[t]={},s.addInputListener(m,this.onInputEvent)}this.devices=e,this.keyMap=i,this.listeners={poll:[],update:[]},this._lastPollTime=-1,this._autopoll=!1,this._animationFrameHandle=null}get polling(){return performance.now()-this._lastPollTime<1e3}destroy(){this.clearKeys();for(let t in this.devices){let e=this.devices[t];e.removeInputListener(m,this.onInputEvent),e.destroy()}this.devices={}}poll(t=performance.now()){for(const t in this.keyMap){let e=this.keyMap[t];for(const i in e){let s=S(e[i]);s.poll(),this.dispatchInputEvent(_,t,i,s)}}this.dispatchPollEvent(t),this._lastPollTime=t}addEventListener(t,e){t in this.listeners?this.listeners[t].unshift(e):this.listeners[t]=[e]}removeEventListener(t,e){if(t in this.listeners){let i=this.listeners[t],s=i.indexOf(e);i.splice(s,1)}}countEventListeners(t){if(!(t in this.listeners))throw new Error(`Cannot count listeners for unknown event '${t}'.`);return this.listeners[t].length}dispatchEvent(t,e){for(let i of this.listeners[t])i(e)}dispatchInputEvent(t,e,i,s){this.dispatchEvent("input",{stage:t,deviceName:e,keyCode:i,input:s})}dispatchPollEvent(t){this.dispatchEvent("poll",{now:t})}onInputEvent(t){const e=t.deviceName,i=this.keyMap[e];switch(t.type){case c:{const s=t.keyCode;let n=S(i[s]);if(n)return n.update(t.event===p.DOWN),this.dispatchInputEvent(E,e,s,n),!0}break;case d:{let s=S(i.PosX);s&&(s.update(t.x,t.dx),this.dispatchInputEvent(E,e,"PosX",s));let n=S(i.PosY);n&&(n.update(t.y,t.dy),this.dispatchInputEvent(E,e,"PosY",n))}break;case u:{let s=S(i.WheelX);s&&(s.update(t.dx,t.dx),this.dispatchInputEvent(E,e,"WheelX",s));let n=S(i.WheelY);n&&(n.update(t.dy,t.dy),this.dispatchInputEvent(E,e,"WheelY",n));let o=S(i.WheelZ);o&&(o.update(t.dz,t.dz),this.dispatchInputEvent(E,e,"WheelZ",o))}}}onAnimationFrame(t){this._autopoll&&(this._animationFrameHandle=requestAnimationFrame(this.onAnimationFrame),this.poll(t))}set autopoll(t){this._autopoll=t,this._animationFrameHandle&&(cancelAnimationFrame(this._animationFrameHandle),this._animationFrameHandle=null),t&&(this._animationFrameHandle=requestAnimationFrame(this.onAnimationFrame))}get autopoll(){return this._autopoll}registerKey(t,e){if(!(t in this.devices))throw new Error(`Invalid device name - missing device with name '${t}' in source.`);let i=this.keyMap[t];if(e in i)i[e].refs+=1;else{let s,n=this.devices[t];if(n.constructor.isAxis(e))s=new y;else{if(!n.constructor.isButton(e))throw new Error(`Unknown key code '${e}' for device ${t}.`);s=new w}i[e]=function(t=null){return{refs:1,input:t}}(s)}return this}unregisterKey(t,e){let i=this.keyMap[t];if(i){let t=i[e];t&&(!function(t){t.refs-=1}(t),t.refs<=0&&delete i[e])}}hasKey(t,e){return t in this.keyMap&&e in this.keyMap[t]}clearKeys(){for(let t in this.devices){let e=this.keyMap[t];for(let t in e)e[t].refs=0;this.keyMap[t]={}}}getInputByKey(t,e){return S(this.keyMap[t][e])}}function S(t){return t?t.input:null}class k extends b{constructor(){super(),this.update=this.update.bind(this),this.adapters=[],this.values=[],this.next={values:[],value:0}}hydrate(t){if(!t)return;Array.isArray(t)||(t=[t]);let e=[],i=0;for(let s of t){"string"==typeof s&&(s={key:s});const{key:t,scale:n=1,event:o="null"}=s,{deviceName:r,keyCode:a}=M(t),l=p.parse(o),h=Number(n);let c=g.createAdapter(this,i,r,a,h,l);e.push(c),++i}this.adapters=e,this.values=new Array(e.length).fill(0),this.next={values:new Array(e.length).fill(0),value:0}}poll(t,e){this.prev=this.value;const i=e.adapterId;let s=this.values[i];this.values[i]=t,this.value=this.value-s+t,this.next.values[i]=0,this.next.value+=t-s}update(t,e){const i=e.adapterId;let s=this.next.values[i];this.next.values[i]=t,this.next.value+=t-s}reset(){this.prev=0,this.values.fill(0),this.value=0,this.next.values.fill(0),this.next.value=0}}function M(t){let e=t.indexOf(":");if(e>=0)return{deviceName:t.substring(0,e),keyCode:t.substring(e+1)};throw new Error("Invalid key string - missing device separator ':'.")}class L{constructor(){this._source=null,this._mapping=null,this._disabled=!0,this._ignoreInputs=this._disabled,this.adapters=new g,this.inputs={},this.listeners={change:[],attach:[],detach:[]},this.onSourceInput=this.onSourceInput.bind(this),this.onSourcePoll=this.onSourcePoll.bind(this)}get source(){return this._source}get mapping(){return this._mapping}get disabled(){return this._disabled}set disabled(t){this.toggle(!t)}setInputMapping(t){const e=this.disabled;this.disabled=!0;let i=this.source,s=this.inputs;i&&C(i,s),this.adapters.clear();let n=this.source,o={};if(t){for(let e in t){let i=t[e],n=s[e]||new k;n.hydrate(i);let r=n.adapters;this.adapters.add(r),o[e]=n}n&&T(n,o)}return this.inputs=o,this._mapping=t,this.dispatchChangeEvent(),this.disabled=!this.source||!this.inputs||e,this}attach(t){if(this.source)throw new Error("Already attached to existing input source.");if(!t)throw new Error("Missing input source to attach context.");return T(t,this.inputs),t.addEventListener("poll",this.onSourcePoll),t.addEventListener("input",this.onSourceInput),this._source=t,this.toggle(!0),this.dispatchAttachEvent(),this}detach(){let t=this.source,e=this.inputs;return t&&(this.toggle(!1),t.removeEventListener("poll",this.onSourcePoll),t.removeEventListener("input",this.onSourceInput),e&&C(t,e),this._source=null,this.dispatchDetachEvent()),this}addEventListener(t,e){t in this.listeners?this.listeners[t].unshift(e):this.listeners[t]=[e]}removeEventListener(t,e){if(t in this.listeners){let i=this.listeners[t],s=i.indexOf(e);i.splice(s,1)}}countEventListeners(t){if(!(t in this.listeners))throw new Error(`Cannot count listeners for unknown event '${t}'.`);return this.listeners[t].length}dispatchEvent(t,e){for(let i of this.listeners[t])i(e)}dispatchChangeEvent(){this.dispatchEvent("change",{})}dispatchAttachEvent(){this.dispatchEvent("attach",{})}dispatchDetachEvent(){this.dispatchEvent("detach",{})}onSourceInput(t){if(t.consumed||this._ignoreInputs){const{deviceName:e,keyCode:i,input:s}=t;this.adapters.reset(e,i,s)}else{const{stage:e,deviceName:i,keyCode:s,input:n}=t;switch(e){case _:this.adapters.poll(i,s,n);break;case E:this.adapters.update(i,s,n);break;default:throw new Error("Unknown input source stage.")}t.consumed=!0}}onSourcePoll(t){this._ignoreInputs!==this.disabled&&(this._ignoreInputs=this.disabled)}toggle(t=this._disabled){let e=!t;if(!e&&!this.source)throw new Error("Input source must be set before enabling input context.");return this._disabled=e,this}hasInput(t){return t in this.inputs&&this.inputs[t].adapters.length>0}getInput(t){if(t in this.inputs)return this.inputs[t];{let e=new k;return this.inputs[t]=e,this._mapping[t]="",this.dispatchChangeEvent(),e}}getInputState(t){return t in this.inputs?this.inputs[t].value:0}getInputChanged(t){if(t in this.inputs){let e=this.inputs[t];return e.value-e.prev}return 0}}function T(t,e){for(let i in e){let{adapters:s}=e[i];for(let e of s){const{deviceName:i,keyCode:s}=e;t.registerKey(i,s)}}}function C(t,e){for(let i in e){let{adapters:s}=e[i];for(let e of s){const{deviceName:i,keyCode:s}=e;t.unregisterKey(i,s)}}}class O extends f{static isAxis(t){return!1}static isButton(t){return!0}constructor(t,e={}){super("Keyboard",t);const{repeat:i=!1}=e;this.repeat=i,this._eventObject={target:t,deviceName:this.deviceName,keyCode:"",event:p.NULL,type:c,value:0,control:!1,shift:!1,alt:!1},this.onKeyDown=this.onKeyDown.bind(this),this.onKeyUp=this.onKeyUp.bind(this),t.addEventListener("keydown",this.onKeyDown),t.addEventListener("keyup",this.onKeyUp)}destroy(){let t=this.eventTarget;t.removeEventListener("keydown",this.onKeyDown),t.removeEventListener("keyup",this.onKeyUp),super.destroy()}onKeyDown(t){if(t.repeat)return t.preventDefault(),t.stopPropagation(),!1;let e=this._eventObject;return e.keyCode=t.code,e.event=p.DOWN,e.value=1,e.control=t.ctrlKey,e.shift=t.shiftKey,e.alt=t.altKey,this.dispatchInput(e)?(t.preventDefault(),t.stopPropagation(),!1):void 0}onKeyUp(t){let e=this._eventObject;if(e.keyCode=t.code,e.event=p.UP,e.value=1,e.control=t.ctrlKey,e.shift=t.shiftKey,e.alt=t.altKey,this.dispatchInput(e))return t.preventDefault(),t.stopPropagation(),!1}}class A extends f{static isAxis(t){return"PosX"===t||"PosY"===t||"WheelX"===t||"WheelY"===t||"WheelZ"===t}static isButton(t){return!this.isAxis(t)}constructor(t,e={eventsOnFocus:!0}){super("Mouse",t),this.canvasTarget=t instanceof HTMLCanvasElement&&t||t.canvas||t.querySelector("canvas")||t.shadowRoot&&t.shadowRoot.querySelector("canvas")||t,this.eventsOnFocus=e.eventsOnFocus,this._downHasFocus=!1,this._eventObject={target:t,deviceName:this.deviceName,keyCode:"",event:p.NULL,type:c,value:0,control:!1,shift:!1,alt:!1},this._positionObject={target:t,deviceName:this.deviceName,keyCode:"Position",event:p.MOVE,type:d,x:0,y:0,dx:0,dy:0},this._wheelObject={target:t,deviceName:this.deviceName,keyCode:"Wheel",event:p.MOVE,type:u,dx:0,dy:0,dz:0},this.onMouseDown=this.onMouseDown.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.onContextMenu=this.onContextMenu.bind(this),this.onWheel=this.onWheel.bind(this),t.addEventListener("mousedown",this.onMouseDown),t.addEventListener("contextmenu",this.onContextMenu),t.addEventListener("wheel",this.onWheel),document.addEventListener("mousemove",this.onMouseMove),document.addEventListener("mouseup",this.onMouseUp)}destroy(){let t=this.eventTarget;t.removeEventListener("mousedown",this.onMouseDown),t.removeEventListener("contextmenu",this.onContextMenu),t.removeEventListener("wheel",this.onWheel),document.removeEventListener("mousemove",this.onMouseMove),document.removeEventListener("mouseup",this.onMouseUp),super.destroy()}setPointerLock(t=!0){t?this.eventTarget.requestPointerLock():this.eventTarget.exitPointerLock()}hasPointerLock(){return document.pointerLockElement===this.eventTarget}onMouseDown(t){this._downHasFocus=!0;let e=this._eventObject;if(e.keyCode="Button"+t.button,e.event=p.DOWN,e.value=1,e.control=t.ctrlKey,e.shift=t.shiftKey,e.alt=t.altKey,this.dispatchInput(e)&&document.activeElement===this.eventTarget)return t.preventDefault(),t.stopPropagation(),!1}onContextMenu(t){return t.preventDefault(),t.stopPropagation(),!1}onWheel(t){let e=this._wheelObject;switch(t.deltaMode){case WheelEvent.DOM_DELTA_LINE:e.dx=10*t.deltaX,e.dy=10*t.deltaY,e.dz=10*t.deltaZ;break;case WheelEvent.DOM_DELTA_PAGE:e.dx=100*t.deltaX,e.dy=100*t.deltaY,e.dz=100*t.deltaZ;break;case WheelEvent.DOM_DELTA_PIXEL:default:e.dx=t.deltaX,e.dy=t.deltaY,e.dz=t.deltaZ}if(this.dispatchInput(e))return t.preventDefault(),t.stopPropagation(),!1}onMouseUp(t){if(!this._downHasFocus)return;this._downHasFocus=!1;let e=this._eventObject;return e.keyCode="Button"+t.button,e.event=p.UP,e.value=1,e.control=t.ctrlKey,e.shift=t.shiftKey,e.alt=t.altKey,this.dispatchInput(e)?(t.preventDefault(),t.stopPropagation(),!1):void 0}onMouseMove(t){if(this.eventsOnFocus&&document.activeElement!==this.eventTarget)return;const e=this.canvasTarget,{clientWidth:i,clientHeight:s}=e,n=e.getBoundingClientRect();let o=t.movementX/i,r=t.movementY/s,a=(t.clientX-n.left)/i,l=(t.clientY-n.top)/s,h=this._positionObject;h.x=a,h.y=l,h.dx=o,h.dy=r,this.dispatchInput(h)}}class I extends HTMLElement{static get[Symbol.for("cuttleTemplate")](){let t=document.createElement("template");return t.innerHTML='<kbd>\n    <span id="key"><slot></slot></span>\n    <span id="value" class="hidden"></span>\n</kbd>\n',Object.defineProperty(this,Symbol.for("cuttleTemplate"),{value:t}),t}static get[Symbol.for("cuttleStyle")](){let t=document.createElement("style");return t.innerHTML='kbd{position:relative;display:inline-block;border-radius:3px;border:1px solid #888;font-size:.85em;font-weight:700;text-rendering:optimizeLegibility;line-height:12px;height:14px;padding:2px 4px;color:#444;background-color:#eee;box-shadow:inset 0 -3px 0 #aaa;overflow:hidden}kbd:empty:after{content:"<?>";opacity:.6}.disabled{opacity:.6;box-shadow:none;background-color:#aaa}.hidden{display:none}#value{position:absolute;top:0;bottom:0;right:0;font-size:.85em;padding:2px 4px 0;color:#ccc;background-color:#333;box-shadow:inset 0 3px 0 #222}',Object.defineProperty(this,Symbol.for("cuttleStyle"),{value:t}),t}static get properties(){return{name:String,value:String,disabled:Boolean}}get disabled(){return this._disabled}set disabled(t){this.toggleAttribute("disabled",t)}get value(){return this._value}set value(t){this.setAttribute("value",t)}get name(){return this._name}set name(t){this.setAttribute("name",t)}constructor(){super(),this.attachShadow({mode:"open"}),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleTemplate")].content.cloneNode(!0)),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleStyle")].cloneNode(!0)),this._keyboardElement=this.shadowRoot.querySelector("kbd"),this._keyElement=this.shadowRoot.querySelector("#key"),this._valueElement=this.shadowRoot.querySelector("#value")}attributeChangedCallback(t,e,i){switch(t){case"name":this._name=i;break;case"value":this._value=i;break;case"disabled":this._disabled=null!==i}((t,e,i)=>{switch(t){case"name":this._keyElement.innerText=i;break;case"value":null!==i?(this._valueElement.classList.toggle("hidden",!1),this._valueElement.innerText=i,this._keyboardElement.style.paddingRight=`${this._valueElement.clientWidth+4}px`):this._valueElement.classList.toggle("hidden",!0);break;case"disabled":this._keyboardElement.classList.toggle("disabled",null!==i)}})(t,0,i)}connectedCallback(){if(Object.prototype.hasOwnProperty.call(this,"name")){let t=this.name;delete this.name,this.name=t}if(Object.prototype.hasOwnProperty.call(this,"value")){let t=this.value;delete this.value,this.value=t}if(Object.prototype.hasOwnProperty.call(this,"disabled")){let t=this.disabled;delete this.disabled,this.disabled=t}}static get observedAttributes(){return["name","value","disabled"]}}window.customElements.define("input-key",I);class P extends HTMLElement{static get[Symbol.for("cuttleTemplate")](){let t=document.createElement("template");return t.innerHTML='<table>\n    <thead>\n        <tr class="tableHeader">\n            <th colspan=4>\n                <slot></slot>\n            </th>\n        </tr>\n        <tr class="colHeader">\n            <th>name</th>\n            <th>key</th>\n            <th>mod</th>\n            <th>value</th>\n        </tr>\n    </thead>\n    <tbody>\n    </tbody>\n</table>\n',Object.defineProperty(this,Symbol.for("cuttleTemplate"),{value:t}),t}static get[Symbol.for("cuttleStyle")](){let t=document.createElement("style");return t.innerHTML=":host{display:block}table{border-collapse:collapse}table,td,th{border:1px solid #666}td,th{padding:5px 10px}td{text-align:center}thead th{padding:0}.colHeader>th{font-size:.8em;padding:0 10px;letter-spacing:3px;background-color:#aaa;color:#666}.colHeader>th,output{font-family:monospace}output{border-radius:.3em;padding:3px}tr:not(.primary) .name,tr:not(.primary) .value{opacity:.3}tr:nth-child(2n){background-color:#eee}",Object.defineProperty(this,Symbol.for("cuttleStyle"),{value:t}),t}static get customEvents(){return["load"]}get onload(){return this._onload}set onload(t){this._onload&&this.removeEventListener("load",this._onload),this._onload=t,this._onload&&this.addEventListener("load",t)}static get observedAttributes(){return["onload","src"]}constructor(){super(),this.attachShadow({mode:"open"}),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleTemplate")].content.cloneNode(!0)),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleStyle")].cloneNode(!0)),this._src="",this._inputMap=null,this._tableElements={},this._titleElement=this.shadowRoot.querySelector("#title"),this._bodyElement=this.shadowRoot.querySelector("tbody"),this._children=this.shadowRoot.querySelector("slot")}get map(){return this._inputMap}get mapElements(){return this._tableElements}connectedCallback(){if(Object.prototype.hasOwnProperty.call(this,"onload")){let t=this.onload;delete this.onload,this.onload=t}!function(t,e){if(Object.prototype.hasOwnProperty.call(t,e)){let i=t[e];delete t[e],t[e]=i}}(this,"src")}attributeChangedCallback(t,e,i){switch(t){case"onload":this.onload=new Function("event","with(document){with(this){"+i+"}}").bind(this)}((t,e,i)=>{switch(t){case"src":if(this._src!==i)if(this._src=i,i.trim().startsWith("{")){let t=JSON.parse(i);this._setInputMap(t)}else fetch(i).then((t=>t.json())).then((t=>this._setInputMap(t)))}})(t,0,i)}get src(){return this.getAttribute("src")}set src(t){switch(typeof t){case"object":{let e=JSON.stringify(t);this._src=e,this._setInputMap(t),this.setAttribute("src",e)}break;case"string":this.setAttribute("src",t);break;default:this.setAttribute("src",JSON.stringify(t))}}_setInputMap(t){let e={},i=[];for(let s in t){let n=[];N(n,s,t[s]),e[s]=n,i.push(...n)}this._bodyElement.innerHTML="";for(let t of i)this._bodyElement.appendChild(t);this._inputMap=t,this._tableElements=e,this.dispatchEvent(new CustomEvent("load",{bubbles:!1,composed:!0,detail:{map:t}}))}}function N(t,e,i){if(Array.isArray(i)){N(t,e,i[0]);let s=i.length;for(let n=1;n<s;++n)t.push(j(e,i[n],!1))}else t.push(j(e,i,!0));return t}function j(t,e,i=!0){if("object"==typeof e){const{key:s,event:n,scale:o}=e;return R(t,s,n,o,0,i)}return R(t,e,null,1,0,i)}function R(t,e,i,s,n,o=!0){let r=document.createElement("tr");o&&r.classList.add("primary");{let e=document.createElement("td");e.textContent=t,e.classList.add("name"),r.appendChild(e)}{let t=document.createElement("td");t.classList.add("key");let i=new I;i.innerText=e,t.appendChild(i),r.appendChild(t)}{let t=document.createElement("td"),e=document.createElement("samp"),n=[];"string"==typeof i&&"null"!==i&&n.push(i),"number"==typeof s&&1!==s&&n.push(`×(${s.toFixed(2)})`),e.innerText=n.join(" "),t.classList.add("mod"),t.appendChild(e),r.appendChild(t)}{let t=document.createElement("td"),e=document.createElement("output");e.innerText=Number(n).toFixed(2),e.classList.add("value"),t.appendChild(e),r.appendChild(t)}return r}window.customElements.define("input-map",P);const F=Symbol("inputSourceState");class H extends x{static for(t){if(t)return function(t){if(D(t)){let e=Object.getOwnPropertyDescriptor(t,F);return e.value.refs+=1,e.value.state}{let e=new x([new O(t),new A(t)]);return Object.defineProperty(t,F,{value:{state:e,refs:1},configurable:!0}),e}}(t);throw new Error("Cannot get input source for null event target.")}static delete(t){t&&function(t){if(D(t)){let e=Object.getOwnPropertyDescriptor(t,F);if(e.value.refs-=1,e.value.refs<=0){let i=e.value.state;delete t[F],i.destroy()}}}(t)}constructor(){throw super(),new Error("Cannot construct InputSource with new - use InputSourceState() instead.")}}function D(t){return Object.prototype.hasOwnProperty.call(t,F)&&Object.getOwnPropertyDescriptor(t,F).value}class W extends HTMLElement{static get[Symbol.for("cuttleTemplate")](){let t=document.createElement("template");return t.innerHTML='<div>\n    <label id="title">\n        input-source\n    </label>\n    <span>|</span>\n    <p>\n        <label for="poll">poll</label>\n        <output id="poll"></output>\n    </p>\n    <p>\n        <label for="focus">focus</label>\n        <output id="focus"></output>\n    </p>\n</div>\n',Object.defineProperty(this,Symbol.for("cuttleTemplate"),{value:t}),t}static get[Symbol.for("cuttleStyle")](){let t=document.createElement("style");return t.innerHTML=':host{display:inline-block}div{font-family:monospace;color:#666;outline:1px solid #666;padding:4px}p{display:inline;margin:0;padding:0}#focus:empty:after,#poll:empty:after{content:"✗";color:red}',Object.defineProperty(this,Symbol.for("cuttleStyle"),{value:t}),t}static get properties(){return{for:String,autopoll:Boolean}}get autopoll(){return this._autopoll}set autopoll(t){this.toggleAttribute("autopoll",t)}get for(){return this._for}set for(t){this.setAttribute("for",t)}static get customEvents(){return["input","poll","change"]}get onchange(){return this._onchange}set onchange(t){this._onchange&&this.removeEventListener("change",this._onchange),this._onchange=t,this._onchange&&this.addEventListener("change",t)}get onpoll(){return this._onpoll}set onpoll(t){this._onpoll&&this.removeEventListener("poll",this._onpoll),this._onpoll=t,this._onpoll&&this.addEventListener("poll",t)}get oninput(){return this._oninput}set oninput(t){this._oninput&&this.removeEventListener("input",this._oninput),this._oninput=t,this._oninput&&this.addEventListener("input",t)}constructor(){super(),this.attachShadow({mode:"open"}),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleTemplate")].content.cloneNode(!0)),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleStyle")].cloneNode(!0)),this._containerElement=this.shadowRoot.querySelector("div"),this._titleElement=this.shadowRoot.querySelector("#title"),this._pollElement=this.shadowRoot.querySelector("#poll"),this._focusElement=this.shadowRoot.querySelector("#focus"),this._eventTarget=null,this._sourceState=null,this.onSourcePoll=this.onSourcePoll.bind(this),this.onSourceInput=this.onSourceInput.bind(this),this.onTargetFocus=this.onTargetFocus.bind(this),this.onTargetBlur=this.onTargetBlur.bind(this),this.onPollStatusCheck=this.onPollStatusCheck.bind(this),this._intervalHandle=0}get eventTarget(){return this._eventTarget}get source(){return this._sourceState}connectedCallback(){if(Object.prototype.hasOwnProperty.call(this,"oninput")){let t=this.oninput;delete this.oninput,this.oninput=t}if(Object.prototype.hasOwnProperty.call(this,"onpoll")){let t=this.onpoll;delete this.onpoll,this.onpoll=t}if(Object.prototype.hasOwnProperty.call(this,"onchange")){let t=this.onchange;delete this.onchange,this.onchange=t}if(Object.prototype.hasOwnProperty.call(this,"for")){let t=this.for;delete this.for,this.for=t}if(Object.prototype.hasOwnProperty.call(this,"autopoll")){let t=this.autopoll;delete this.autopoll,this.autopoll=t}this.hasAttribute("tabindex")||this.setAttribute("tabindex",0),this.hasAttribute("for")||this._eventTarget||this.setEventTarget(this),this._intervalHandle=setInterval(this.onPollStatusCheck,1e3)}disconnectedCallback(){this.clearEventTarget(),clearInterval(this._intervalHandle)}attributeChangedCallback(t,e,i){switch(t){case"for":this._for=i;break;case"autopoll":this._autopoll=null!==i;break;case"oninput":this.oninput=new Function("event","with(document){with(this){"+i+"}}").bind(this);break;case"onpoll":this.onpoll=new Function("event","with(document){with(this){"+i+"}}").bind(this);break;case"onchange":this.onchange=new Function("event","with(document){with(this){"+i+"}}").bind(this)}((t,e,i)=>{switch(t){case"for":{let t,e;i?(t=document.getElementById(i),e=`${t.tagName.toLowerCase()}#${i}`):(t=this,e="input-source"),this.setEventTarget(i?document.getElementById(i):this),this._titleElement.innerHTML=`for(${e})`}break;case"autopoll":this._sourceState&&(this._sourceState.autopoll=this._autopoll)}})(t,0,i)}setEventTarget(t){if(this.clearEventTarget(),t){let e=H.for(t);this._sourceState=e,this._eventTarget=t,e.autopoll=this.autopoll,e.addEventListener("poll",this.onSourcePoll),e.addEventListener("input",this.onSourceInput),t.addEventListener("focus",this.onTargetFocus),t.addEventListener("blur",this.onTargetBlur),this.dispatchEvent(new CustomEvent("change",{composed:!0,bubbles:!1}))}return this}clearEventTarget(){if(this._eventTarget){let t=this._eventTarget,e=this._sourceState;this._eventTarget=null,this._sourceState=null,t&&(t.removeEventListener("focus",this.onTargetFocus),t.removeEventListener("blur",this.onTargetBlur),e.removeEventListener("poll",this.onSourcePoll),e.removeEventListener("input",this.onSourceInput),H.delete(t))}}poll(t){this._sourceState.poll(t)}onPollStatusCheck(){this._sourceState&&this._sourceState.polling?this._pollElement.innerHTML="✓":this._pollElement.innerHTML=""}onSourceInput({stage:t,deviceName:e,keyCode:i,input:s}){this.dispatchEvent(new CustomEvent("input",{composed:!0,bubbles:!1,detail:{stage:t,deviceName:e,keyCode:i,input:s}}))}onSourcePoll({now:t}){this.dispatchEvent(new CustomEvent("poll",{composed:!0,bubbles:!1,detail:{now:t}}))}onTargetFocus(){this._focusElement.innerHTML="✓"}onTargetBlur(){this._focusElement.innerHTML=""}static get observedAttributes(){return["oninput","onpoll","onchange","for","autopoll"]}}window.customElements.define("input-source",W);class K extends HTMLElement{static get[Symbol.for("cuttleTemplate")](){let t=document.createElement("template");return t.innerHTML="<input-map>\n    <slot></slot>\n    <input-source></input-source>\n</input-map>\n",Object.defineProperty(this,Symbol.for("cuttleTemplate"),{value:t}),t}static get[Symbol.for("cuttleStyle")](){let t=document.createElement("style");return t.innerHTML=":host{display:inline-block}",Object.defineProperty(this,Symbol.for("cuttleStyle"),{value:t}),t}static get properties(){return{for:String,autopoll:Boolean,disabled:Boolean}}get disabled(){return this._disabled}set disabled(t){this.toggleAttribute("disabled",t)}get autopoll(){return this._autopoll}set autopoll(t){this.toggleAttribute("autopoll",t)}get for(){return this._for}set for(t){this.setAttribute("for",t)}static get observedAttributes(){return["for","autopoll","disabled","src"]}constructor(t=new L){super(),this.attachShadow({mode:"open"}),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleTemplate")].content.cloneNode(!0)),this.shadowRoot.appendChild(this.constructor[Symbol.for("cuttleStyle")].cloneNode(!0)),this._src="",this._mapElement=this.shadowRoot.querySelector("input-map"),this._sourceElement=this.shadowRoot.querySelector("input-source"),this._context=t,this.onSourcePoll=this.onSourcePoll.bind(this),this.onSourceChange=this.onSourceChange.bind(this),this.onContextChange=this.onContextChange.bind(this),this._sourceElement.addEventListener("poll",this.onSourcePoll),this._sourceElement.addEventListener("change",this.onSourceChange),this._context.addEventListener("change",this.onContextChange)}get context(){return this._context}get source(){return this._sourceElement.source}get mapping(){return this._mapElement.map}get src(){return this._src}set src(t){this.setAttribute("src","string"==typeof t?t:JSON.stringify(t))}connectedCallback(){if(Object.prototype.hasOwnProperty.call(this,"for")){let t=this.for;delete this.for,this.for=t}if(Object.prototype.hasOwnProperty.call(this,"autopoll")){let t=this.autopoll;delete this.autopoll,this.autopoll=t}if(Object.prototype.hasOwnProperty.call(this,"disabled")){let t=this.disabled;delete this.disabled,this.disabled=t}!function(t,e){if(Object.prototype.hasOwnProperty.call(t,e)){let i=t[e];delete t[e],t[e]=i}}(this,"src")}attributeChangedCallback(t,e,i){switch(t){case"for":this._for=i;break;case"autopoll":this._autopoll=null!==i;break;case"disabled":this._disabled=null!==i}((t,e,i)=>{switch(t){case"for":this._sourceElement.for=i;break;case"src":if(this._src!==i)if(this._src=i,i.trim().startsWith("{")){let t=JSON.parse(i);this.updateMapping(t)}else fetch(i).then((t=>t.json())).then((t=>this.updateMapping(t)));break;case"autopoll":this._sourceElement.autopoll=this._autopoll;break;case"disabled":this._context.source&&(this._context.disabled=this._disabled)}})(t,0,i)}updateMapping(t){this._context.setInputMapping(t),this._mapElement.src=t}onSourcePoll(){for(let[t,e]of Object.entries(this._mapElement.mapElements)){let i=this._context.getInputState(t);e[0].querySelector("output").innerText=Number(i).toFixed(2)}}onSourceChange(){this._context.source&&this._context.detach(),this._sourceElement.source&&(this._context.attach(this._sourceElement.source),this._context.disabled=this._disabled)}onContextChange(){this._mapElement.src=this._context.mapping}hasInput(t){return this._context.hasInput(t)}getInput(t){return this._context.getInput(t)}getInputState(t){return this._context.getInputState(t)}getInputChanged(t){return this._context.getInputChanged(t)}}window.customElements.define("input-port",K);const q=(new L).setInputMapping({activate:{key:"Mouse:Button0",event:"up"},mark:{key:"Mouse:Button2",event:"up"},restart:{key:"Keyboard:KeyR",event:"up"},cursorX:{key:"Mouse:PosX",scale:1},cursorY:{key:"Mouse:PosY",scale:1}}),U=q.getInput("cursorX"),B=q.getInput("cursorY"),$=q.getInput("activate"),z=q.getInput("mark"),V=q.getInput("restart");var X="undefined"!=typeof Float32Array?Float32Array:Array;function Y(){var t=new X(16);return X!=Float32Array&&(t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[11]=0,t[12]=0,t[13]=0,t[14]=0),t[0]=1,t[5]=1,t[10]=1,t[15]=1,t}function G(){var t=new X(3);return X!=Float32Array&&(t[0]=0,t[1]=0,t[2]=0),t}function J(t,e,i){var s=new X(3);return s[0]=t,s[1]=e,s[2]=i,s}Math.hypot||(Math.hypot=function(){for(var t=0,e=arguments.length;e--;)t+=arguments[e]*arguments[e];return Math.sqrt(t)});function Z(){var t=new X(4);return X!=Float32Array&&(t[0]=0,t[1]=0,t[2]=0),t[3]=1,t}G(),function(){var t,e=(t=new X(4),X!=Float32Array&&(t[0]=0,t[1]=0,t[2]=0,t[3]=0),t)}();var Q;G(),J(1,0,0),J(0,1,0),Z(),Z(),Q=new X(9),X!=Float32Array&&(Q[1]=0,Q[2]=0,Q[3]=0,Q[5]=0,Q[6]=0,Q[7]=0),Q[0]=1,Q[4]=1,Q[8]=1;class tt{static screenToWorld(t,e,i,s){let n=function(t,e,i){var s=e[0],n=e[1],o=e[2],r=e[3],a=e[4],l=e[5],h=e[6],c=e[7],d=e[8],u=e[9],p=e[10],m=e[11],f=e[12],g=e[13],v=e[14],b=e[15],y=i[0],w=i[1],E=i[2],_=i[3];return t[0]=y*s+w*a+E*d+_*f,t[1]=y*n+w*l+E*u+_*g,t[2]=y*o+w*h+E*p+_*v,t[3]=y*r+w*c+E*m+_*b,y=i[4],w=i[5],E=i[6],_=i[7],t[4]=y*s+w*a+E*d+_*f,t[5]=y*n+w*l+E*u+_*g,t[6]=y*o+w*h+E*p+_*v,t[7]=y*r+w*c+E*m+_*b,y=i[8],w=i[9],E=i[10],_=i[11],t[8]=y*s+w*a+E*d+_*f,t[9]=y*n+w*l+E*u+_*g,t[10]=y*o+w*h+E*p+_*v,t[11]=y*r+w*c+E*m+_*b,y=i[12],w=i[13],E=i[14],_=i[15],t[12]=y*s+w*a+E*d+_*f,t[13]=y*n+w*l+E*u+_*g,t[14]=y*o+w*h+E*p+_*v,t[15]=y*r+w*c+E*m+_*b,t}(Y(),s,i);!function(t,e){var i=e[0],s=e[1],n=e[2],o=e[3],r=e[4],a=e[5],l=e[6],h=e[7],c=e[8],d=e[9],u=e[10],p=e[11],m=e[12],f=e[13],g=e[14],v=e[15],b=i*a-s*r,y=i*l-n*r,w=i*h-o*r,E=s*l-n*a,_=s*h-o*a,x=n*h-o*l,S=c*f-d*m,k=c*g-u*m,M=c*v-p*m,L=d*g-u*f,T=d*v-p*f,C=u*v-p*g,O=b*C-y*T+w*L+E*M-_*k+x*S;O&&(O=1/O,t[0]=(a*C-l*T+h*L)*O,t[1]=(n*T-s*C-o*L)*O,t[2]=(f*x-g*_+v*E)*O,t[3]=(u*_-d*x-p*E)*O,t[4]=(l*M-r*C-h*k)*O,t[5]=(i*C-n*M+o*k)*O,t[6]=(g*w-m*x-v*y)*O,t[7]=(c*x-u*w+p*y)*O,t[8]=(r*T-a*M+h*S)*O,t[9]=(s*M-i*T-o*S)*O,t[10]=(m*_-f*w+v*b)*O,t[11]=(d*w-c*_-p*b)*O,t[12]=(a*k-r*L-l*S)*O,t[13]=(i*L-s*k+n*S)*O,t[14]=(f*y-m*E-g*b)*O,t[15]=(c*E-d*y+u*b)*O)}(n,n);let o=J(t,e,0);return function(t,e,i){var s=e[0],n=e[1],o=e[2],r=i[3]*s+i[7]*n+i[11]*o+i[15];r=r||1,t[0]=(i[0]*s+i[4]*n+i[8]*o+i[12])/r,t[1]=(i[1]*s+i[5]*n+i[9]*o+i[13])/r,t[2]=(i[2]*s+i[6]*n+i[10]*o+i[14])/r}(o,o,n),o}constructor(t=-1,e=1,i=-1,s=1,n=0,o=1){this.position=G(),this.rotation=Z(),this.scale=J(1,1,1),this.clippingPlane={left:t,right:e,top:i,bottom:s,near:n,far:o},this._viewMatrix=Y(),this._projectionMatrix=Y()}get x(){return this.position[0]}set x(t){this.position[0]=t}get y(){return this.position[1]}set y(t){this.position[1]=t}get z(){return this.position[2]}set z(t){this.position[2]=t}moveTo(t,e,i=0,s=1){let n=J(t,e,i);!function(t,e,i,s){var n=e[0],o=e[1],r=e[2];t[0]=n+s*(i[0]-n),t[1]=o+s*(i[1]-o),t[2]=r+s*(i[2]-r)}(this.position,this.position,n,Math.min(1,s))}getViewMatrix(t=this._viewMatrix){let e=-Math.round(this.x),i=-Math.round(this.y),s=0===this.z?1:1/this.z,n=J(e,i,0),o=J(this.scale[0]*s,this.scale[1]*s,1);return function(t,e,i,s){var n=e[0],o=e[1],r=e[2],a=e[3],l=n+n,h=o+o,c=r+r,d=n*l,u=n*h,p=n*c,m=o*h,f=o*c,g=r*c,v=a*l,b=a*h,y=a*c,w=s[0],E=s[1],_=s[2];t[0]=(1-(m+g))*w,t[1]=(u+y)*w,t[2]=(p-b)*w,t[3]=0,t[4]=(u-y)*E,t[5]=(1-(d+g))*E,t[6]=(f+v)*E,t[7]=0,t[8]=(p+b)*_,t[9]=(f-v)*_,t[10]=(1-(d+m))*_,t[11]=0,t[12]=i[0],t[13]=i[1],t[14]=i[2],t[15]=1}(t,this.rotation,n,o),t}getProjectionMatrix(t=this._projectionMatrix){let{left:e,right:i,top:s,bottom:n,near:o,far:r}=this.clippingPlane;return function(t,e,i,s,n,o,r){var a=1/(e-i),l=1/(s-n),h=1/(o-r);t[0]=-2*a,t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=-2*l,t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=2*h,t[11]=0,t[12]=(e+i)*a,t[13]=(n+s)*l,t[14]=(r+o)*h,t[15]=1}(t,e,i,s,n,o,r),t}}class et{constructor(){this.prevTransformMatrix=null,this.domProjectionMatrix=new DOMMatrix,this.domViewMatrix=new DOMMatrix,this.ctx=null}begin(t,e,i){if(this.ctx)throw new Error("View already begun - maybe missing end() call?");e&&it(this.domViewMatrix,e),i&&it(this.domProjectionMatrix,i),this.prevTransformMatrix=t.getTransform(),t.setTransform(this.domProjectionMatrix);const{a:s,b:n,c:o,d:r,e:a,f:l}=this.domViewMatrix;t.transform(s,n,o,r,a,l),this.ctx=t}end(t){t.setTransform(this.prevTransformMatrix),this.ctx=null}}function it(t,e){return t.a=e[0],t.b=e[1],t.c=e[4],t.d=e[5],t.e=e[12],t.f=e[13],t}const st=16;function nt(){this.mines=new r(16,16),this.minesView=new et,this.camera=new tt,this.camera.x=-32,this.camera.y=-32,this.firstAction=!1,this.health=3,this.gameOver=!1,this.gameWin=!1,this.gameTime=0}function ot(t){}function rt(t){var e,s;if(V.value)at(this);else if(this.gameOver||this.gameWin){if($.value||z.value)return void at(this)}else{const n=this.display.width,o=this.display.height;this.firstAction&&(this.gameTime+=t);let r=!1;if($.value){let t=U.value*n,a=B.value*o,l=tt.screenToWorld(t,a,this.camera.getViewMatrix(),this.camera.getProjectionMatrix()),h=i(Math.floor(l[0]/st),0,this.mines.width-1),c=i(Math.floor(l[1]/st),0,this.mines.height-1);this.mines.dig(h,c)||(s=1,(e=this).health-=s,e.health<=0&&function(t){t.gameOver=!0}(e)),this.mines.checkWinCondition()&&function(t){t.gameWin=!0}(this),r=!0}if(z.value){let t=U.value*n,e=B.value*o,s=tt.screenToWorld(t,e,this.camera.getViewMatrix(),this.camera.getProjectionMatrix()),a=i(Math.floor(s[0]/st),0,this.mines.width-1),l=i(Math.floor(s[1]/st),0,this.mines.height-1);this.mines.mark(a,l),r=!0}r&&!this.firstAction&&(this.firstAction=!0)}}function at(t){t.mines.reset(),t.gameOver=!1,t.gameWin=!1,t.gameTime=0,t.firstAction=!1,t.health=3}async function lt(t,e){return new Promise(((e,i)=>{let s=new Image;s.addEventListener("load",(()=>{e(s)})),s.addEventListener("error",(t=>{i(t)})),s.src=t}))}const ht={LOADED:!1,TILE_IMAGE:null,NUMS_IMAGE:null,MARK_IMAGE:null};async function ct(){await async function(){const t="../../../res/";ht.TILE_IMAGE=await lt(t+"mines/tile.png"),ht.NUMS_IMAGE=await lt(t+"mines/nums.png"),ht.MARK_IMAGE=await lt(t+"mines/flag.png"),ht.LOADED=!0}()}function dt(t,e){let s=t.context;const n=e.camera.getViewMatrix(),o=e.camera.getProjectionMatrix();e.minesView.begin(s,n,o),function(t,e,i=16){if(!ht.LOADED)throw new Error("Assets for this renderer have not yet been loaded.");const s=e.width,n=e.height,o=e.data,r=i/2;t.fillStyle="#777777",t.fillRect(0,0,s*i,n*i),function(t,e,i,s,n,o,r){t.strokeStyle="#888888",t.beginPath();for(let o=0;o<n;o+=r)t.moveTo(e,o+i),t.lineTo(e+s,o+i);for(let r=0;r<s;r+=o)t.moveTo(r+e,i),t.lineTo(r+e,i+n);t.stroke()}(t,0,0,s*i,n*i,i,i);for(let e=0;e<n;++e)for(let n=0;n<s;++n){let a=n*i,l=e*i,h=n+e*s;if(o.solids[h]>0)t.drawImage(ht.TILE_IMAGE,a,l,i,i),o.marks[h]>0&&t.drawImage(ht.MARK_IMAGE,a,l,i,i);else if(o.tiles[h]>0)t.fillStyle="rgba(0, 0, 0, 0.2)",t.fillRect(a,l,i,i),t.textAlign="center",t.textBaseline="middle",t.font="10px sans-serif",t.fillStyle="#000000",t.fillText("X",a+r,l+r);else if(o.overlay[h]>0){let e=o.overlay[h]-1;t.drawImage(ht.NUMS_IMAGE,32*e,0,32,32,a,l,i,i)}}}(s,e.mines,st),e.minesView.end(s);const r=e.mines.width,a=e.mines.height,l=32,h=st;let c=U.value*t.width,d=B.value*t.height,u=tt.screenToWorld(c,d,n,o),p=i(Math.floor(u[0]/h),0,r-1),m=i(Math.floor(u[1]/h),0,a-1);s.fillStyle="rgba(0, 0, 0, 0.2)",s.fillRect(32+p*h,l+m*h,h,h),s.fillStyle="rgba(0, 0, 0, 0.3)",s.fillRect(0,0,t.width,l),s.fillRect(0,t.height-l,t.width,l),s.fillRect(0,l,32,t.height-64),s.fillRect(t.width-32,l,32,t.height-64);for(let t=0;t<3;++t){let i=t<e.health?"salmon":"darkgray";s.fillStyle=i,s.fillRect(0+t*h,0,13,13)}e.gameOver?(ut(s,t.width/2,t.height/2,.7*t.width,8,"black"),pt(s,t.width/2+1,t.height/2+1,"Game Over",32,"black"),pt(s,t.width/2-1,t.height/2-1,"Game Over",32,"white"),pt(s,t.width/2+1,t.height/2+24+1,"Click to continue",16,"black"),pt(s,t.width/2-1,t.height/2+24-1,"Click to continue",16,"white")):e.gameWin&&(ut(s,t.width/2,t.height/2,.7*t.width,8,"black"),pt(s,t.width/2+1,t.height/2+1,"Success!",32,"black"),pt(s,t.width/2-1,t.height/2-1,"Success!",32,"gold"),pt(s,t.width/2+1,t.height/2+24+1,"Click to continue",16,"black"),pt(s,t.width/2-1,t.height/2+24-1,"Click to continue",16,"white")),pt(s,t.width/2,t.height-h,`Time: ${Math.floor(e.gameTime)}`,16,"white")}function ut(t,e,i,s,n,o){t.translate(e,i);{let e=s/2,i=n/2;t.fillStyle=o,t.fillRect(-e,-i,s,n)}t.translate(-e,-i)}function pt(t,e,i,s,n,o){t.translate(e,i),t.textAlign="center",t.textBaseline="middle",t.font=`${n}px sans-serif`,t.fillStyle=o,t.fillText(s,0,0),t.translate(-e,-i)}function mt(t){t instanceof PromiseRejectionEvent?window.alert(t.reason.stack):t instanceof ErrorEvent?window.alert(t.error.stack):window.alert(JSON.stringify(t))}document.addEventListener("DOMContentLoaded",(async function(){const t=document.querySelector("display-port"),e=t.canvas.getContext("2d");e.imageSmoothingEnabled=!1,function(){const t=H.for(document.querySelector("#main"));q.attach(t)}();const i={display:t};await ct.call(i),nt.call(i),t.addEventListener("frame",(s=>{const n=s.detail.deltaTime/1e3;ot.call(i,n),q.source.poll(),rt.call(i,n);const o={context:e,width:t.width,height:t.height};e.clearRect(0,0,o.width,o.height),dt.call(i,o,i)}))})),window.addEventListener("unhandledrejection",mt,!0),window.addEventListener("error",mt,!0)}();
  