@benev/tact 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,7 +27,7 @@ var Js=Object.defineProperty;var Ys=(s,t)=>{for(var e in t)Js(s,e,{get:t[e],enum
27
27
  font-family: monospace;
28
28
  color: red;
29
29
  }
30
- `;var y=class s{x;constructor(t){this.x=t}static new(t){return new this(t)}clone(){return new s(this.x)}set(t){return this.x=t,this}static isBetween(t,e=0,r=1){let i=Math.min(e,r),o=Math.max(e,r);return t>=i&&t<=o}isBetween(t=0,e=1){return s.isBetween(this.x,t,e)}static isNear(t,e,r=.01){return Math.abs(t-e)<=r}isNear(t,e=.01){return s.isNear(this.x,t,e)}static add(...t){let e=0;for(let r of t)e+=r;return e}add(...t){return this.x=s.add(this.x,...t),this}subtract(...t){for(let e of t)this.x-=e;return this}static atLeast(t,e=0){return Math.max(t,e)}atLeast(t=0){return this.x=s.atLeast(this.x,t),this}static atMost(t,e=1){return Math.min(t,e)}atMost(t=1){return this.x=s.atMost(this.x,t),this}static clamp(t,e=0,r=1){return t=s.atLeast(t,Math.min(e,r)),t=s.atMost(t,Math.max(e,r)),t}clamp(t=0,e=1){return this.x=s.clamp(this.x,t,e),this}static lerp(t,e,r,i){let n=(e-t)*r;return i!==void 0&&Math.abs(n)>i&&(n=Math.sign(n)*i),t+n}lerp(t,e,r){return this.x=s.lerp(this.x,t,e,r),this}static step(t,e,r){let i=e-t;return Math.abs(i)<=r?e:t+Math.sign(i)*r}step(t,e){return this.x=s.step(this.x,t,e),this}static creep(t,e,r,i){let o=t*(1-Math.exp(-e*r));if(i!==void 0){let n=i*r;Math.abs(o)>n&&(o=Math.sign(o)*n)}return o}static approach(t,e,r,i,o){let n=e-t,c=this.creep(n,r,i,o);return t+c}approach(t,e,r,i){return this.x=s.approach(this.x,t,e,r,i),this}static wrap(t,e=0,r=1){let i=Math.min(e,r),n=Math.max(e,r)-i,c=t-i,a=c<0?n- -c%n:c%n;return i+a}wrap(t=0,e=1){return this.x=s.wrap(this.x,t,e),this}static constrainProximity(t,e,r){let i=e-t,o=Math.abs(i),n=o>r?r:o,c=i<0?-n:n;return t+c}constrainProximity(t,e){return this.x=s.constrainProximity(this.x,t,e),this}static inverse(t){return 1-t}inverse(){return this.x=s.inverse(this.x),this}static center(t){return t*2-1}center(){return this.x=s.center(this.x),this}static uncenter(t){return(t+1)/2}uncenter(){return this.x=s.uncenter(this.x),this}static map(t,e,r){let o=(r-e)*t;return e+o}map(t,e){return this.x=s.map(this.x,t,e),this}static remap(t,e,r,i=0,o=1,n=!1){let a=(t-e)/(r-e)*(o-i)+i;return n?s.clamp(a,i,o):a}remap(t,e,r=0,i=1,o=!1){return this.x=s.remap(this.x,t,e,r,i,o),this}static magnify(t){return 4*Math.pow(t-.5,3)+.5}magnify(){return this.x=s.magnify(this.x),this}static floor(t){return Math.floor(t)}floor(){return this.x=s.floor(this.x),this}static ceil(t){return Math.ceil(t)}ceil(){return this.x=s.ceil(this.x),this}static round(t){return Math.round(t)}round(){return this.x=s.round(this.x),this}static smooth(t,e,r){return r<=1?e:t+(e-t)/r}smooth(t,e){return this.x=s.smooth(this.x,t,e),this}};var w=class s{x;y;constructor(t,e){this.x=t,this.y=e}static new(t,e){return new this(t,e)}static zero(){return new this(0,0)}static all(t){return new this(t,t)}static from(t){return Array.isArray(t)?new this(...t):new this(t.x,t.y)}static magnitudeSquared(t,e){return t*t+e*e}static magnitude(t,e){return Math.sqrt(this.magnitudeSquared(t,e))}static average(...t){return this.zero().add(...t).divideBy(t.length)}static min(...t){return new this(Math.min(...t.map(e=>e.x)),Math.min(...t.map(e=>e.y)))}static max(...t){return new this(Math.max(...t.map(e=>e.x)),Math.max(...t.map(e=>e.y)))}static fromAngle(t){return new this(Math.cos(t),Math.sin(t))}clone(){return new s(this.x,this.y)}*[Symbol.iterator](){yield this.x,yield this.y}toJSON(){return[this.x,this.y]}toString(){return`(Vec2 x${this.x.toFixed(2)}, y${this.y.toFixed(2)})`}set_(t,e){return this.x=t,this.y=e,this}set({x:t,y:e}){return this.x=t,this.y=e,this}magnitudeSquared(){return s.magnitudeSquared(this.x,this.y)}magnitude(){return s.magnitude(this.x,this.y)}rotation(){return Math.atan2(this.y,this.x)}equals_(t,e){return this.x===t&&this.y===e}equals(...t){return t.every(({x:e,y:r})=>this.equals_(e,r))}near_(t,e,r=1e-6){return Math.abs(this.x-t)<=r&&Math.abs(this.y-e)<=r}near({x:t,y:e},r=1e-6){return this.near_(t,e,r)}dot_(t,e){return this.x*t+this.y*e}dot({x:t,y:e}){return this.dot_(t,e)}distanceSquared_(t,e){return t=this.x-t,e=this.y-e,t*t+e*e}distanceSquared({x:t,y:e}){return this.distanceSquared_(t,e)}distance_(t,e){return Math.sqrt(this.distanceSquared_(t,e))}distance({x:t,y:e}){return this.distance_(t,e)}angleBetween_(t,e){let r=this.dot_(t,e),i=this.magnitude()*s.magnitude(t,e);if(i===0)return 0;let o=y.clamp(r/i,-1,1);return Math.acos(o)}angleBetween({x:t,y:e}){return this.angleBetween_(t,e)}normalize(){return this.divideBy(this.magnitude())}half(){return this.divideBy(2)}double(){return this.multiplyBy(2)}abs(){return this.map(t=>Math.abs(t))}rotate(t){let{x:e,y:r}=this;return this.x=e*Math.cos(t)-r*Math.sin(t),this.y=e*Math.sin(t)+r*Math.cos(t),this}perpendicular(){let{x:t,y:e}=this;return this.x=-e,this.y=t,this}clampMagnitude(t){return this.magnitude()>t&&this.normalize().multiplyBy(t),this}floor(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this}ceil(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this}round(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this}map(t){return this.x=t(this.x,0),this.y=t(this.y,1),this}morph(t){let e=t(this);return e&&e!==this&&this.set(e),this}clamp(t={x:0,y:0},e={x:1,y:1}){return this.x=y.clamp(this.x,t.x,e.x),this.y=y.clamp(this.y,t.y,e.y),this}clampBy(t=0,e=1){return this.x=y.clamp(this.x,t,e),this.y=y.clamp(this.y,t,e),this}negate(){return this.map(t=>t*-1)}addBy(t){return this.x+=t,this.y+=t,this}subtractBy(t){return this.x-=t,this.y-=t,this}multiplyBy(t){return this.x*=t,this.y*=t,this}divideBy(t){return t===0?this:(this.x/=t,this.y/=t,this)}add_(t,e){return this.x+=t,this.y+=e,this}add(...t){for(let{x:e,y:r}of t)this.add_(e,r);return this}subtract_(t,e){return this.x-=t,this.y-=e,this}subtract(...t){for(let{x:e,y:r}of t)this.subtract_(e,r);return this}multiply_(t,e){return this.x*=t,this.y*=e,this}multiply(...t){for(let{x:e,y:r}of t)this.multiply_(e,r);return this}divide_(t,e){return t!==0&&(this.x/=t),e!==0&&(this.y/=e),this}divide(...t){for(let{x:e,y:r}of t)this.divide_(e,r);return this}lerp_(t,e,r){return this.x+=(t-this.x)*r,this.y+=(e-this.y)*r,this}lerp({x:t,y:e},r){return this.lerp_(t,e,r)}approach_(t,e,r,i,o){return this.x=y.approach(this.x,t,r,i,o),this.y=y.approach(this.y,e,r,i,o),this}approach({x:t,y:e},r,i,o){return this.approach_(t,e,r,i,o)}reflect_(t,e){let r=2*this.dot_(t,e);return this.x-=r*t,this.y-=r*e,this}reflect({x:t,y:e}){return this.reflect_(t,e)}rotateAroundPoint_(t,e,r){let i=this.x-t,o=this.y-e,n=Math.cos(r),c=Math.sin(r);return this.x=n*i-c*o+t,this.y=c*i+n*o+e,this}rotateAroundPoint({x:t,y:e},r){return this.rotateAroundPoint_(t,e,r)}smooth_(t,e,r){return this.x=y.smooth(this.x,t,r),this.y=y.smooth(this.y,e,r),this}smooth({x:t,y:e},r){return this.smooth_(t,e,r)}};var Fe=class{state;players;constructor(t,e){this.state=t,this.players=e}tick(){for(let t of this.players){let{agent:e,port:{actions:r}}=t,i=1;e.position.y+=i*r.gameplay.up.value,e.position.y-=i*r.gameplay.down.value,e.position.x-=i*r.gameplay.left.value,e.position.x+=i*r.gameplay.right.value,e.position.clamp(w.zero(),this.state.arenaSize)}}};var Ge=class{label="0";alive=!0;color="#444";position=w.zero()};var Ke=class{arenaSize=new w(200,100);agents=new Set;makeAgent(t){let e=new Ge;return e.label=t,e.position.set_(.5,.5).multiply(this.arenaSize),this.agents.add(e),e}deleteAgent(t){return this.agents.delete(t),this}};var jt=class{};function Dt(s,{start:t,end:e}){return!(t&&s<t||e&&s>e)}var Je=class extends jt{#t=new Map;async gets(...t){return t.map(e=>this.#t.get(e))}async hasKeys(...t){return t.map(e=>this.#t.has(e))}async*keys(t={}){if(t.limit===0)return;let e=0;for(let r of this.#t.keys())if(Dt(r,t)&&(yield r,e+=1),e>=(t.limit??1/0))break}async*entries(t={}){if(t.limit===0)return;let e=0;for(let[r,i]of this.#t.entries())if(Dt(r,t)&&(yield[r,i],e+=1),e>=(t.limit??1/0))break}async transaction(...t){for(let[e,r]of t)r===void 0?this.#t.delete(e):this.#t.set(e,r)}};function Es(s,t,e){let r=Object.entries(t);for(let[i,o]of r)s.addEventListener(i,o,e);return function(){for(let[o,n]of r)s.removeEventListener(o,n)}}var zt=class extends jt{storage;static onStorageEvent=t=>Es(window,{storage:t});constructor(t=window.localStorage){super(),this.storage=t}async gets(...t){return t.map(e=>this.storage.getItem(e)??void 0)}async hasKeys(...t){return t.map(e=>this.storage.getItem(e)!==null)}async*keys(t={}){if(t.limit===0)return;let e=0;for(let r of Object.keys(this.storage))if(Dt(r,t)&&(yield r,e+=1),e>=(t.limit??1/0))break}async*entries(t={}){if(t.limit===0)return;let e=0;for(let[r,i]of Object.entries(this.storage))if(Dt(r,t)&&(yield[r,i],e+=1),e>=(t.limit??1/0))break}async transaction(...t){for(let[e,r]of t)r===void 0?this.storage.removeItem(e):this.storage.setItem(e,r)}};async function Ms(s){let t=[];for await(let e of s)t.push(e);return t}var Ye=class{kv;key;write;constructor(t,e){this.kv=t,this.key=e,this.write=new Nr(t.write,e)}async set(t){return this.kv.set(this.key,t)}async get(){return this.kv.get(this.key)}async require(){return this.kv.require(this.key)}async guarantee(t){let e=await this.get();return e===void 0&&(e=await t(),await this.set(e)),e}},Nr=class{key;#t;constructor(t,e){this.key=e,this.#t=t}set(t){return this.#t.set(this.key,t)}};var oe={parse:s=>JSON.parse(s),stringify:s=>JSON.stringify(s)};var Xe=class{#t;constructor(t){this.#t=t}sets(...t){return t.map(([e,r])=>{let i=this.#t.prefix(e),o=r===void 0?void 0:oe.stringify(r);return[i,o]})}set(t,e){return this.sets([t,e])}del(...t){return t.map(e=>[this.#t.prefix(e),void 0])}};function*Ps(s,t){if(s<=0)throw new Error("chunk size must be greater than zero");for(let e=0;e<t.length;e+=s)yield t.slice(e,e+s)}var Ze=class{#t;constructor(t){let{scopes:e,divisor:r,delimiter:i}=t;this.#t=e.length>0?e.join(r)+i:""}prefix=t=>this.#t+t;unprefix=t=>{let e=this.#t.length;return t.slice(e)};scan=t=>{let{limit:e}=t,r=this.#t+(t.start??""),i=this.#t+(t.end??"\xFF");return{limit:e,start:r,end:i}}};var Qe=class s{driver;static collect=Ms;write;#t;#e;constructor(t=new Je,e={}){this.driver=t,this.#t={scopes:[],divisor:".",delimiter:":",chunkSize:1e4,...e},this.#e=new Ze(this.#t),this.write=new Xe(this.#e)}async gets(...t){return(await this.driver.gets(...t.map(this.#e.prefix))).map(r=>r===void 0?r:oe.parse(r))}async get(t){let[e]=await this.gets(t);return e}async requires(...t){let e=await this.gets(...t);for(let r of e)if(r===void 0)throw new Error("required key not found");return e}async require(t){let[e]=await this.requires(t);return e}async hasKeys(...t){return this.driver.hasKeys(...t.map(this.#e.prefix))}async has(t){let[e]=await this.hasKeys(t);return e}async*keys(t={}){for await(let e of this.driver.keys(this.#e.scan(t)))yield this.#e.unprefix(e)}async clear(t={}){let e=[];for await(let r of this.keys(t))e.push(r);for(let r of Ps(this.#t.chunkSize,e))await this.del(...r)}async*entries(t={}){for await(let[e,r]of this.driver.entries(this.#e.scan(t)))yield[this.#e.unprefix(e),oe.parse(r)]}async*values(t){for await(let[,e]of this.entries(t))yield e}async transaction(t){let e=t(this.write).flat();return this.driver.transaction(...e)}async set(t,e){return this.transaction(r=>[r.set(t,e)])}async sets(...t){return this.transaction(e=>[e.sets(...t)])}async del(...t){return this.transaction(e=>[e.del(...t)])}async guarantee(t,e){let r=await this.get(t);return r===void 0&&(r=await e(),await this.transaction(i=>[i.set(t,r)])),r}async versionMigration(t,e,r){let i=this,o=await i.get(t)??0;typeof o!="number"&&(o=0),o!==e&&(await r(o),await i.set(t,e))}store(t){return new Ye(this,t)}scope(t,e=this.#t.delimiter){return new s(this.driver,{...this.#t,delimiter:e,scopes:[...this.#t.scopes,t]})}flatten(){return new s(this.driver,{...this.#t,delimiter:""})}};var ne=class s{profiles=new N;portProfiles=[];metaBindings=null;constructor(t){if(t){for(let e of t.profiles)this.profiles.set(e.id,e);this.portProfiles=t.portProfiles.map(e=>e&&(this.profiles.has(e)?e:null)),this.metaBindings=t.metaBindings}}toJSON(){return C.clone({profiles:[...this.profiles.values()],portProfiles:this.portProfiles,metaBindings:this.metaBindings})}clone(){return new s(this.toJSON())}getProfile(t){return this.profiles.get(t)}getProfileForPort(t){let e=this.portProfiles.at(t);return e?this.getProfile(e):void 0}};var tr=class{store;$catalog;static async load(t){let e=g(new ne(await t.get()));return new this(t,e)}dispose=O();constructor(t,e){this.store=t,this.$catalog=e,this.dispose.schedule(zt.onStorageEvent(async()=>this.reload()))}async reload(){let t=new ne(await this.store.get());await this.$catalog.set(t)}async save(t){await this.store.set(t.toJSON()),await this.$catalog(t)}async#t(t){let e=this.$catalog().clone(),r=t(e);return await this.store.set(e.toJSON()),await this.$catalog(e),r}async createProfile(t,e){return this.#t(r=>{let i=wt.random(),o={id:i,label:t,bindings:e};return r.profiles.set(i,o),o})}async deleteProfile(t){return this.#t(e=>{e.profiles.delete(t)})}async assignPortProfile(t,e){return this.#t(r=>{r.portProfiles[t]=e})}};function j(s){return s>0}var er=class{on=B();onDown=B();#t=0;#e=0;get value(){return this.#t}set value(t){this.#e=this.#t,this.#t=t,this.on.publish(this),this.down&&this.onDown.publish(this)}get previous(){return this.#e}get changed(){return this.#t!==this.#e}get pressed(){return j(this.#t)}get down(){return!j(this.#e)&&j(this.#t)}get up(){return j(this.#e)&&!j(this.#t)}};var H=class extends N{constructor(t){if(super(),t)for(let[e,r]of t)this.set(e,r)}zero(){for(let t of this.keys())this.set(t,0);return this}mergeSample([t,e]){let r=this.get(t)??0;return e>r&&this.set(t,e),this}};function Cs([,,s],t){return{lastValue:0,holdStart:t,settings:Pi(s)}}function Mi(){return{scale:1,invert:!1,clamp:null,range:null,bottom:null,top:null,timing:["direct"]}}function Pi(s={}){return{...Mi(),...s}}var Bs=(s,t,e)=>jr(e).line(function(i){let{settings:o}=t;if(o.clamp){let[n,c]=o.clamp;i=y.clamp(i,n,c)}if(o.range){let[n,c]=o.range;i=y.isBetween(i,n,c)?y.remap(i,n,c,0,1,!0):0}return o.bottom&&(i=i<o.bottom?0:i),o.top&&(i=Math.min(o.top,i)),i},function(i){return t.settings.invert?1-i:i},function(i){return t.settings.scale*i},function(i){let{settings:o}=t,n=(o.timing[0]==="direct"?void 0:o.timing[1])??250,c=!j(t.lastValue)&&j(i),a=j(t.lastValue)&&!j(i),h=s-t.holdStart>=n;switch(c&&(t.holdStart=s),t.lastValue=i,o.timing[0]){case"direct":return i;case"tap":return a&&!h?1:0;case"hold":return j(i)&&h?i:0;default:throw new Error("unknown bindings timing")}});function Os(s){return s.length>0?Math.max(...s):0}function Rs(s){return s.length>0?Math.min(...s):0}var rr=class{bindings;actions;#t=new Set;#e=new H;#r=0;#s=new G;#i=ee();constructor(t){this.bindings=t,this.actions=te(t,(e,r)=>te(e,i=>{let o=new er;return this.#i.subscribe(()=>{o.value=this.#t.has(r)?this.#n([])(i,r):0}),o}))}resolve(t,e,r){return this.#r=t,this.#t=e,this.#e=r,this.#i(),this.actions}#o(t,e,r){let i=this.#s.guarantee(t.join("/"),()=>Cs(["code",e,r],this.#r)),o=this.#e.get(e)??0;return Bs(this.#r,i,o)}#n=t=>(e,r)=>{let i=[...t,String(r),typeof e=="string"?e:e[0]],o=this.#n(i);if(typeof e=="string")return this.#o(i,e);switch(e[0]){case"code":{let[,n,c]=e;return this.#o(i,n,c)}case"and":{let[,...n]=e,c=n.map(o);return Rs(c)}case"or":{let[,...n]=e,c=n.map(o);return Os(c)}case"not":{let[,n]=e;return o(n,"subject")>0?0:1}case"cond":{let[,n,c]=e;return o(c,"guard")>0?o(n,"subject"):0}case"mods":{let[,n,c]=e,a=(p,...l)=>p?["or",...l]:["not",["or",...l]];return o(["cond",n,["and",a(c.ctrl??!1,"ControlLeft","ControlRight"),a(c.alt??!1,"AltLeft","AltRight"),a(c.meta??!1,"MetaLeft","MetaRight"),a(c.shift??!1,"ShiftLeft","ShiftRight")]],"guard")>0?o(n,"subject"):0}}}};var Q=class{*[Symbol.iterator](){for(let t of this.samples())yield t}};var Tt=class extends Q{devices=new $e;#t=new H;constructor(...t){super(),this.devices.adds(...t)}*samples(){this.#t.zero();for(let t of this.devices)for(let e of t.samples())this.#t.mergeSample(e);yield*this.#t.entries()}};var tt=class extends rr{#t=new Tt;devices=this.#t.devices;modes=new $e;constructor(t){super(t),this.modes.adds(...Object.keys(t))}resolve(t=Date.now()){let e=new H(this.#t.samples());return super.resolve(t,this.modes,e)}};var dt="meta";var Nt=()=>({[dt]:{revealOverlay:["or","Backslash","gamepad.beta","gamepad.gamma"],shimmyNext:["or","BracketRight",["and",["or","gamepad.beta","gamepad.gamma"],"gamepad.right"],["and",["or","gamepad.beta","gamepad.gamma"],"gamepad.bumper.right"]],shimmyPrevious:["or","BracketLeft",["and",["or","gamepad.beta","gamepad.gamma"],"gamepad.left"],["and",["or","gamepad.beta","gamepad.gamma"],"gamepad.bumper.left"]]}});var sr=class{device;metaPort;constructor(t,e=Nt()){this.device=t,this.metaPort=new tt(e),this.metaPort.devices.add(t)}};var ir=class{ports;metaBindings;on=B();#t=new N;metaPort;constructor(t,e=Nt()){this.ports=t,this.metaBindings=e,this.metaPort=new tt(e)}poll(t=Date.now()){return this.#r(t),this.#s(t),this.#i(t)}portByIndex(t){let e=this.ports.at(t);if(!e)throw new Error(`port ${t} not found`);return e}portByDevice(t){for(let e of this.ports)if(e.devices.has(t))return e}shimmy(t,e){let r=this.ports.findIndex(c=>c.devices.has(t)),i=Math.max(0,this.ports.length-1),o=y.clamp(r+e,0,i),n=this.portByIndex(o);return this.reassign(t,n),n}reassign(t,e=this.getLonelyPort()){this.ports.forEach(r=>r.devices.delete(t)),e.devices.add(t),this.#e()}unplug(t){this.ports.forEach(e=>e.devices.delete(t)),this.#t.delete(t),this.#e()}plug=(t,e=this.getLonelyPort())=>(this.unplug(t),this.#t.set(t,new sr(t,this.metaBindings)),e.devices.add(t),this.#e(),()=>this.unplug(t));getLonelyPort(){let t=this.ports.at(0);for(let e of this.ports)e.devices.size<t.devices.size&&(t=e);return t}#e=T(0,()=>this.on.publish());#r(t){this.metaPort.devices.clear(),this.metaPort.devices.adds(...this.#t.keys()),this.metaPort.resolve(t)}#s(t){for(let e of this.#t.values()){let r=e.metaPort.resolve(t);r[dt].shimmyNext.down&&this.shimmy(e.device,1),r[dt].shimmyPrevious.down&&this.shimmy(e.device,-1)}}#i(t){return this.ports.map(e=>(e.resolve(t),e))}};function js(s,t){let e=C.clone(s);if(!t)return e;for(let[r,i]of Object.entries(e))for(let o of Object.keys(i)){let n=t[r]?.[o];n&&(i[o]=n)}return e}function ae(s){return s>=0?[s,0]:[0,Math.abs(s)]}function ce(s){let[t,e]=ae(s.x),[r,i]=ae(s.y);return{up:i,down:r,left:e,right:t}}var K=class extends Q{channel;$vector=g(w.zero());$breakdown=Tr(()=>ce(this.$vector.get()));constructor(t="stick"){super(),this.channel=t}samples(){let{channel:t}=this,{up:e,down:r,left:i,right:o}=this.$breakdown.get();return[[`${t}.up`,e],[`${t}.down`,r],[`${t}.left`,i],[`${t}.right`,o]]}};var U=class extends Q{on=B();sampleMap=new H;setSample(t,e){return this.sampleMap.set(t,e),this.on.pub(t,e),this}*samples(){for(let t of this.sampleMap)yield t}};var Lt=class extends U{stickLeft=new K;stickRight=new K;samples(){let t=this.stickLeft.$breakdown();this.setSample("gamepad.stick.left.up",t.up),this.setSample("gamepad.stick.left.down",t.down),this.setSample("gamepad.stick.left.left",t.left),this.setSample("gamepad.stick.left.right",t.right);let e=this.stickRight.$breakdown();return this.setSample("gamepad.stick.right.up",e.up),this.setSample("gamepad.stick.right.down",e.down),this.setSample("gamepad.stick.right.left",e.left),this.setSample("gamepad.stick.right.right",e.right),super.samples()}};var or=class s extends U{client=new w(0,0);movement=new w(0,0);dispose=O();constructor(t=window){super(),this.dispose.schedule(ht(t,{pointerdown:e=>{let r=s.buttonCode(e);this.setSample(r,1)},pointerup:e=>{let r=s.buttonCode(e);this.setSample(r,0)},pointermove:e=>{this.client.x=e.clientX,this.client.y=e.clientY,this.movement.x+=e.movementX,this.movement.y+=e.movementY},wheel:e=>{this.sampleMap.set("pointer.wheel.right",0),this.sampleMap.set("pointer.wheel.left",0),this.sampleMap.set("pointer.wheel.up",0),this.sampleMap.set("pointer.wheel.down",0);for(let[r,i]of s.wheelCodes(e))this.setSample(r,i)}}))}static buttonCode(t){switch(t.button){case 0:return"pointer.button.left";case 1:return"pointer.button.middle";case 2:return"pointer.button.right";default:return`pointer.button.${t.button+1}`}}static wheelCodes(t){let e=[];return t.deltaX&&e.push([t.deltaX>0?"pointer.wheel.right":"pointer.wheel.left",t.deltaX]),t.deltaY&&e.push([t.deltaY>0?"pointer.wheel.down":"pointer.wheel.up",t.deltaY]),e}samples(){return this.#t(),super.samples()}#t(){let[t,e]=this.movement,[r,i]=ae(t),[o,n]=ae(e);t&&(t>=0?this.setSample("pointer.move.right",Math.abs(i)):this.setSample("pointer.move.left",Math.abs(r))),e&&(e>=0?this.setSample("pointer.move.up",Math.abs(n)):this.setSample("pointer.move.down",Math.abs(o))),this.movement.set_(0,0)}};var nr=class extends U{dispose=O();constructor(t=window){super(),this.dispose.schedule(ht(t,this.#t)),this.dispose.schedule(ht(window,this.#e))}#t={keydown:t=>{if(t.repeat)return null;this.setSample(t.code,1)},keyup:t=>{if(t.repeat)return null;this.setSample(t.code,0)}};#e={blur:()=>{this.sampleMap.zero()}}};var et=class extends Tt{keyboard=new nr;pointer=new or;vpad=new Lt;constructor(){super(),this.devices.add(this.keyboard).add(this.pointer).add(this.vpad)}};function he(s,t){let e=w.from(s),r=e.distance_(0,0),i=y.remap(r,t.x,t.y,0,1,!0);return e.normalize().multiplyBy(i)}var Ci=["gamepad.a","gamepad.b","gamepad.x","gamepad.y","gamepad.bumper.left","gamepad.bumper.right","gamepad.trigger.left","gamepad.trigger.right","gamepad.alpha","gamepad.beta","gamepad.stick.left.click","gamepad.stick.right.click","gamepad.up","gamepad.down","gamepad.left","gamepad.right","gamepad.gamma"],qt=class extends U{pad;range=new w(.2,.9);constructor(t){super(),this.pad=t}get gamepad(){return this.pad.gamepad}samples(){return this.#t(this.pad.gamepad),this.#e(this.pad.gamepad),super.samples()}#t(t){let e=0,r=i=>{e=Math.max(e,i)};for(let[i,o]of Ci.entries()){let n=t.buttons.at(i)?.value??0;r(n),this.setSample(o,n)}this.setSample("gamepad.any",e)}#e(t){let[e=0,r=0,i=0,o=0]=t.axes,n=ce(he(new w(e,r),this.range)),c=ce(he(new w(i,o),this.range));this.setSample("gamepad.stick.left.up",n.up),this.setSample("gamepad.stick.left.down",n.down),this.setSample("gamepad.stick.left.left",n.left),this.setSample("gamepad.stick.left.right",n.right),this.setSample("gamepad.stick.right.up",c.up),this.setSample("gamepad.stick.right.down",c.down),this.setSample("gamepad.stick.right.left",c.left),this.setSample("gamepad.stick.right.right",c.right)}};var ar=class{fallback="\u2754";#t=new Map;constructor(t=[[et,"\u{1F4BB}"],[qt,"\u{1F3AE}"],[Lt,"\u{1F4F1}"]]){this.add(...t)}get(t){return this.#t.get(t)??this.fallback}add(...t){for(let[e,r]of t)this.#t.set(e,r);return this}clear(){return this.#t.clear(),this}};var Bi=s=>Se(s).map(t=>`hsl(${(t*(360/s)+180)%360}deg, 100%, 50%)`),cr=class extends ke{constructor(){super(()=>Bi(12))}};var Lr=class{icon;label;color;constructor(t,e,r){this.icon=t,this.label=e,this.color=r}},It=class{icons=new ar;#t=new os;#e=new cr;get(t){return this.#t.guarantee(t,()=>new Lr(this.icons.get(t.constructor),wt.random(4),this.#e.takeFirst()))}};var hr=class{hub;deviceSkins;auto=!0;dispose=O();#t=Date.now();#e=g(!1);$manual=g(!1);$visible=g.derived(()=>this.$manual()||this.auto&&this.#e());$showLabels=g(!1);constructor(t,e=new It){this.hub=t,this.deviceSkins=e,this.dispose.schedule(t.on(async()=>{Date.now()-this.#t>100&&await this.bump()}))}#r=T(1500,()=>this.#e(!1));async bump(){await this.#e(!0),await this.#r()}async toggle(t=!this.$manual()){return this.$manual.set(t)}};var Ds=E`
30
+ `;var y=class s{x;constructor(t){this.x=t}static new(t){return new this(t)}clone(){return new s(this.x)}set(t){return this.x=t,this}static isBetween(t,e=0,r=1){let i=Math.min(e,r),o=Math.max(e,r);return t>=i&&t<=o}isBetween(t=0,e=1){return s.isBetween(this.x,t,e)}static isNear(t,e,r=.01){return Math.abs(t-e)<=r}isNear(t,e=.01){return s.isNear(this.x,t,e)}static add(...t){let e=0;for(let r of t)e+=r;return e}add(...t){return this.x=s.add(this.x,...t),this}subtract(...t){for(let e of t)this.x-=e;return this}static atLeast(t,e=0){return Math.max(t,e)}atLeast(t=0){return this.x=s.atLeast(this.x,t),this}static atMost(t,e=1){return Math.min(t,e)}atMost(t=1){return this.x=s.atMost(this.x,t),this}static clamp(t,e=0,r=1){return t=s.atLeast(t,Math.min(e,r)),t=s.atMost(t,Math.max(e,r)),t}clamp(t=0,e=1){return this.x=s.clamp(this.x,t,e),this}static lerp(t,e,r,i){let n=(e-t)*r;return i!==void 0&&Math.abs(n)>i&&(n=Math.sign(n)*i),t+n}lerp(t,e,r){return this.x=s.lerp(this.x,t,e,r),this}static step(t,e,r){let i=e-t;return Math.abs(i)<=r?e:t+Math.sign(i)*r}step(t,e){return this.x=s.step(this.x,t,e),this}static creep(t,e,r,i){let o=t*(1-Math.exp(-e*r));if(i!==void 0){let n=i*r;Math.abs(o)>n&&(o=Math.sign(o)*n)}return o}static approach(t,e,r,i,o){let n=e-t,c=this.creep(n,r,i,o);return t+c}approach(t,e,r,i){return this.x=s.approach(this.x,t,e,r,i),this}static wrap(t,e=0,r=1){let i=Math.min(e,r),n=Math.max(e,r)-i,c=t-i,a=c<0?n- -c%n:c%n;return i+a}wrap(t=0,e=1){return this.x=s.wrap(this.x,t,e),this}static constrainProximity(t,e,r){let i=e-t,o=Math.abs(i),n=o>r?r:o,c=i<0?-n:n;return t+c}constrainProximity(t,e){return this.x=s.constrainProximity(this.x,t,e),this}static inverse(t){return 1-t}inverse(){return this.x=s.inverse(this.x),this}static center(t){return t*2-1}center(){return this.x=s.center(this.x),this}static uncenter(t){return(t+1)/2}uncenter(){return this.x=s.uncenter(this.x),this}static map(t,e,r){let o=(r-e)*t;return e+o}map(t,e){return this.x=s.map(this.x,t,e),this}static remap(t,e,r,i=0,o=1,n=!1){let a=(t-e)/(r-e)*(o-i)+i;return n?s.clamp(a,i,o):a}remap(t,e,r=0,i=1,o=!1){return this.x=s.remap(this.x,t,e,r,i,o),this}static magnify(t){return 4*Math.pow(t-.5,3)+.5}magnify(){return this.x=s.magnify(this.x),this}static floor(t){return Math.floor(t)}floor(){return this.x=s.floor(this.x),this}static ceil(t){return Math.ceil(t)}ceil(){return this.x=s.ceil(this.x),this}static round(t){return Math.round(t)}round(){return this.x=s.round(this.x),this}static smooth(t,e,r){return r<=1?e:t+(e-t)/r}smooth(t,e){return this.x=s.smooth(this.x,t,e),this}};var w=class s{x;y;constructor(t,e){this.x=t,this.y=e}static new(t,e){return new this(t,e)}static zero(){return new this(0,0)}static all(t){return new this(t,t)}static from(t){return Array.isArray(t)?new this(...t):new this(t.x,t.y)}static magnitudeSquared(t,e){return t*t+e*e}static magnitude(t,e){return Math.sqrt(this.magnitudeSquared(t,e))}static average(...t){return this.zero().add(...t).divideBy(t.length)}static min(...t){return new this(Math.min(...t.map(e=>e.x)),Math.min(...t.map(e=>e.y)))}static max(...t){return new this(Math.max(...t.map(e=>e.x)),Math.max(...t.map(e=>e.y)))}static fromAngle(t){return new this(Math.cos(t),Math.sin(t))}clone(){return new s(this.x,this.y)}*[Symbol.iterator](){yield this.x,yield this.y}toJSON(){return[this.x,this.y]}toString(){return`(Vec2 x${this.x.toFixed(2)}, y${this.y.toFixed(2)})`}set_(t,e){return this.x=t,this.y=e,this}set({x:t,y:e}){return this.x=t,this.y=e,this}magnitudeSquared(){return s.magnitudeSquared(this.x,this.y)}magnitude(){return s.magnitude(this.x,this.y)}rotation(){return Math.atan2(this.y,this.x)}equals_(t,e){return this.x===t&&this.y===e}equals(...t){return t.every(({x:e,y:r})=>this.equals_(e,r))}near_(t,e,r=1e-6){return Math.abs(this.x-t)<=r&&Math.abs(this.y-e)<=r}near({x:t,y:e},r=1e-6){return this.near_(t,e,r)}dot_(t,e){return this.x*t+this.y*e}dot({x:t,y:e}){return this.dot_(t,e)}distanceSquared_(t,e){return t=this.x-t,e=this.y-e,t*t+e*e}distanceSquared({x:t,y:e}){return this.distanceSquared_(t,e)}distance_(t,e){return Math.sqrt(this.distanceSquared_(t,e))}distance({x:t,y:e}){return this.distance_(t,e)}angleBetween_(t,e){let r=this.dot_(t,e),i=this.magnitude()*s.magnitude(t,e);if(i===0)return 0;let o=y.clamp(r/i,-1,1);return Math.acos(o)}angleBetween({x:t,y:e}){return this.angleBetween_(t,e)}normalize(){return this.divideBy(this.magnitude())}half(){return this.divideBy(2)}double(){return this.multiplyBy(2)}abs(){return this.map(t=>Math.abs(t))}rotate(t){let{x:e,y:r}=this;return this.x=e*Math.cos(t)-r*Math.sin(t),this.y=e*Math.sin(t)+r*Math.cos(t),this}perpendicular(){let{x:t,y:e}=this;return this.x=-e,this.y=t,this}clampMagnitude(t){return this.magnitude()>t&&this.normalize().multiplyBy(t),this}floor(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this}ceil(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this}round(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this}map(t){return this.x=t(this.x,0),this.y=t(this.y,1),this}morph(t){let e=t(this);return e&&e!==this&&this.set(e),this}clamp(t={x:0,y:0},e={x:1,y:1}){return this.x=y.clamp(this.x,t.x,e.x),this.y=y.clamp(this.y,t.y,e.y),this}clampBy(t=0,e=1){return this.x=y.clamp(this.x,t,e),this.y=y.clamp(this.y,t,e),this}negate(){return this.map(t=>t*-1)}addBy(t){return this.x+=t,this.y+=t,this}subtractBy(t){return this.x-=t,this.y-=t,this}multiplyBy(t){return this.x*=t,this.y*=t,this}divideBy(t){return t===0?this:(this.x/=t,this.y/=t,this)}add_(t,e){return this.x+=t,this.y+=e,this}add(...t){for(let{x:e,y:r}of t)this.add_(e,r);return this}subtract_(t,e){return this.x-=t,this.y-=e,this}subtract(...t){for(let{x:e,y:r}of t)this.subtract_(e,r);return this}multiply_(t,e){return this.x*=t,this.y*=e,this}multiply(...t){for(let{x:e,y:r}of t)this.multiply_(e,r);return this}divide_(t,e){return t!==0&&(this.x/=t),e!==0&&(this.y/=e),this}divide(...t){for(let{x:e,y:r}of t)this.divide_(e,r);return this}lerp_(t,e,r){return this.x+=(t-this.x)*r,this.y+=(e-this.y)*r,this}lerp({x:t,y:e},r){return this.lerp_(t,e,r)}approach_(t,e,r,i,o){return this.x=y.approach(this.x,t,r,i,o),this.y=y.approach(this.y,e,r,i,o),this}approach({x:t,y:e},r,i,o){return this.approach_(t,e,r,i,o)}reflect_(t,e){let r=2*this.dot_(t,e);return this.x-=r*t,this.y-=r*e,this}reflect({x:t,y:e}){return this.reflect_(t,e)}rotateAroundPoint_(t,e,r){let i=this.x-t,o=this.y-e,n=Math.cos(r),c=Math.sin(r);return this.x=n*i-c*o+t,this.y=c*i+n*o+e,this}rotateAroundPoint({x:t,y:e},r){return this.rotateAroundPoint_(t,e,r)}smooth_(t,e,r){return this.x=y.smooth(this.x,t,r),this.y=y.smooth(this.y,e,r),this}smooth({x:t,y:e},r){return this.smooth_(t,e,r)}};var Fe=class{state;players;constructor(t,e){this.state=t,this.players=e}tick(){for(let t of this.players){let{agent:e,port:{actions:r}}=t,i=1;e.position.y+=i*r.gameplay.up.value,e.position.y-=i*r.gameplay.down.value,e.position.x-=i*r.gameplay.left.value,e.position.x+=i*r.gameplay.right.value,e.position.clamp(w.zero(),this.state.arenaSize)}}};var Ge=class{label="0";alive=!0;color="#444";position=w.zero()};var Ke=class{arenaSize=new w(200,100);agents=new Set;makeAgent(t){let e=new Ge;return e.label=t,e.position.set_(.5,.5).multiply(this.arenaSize),this.agents.add(e),e}deleteAgent(t){return this.agents.delete(t),this}};var jt=class{};function Dt(s,{start:t,end:e}){return!(t&&s<t||e&&s>e)}var Je=class extends jt{#t=new Map;async gets(...t){return t.map(e=>this.#t.get(e))}async hasKeys(...t){return t.map(e=>this.#t.has(e))}async*keys(t={}){if(t.limit===0)return;let e=0;for(let r of this.#t.keys())if(Dt(r,t)&&(yield r,e+=1),e>=(t.limit??1/0))break}async*entries(t={}){if(t.limit===0)return;let e=0;for(let[r,i]of this.#t.entries())if(Dt(r,t)&&(yield[r,i],e+=1),e>=(t.limit??1/0))break}async transaction(...t){for(let[e,r]of t)r===void 0?this.#t.delete(e):this.#t.set(e,r)}};function Es(s,t,e){let r=Object.entries(t);for(let[i,o]of r)s.addEventListener(i,o,e);return function(){for(let[o,n]of r)s.removeEventListener(o,n)}}var zt=class extends jt{storage;static onStorageEvent=t=>Es(window,{storage:t});constructor(t=window.localStorage){super(),this.storage=t}async gets(...t){return t.map(e=>this.storage.getItem(e)??void 0)}async hasKeys(...t){return t.map(e=>this.storage.getItem(e)!==null)}async*keys(t={}){if(t.limit===0)return;let e=0;for(let r of Object.keys(this.storage))if(Dt(r,t)&&(yield r,e+=1),e>=(t.limit??1/0))break}async*entries(t={}){if(t.limit===0)return;let e=0;for(let[r,i]of Object.entries(this.storage))if(Dt(r,t)&&(yield[r,i],e+=1),e>=(t.limit??1/0))break}async transaction(...t){for(let[e,r]of t)r===void 0?this.storage.removeItem(e):this.storage.setItem(e,r)}};async function Ms(s){let t=[];for await(let e of s)t.push(e);return t}var Ye=class{kv;key;write;constructor(t,e){this.kv=t,this.key=e,this.write=new Nr(t.write,e)}async set(t){return this.kv.set(this.key,t)}async get(){return this.kv.get(this.key)}async require(){return this.kv.require(this.key)}async guarantee(t){let e=await this.get();return e===void 0&&(e=await t(),await this.set(e)),e}},Nr=class{key;#t;constructor(t,e){this.key=e,this.#t=t}set(t){return this.#t.set(this.key,t)}};var oe={parse:s=>JSON.parse(s),stringify:s=>JSON.stringify(s)};var Xe=class{#t;constructor(t){this.#t=t}sets(...t){return t.map(([e,r])=>{let i=this.#t.prefix(e),o=r===void 0?void 0:oe.stringify(r);return[i,o]})}set(t,e){return this.sets([t,e])}del(...t){return t.map(e=>[this.#t.prefix(e),void 0])}};function*Ps(s,t){if(s<=0)throw new Error("chunk size must be greater than zero");for(let e=0;e<t.length;e+=s)yield t.slice(e,e+s)}var Ze=class{#t;constructor(t){let{scopes:e,divisor:r,delimiter:i}=t;this.#t=e.length>0?e.join(r)+i:""}prefix=t=>this.#t+t;unprefix=t=>{let e=this.#t.length;return t.slice(e)};scan=t=>{let{limit:e}=t,r=this.#t+(t.start??""),i=this.#t+(t.end??"\xFF");return{limit:e,start:r,end:i}}};var Qe=class s{driver;static collect=Ms;write;#t;#e;constructor(t=new Je,e={}){this.driver=t,this.#t={scopes:[],divisor:".",delimiter:":",chunkSize:1e4,...e},this.#e=new Ze(this.#t),this.write=new Xe(this.#e)}async gets(...t){return(await this.driver.gets(...t.map(this.#e.prefix))).map(r=>r===void 0?r:oe.parse(r))}async get(t){let[e]=await this.gets(t);return e}async requires(...t){let e=await this.gets(...t);for(let r of e)if(r===void 0)throw new Error("required key not found");return e}async require(t){let[e]=await this.requires(t);return e}async hasKeys(...t){return this.driver.hasKeys(...t.map(this.#e.prefix))}async has(t){let[e]=await this.hasKeys(t);return e}async*keys(t={}){for await(let e of this.driver.keys(this.#e.scan(t)))yield this.#e.unprefix(e)}async clear(t={}){let e=[];for await(let r of this.keys(t))e.push(r);for(let r of Ps(this.#t.chunkSize,e))await this.del(...r)}async*entries(t={}){for await(let[e,r]of this.driver.entries(this.#e.scan(t)))yield[this.#e.unprefix(e),oe.parse(r)]}async*values(t){for await(let[,e]of this.entries(t))yield e}async transaction(t){let e=t(this.write).flat();return this.driver.transaction(...e)}async set(t,e){return this.transaction(r=>[r.set(t,e)])}async sets(...t){return this.transaction(e=>[e.sets(...t)])}async del(...t){return this.transaction(e=>[e.del(...t)])}async guarantee(t,e){let r=await this.get(t);return r===void 0&&(r=await e(),await this.transaction(i=>[i.set(t,r)])),r}async versionMigration(t,e,r){let i=this,o=await i.get(t)??0;typeof o!="number"&&(o=0),o!==e&&(await r(o),await i.set(t,e))}store(t){return new Ye(this,t)}scope(t,e=this.#t.delimiter){return new s(this.driver,{...this.#t,delimiter:e,scopes:[...this.#t.scopes,t]})}flatten(){return new s(this.driver,{...this.#t,delimiter:""})}};var ne=class s{profiles=new N;portProfiles=[];metaBindings=null;constructor(t){if(t){for(let e of t.profiles)this.profiles.set(e.id,e);this.portProfiles=t.portProfiles.map(e=>e&&(this.profiles.has(e)?e:null)),this.metaBindings=t.metaBindings}}toJSON(){return C.clone({profiles:[...this.profiles.values()],portProfiles:this.portProfiles,metaBindings:this.metaBindings})}clone(){return new s(this.toJSON())}getProfile(t){return this.profiles.get(t)}getProfileForPort(t){let e=this.portProfiles.at(t);return e?this.getProfile(e):void 0}};var tr=class{store;$catalog;static async load(t){let e=g(new ne(await t.get()));return new this(t,e)}dispose=O();constructor(t,e){this.store=t,this.$catalog=e,this.dispose.schedule(zt.onStorageEvent(async()=>this.reload()))}async reload(){let t=new ne(await this.store.get());await this.$catalog.set(t)}async save(t){await this.store.set(t.toJSON()),await this.$catalog(t)}async#t(t){let e=this.$catalog().clone(),r=t(e);return await this.store.set(e.toJSON()),await this.$catalog(e),r}async createProfile(t,e){return this.#t(r=>{let i=wt.random(),o={id:i,label:t,bindings:e};return r.profiles.set(i,o),o})}async deleteProfile(t){return this.#t(e=>{e.profiles.delete(t)})}async assignPortProfile(t,e){return this.#t(r=>{r.portProfiles[t]=e})}};function j(s){return s>0}var er=class{on=B();onDown=B();#t=0;#e=0;get value(){return this.#t}set value(t){this.#e=this.#t,this.#t=t,this.on.publish(this),this.down&&this.onDown.publish(this)}get previous(){return this.#e}get changed(){return this.#t!==this.#e}get pressed(){return j(this.#t)}get down(){return!j(this.#e)&&j(this.#t)}get up(){return j(this.#e)&&!j(this.#t)}};var H=class extends N{constructor(t){if(super(),t)for(let[e,r]of t)this.set(e,r)}zero(){for(let t of this.keys())this.set(t,0);return this}mergeSample([t,e]){let r=this.get(t)??0;return e>r&&this.set(t,e),this}};function Cs([,,s],t){return{lastValue:0,holdStart:t,settings:Pi(s)}}function Mi(){return{scale:1,invert:!1,clamp:null,range:null,bottom:null,top:null,timing:["direct"]}}function Pi(s={}){return{...Mi(),...s}}var Bs=(s,t,e)=>jr(e).line(function(i){let{settings:o}=t;if(o.clamp){let[n,c]=o.clamp;i=y.clamp(i,n,c)}if(o.range){let[n,c]=o.range;i=y.isBetween(i,n,c)?y.remap(i,n,c,0,1,!0):0}return o.bottom&&(i=i<o.bottom?0:i),o.top&&(i=Math.min(o.top,i)),i},function(i){return t.settings.invert?1-i:i},function(i){return t.settings.scale*i},function(i){let{settings:o}=t,n=(o.timing[0]==="direct"?void 0:o.timing[1])??250,c=!j(t.lastValue)&&j(i),a=j(t.lastValue)&&!j(i);c&&(t.holdStart=s);let h=s-t.holdStart>=n;switch(t.lastValue=i,o.timing[0]){case"direct":return i;case"tap":return a&&!h?1:0;case"hold":return j(i)&&h?i:0;default:throw new Error("unknown bindings timing")}});function Os(s){return s.length>0?Math.max(...s):0}function Rs(s){return s.length>0?Math.min(...s):0}var rr=class{bindings;actions;#t=new Set;#e=new H;#r=0;#s=new G;#i=ee();constructor(t){this.bindings=t,this.actions=te(t,(e,r)=>te(e,i=>{let o=new er;return this.#i.subscribe(()=>{o.value=this.#t.has(r)?this.#n([])(i,r):0}),o}))}resolve(t,e,r){return this.#r=t,this.#t=e,this.#e=r,this.#i(),this.actions}#o(t,e,r){let i=this.#s.guarantee(t.join("/"),()=>Cs(["code",e,r],this.#r)),o=this.#e.get(e)??0;return Bs(this.#r,i,o)}#n=t=>(e,r)=>{let i=[...t,String(r),typeof e=="string"?e:e[0]],o=this.#n(i);if(typeof e=="string")return this.#o(i,e);switch(e[0]){case"code":{let[,n,c]=e;return this.#o(i,n,c)}case"and":{let[,...n]=e,c=n.map(o);return Rs(c)}case"or":{let[,...n]=e,c=n.map(o);return Os(c)}case"not":{let[,n]=e;return o(n,"subject")>0?0:1}case"cond":{let[,n,c]=e;return o(c,"guard")>0?o(n,"subject"):0}case"mods":{let[,n,c]=e,a=(p,...l)=>p?["or",...l]:["not",["or",...l]];return o(["cond",n,["and",a(c.ctrl??!1,"ControlLeft","ControlRight"),a(c.alt??!1,"AltLeft","AltRight"),a(c.meta??!1,"MetaLeft","MetaRight"),a(c.shift??!1,"ShiftLeft","ShiftRight")]],"guard")>0?o(n,"subject"):0}}}};var Q=class{*[Symbol.iterator](){for(let t of this.samples())yield t}};var Tt=class extends Q{devices=new $e;#t=new H;constructor(...t){super(),this.devices.adds(...t)}*samples(){this.#t.zero();for(let t of this.devices)for(let e of t.samples())this.#t.mergeSample(e);yield*this.#t.entries()}};var tt=class extends rr{#t=new Tt;devices=this.#t.devices;modes=new $e;constructor(t){super(t),this.modes.adds(...Object.keys(t))}resolve(t=Date.now()){let e=new H(this.#t.samples());return super.resolve(t,this.modes,e)}};var dt="meta";var Nt=()=>({[dt]:{revealOverlay:["or","Backslash","gamepad.beta","gamepad.gamma"],shimmyNext:["or","BracketRight",["and",["or","gamepad.beta","gamepad.gamma"],"gamepad.right"],["and",["or","gamepad.beta","gamepad.gamma"],"gamepad.bumper.right"]],shimmyPrevious:["or","BracketLeft",["and",["or","gamepad.beta","gamepad.gamma"],"gamepad.left"],["and",["or","gamepad.beta","gamepad.gamma"],"gamepad.bumper.left"]]}});var sr=class{device;metaPort;constructor(t,e=Nt()){this.device=t,this.metaPort=new tt(e),this.metaPort.devices.add(t)}};var ir=class{ports;metaBindings;on=B();#t=new N;metaPort;constructor(t,e=Nt()){this.ports=t,this.metaBindings=e,this.metaPort=new tt(e)}poll(t=Date.now()){return this.#r(t),this.#s(t),this.#i(t)}portByIndex(t){let e=this.ports.at(t);if(!e)throw new Error(`port ${t} not found`);return e}portByDevice(t){for(let e of this.ports)if(e.devices.has(t))return e}shimmy(t,e){let r=this.ports.findIndex(c=>c.devices.has(t)),i=Math.max(0,this.ports.length-1),o=y.clamp(r+e,0,i),n=this.portByIndex(o);return this.reassign(t,n),n}reassign(t,e=this.getLonelyPort()){this.ports.forEach(r=>r.devices.delete(t)),e.devices.add(t),this.#e()}unplug(t){this.ports.forEach(e=>e.devices.delete(t)),this.#t.delete(t),this.#e()}plug=(t,e=this.getLonelyPort())=>(this.unplug(t),this.#t.set(t,new sr(t,this.metaBindings)),e.devices.add(t),this.#e(),()=>this.unplug(t));getLonelyPort(){let t=this.ports.at(0);for(let e of this.ports)e.devices.size<t.devices.size&&(t=e);return t}#e=T(0,()=>this.on.publish());#r(t){this.metaPort.devices.clear(),this.metaPort.devices.adds(...this.#t.keys()),this.metaPort.resolve(t)}#s(t){for(let e of this.#t.values()){let r=e.metaPort.resolve(t);r[dt].shimmyNext.down&&this.shimmy(e.device,1),r[dt].shimmyPrevious.down&&this.shimmy(e.device,-1)}}#i(t){return this.ports.map(e=>(e.resolve(t),e))}};function js(s,t){let e=C.clone(s);if(!t)return e;for(let[r,i]of Object.entries(e))for(let o of Object.keys(i)){let n=t[r]?.[o];n&&(i[o]=n)}return e}function ae(s){return s>=0?[s,0]:[0,Math.abs(s)]}function ce(s){let[t,e]=ae(s.x),[r,i]=ae(s.y);return{up:i,down:r,left:e,right:t}}var K=class extends Q{channel;$vector=g(w.zero());$breakdown=Tr(()=>ce(this.$vector.get()));constructor(t="stick"){super(),this.channel=t}samples(){let{channel:t}=this,{up:e,down:r,left:i,right:o}=this.$breakdown.get();return[[`${t}.up`,e],[`${t}.down`,r],[`${t}.left`,i],[`${t}.right`,o]]}};var U=class extends Q{on=B();sampleMap=new H;setSample(t,e){return this.sampleMap.set(t,e),this.on.pub(t,e),this}*samples(){for(let t of this.sampleMap)yield t}};var Lt=class extends U{stickLeft=new K;stickRight=new K;samples(){let t=this.stickLeft.$breakdown();this.setSample("gamepad.stick.left.up",t.up),this.setSample("gamepad.stick.left.down",t.down),this.setSample("gamepad.stick.left.left",t.left),this.setSample("gamepad.stick.left.right",t.right);let e=this.stickRight.$breakdown();return this.setSample("gamepad.stick.right.up",e.up),this.setSample("gamepad.stick.right.down",e.down),this.setSample("gamepad.stick.right.left",e.left),this.setSample("gamepad.stick.right.right",e.right),super.samples()}};var or=class s extends U{client=new w(0,0);movement=new w(0,0);dispose=O();constructor(t=window){super(),this.dispose.schedule(ht(t,{pointerdown:e=>{let r=s.buttonCode(e);this.setSample(r,1)},pointerup:e=>{let r=s.buttonCode(e);this.setSample(r,0)},pointermove:e=>{this.client.x=e.clientX,this.client.y=e.clientY,this.movement.x+=e.movementX,this.movement.y+=e.movementY},wheel:e=>{this.sampleMap.set("pointer.wheel.right",0),this.sampleMap.set("pointer.wheel.left",0),this.sampleMap.set("pointer.wheel.up",0),this.sampleMap.set("pointer.wheel.down",0);for(let[r,i]of s.wheelCodes(e))this.setSample(r,i)}}))}static buttonCode(t){switch(t.button){case 0:return"pointer.button.left";case 1:return"pointer.button.middle";case 2:return"pointer.button.right";default:return`pointer.button.${t.button+1}`}}static wheelCodes(t){let e=[];return t.deltaX&&e.push([t.deltaX>0?"pointer.wheel.right":"pointer.wheel.left",t.deltaX]),t.deltaY&&e.push([t.deltaY>0?"pointer.wheel.down":"pointer.wheel.up",t.deltaY]),e}samples(){return this.#t(),super.samples()}#t(){let[t,e]=this.movement,[r,i]=ae(t),[o,n]=ae(e);t&&(t>=0?this.setSample("pointer.move.right",Math.abs(i)):this.setSample("pointer.move.left",Math.abs(r))),e&&(e>=0?this.setSample("pointer.move.up",Math.abs(n)):this.setSample("pointer.move.down",Math.abs(o))),this.movement.set_(0,0)}};var nr=class extends U{dispose=O();constructor(t=window){super(),this.dispose.schedule(ht(t,this.#t)),this.dispose.schedule(ht(window,this.#e))}#t={keydown:t=>{if(t.repeat)return null;this.setSample(t.code,1)},keyup:t=>{if(t.repeat)return null;this.setSample(t.code,0)}};#e={blur:()=>{this.sampleMap.zero()}}};var et=class extends Tt{keyboard=new nr;pointer=new or;vpad=new Lt;constructor(){super(),this.devices.add(this.keyboard).add(this.pointer).add(this.vpad)}};function he(s,t){let e=w.from(s),r=e.distance_(0,0),i=y.remap(r,t.x,t.y,0,1,!0);return e.normalize().multiplyBy(i)}var Ci=["gamepad.a","gamepad.b","gamepad.x","gamepad.y","gamepad.bumper.left","gamepad.bumper.right","gamepad.trigger.left","gamepad.trigger.right","gamepad.alpha","gamepad.beta","gamepad.stick.left.click","gamepad.stick.right.click","gamepad.up","gamepad.down","gamepad.left","gamepad.right","gamepad.gamma"],qt=class extends U{pad;range=new w(.2,.9);constructor(t){super(),this.pad=t}get gamepad(){return this.pad.gamepad}samples(){return this.#t(this.pad.gamepad),this.#e(this.pad.gamepad),super.samples()}#t(t){let e=0,r=i=>{e=Math.max(e,i)};for(let[i,o]of Ci.entries()){let n=t.buttons.at(i)?.value??0;r(n),this.setSample(o,n)}this.setSample("gamepad.any",e)}#e(t){let[e=0,r=0,i=0,o=0]=t.axes,n=ce(he(new w(e,r),this.range)),c=ce(he(new w(i,o),this.range));this.setSample("gamepad.stick.left.up",n.up),this.setSample("gamepad.stick.left.down",n.down),this.setSample("gamepad.stick.left.left",n.left),this.setSample("gamepad.stick.left.right",n.right),this.setSample("gamepad.stick.right.up",c.up),this.setSample("gamepad.stick.right.down",c.down),this.setSample("gamepad.stick.right.left",c.left),this.setSample("gamepad.stick.right.right",c.right)}};var ar=class{fallback="\u2754";#t=new Map;constructor(t=[[et,"\u{1F4BB}"],[qt,"\u{1F3AE}"],[Lt,"\u{1F4F1}"]]){this.add(...t)}get(t){return this.#t.get(t)??this.fallback}add(...t){for(let[e,r]of t)this.#t.set(e,r);return this}clear(){return this.#t.clear(),this}};var Bi=s=>Se(s).map(t=>`hsl(${(t*(360/s)+180)%360}deg, 100%, 50%)`),cr=class extends ke{constructor(){super(()=>Bi(12))}};var Lr=class{icon;label;color;constructor(t,e,r){this.icon=t,this.label=e,this.color=r}},It=class{icons=new ar;#t=new os;#e=new cr;get(t){return this.#t.guarantee(t,()=>new Lr(this.icons.get(t.constructor),wt.random(4),this.#e.takeFirst()))}};var hr=class{hub;deviceSkins;auto=!0;dispose=O();#t=Date.now();#e=g(!1);$manual=g(!1);$visible=g.derived(()=>this.$manual()||this.auto&&this.#e());$showLabels=g(!1);constructor(t,e=new It){this.hub=t,this.deviceSkins=e,this.dispose.schedule(t.on(async()=>{Date.now()-this.#t>100&&await this.bump()}))}#r=T(1500,()=>this.#e(!1));async bump(){await this.#e(!0),await this.#r()}async toggle(t=!this.$manual()){return this.$manual.set(t)}};var Ds=E`
31
31
 
32
32
  :host {
33
33
  --gap: 0.3em;