text: class q{}class T{constructor(t,e){this.items={},this.items=t.reduce((s,i)=>({...s,[i]:!0}),{}),this.onChange=e}*[Symbol.iterator](){yield*this.all()}all(){return Object.keys(this.items)}has(...t){for(const e of t)if(!(e in this.items))return!1;return!0}add(...t){let e=!1;for(const s of t)s in this.items||(this.items[s]=!0,e=!0);return e&&this.onChange?.(),this}clear(){this.items={},this.onChange?.()}remove(...t){let e=!1;for(const s of t)s in this.items&&(delete this.items[s],e=!0);return e&&this.onChange?.(),!0}}const A="_a",N="$id$",j={PRE_LOAD:100,ON_LOAD:200,POST_LOAD:300,PRE_UPDATE:400,ON_UPDATE:500,POST_UPDATE:600,PRE_RENDER:700,ON_RENDER:800,POST_RENDER:900};var l;(function(n){n[n.SOME=0]="SOME",n[n.ALL=1]="ALL",n[n.ANY=2]="ANY",n[n.NONE=3]="NONE"})(l||(l={}));var D;(function(n){n[n.NONE=0]="NONE",n[n.PENDING=1]="PENDING",n[n.RESOLVED=2]="RESOLVED",n[n.FAILED=3]="FAILED"})(D||(D={}));class _{constructor(t){this.stack=[],this.refs={},this.entities={},this.ctx=t}deserialize(t){this.deserializeEntities(t)}set(t,e,s){let i=t;for(;e.length;){const r=e.shift();r&&(e.length?i=i[r]:i[r]=s)}}recreateEntityReferences(){for(const[t,e]of Object.entries(this.refs)){const s=this.entities[t];for(const[i,r]of e)this.set(s.$,i,this.entities[r])}}deserializeInPlace(t,e,s){const{refs:i,stack:r}=this;let o=s;switch(r.push(s),typeof s){case"object":{if(Array.isArray(s))o=s.map((h,c)=>{const a=t.concat([c+""]);return this.deserializeInPlace(a,e,h)});else if(s)for(const h of Object.getOwnPropertyNames(s)){const[c,a]=[s[h],[...t,h]];r.includes(c)||this.set(o,[h],c&&this.deserializeInPlace(a,e,c))}break}case"string":{if(s.startsWith(N)){const h=s.replace(`${N}|`,""),c=i[e]??[];c.push([t,h]),i[e]=c,o=null}/^[0-9]+n$/.test(s)&&(o=BigInt(s.slice(0,-1)));break}}return r.pop(),o}deserializeEntities(t){const{components:e,entities:s}=this.ctx.manager.registrations,i={},r=[];for(const{id:o,tags:h,type:c,$:a}of t.entities){if(!(c in s)){const g=c.split("|").filter(m=>!e[m]);if(g.length){console.warn(`Missing entities/components: ${g.join(", ")}`);continue}s[c]=i[c]=S.with(...c.split("|").map(m=>e[m]))}for(const p of Object.getOwnPropertyNames(a))a[p]=this.deserializeInPlace([p],o,a[p]);r.push({id:o,entity:s[c],data:a,tags:h})}this.ctx.register(Object.values(i));for(const{id:o,entity:h,data:c,tags:a}of r){const p=this.entities[o]=this.ctx.create(h,c,a);p.id=o}this.stack=[],this.recreateEntityReferences(),this.stack=[]}}class v{constructor(){this.map=new Map}all(){const t=[];for(const e of this.map.values())t.push(...e);return Array.from(new Set(t))}keys(){return Array.from(this.map.keys())}get(t){const e=[];for(const s of t)e.push(...this.map.get(s)??[]);return e}append(t,e){const s=this.map.get(t)??new Set;this.map.set(t,s.add(e))}remove(t,e){const s=this.map.get(t);s&&(s.delete(e),s.size||this.map.delete(t))}}function E(...n){let t=0n;for(const e of n)t|=e??0n;return t}const O={any(n,t){return!n||(n&t)>0n},all(n,t){return(n&t)===n},none(n,t){return!t||!(t&n)}},L=new Map;function C(n,...t){const e=L.get(n)??[],s=class extends n{},i=[...e,...t];return L.set(s,i),Object.defineProperty(s.prototype,"items",{value:i.slice(),writable:!0}),s}function V(...n){const t=class extends ${};return Object.defineProperty(t.prototype,"items",{value:n.slice(),writable:!0}),t}let Y="ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW",B=(n=21)=>{let t="",e=n;for(;e--;)t+=Y[Math.random()*64|0];return t};const F={fn:()=>B()};function P(){return F.fn()}function*G(){let n=1n;do yield 1n<<n++;while(!0)}const U=()=>{const n=G();return()=>n.next().value};function H(n){return!!(n.prototype?.tick??n.prototype?.stop??n.prototype?.start)}const J={[l.NONE]:O.none,[l.ALL]:O.all,[l.ANY]:O.any};class K{constructor(t,e){this.id=P(),this.key=null,this.results=new Set,this.constraints=new Set,this.keys=new Map,this.executed=!1,this.targets={[l.ANY]:null,[l.ALL]:null,[l.NONE]:null},this.reducer=(s,i)=>{const r=i.constraint,o=this.manager.getID(...i.ids);return s[r]=s[r]?E(s[r],o):o,s},this.manager=t,this.steps=e.filter(s=>s.constraint!==l.SOME),this.init()}refresh(){this.executed=!0;const t=this.manager.index.keys().filter(e=>this.filter(e));this.results=new Set(this.manager.index.get(t))}update(t,e){for(const s of e)this.results.delete(s);for(const s of t){const i=this.keys.get(s.key)??null;(i||i===null&&this.filter(s.key))&&this.results.add(s)}}*[Symbol.iterator](){this.executed||this.refresh(),yield*this.results}get(){return Array.from(this)}first(){for(const t of this)return t;return null}init(){const t={[l.ALL]:[],[l.ANY]:[],[l.NONE]:[]};for(const e of this.steps)e.constraint!==l.SOME&&(this.constraints.add(e.constraint),t[e.constraint].push(e));for(const e of this.constraints)this.targets=t[e].reduce(this.reducer,this.targets);this.key=E(...Object.values(this.targets))}filter(t){let e=this.keys.get(t)??null;if(e===null){e=!0;for(const s of this.constraints){const i=this.targets[s];if(!i||!J[s](i,t)){e=!1;break}}this.keys.set(t,e)}return e}}class I{constructor(t){this.handle={components:{all:this.component(l.ALL),any:this.component(l.ANY),some:this.component(l.SOME),none:this.component(l.NONE)},tags:{all:this.tag(l.ALL),any:this.tag(l.ANY),some:this.tag(l.SOME),none:this.tag(l.NONE)}},this.components=Object.assign(this.handle.components.all,this.handle.components),this.tags=Object.assign(this.handle.tags.all,this.handle.tags),this.key="",this.resolved=null,this.criteria=[],this.manager=t,this.reset()}get query(){return this.resolved??=this.manager.getQuery(this.criteria,this.key)}get(){return this.query.get()}first(){return this.query.first()}[Symbol.iterator](){return this.query[Symbol.iterator]()}reset(){return this.state&&(this.key+=this.state.tag+"|"+this.state.ids.join(",")+"::",this.criteria.push({constraint:this.state.tag??l.ALL,ids:this.state.ids.slice()})),this.state={tag:null,ids:[]},this}tag(t){return(...e)=>{const s=this.manager.registrations.tags;return this.state.ids.push(...e.map(i=>s[i])),this.state.tag=t,this.reset()}}component(t){return(...e)=>(this.state.ids.push(...e.map(s=>s.type)),this.state.tag=t,this.reset())}}class W{constructor(){this.id=U(),this.registry={}}add(...t){const e=[];for(const s of t)this.registry[s]||(this.registry[s]=this.id()),e.push(this.registry[s]);return E(...e)}getID(...t){const e=E(...t.map(s=>this.registry[s]??0n));return e===0n?null:e}}class X{constructor(){this.index=new v,this.registrations={tags:{},entities:{},components:{}},this.registry=new W,this.queries={},this.toDestroy=new Set,this.toIndex=new Map}get query(){return new I(this)}getQuery(t,e){return this.queries[e]||(this.queries[e]=new K(this,t)),this.queries[e]}register(t,e,s){const i=this.registrations;for(const r of t){const o=r.name===A?r.prototype.items.map(h=>h.type).join("|"):r.name;i.entities[o]=r}for(const r of e)i.components[r.type]=r;for(const r of s)i.tags[r]=P();this.registry.add(...e.map(r=>r.type),...s.map(r=>i.tags[r]))}indexEntity(t){this.toIndex.set(t,t.key??null)}tick(){const t=[],e=Array.from(this.toDestroy);let s=0n;for(const[i,r]of this.toIndex)if(i.key=this.getEntityKey(i),i.key!==r){if(s|=r|i.key,!r){t.push(i),this.index.append(i.key,i);continue}e.push(i),this.index.remove(r,i),this.toDestroy.has(i)||(this.index.append(i.key,i),t.push(i))}for(const i of Object.values(this.queries))i.key&&O.any(s,i.key)&&i.update(t,e);this.toIndex.clear(),this.toDestroy.clear()}getID(...t){return this.registry.getID(...t)}destroy(t){this.toDestroy.add(t)}stop(){this.queries={},this.index=new v,this.toDestroy.clear(),this.toIndex.clear()}create(t,e={},s=[]){const i=new t(this,e,s);return this.indexEntity(i),i}getEntityKey(t){const e=this.registrations.tags,s=Array.from(t.tags).map(i=>e[i]);return this.registry.add(...t.items.map(i=>i.type),...s)}}class Z{constructor(t){this.stack=[],this.ctx=t}serialize(t={}){return{entities:this.serializeEntities(t)}}visit(t){const e={};for(const s of Object.getOwnPropertyNames(t)){const i=this.serializeValue(t[s]);i!==void 0&&(e[s]=i)}return e}serializeValue(t){let e=t;switch(this.stack.push(t),typeof t){case"object":{if(Array.isArray(t))e=t.map(s=>typeof s=="object"?s&&this.visit(s):this.serializeValue(s));else if(t)if(t.toJSON)e=t.toJSON();else if(t instanceof S)e=[N,t.id].join("|");else for(const s of Object.getOwnPropertyNames(t)){const i=t[s];this.stack.includes(i)||(t[s]=i&&this.serializeValue(i))}break}case"bigint":e=`${t}n`;break;case"number":case"string":case"boolean":{e=t;break}}return this.stack.pop(),e}serializeEntities(t){this.stack=[];const e=t.entityFilter??(()=>!0),i=this.ctx.manager.index.all().filter(e).map(r=>{const o=!r.constructor.name||r.constructor.name===A?r.items.map(h=>h.type).join("|"):r.constructor.name;return{id:r.id,type:o,tags:Array.from(r.tags),$:this.visit(r.$)}});return this.stack=[],i}}class Q{constructor(t){this.ctx=t}}class tt{constructor(t){this.ref=null,this.ref=t??null}}class S{constructor(t,e={},s=[]){this.key=0n,this.id=P(),this.components={all:this.allComponents.bind(this),has:this.hasComponents.bind(this),add:this.addComponent.bind(this),remove:this.removeComponents.bind(this),delete:this.removeComponents.bind(this),[Symbol.iterator]:function*(){yield*this.all()}},this.referenced=new Set,this.manager=t,this.tags=new T(s,()=>this.manager.indexEntity(this)),this.has=this.components.has.bind(this),this.is=this.tags.has.bind(this.tags),this.$=this.getBindings(e),this.items=this.items.slice()}static with(...t){return C(this,...t)}destroy(){for(const t of this.referenced)t.ref=null;this.manager.destroy(this)}getBindings(t){const e={};for(const s of this.items){const[i,r]=[s.type,new s];r instanceof tt?(Object.defineProperty(e,i,{get:()=>r.ref,set:o=>{r.ref=o,o?.referenced.add(r)}}),e[i]=t[i]??null):e[i]=Object.assign(r,t[i]??{})}return e}allComponents(){return Object.values(this.$)}hasComponents(...t){return t.every(e=>e.type in this.$)}addComponent(t,e){const s=t.type;this.$[s]||(this.$[s]=new t(e),this.items.push(t),this.manager.indexEntity(this))}removeComponents(...t){for(const e of t)e.type in this.$&&(this.items.splice(this.items.indexOf(e),1),delete this.$[e.type],this.manager.indexEntity(this))}}class et{constructor(t){this.ctx=t}}function R(...n){return class extends et{constructor(){super(...arguments);this.pipeline=[]}async tick(e,s){for(const i of this.pipeline)await i.tick?.(e,s),this.ctx.manager.tick()}async stop(){for(const e of this.pipeline)await e.stop?.(),this.ctx.manager.tick()}async start(){this.pipeline=n.map(e=>H(e)?new e(this.ctx):{tick:(s,i)=>e(this.ctx,s,i)});for(const e of this.pipeline)await e.start?.(),this.ctx.manager.tick()}}}function st(n,...t){return Object.assign(t.length===1?t.shift():R(...t),{phase:n})}class ${constructor(){this.isLocked=!1,this.manager=new X,this.$=this.getPlugins(),this.system=this.getSystem()}static with(...t){return V(...t)}get items(){return[]}async tick(t,e){this.isLocked?console.warn("Tick duration exceeds tick rate."):(this.isLocked=!0,await this.system.tick?.(t,e),this.manager.tick(),this.isLocked=!1)}load(t){new _(this).deserialize(t),this.manager.tick()}save(t){return new Z(this).serialize(t)}register(t=[],e=[],s=[]){this.manager.register(Object.values(t),Object.values(e),s)}async start(){for(const t of Object.values(this.$))await t.start?.();await this.system.start?.(),this.manager.tick(),await this.tick(0,0)}async stop(){await this.system.stop?.();for(const t of Object.values(this.$))await t.stop?.();this.manager.stop()}create(t,e={},s=[]){return this.manager.create(t,e,s)}get query(){return new I(this.manager)}getPlugins(){const t={};for(const e of this.items){const s=new e(this);this.register(Object.values(s.$?.entities??{}),Object.values(s.$?.components??{}),s.$?.tags??[]),t[e.type]=s}return t}getSystem(){const t=j.POST_UPDATE-1,e=Object.values(this.items).map(s=>this.$[s.type]).filter(s=>s.$?.systems?.length).reduce((s,i)=>s.concat(i.$?.systems??[]),[]).sort((s,i)=>(s.phase??t)-(i.phase??t));return new(R(...e))(this)}}var it=typeof globalThis!="undefined"?globalThis:typeof window!="undefined"?window:typeof global!="undefined"?global:typeof self!="undefined"?self:{},z={exports:{}};(function(n,t){(function(e,s){n.exports=s()})(it,function(){var e=function(){function s(u){return o.appendChild(u.dom),u}function i(u){for(var d=0;d<o.children.length;d++)o.children[d].style.display=d===u?"block":"none";r=u}var r=0,o=document.createElement("div");o.style.cssText="position:fixed;top:0;left:0;cursor:pointer;opacity:0.9;z-index:10000",o.addEventListener("click",function(u){u.preventDefault(),i(++r%o.children.length)},!1);var h=(performance||Date).now(),c=h,a=0,p=s(new e.Panel("FPS","#0ff","#002")),g=s(new e.Panel("MS","#0f0","#020"));if(self.performance&&self.performance.memory)var m=s(new e.Panel("MB","#f08","#201"));return i(0),{REVISION:16,dom:o,addPanel:s,showPanel:i,begin:function(){h=(performance||Date).now()},end:function(){a++;var u=(performance||Date).now();if(g.update(u-h,200),u>c+1e3&&(p.update(1e3*a/(u-c),100),c=u,a=0,m)){var d=performance.memory;m.update(d.usedJSHeapSize/1048576,d.jsHeapSizeLimit/1048576)}return u},update:function(){h=this.end()},domElement:o,setMode:i}};return e.Panel=function(s,i,r){var o=Infinity,h=0,c=Math.round,a=c(window.devicePixelRatio||1),p=80*a,g=48*a,m=3*a,u=2*a,d=3*a,y=15*a,w=74*a,k=30*a,x=document.createElement("canvas");x.width=p,x.height=g,x.style.cssText="width:80px;height:48px";var f=x.getContext("2d");return f.font="bold "+9*a+"px Helvetica,Arial,sans-serif",f.textBaseline="top",f.fillStyle=r,f.fillRect(0,0,p,g),f.fillStyle=i,f.fillText(s,m,u),f.fillRect(d,y,w,k),f.fillStyle=r,f.globalAlpha=.9,f.fillRect(d,y,w,k),{dom:x,update:function(b,M){o=Math.min(o,b),h=Math.max(h,b),f.fillStyle=r,f.globalAlpha=1,f.fillRect(0,0,p,y),f.fillStyle=i,f.fillText(c(b)+" "+s+" ("+c(o)+"-"+c(h)+")",m,u),f.drawImage(x,d+a,y,w-a,k,d,y,w-a,k),f.fillRect(d+w-a,y,a,k),f.fillStyle=r,f.globalAlpha=.9,f.fillRect(d+w-a,y,a,c((1-b/M)*k))}}},e})})(z);var nt=z.exports;export{q as C,S as E,Q as P,nt as S,j as a,$ as b,st as p};
  