@oasys/oecs 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.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var $=Object.defineProperty;var z=(a,t,e)=>t in a?$(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e;var l=(a,t,e)=>z(a,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class Y extends Error{constructor(t,e,s){super(t),this.is_operational=e,this.context=s,this.name=this.constructor.name,Error.captureStackTrace(this,this.constructor)}}var u=(a=>(a.EID_MAX_INDEX_OVERFLOW="EID_MAX_INDEX_OVERFLOW",a.EID_MAX_GEN_OVERFLOW="EID_MAX_GEN_OVERFLOW",a.COMPONENT_NOT_REGISTERED="COMPONENT_NOT_REGISTERED",a.ENTITY_NOT_ALIVE="ENTITY_NOT_ALIVE",a.CIRCULAR_SYSTEM_DEPENDENCY="CIRCULAR_SYSTEM_DEPENDENCY",a.DUPLICATE_SYSTEM="DUPLICATE_SYSTEM",a.ARCHETYPE_NOT_FOUND="ARCHETYPE_NOT_FOUND",a))(u||{});class g extends Y{constructor(t,e,s){super(e??t,!0,s),this.category=t}}var C=(a=>(a.ASSERTION_FAIL_CONDITION="ASSERTION_FAIL_CONDITION",a.VALIDATION_FAIL_CONDITION="VALIDATION_FAIL_CONDITION",a))(C||{});class B extends Y{constructor(t,e,s){super(e,!1,s),this.category=t}}const P=a=>Number.isInteger(a)&&a>=0;function b(a,t,e){if(process.env.NODE_ENV!=="production"&&!t(a))throw new B(C.VALIDATION_FAIL_CONDITION,`Expected value to meet validation: ${e}`);return a}const Q=4;class T{constructor(t){l(this,"_words");this._words=t??new Array(Q).fill(0)}has(t){const e=t>>>5;return e>=this._words.length?!1:(this._words[e]&1<<(t&31))!==0}set(t){const e=t>>>5;e>=this._words.length&&this.grow(e+1),this._words[e]|=1<<(t&31)}clear(t){const e=t>>>5;e>=this._words.length||(this._words[e]&=~(1<<(t&31)))}overlaps(t){const e=this._words,s=t._words,n=e.length<s.length?e.length:s.length;for(let r=0;r<n;r++)if((e[r]&s[r])!==0)return!0;return!1}contains(t){const e=t._words,s=this._words,n=s.length;for(let r=0;r<e.length;r++){const o=e[r];if(o!==0&&(r>=n||(s[r]&o)!==o))return!1}return!0}equals(t){const e=this._words,s=t._words,n=e.length>s.length?e.length:s.length;for(let r=0;r<n;r++){const o=r<e.length?e[r]:0,_=r<s.length?s[r]:0;if(o!==_)return!1}return!0}copy(){return new T(this._words.slice())}copy_with_set(t){const e=t>>>5,s=e+1,n=this._words.length>s?this._words.length:s,r=new Array(n).fill(0);for(let o=0;o<this._words.length;o++)r[o]=this._words[o];return r[e]|=1<<(t&31),new T(r)}copy_with_clear(t){const e=this._words.slice(),s=t>>>5;return s<e.length&&(e[s]&=~(1<<(t&31))),new T(e)}hash(){let t=2166136261;const e=this._words;let s=e.length-1;for(;s>=0&&e[s]===0;)s--;for(let n=0;n<=s;n++)t^=e[n],t=Math.imul(t,16777619);return t}for_each(t){const e=this._words;for(let s=0;s<e.length;s++){let n=e[s];if(n===0)continue;const r=s<<5;for(;n!==0;){const o=n&-n>>>0,_=31-Math.clz32(o);t(r+_),n^=o}}}grow(t){let e=this._words.length;for(;e<t;)e*=2;const s=new Array(e).fill(0);for(let n=0;n<this._words.length;n++)s[n]=this._words[n];this._words=s}}const D=20,S=(1<<D)-1,K=S,J=31-D,k=(1<<J)-1,Z=(a,t)=>{if(process.env.NODE_ENV!=="production"){if(a<0||a>K)throw new g(u.EID_MAX_INDEX_OVERFLOW);if(t<0||t>k)throw new g(u.EID_MAX_GEN_OVERFLOW)}return t<<D|a},m=a=>a&S,R=a=>a>>D,H=a=>b(a,P,"ComponentID must be a non-negative integer"),tt=a=>b(a,P,"EventID must be a non-negative integer");class et{constructor(t){l(this,"field_names");l(this,"columns");l(this,"reader");this.field_names=t,this.columns=[];for(let s=0;s<t.length;s++)this.columns.push([]);const e={length:0};for(let s=0;s<t.length;s++)e[t[s]]=this.columns[s];this.reader=e}emit(t){const e=this.field_names,s=this.columns;for(let n=0;n<e.length;n++)s[n].push(t[e[n]]);this.reader.length++}emit_signal(){this.reader.length++}clear(){this.reader.length=0;const t=this.columns;for(let e=0;e<t.length;e++)t[e].length=0}}const st=a=>b(a,P,"ArchetypeID must be a non-negative integer");class nt{constructor(t,e,s){l(this,"id");l(this,"mask");l(this,"has_columns");l(this,"entity_ids",[]);l(this,"length",0);l(this,"edges",[]);l(this,"column_groups",[]);l(this,"_column_ids",[]);if(this.id=t,this.mask=e,s)for(let n=0;n<s.length;n++){const r=s[n],o=new Array(r.field_names.length);for(let h=0;h<r.field_names.length;h++)o[h]=[];const _=Object.create(null);for(let h=0;h<r.field_names.length;h++)_[r.field_names[h]]=o[h];this.column_groups[r.component_id]={layout:r,columns:o,record:_},this._column_ids.push(r.component_id)}this.has_columns=this._column_ids.length>0}get entity_count(){return this.length}get entity_list(){return this.entity_ids}has_component(t){return this.mask.has(t)}matches(t){return this.mask.contains(t)}get_column(t,e){const s=this.column_groups[t];if(process.env.NODE_ENV!=="production"&&!s)throw new g(u.COMPONENT_NOT_REGISTERED,`Component ${t} not in archetype ${this.id}`);const n=s.layout.field_index[e];if(process.env.NODE_ENV!=="production"&&n===void 0)throw new g(u.COMPONENT_NOT_REGISTERED,`Field "${e}" does not exist on component`);return s.columns[n]}get_column_group(t){const e=this.column_groups[t];return e?e.record:{}}write_fields(t,e,s){const n=this.column_groups[e];if(!n)return;const{field_names:r}=n.layout;for(let o=0;o<r.length;o++)n.columns[o][t]=s[r[o]]}read_field(t,e,s){const n=this.column_groups[e];if(!n)return NaN;const r=n.layout.field_index[s];return r===void 0?NaN:n.columns[r][t]??NaN}copy_shared_from(t,e,s){const n=t.column_groups,r=this._column_ids;for(let o=0;o<r.length;o++){const _=r[o],h=n[_];if(!h)continue;const i=this.column_groups[_];for(let c=0;c<i.columns.length;c++)i.columns[c][s]=h.columns[c][e]}}add_entity(t){const e=this.length;this.entity_ids.push(t);const s=this._column_ids;for(let n=0;n<s.length;n++){const r=this.column_groups[s[n]];for(let o=0;o<r.columns.length;o++)r.columns[o].push(0)}return this.length++,e}remove_entity(t){const e=this.length-1;let s=-1;const n=this._column_ids;if(t!==e){this.entity_ids[t]=this.entity_ids[e],s=m(this.entity_ids[t]);for(let r=0;r<n.length;r++){const o=this.column_groups[n[r]];for(let _=0;_<o.columns.length;_++)o.columns[_][t]=o.columns[_][e],o.columns[_].pop()}}else for(let r=0;r<n.length;r++){const o=this.column_groups[n[r]];for(let _=0;_<o.columns.length;_++)o.columns[_].pop()}return this.entity_ids.pop(),this.length--,s}add_entity_tag(t){const e=this.length;return this.entity_ids.push(t),this.length++,e}remove_entity_tag(t){const e=this.length-1;let s=-1;return t!==e&&(this.entity_ids[t]=this.entity_ids[e],s=m(this.entity_ids[t])),this.entity_ids.pop(),this.length--,s}get_edge(t){return this.edges[t]}set_edge(t,e){this.edges[t]=e}}function j(a,t,e){const s=a.get(t);s!==void 0?s.push(e):a.set(t,[e])}const w=-1,M=Object.freeze(Object.create(null));class rt{constructor(){l(this,"entity_generations",[]);l(this,"entity_high_water",0);l(this,"entity_free_indices",[]);l(this,"entity_alive_count",0);l(this,"component_metas",[]);l(this,"component_count",0);l(this,"event_channels",[]);l(this,"event_count",0);l(this,"archetypes",[]);l(this,"archetype_map",new Map);l(this,"next_archetype_id",0);l(this,"component_index",new Map);l(this,"registered_queries",[]);l(this,"empty_archetype_id");l(this,"entity_archetype",[]);l(this,"entity_row",[]);l(this,"pending_destroy",[]);l(this,"pending_add_ids",[]);l(this,"pending_add_defs",[]);l(this,"pending_add_values",[]);l(this,"pending_remove_ids",[]);l(this,"pending_remove_defs",[]);this.empty_archetype_id=this.arch_get_or_create_from_mask(new T)}arch_get(t){if(process.env.NODE_ENV!=="production"&&(t<0||t>=this.archetypes.length))throw new g(u.ARCHETYPE_NOT_FOUND,`Archetype with ID ${t} not found`);return this.archetypes[t]}arch_get_or_create_from_mask(t){const e=t.hash(),s=this.archetype_map.get(e);if(s!==void 0){for(let h=0;h<s.length;h++)if(this.archetypes[s[h]].mask.equals(t))return s[h]}const n=st(this.next_archetype_id++),r=[];t.for_each(h=>{const i=h,c=this.component_metas[i];c&&c.field_names.length>0&&r.push({component_id:i,field_names:c.field_names,field_index:c.field_index})});const o=new nt(n,t,r);this.archetypes.push(o),j(this.archetype_map,e,n),t.for_each(h=>{const i=h;let c=this.component_index.get(i);c||(c=new Set,this.component_index.set(i,c)),c.add(n)});const _=this.registered_queries;for(let h=0;h<_.length;h++){const i=_[h];o.matches(i.include_mask)&&(!i.exclude_mask||!o.mask.overlaps(i.exclude_mask))&&(!i.any_of_mask||o.mask.overlaps(i.any_of_mask))&&i.result.push(o)}return n}arch_resolve_add(t,e){const s=this.arch_get(t);if(s.mask.has(e))return t;const n=s.get_edge(e);if((n==null?void 0:n.add)!=null)return n.add;const r=this.arch_get_or_create_from_mask(s.mask.copy_with_set(e));return this.arch_cache_edge(s,this.arch_get(r),e),r}arch_resolve_remove(t,e){const s=this.arch_get(t);if(!s.mask.has(e))return t;const n=s.get_edge(e);if((n==null?void 0:n.remove)!=null)return n.remove;const r=this.arch_get_or_create_from_mask(s.mask.copy_with_clear(e));return this.arch_cache_edge(this.arch_get(r),s,e),r}arch_cache_edge(t,e,s){const n=t.get_edge(s)??{add:null,remove:null};n.add=e.id,t.set_edge(s,n);const r=e.get_edge(s)??{add:null,remove:null};r.remove=t.id,e.set_edge(s,r)}create_entity(){let t,e;this.entity_free_indices.length>0?(t=this.entity_free_indices.pop(),e=this.entity_generations[t]):(t=this.entity_high_water++,this.entity_generations[t]=0,e=0),this.entity_alive_count++;const s=Z(t,e);return this.entity_archetype[t]=this.empty_archetype_id,this.entity_row[t]=w,s}destroy_entity(t){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new g(u.ENTITY_NOT_ALIVE);return}const e=m(t),s=this.entity_row[e];if(s!==w){const o=this.arch_get(this.entity_archetype[e]).remove_entity(s);o!==-1&&(this.entity_row[o]=s)}this.entity_archetype[e]=w,this.entity_row[e]=w;const n=R(t);if(process.env.NODE_ENV!=="production"&&n>=k)throw new g(u.EID_MAX_GEN_OVERFLOW);this.entity_generations[e]=n+1&k,this.entity_free_indices.push(e),this.entity_alive_count--}is_alive(t){const e=m(t);return e<this.entity_high_water&&this.entity_generations[e]===R(t)}get entity_count(){return this.entity_alive_count}destroy_entity_deferred(t){if(process.env.NODE_ENV!=="production"&&!this.is_alive(t))throw new g(u.ENTITY_NOT_ALIVE);this.pending_destroy.push(t)}flush_destroyed(){const t=this.pending_destroy;if(t.length===0)return;const e=this.entity_archetype,s=this.entity_row,n=this.entity_generations,r=this.archetypes,o=this.entity_high_water;for(let _=0;_<t.length;_++){const h=t[_],i=h&S,c=h>>D;if(i>=o||n[i]!==c)continue;const d=s[i];if(d!==w){const f=r[e[i]],p=f.has_columns?f.remove_entity(d):f.remove_entity_tag(d);p!==-1&&(s[p]=d)}if(e[i]=w,s[i]=w,process.env.NODE_ENV!=="production"&&c>=k)throw new g(u.EID_MAX_GEN_OVERFLOW);n[i]=c+1&k,this.entity_free_indices.push(i),this.entity_alive_count--}t.length=0}get pending_destroy_count(){return this.pending_destroy.length}add_component_deferred(t,e,s){if(process.env.NODE_ENV!=="production"&&!this.is_alive(t))throw new g(u.ENTITY_NOT_ALIVE);this.pending_add_ids.push(t),this.pending_add_defs.push(e),this.pending_add_values.push(s??M)}remove_component_deferred(t,e){if(process.env.NODE_ENV!=="production"&&!this.is_alive(t))throw new g(u.ENTITY_NOT_ALIVE);this.pending_remove_ids.push(t),this.pending_remove_defs.push(e)}flush_structural(){this.pending_add_ids.length>0&&this._flush_adds(),this.pending_remove_ids.length>0&&this._flush_removes()}_flush_adds(){const t=this.pending_add_ids,e=this.pending_add_defs,s=this.pending_add_values,n=t.length,r=this.entity_archetype,o=this.entity_row,_=this.entity_generations,h=this.archetypes,i=this.component_metas,c=this.entity_high_water;for(let d=0;d<n;d++){const f=t[d],p=f&S,v=f>>D;if(p>=c||_[p]!==v)continue;const O=r[p],E=e[d],y=h[O];if(y.mask.has(E)){i[E].field_names.length>0&&y.write_fields(o[p],E,s[d]);continue}const A=this.arch_resolve_add(O,E),N=h[A],I=o[p],x=!N.has_columns&&!y.has_columns,V=x?N.add_entity_tag(f):N.add_entity(f);if(I!==w){x||N.copy_shared_from(y,I,V);const q=x?y.remove_entity_tag(I):y.remove_entity(I);q!==-1&&(o[q]=I)}i[E].field_names.length>0&&N.write_fields(V,E,s[d]),r[p]=A,o[p]=V}t.length=0,e.length=0,s.length=0}_flush_removes(){const t=this.pending_remove_ids,e=this.pending_remove_defs,s=t.length,n=this.entity_archetype,r=this.entity_row,o=this.entity_generations,_=this.archetypes,h=this.entity_high_water;for(let i=0;i<s;i++){const c=t[i],d=c&S,f=c>>D;if(d>=h||o[d]!==f)continue;const p=n[d],v=e[i],O=_[p];if(!O.mask.has(v))continue;const E=this.arch_resolve_remove(p,v),y=_[E],A=r[d],N=!y.has_columns&&!O.has_columns,I=N?y.add_entity_tag(c):y.add_entity(c);N||y.copy_shared_from(O,A,I);const x=N?O.remove_entity_tag(A):O.remove_entity(A);x!==-1&&(r[x]=A),n[d]=E,r[d]=I}t.length=0,e.length=0}get pending_structural_count(){return this.pending_add_ids.length+this.pending_remove_ids.length}register_component(t){const e=H(this.component_count++),s=t,n=Object.create(null);for(let r=0;r<s.length;r++)n[s[r]]=r;return this.component_metas.push({field_names:s,field_index:n}),e}add_component(t,e,s){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new g(u.ENTITY_NOT_ALIVE);return}const n=m(t),r=this.entity_archetype[n],o=this.arch_get(r);if(o.has_component(e)){o.write_fields(this.entity_row[n],e,s);return}const _=this.arch_resolve_add(r,e),h=this.arch_get(_),i=this.entity_row[n],c=h.add_entity(t);if(i!==w){h.copy_shared_from(o,i,c);const d=o.remove_entity(i);d!==-1&&(this.entity_row[d]=i)}h.write_fields(c,e,s),this.entity_archetype[n]=_,this.entity_row[n]=c}add_components(t,e){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new g(u.ENTITY_NOT_ALIVE);return}const s=m(t),n=this.entity_archetype[s];let r=n;for(let o=0;o<e.length;o++)r=this.arch_resolve_add(r,e[o].def);if(r!==n){const o=this.arch_get(n),_=this.arch_get(r),h=this.entity_row[s],i=_.add_entity(t);if(h!==w){_.copy_shared_from(o,h,i);const c=o.remove_entity(h);c!==-1&&(this.entity_row[c]=h)}for(let c=0;c<e.length;c++)_.write_fields(i,e[c].def,e[c].values??M);this.entity_archetype[s]=r,this.entity_row[s]=i}else{const o=this.arch_get(n),_=this.entity_row[s];for(let h=0;h<e.length;h++)o.write_fields(_,e[h].def,e[h].values??M)}}remove_component(t,e){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new g(u.ENTITY_NOT_ALIVE);return}const s=m(t),n=this.entity_archetype[s],r=this.arch_get(n);if(!r.has_component(e))return;const o=this.arch_resolve_remove(n,e),_=this.arch_get(o),h=this.entity_row[s],i=_.add_entity(t);_.copy_shared_from(r,h,i);const c=r.remove_entity(h);c!==-1&&(this.entity_row[c]=h),this.entity_archetype[s]=o,this.entity_row[s]=i}remove_components(t,e){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new g(u.ENTITY_NOT_ALIVE);return}const s=m(t),n=this.entity_archetype[s];let r=n;for(let d=0;d<e.length;d++)r=this.arch_resolve_remove(r,e[d]);if(r===n)return;const o=this.arch_get(n),_=this.arch_get(r),h=this.entity_row[s],i=_.add_entity(t);_.copy_shared_from(o,h,i);const c=o.remove_entity(h);c!==-1&&(this.entity_row[c]=h),this.entity_archetype[s]=r,this.entity_row[s]=i}has_component(t,e){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new g(u.ENTITY_NOT_ALIVE);return!1}const s=m(t);return this.arch_get(this.entity_archetype[s]).has_component(e)}get_entity_archetype(t){return this.arch_get(this.entity_archetype[m(t)])}get_entity_row(t){return this.entity_row[m(t)]}get_matching_archetypes(t,e,s){const n=t._words;let r=!1;for(let i=0;i<n.length;i++)if(n[i]!==0){r=!0;break}if(!r)return this.archetypes.filter(i=>(!e||!i.mask.overlaps(e))&&(!s||i.mask.overlaps(s)));let o,_=!1;for(let i=0;i<n.length;i++){let c=n[i];if(c===0)continue;const d=i<<5;for(;c!==0;){const f=c&-c>>>0,p=d+(31-Math.clz32(f));c^=f;const v=this.component_index.get(p);if(!v||v.size===0){_=!0;break}(!o||v.size<o.size)&&(o=v)}if(_)break}if(_||!o)return[];const h=[];for(const i of o){const c=this.arch_get(i);c.matches(t)&&(!e||!c.mask.overlaps(e))&&(!s||c.mask.overlaps(s))&&h.push(c)}return h}register_query(t,e,s){const n=this.get_matching_archetypes(t,e,s);return this.registered_queries.push({include_mask:t.copy(),exclude_mask:e?e.copy():null,any_of_mask:s?s.copy():null,result:n}),n}get archetype_count(){return this.archetypes.length}register_event(t){const e=tt(this.event_count++),s=new et(t);return this.event_channels.push(s),e}emit_event(t,e){this.event_channels[t].emit(e)}emit_signal(t){this.event_channels[t].emit_signal()}get_event_reader(t){return this.event_channels[t].reader}clear_events(){const t=this.event_channels;for(let e=0;e<t.length;e++)t[e].clear()}}var F=(a=>(a.PRE_STARTUP="PRE_STARTUP",a.STARTUP="STARTUP",a.POST_STARTUP="POST_STARTUP",a.PRE_UPDATE="PRE_UPDATE",a.UPDATE="UPDATE",a.POST_UPDATE="POST_UPDATE",a))(F||{});const L=["PRE_STARTUP","STARTUP","POST_STARTUP"],U=["PRE_UPDATE","UPDATE","POST_UPDATE"];class ot{constructor(){l(this,"label_systems",new Map);l(this,"sorted_cache",new Map);l(this,"system_index",new Map);l(this,"next_insertion_order",0);for(let t=0;t<L.length;t++)this.label_systems.set(L[t],[]);for(let t=0;t<U.length;t++)this.label_systems.set(U[t],[])}add_systems(t,...e){for(const s of e){const n="system"in s?s.system:s,r="system"in s?s.ordering:void 0;if(process.env.NODE_ENV!=="production"&&this.system_index.has(n))throw new g(u.DUPLICATE_SYSTEM,`System ${n.id} is already scheduled`);const o={descriptor:n,insertion_order:this.next_insertion_order++,before:new Set((r==null?void 0:r.before)??[]),after:new Set((r==null?void 0:r.after)??[])};this.label_systems.get(t).push(o),this.system_index.set(n,t),this.sorted_cache.delete(t)}}remove_system(t){const e=this.system_index.get(t);if(e===void 0)return;const s=this.label_systems.get(e),n=s.findIndex(r=>r.descriptor===t);if(n!==-1){const r=s.length-1;n!==r&&(s[n]=s[r]),s.pop();for(const o of s)o.before.delete(t),o.after.delete(t)}this.system_index.delete(t),this.sorted_cache.delete(e)}run_startup(t){for(const e of L)this.run_label(e,t,0)}run_update(t,e){for(const s of U)this.run_label(s,t,e)}get_all_systems(){const t=[];for(const e of this.label_systems.values())for(const s of e)t.push(s.descriptor);return t}has_system(t){return this.system_index.has(t)}clear(){for(const t of this.label_systems.values())t.length=0;this.sorted_cache.clear(),this.system_index.clear()}run_label(t,e,s){const n=this.get_sorted(t);for(let r=0;r<n.length;r++)n[r].fn(e,s);e.flush()}get_sorted(t){const e=this.sorted_cache.get(t);if(e!==void 0)return e;const s=this.label_systems.get(t),n=this.topological_sort(s,t);return this.sorted_cache.set(t,n),n}topological_sort(t,e){if(t.length===0)return[];const s=new Map,n=new Map,r=new Map,o=new Set;for(const i of t)s.set(i.descriptor,new Set),n.set(i.descriptor,0),r.set(i.descriptor,i.insertion_order),o.add(i.descriptor);for(const i of t){for(const c of i.before)o.has(c)&&(s.get(i.descriptor).add(c),n.set(c,n.get(c)+1));for(const c of i.after)o.has(c)&&(s.get(c).add(i.descriptor),n.set(i.descriptor,n.get(i.descriptor)+1))}let _=[];for(const i of t)n.get(i.descriptor)===0&&_.push(i.descriptor);_.sort((i,c)=>r.get(c)-r.get(i));const h=[];for(;_.length>0;){const i=_.pop();h.push(i);for(const c of s.get(i)){const d=n.get(c)-1;n.set(c,d),d===0&&_.push(c)}_.sort((c,d)=>r.get(d)-r.get(c))}if(h.length!==t.length){const i=new Set(h),c=t.filter(d=>!i.has(d.descriptor)).map(d=>`system_${d.descriptor.id}`);throw new g(u.CIRCULAR_SYSTEM_DEPENDENCY,`Circular system dependency detected in ${e}: [${c.join(", ")}]`)}return h}}const it=Object.freeze(Object.create(null));class X{constructor(t,e,s,n,r,o){l(this,"_archetypes");l(this,"_defs");l(this,"_resolver");l(this,"_include");l(this,"_exclude");l(this,"_any_of");l(this,"_args_buf");this._archetypes=t,this._defs=e,this._resolver=s,this._include=n,this._exclude=r,this._any_of=o,this._args_buf=new Array(e.length+1)}get length(){return this._archetypes.length}count(){const t=this._archetypes;let e=0;for(let s=0;s<t.length;s++)e+=t[s].entity_count;return e}get archetypes(){return this._archetypes}*[Symbol.iterator](){const t=this._archetypes;for(let e=0;e<t.length;e++)t[e].entity_count>0&&(yield t[e])}each(t){const e=this._archetypes,s=this._defs,n=this._args_buf;for(let r=0;r<e.length;r++){const o=e[r],_=o.entity_count;if(_!==0){for(let h=0;h<s.length;h++)n[h]=o.get_column_group(s[h]);n[s.length]=_,t.apply(null,n)}}}and(...t){const e=this._include.copy(),s=this._defs.slice();for(let n=0;n<t.length;n++)e.has(t[n])||(e.set(t[n]),s.push(t[n]));return this._resolver._resolve_query(e,this._exclude,this._any_of,s)}not(...t){const e=this._exclude?this._exclude.copy():new T;for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolver._resolve_query(this._include,e,this._any_of,this._defs)}or(...t){const e=this._any_of?this._any_of.copy():new T;for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolver._resolve_query(this._include,this._exclude,e,this._defs)}}class G{constructor(t){this._resolver=t}every(...t){const e=new T;for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolver._resolve_query(e,null,null,t)}}class W{constructor(t){l(this,"store");this.store=t}create_entity(){return this.store.create_entity()}get_field(t,e,s){const n=this.store.get_entity_archetype(e),r=this.store.get_entity_row(e);return n.read_field(r,t,s)}set_field(t,e,s,n){const r=this.store.get_entity_archetype(e),o=this.store.get_entity_row(e),_=r.get_column(t,s);_[o]=n}destroy_entity(t){return this.store.destroy_entity_deferred(t),this}flush_destroyed(){this.store.flush_destroyed()}add_component(t,e,s){return this.store.add_component_deferred(t,e,s??it),this}remove_component(t,e){return this.store.remove_component_deferred(t,e),this}flush(){this.store.flush_structural(),this.store.flush_destroyed()}emit(t,e){e===void 0?this.store.emit_signal(t):this.store.emit_event(t,e)}read(t){return this.store.get_event_reader(t)}}const _t=a=>b(a,P,"SystemID must be a non-negative integer"),ct=Object.freeze(Object.create(null));class ht{constructor(){l(this,"store");l(this,"schedule");l(this,"ctx");l(this,"systems",new Set);l(this,"next_system_id",0);l(this,"query_cache",new Map);l(this,"scratch_mask",new T);this.store=new rt,this.schedule=new ot,this.ctx=new W(this.store)}register_component(t){return this.store.register_component(t)}register_tag(){return this.store.register_component([])}register_event(t){return this.store.register_event(t)}register_signal(){return this.store.register_event([])}create_entity(){return this.store.create_entity()}destroy_entity(t){this.store.destroy_entity_deferred(t)}is_alive(t){return this.store.is_alive(t)}get entity_count(){return this.store.entity_count}add_component(t,e,s){this.store.add_component(t,e,s??ct)}add_components(t,e){this.store.add_components(t,e)}remove_component(t,e){return this.store.remove_component(t,e),this}remove_components(t,...e){this.store.remove_components(t,e)}has_component(t,e){return this.store.has_component(t,e)}get_field(t,e,s){const n=this.store.get_entity_archetype(e),r=this.store.get_entity_row(e);return n.read_field(r,t,s)}emit(t,e){e===void 0?this.store.emit_signal(t):this.store.emit_event(t,e)}query(...t){const e=this.scratch_mask;e._words.fill(0);for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolve_query(e.copy(),null,null,t)}_resolve_query(t,e,s,n){const r=t.hash(),o=e?e.hash():0,_=s?s.hash():0,h=r^Math.imul(o,2654435769)^Math.imul(_,1367130551)|0,i=this._find_cached(h,t,e,s);if(i!==void 0)return i.query;const c=this.store.register_query(t,e??void 0,s??void 0),d=new X(c,n,this,t.copy(),(e==null?void 0:e.copy())??null,(s==null?void 0:s.copy())??null);return j(this.query_cache,h,{include_mask:t.copy(),exclude_mask:(e==null?void 0:e.copy())??null,any_of_mask:(s==null?void 0:s.copy())??null,query:d}),d}_find_cached(t,e,s,n){const r=this.query_cache.get(t);if(r)for(let o=0;o<r.length;o++){const _=r[o];if(!(!_.include_mask.equals(e)||!(s===null?_.exclude_mask===null:_.exclude_mask!==null&&_.exclude_mask.equals(s))||!(n===null?_.any_of_mask===null:_.any_of_mask!==null&&_.any_of_mask.equals(n))))return _}}register_system(t,e){let s;if(typeof t=="function"){const o=e(new G(this)),_=this.ctx;s={fn:(h,i)=>t(o,_,i)}}else s=t;const n=_t(this.next_system_id++),r=Object.freeze(Object.assign({id:n},s));return this.systems.add(r),r}add_systems(t,...e){return this.schedule.add_systems(t,...e),this}remove_system(t){var e;this.schedule.remove_system(t),(e=t.on_removed)==null||e.call(t),this.systems.delete(t)}get system_count(){return this.systems.size}startup(){var t;for(const e of this.systems.values())(t=e.on_added)==null||t.call(e,this.store);this.schedule.run_startup(this.ctx)}update(t){this.store.clear_events(),this.schedule.run_update(this.ctx,t)}flush(){this.ctx.flush()}dispose(){var t,e;for(const s of this.systems.values())(t=s.dispose)==null||t.call(s),(e=s.on_removed)==null||e.call(s);this.systems.clear(),this.schedule.clear()}}exports.Query=X;exports.QueryBuilder=G;exports.SCHEDULE=F;exports.SystemContext=W;exports.World=ht;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class R extends Error{constructor(t,e,s){super(t),this.is_operational=e,this.context=s,this.name=this.constructor.name,Error.captureStackTrace(this,this.constructor)}}var d=(a=>(a.EID_MAX_INDEX_OVERFLOW="EID_MAX_INDEX_OVERFLOW",a.EID_MAX_GEN_OVERFLOW="EID_MAX_GEN_OVERFLOW",a.COMPONENT_NOT_REGISTERED="COMPONENT_NOT_REGISTERED",a.ENTITY_NOT_ALIVE="ENTITY_NOT_ALIVE",a.CIRCULAR_SYSTEM_DEPENDENCY="CIRCULAR_SYSTEM_DEPENDENCY",a.DUPLICATE_SYSTEM="DUPLICATE_SYSTEM",a.ARCHETYPE_NOT_FOUND="ARCHETYPE_NOT_FOUND",a))(d||{});class u extends R{constructor(t,e,s){super(e??t,!0,s),this.category=t}}var Y=(a=>(a.ASSERTION_FAIL_CONDITION="ASSERTION_FAIL_CONDITION",a.VALIDATION_FAIL_CONDITION="VALIDATION_FAIL_CONDITION",a))(Y||{});class W extends R{constructor(t,e,s){super(e,!1,s),this.category=t}}const k=a=>Number.isInteger(a)&&a>=0;function S(a,t,e){if(process.env.NODE_ENV!=="production"&&!t(a))throw new W(Y.VALIDATION_FAIL_CONDITION,`Expected value to meet validation: ${e}`);return a}const $=4;class T{_words;constructor(t){this._words=t??new Array($).fill(0)}has(t){const e=t>>>5;return e>=this._words.length?!1:(this._words[e]&1<<(t&31))!==0}set(t){const e=t>>>5;e>=this._words.length&&this.grow(e+1),this._words[e]|=1<<(t&31)}clear(t){const e=t>>>5;e>=this._words.length||(this._words[e]&=~(1<<(t&31)))}overlaps(t){const e=this._words,s=t._words,n=e.length<s.length?e.length:s.length;for(let r=0;r<n;r++)if((e[r]&s[r])!==0)return!0;return!1}contains(t){const e=t._words,s=this._words,n=s.length;for(let r=0;r<e.length;r++){const i=e[r];if(i!==0&&(r>=n||(s[r]&i)!==i))return!1}return!0}equals(t){const e=this._words,s=t._words,n=e.length>s.length?e.length:s.length;for(let r=0;r<n;r++){const i=r<e.length?e[r]:0,_=r<s.length?s[r]:0;if(i!==_)return!1}return!0}copy(){return new T(this._words.slice())}copy_with_set(t){const e=t>>>5,s=e+1,n=this._words.length>s?this._words.length:s,r=new Array(n).fill(0);for(let i=0;i<this._words.length;i++)r[i]=this._words[i];return r[e]|=1<<(t&31),new T(r)}copy_with_clear(t){const e=this._words.slice(),s=t>>>5;return s<e.length&&(e[s]&=~(1<<(t&31))),new T(e)}hash(){let t=2166136261;const e=this._words;let s=e.length-1;for(;s>=0&&e[s]===0;)s--;for(let n=0;n<=s;n++)t^=e[n],t=Math.imul(t,16777619);return t}for_each(t){const e=this._words;for(let s=0;s<e.length;s++){let n=e[s];if(n===0)continue;const r=s<<5;for(;n!==0;){const i=n&-n>>>0,_=31-Math.clz32(i);t(r+_),n^=i}}}grow(t){let e=this._words.length;for(;e<t;)e*=2;const s=new Array(e).fill(0);for(let n=0;n<this._words.length;n++)s[n]=this._words[n];this._words=s}}const I=20,P=(1<<I)-1,z=P,B=31-I,D=(1<<B)-1,Q=(a,t)=>{if(process.env.NODE_ENV!=="production"){if(a<0||a>z)throw new u(d.EID_MAX_INDEX_OVERFLOW);if(t<0||t>D)throw new u(d.EID_MAX_GEN_OVERFLOW)}return t<<I|a},y=a=>a&P,q=a=>a>>I,K=a=>S(a,k,"ComponentID must be a non-negative integer"),J=a=>S(a,k,"EventID must be a non-negative integer");class Z{field_names;columns;reader;constructor(t){this.field_names=t,this.columns=[];for(let s=0;s<t.length;s++)this.columns.push([]);const e={length:0};for(let s=0;s<t.length;s++)e[t[s]]=this.columns[s];this.reader=e}emit(t){const e=this.field_names,s=this.columns;for(let n=0;n<e.length;n++)s[n].push(t[e[n]]);this.reader.length++}emit_signal(){this.reader.length++}clear(){this.reader.length=0;const t=this.columns;for(let e=0;e<t.length;e++)t[e].length=0}}const H=a=>S(a,k,"ArchetypeID must be a non-negative integer");class tt{id;mask;has_columns;entity_ids=[];length=0;edges=[];column_groups=[];_column_ids=[];constructor(t,e,s){if(this.id=t,this.mask=e,s)for(let n=0;n<s.length;n++){const r=s[n],i=new Array(r.field_names.length);for(let h=0;h<r.field_names.length;h++)i[h]=[];const _=Object.create(null);for(let h=0;h<r.field_names.length;h++)_[r.field_names[h]]=i[h];this.column_groups[r.component_id]={layout:r,columns:i,record:_},this._column_ids.push(r.component_id)}this.has_columns=this._column_ids.length>0}get entity_count(){return this.length}get entity_list(){return this.entity_ids}has_component(t){return this.mask.has(t)}matches(t){return this.mask.contains(t)}get_column(t,e){const s=this.column_groups[t];if(process.env.NODE_ENV!=="production"&&!s)throw new u(d.COMPONENT_NOT_REGISTERED,`Component ${t} not in archetype ${this.id}`);const n=s.layout.field_index[e];if(process.env.NODE_ENV!=="production"&&n===void 0)throw new u(d.COMPONENT_NOT_REGISTERED,`Field "${e}" does not exist on component`);return s.columns[n]}get_column_group(t){const e=this.column_groups[t];return e?e.record:{}}write_fields(t,e,s){const n=this.column_groups[e];if(!n)return;const{field_names:r}=n.layout;for(let i=0;i<r.length;i++)n.columns[i][t]=s[r[i]]}read_field(t,e,s){const n=this.column_groups[e];if(!n)return NaN;const r=n.layout.field_index[s];return r===void 0?NaN:n.columns[r][t]??NaN}copy_shared_from(t,e,s){const n=t.column_groups,r=this._column_ids;for(let i=0;i<r.length;i++){const _=r[i],h=n[_];if(!h)continue;const o=this.column_groups[_];for(let c=0;c<o.columns.length;c++)o.columns[c][s]=h.columns[c][e]}}add_entity(t){const e=this.length;this.entity_ids.push(t);const s=this._column_ids;for(let n=0;n<s.length;n++){const r=this.column_groups[s[n]];for(let i=0;i<r.columns.length;i++)r.columns[i].push(0)}return this.length++,e}remove_entity(t){const e=this.length-1;let s=-1;const n=this._column_ids;if(t!==e){this.entity_ids[t]=this.entity_ids[e],s=y(this.entity_ids[t]);for(let r=0;r<n.length;r++){const i=this.column_groups[n[r]];for(let _=0;_<i.columns.length;_++)i.columns[_][t]=i.columns[_][e],i.columns[_].pop()}}else for(let r=0;r<n.length;r++){const i=this.column_groups[n[r]];for(let _=0;_<i.columns.length;_++)i.columns[_].pop()}return this.entity_ids.pop(),this.length--,s}add_entity_tag(t){const e=this.length;return this.entity_ids.push(t),this.length++,e}remove_entity_tag(t){const e=this.length-1;let s=-1;return t!==e&&(this.entity_ids[t]=this.entity_ids[e],s=y(this.entity_ids[t])),this.entity_ids.pop(),this.length--,s}get_edge(t){return this.edges[t]}set_edge(t,e){this.edges[t]=e}}function F(a,t,e){const s=a.get(t);s!==void 0?s.push(e):a.set(t,[e])}const m=-1,V=Object.freeze(Object.create(null));class et{entity_generations=[];entity_high_water=0;entity_free_indices=[];entity_alive_count=0;component_metas=[];component_count=0;event_channels=[];event_count=0;archetypes=[];archetype_map=new Map;next_archetype_id=0;component_index=new Map;registered_queries=[];empty_archetype_id;entity_archetype=[];entity_row=[];pending_destroy=[];pending_add_ids=[];pending_add_defs=[];pending_add_values=[];pending_remove_ids=[];pending_remove_defs=[];constructor(){this.empty_archetype_id=this.arch_get_or_create_from_mask(new T)}arch_get(t){if(process.env.NODE_ENV!=="production"&&(t<0||t>=this.archetypes.length))throw new u(d.ARCHETYPE_NOT_FOUND,`Archetype with ID ${t} not found`);return this.archetypes[t]}arch_get_or_create_from_mask(t){const e=t.hash(),s=this.archetype_map.get(e);if(s!==void 0){for(let h=0;h<s.length;h++)if(this.archetypes[s[h]].mask.equals(t))return s[h]}const n=H(this.next_archetype_id++),r=[];t.for_each(h=>{const o=h,c=this.component_metas[o];c&&c.field_names.length>0&&r.push({component_id:o,field_names:c.field_names,field_index:c.field_index})});const i=new tt(n,t,r);this.archetypes.push(i),F(this.archetype_map,e,n),t.for_each(h=>{const o=h;let c=this.component_index.get(o);c||(c=new Set,this.component_index.set(o,c)),c.add(n)});const _=this.registered_queries;for(let h=0;h<_.length;h++){const o=_[h];i.matches(o.include_mask)&&(!o.exclude_mask||!i.mask.overlaps(o.exclude_mask))&&(!o.any_of_mask||i.mask.overlaps(o.any_of_mask))&&o.result.push(i)}return n}arch_resolve_add(t,e){const s=this.arch_get(t);if(s.mask.has(e))return t;const n=s.get_edge(e);if(n?.add!=null)return n.add;const r=this.arch_get_or_create_from_mask(s.mask.copy_with_set(e));return this.arch_cache_edge(s,this.arch_get(r),e),r}arch_resolve_remove(t,e){const s=this.arch_get(t);if(!s.mask.has(e))return t;const n=s.get_edge(e);if(n?.remove!=null)return n.remove;const r=this.arch_get_or_create_from_mask(s.mask.copy_with_clear(e));return this.arch_cache_edge(this.arch_get(r),s,e),r}arch_cache_edge(t,e,s){const n=t.get_edge(s)??{add:null,remove:null};n.add=e.id,t.set_edge(s,n);const r=e.get_edge(s)??{add:null,remove:null};r.remove=t.id,e.set_edge(s,r)}create_entity(){let t,e;this.entity_free_indices.length>0?(t=this.entity_free_indices.pop(),e=this.entity_generations[t]):(t=this.entity_high_water++,this.entity_generations[t]=0,e=0),this.entity_alive_count++;const s=Q(t,e);return this.entity_archetype[t]=this.empty_archetype_id,this.entity_row[t]=m,s}destroy_entity(t){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new u(d.ENTITY_NOT_ALIVE);return}const e=y(t),s=this.entity_row[e];if(s!==m){const i=this.arch_get(this.entity_archetype[e]).remove_entity(s);i!==-1&&(this.entity_row[i]=s)}this.entity_archetype[e]=m,this.entity_row[e]=m;const n=q(t);if(process.env.NODE_ENV!=="production"&&n>=D)throw new u(d.EID_MAX_GEN_OVERFLOW);this.entity_generations[e]=n+1&D,this.entity_free_indices.push(e),this.entity_alive_count--}is_alive(t){const e=y(t);return e<this.entity_high_water&&this.entity_generations[e]===q(t)}get entity_count(){return this.entity_alive_count}destroy_entity_deferred(t){if(process.env.NODE_ENV!=="production"&&!this.is_alive(t))throw new u(d.ENTITY_NOT_ALIVE);this.pending_destroy.push(t)}flush_destroyed(){const t=this.pending_destroy;if(t.length===0)return;const e=this.entity_archetype,s=this.entity_row,n=this.entity_generations,r=this.archetypes,i=this.entity_high_water;for(let _=0;_<t.length;_++){const h=t[_],o=h&P,c=h>>I;if(o>=i||n[o]!==c)continue;const l=s[o];if(l!==m){const p=r[e[o]],g=p.has_columns?p.remove_entity(l):p.remove_entity_tag(l);g!==-1&&(s[g]=l)}if(e[o]=m,s[o]=m,process.env.NODE_ENV!=="production"&&c>=D)throw new u(d.EID_MAX_GEN_OVERFLOW);n[o]=c+1&D,this.entity_free_indices.push(o),this.entity_alive_count--}t.length=0}get pending_destroy_count(){return this.pending_destroy.length}add_component_deferred(t,e,s){if(process.env.NODE_ENV!=="production"&&!this.is_alive(t))throw new u(d.ENTITY_NOT_ALIVE);this.pending_add_ids.push(t),this.pending_add_defs.push(e),this.pending_add_values.push(s??V)}remove_component_deferred(t,e){if(process.env.NODE_ENV!=="production"&&!this.is_alive(t))throw new u(d.ENTITY_NOT_ALIVE);this.pending_remove_ids.push(t),this.pending_remove_defs.push(e)}flush_structural(){this.pending_add_ids.length>0&&this._flush_adds(),this.pending_remove_ids.length>0&&this._flush_removes()}_flush_adds(){const t=this.pending_add_ids,e=this.pending_add_defs,s=this.pending_add_values,n=t.length,r=this.entity_archetype,i=this.entity_row,_=this.entity_generations,h=this.archetypes,o=this.component_metas,c=this.entity_high_water;for(let l=0;l<n;l++){const p=t[l],g=p&P,w=p>>I;if(g>=c||_[g]!==w)continue;const N=r[g],v=e[l],f=h[N];if(f.mask.has(v)){o[v].field_names.length>0&&f.write_fields(i[g],v,s[l]);continue}const O=this.arch_resolve_add(N,v),E=h[O],x=i[g],A=!E.has_columns&&!f.has_columns,b=A?E.add_entity_tag(p):E.add_entity(p);if(x!==m){A||E.copy_shared_from(f,x,b);const U=A?f.remove_entity_tag(x):f.remove_entity(x);U!==-1&&(i[U]=x)}o[v].field_names.length>0&&E.write_fields(b,v,s[l]),r[g]=O,i[g]=b}t.length=0,e.length=0,s.length=0}_flush_removes(){const t=this.pending_remove_ids,e=this.pending_remove_defs,s=t.length,n=this.entity_archetype,r=this.entity_row,i=this.entity_generations,_=this.archetypes,h=this.entity_high_water;for(let o=0;o<s;o++){const c=t[o],l=c&P,p=c>>I;if(l>=h||i[l]!==p)continue;const g=n[l],w=e[o],N=_[g];if(!N.mask.has(w))continue;const v=this.arch_resolve_remove(g,w),f=_[v],O=r[l],E=!f.has_columns&&!N.has_columns,x=E?f.add_entity_tag(c):f.add_entity(c);E||f.copy_shared_from(N,O,x);const A=E?N.remove_entity_tag(O):N.remove_entity(O);A!==-1&&(r[A]=O),n[l]=v,r[l]=x}t.length=0,e.length=0}get pending_structural_count(){return this.pending_add_ids.length+this.pending_remove_ids.length}register_component(t){const e=K(this.component_count++),s=t,n=Object.create(null);for(let r=0;r<s.length;r++)n[s[r]]=r;return this.component_metas.push({field_names:s,field_index:n}),e}add_component(t,e,s){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new u(d.ENTITY_NOT_ALIVE);return}const n=y(t),r=this.entity_archetype[n],i=this.arch_get(r);if(i.has_component(e)){i.write_fields(this.entity_row[n],e,s);return}const _=this.arch_resolve_add(r,e),h=this.arch_get(_),o=this.entity_row[n],c=h.add_entity(t);if(o!==m){h.copy_shared_from(i,o,c);const l=i.remove_entity(o);l!==-1&&(this.entity_row[l]=o)}h.write_fields(c,e,s),this.entity_archetype[n]=_,this.entity_row[n]=c}add_components(t,e){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new u(d.ENTITY_NOT_ALIVE);return}const s=y(t),n=this.entity_archetype[s];let r=n;for(let i=0;i<e.length;i++)r=this.arch_resolve_add(r,e[i].def);if(r!==n){const i=this.arch_get(n),_=this.arch_get(r),h=this.entity_row[s],o=_.add_entity(t);if(h!==m){_.copy_shared_from(i,h,o);const c=i.remove_entity(h);c!==-1&&(this.entity_row[c]=h)}for(let c=0;c<e.length;c++)_.write_fields(o,e[c].def,e[c].values??V);this.entity_archetype[s]=r,this.entity_row[s]=o}else{const i=this.arch_get(n),_=this.entity_row[s];for(let h=0;h<e.length;h++)i.write_fields(_,e[h].def,e[h].values??V)}}remove_component(t,e){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new u(d.ENTITY_NOT_ALIVE);return}const s=y(t),n=this.entity_archetype[s],r=this.arch_get(n);if(!r.has_component(e))return;const i=this.arch_resolve_remove(n,e),_=this.arch_get(i),h=this.entity_row[s],o=_.add_entity(t);_.copy_shared_from(r,h,o);const c=r.remove_entity(h);c!==-1&&(this.entity_row[c]=h),this.entity_archetype[s]=i,this.entity_row[s]=o}remove_components(t,e){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new u(d.ENTITY_NOT_ALIVE);return}const s=y(t),n=this.entity_archetype[s];let r=n;for(let l=0;l<e.length;l++)r=this.arch_resolve_remove(r,e[l]);if(r===n)return;const i=this.arch_get(n),_=this.arch_get(r),h=this.entity_row[s],o=_.add_entity(t);_.copy_shared_from(i,h,o);const c=i.remove_entity(h);c!==-1&&(this.entity_row[c]=h),this.entity_archetype[s]=r,this.entity_row[s]=o}has_component(t,e){if(!this.is_alive(t)){if(process.env.NODE_ENV!=="production")throw new u(d.ENTITY_NOT_ALIVE);return!1}const s=y(t);return this.arch_get(this.entity_archetype[s]).has_component(e)}get_entity_archetype(t){return this.arch_get(this.entity_archetype[y(t)])}get_entity_row(t){return this.entity_row[y(t)]}get_matching_archetypes(t,e,s){const n=t._words;let r=!1;for(let o=0;o<n.length;o++)if(n[o]!==0){r=!0;break}if(!r)return this.archetypes.filter(o=>(!e||!o.mask.overlaps(e))&&(!s||o.mask.overlaps(s)));let i,_=!1;for(let o=0;o<n.length;o++){let c=n[o];if(c===0)continue;const l=o<<5;for(;c!==0;){const p=c&-c>>>0,g=l+(31-Math.clz32(p));c^=p;const w=this.component_index.get(g);if(!w||w.size===0){_=!0;break}(!i||w.size<i.size)&&(i=w)}if(_)break}if(_||!i)return[];const h=[];for(const o of i){const c=this.arch_get(o);c.matches(t)&&(!e||!c.mask.overlaps(e))&&(!s||c.mask.overlaps(s))&&h.push(c)}return h}register_query(t,e,s){const n=this.get_matching_archetypes(t,e,s);return this.registered_queries.push({include_mask:t.copy(),exclude_mask:e?e.copy():null,any_of_mask:s?s.copy():null,result:n}),n}get archetype_count(){return this.archetypes.length}register_event(t){const e=J(this.event_count++),s=new Z(t);return this.event_channels.push(s),e}emit_event(t,e){this.event_channels[t].emit(e)}emit_signal(t){this.event_channels[t].emit_signal()}get_event_reader(t){return this.event_channels[t].reader}clear_events(){const t=this.event_channels;for(let e=0;e<t.length;e++)t[e].clear()}}var C=(a=>(a.PRE_STARTUP="PRE_STARTUP",a.STARTUP="STARTUP",a.POST_STARTUP="POST_STARTUP",a.FIXED_UPDATE="FIXED_UPDATE",a.PRE_UPDATE="PRE_UPDATE",a.UPDATE="UPDATE",a.POST_UPDATE="POST_UPDATE",a))(C||{});const M=["PRE_STARTUP","STARTUP","POST_STARTUP"],L=["PRE_UPDATE","UPDATE","POST_UPDATE"];class st{label_systems=new Map;sorted_cache=new Map;system_index=new Map;next_insertion_order=0;constructor(){for(let t=0;t<M.length;t++)this.label_systems.set(M[t],[]);this.label_systems.set("FIXED_UPDATE",[]);for(let t=0;t<L.length;t++)this.label_systems.set(L[t],[])}add_systems(t,...e){for(const s of e){const n="system"in s?s.system:s,r="system"in s?s.ordering:void 0;if(process.env.NODE_ENV!=="production"&&this.system_index.has(n))throw new u(d.DUPLICATE_SYSTEM,`System ${n.id} is already scheduled`);const i={descriptor:n,insertion_order:this.next_insertion_order++,before:new Set(r?.before??[]),after:new Set(r?.after??[])};this.label_systems.get(t).push(i),this.system_index.set(n,t),this.sorted_cache.delete(t)}}remove_system(t){const e=this.system_index.get(t);if(e===void 0)return;const s=this.label_systems.get(e),n=s.findIndex(r=>r.descriptor===t);if(n!==-1){const r=s.length-1;n!==r&&(s[n]=s[r]),s.pop();for(const i of s)i.before.delete(t),i.after.delete(t)}this.system_index.delete(t),this.sorted_cache.delete(e)}run_startup(t){for(const e of M)this.run_label(e,t,0)}run_update(t,e){for(const s of L)this.run_label(s,t,e)}run_fixed_update(t,e){this.run_label("FIXED_UPDATE",t,e)}has_fixed_systems(){return this.label_systems.get("FIXED_UPDATE").length>0}get_all_systems(){const t=[];for(const e of this.label_systems.values())for(const s of e)t.push(s.descriptor);return t}has_system(t){return this.system_index.has(t)}clear(){for(const t of this.label_systems.values())t.length=0;this.sorted_cache.clear(),this.system_index.clear()}run_label(t,e,s){const n=this.get_sorted(t);for(let r=0;r<n.length;r++)n[r].fn(e,s);e.flush()}get_sorted(t){const e=this.sorted_cache.get(t);if(e!==void 0)return e;const s=this.label_systems.get(t),n=this.topological_sort(s,t);return this.sorted_cache.set(t,n),n}topological_sort(t,e){if(t.length===0)return[];const s=new Map,n=new Map,r=new Map,i=new Set;for(const o of t)s.set(o.descriptor,new Set),n.set(o.descriptor,0),r.set(o.descriptor,o.insertion_order),i.add(o.descriptor);for(const o of t){for(const c of o.before)i.has(c)&&(s.get(o.descriptor).add(c),n.set(c,n.get(c)+1));for(const c of o.after)i.has(c)&&(s.get(c).add(o.descriptor),n.set(o.descriptor,n.get(o.descriptor)+1))}let _=[];for(const o of t)n.get(o.descriptor)===0&&_.push(o.descriptor);_.sort((o,c)=>r.get(c)-r.get(o));const h=[];for(;_.length>0;){const o=_.pop();h.push(o);for(const c of s.get(o)){const l=n.get(c)-1;n.set(c,l),l===0&&_.push(c)}_.sort((c,l)=>r.get(l)-r.get(c))}if(h.length!==t.length){const o=new Set(h),c=t.filter(l=>!o.has(l.descriptor)).map(l=>`system_${l.descriptor.id}`);throw new u(d.CIRCULAR_SYSTEM_DEPENDENCY,`Circular system dependency detected in ${e}: [${c.join(", ")}]`)}return h}}const nt=Object.freeze(Object.create(null));class X{_archetypes;_defs;_resolver;_include;_exclude;_any_of;_args_buf;constructor(t,e,s,n,r,i){this._archetypes=t,this._defs=e,this._resolver=s,this._include=n,this._exclude=r,this._any_of=i,this._args_buf=new Array(e.length+1)}get length(){return this._archetypes.length}count(){const t=this._archetypes;let e=0;for(let s=0;s<t.length;s++)e+=t[s].entity_count;return e}get archetypes(){return this._archetypes}*[Symbol.iterator](){const t=this._archetypes;for(let e=0;e<t.length;e++)t[e].entity_count>0&&(yield t[e])}each(t){const e=this._archetypes,s=this._defs,n=this._args_buf;for(let r=0;r<e.length;r++){const i=e[r],_=i.entity_count;if(_!==0){for(let h=0;h<s.length;h++)n[h]=i.get_column_group(s[h]);n[s.length]=_,t.apply(null,n)}}}and(...t){const e=this._include.copy(),s=this._defs.slice();for(let n=0;n<t.length;n++)e.has(t[n])||(e.set(t[n]),s.push(t[n]));return this._resolver._resolve_query(e,this._exclude,this._any_of,s)}not(...t){const e=this._exclude?this._exclude.copy():new T;for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolver._resolve_query(this._include,e,this._any_of,this._defs)}or(...t){const e=this._any_of?this._any_of.copy():new T;for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolver._resolve_query(this._include,this._exclude,e,this._defs)}}class j{constructor(t){this._resolver=t}every(...t){const e=new T;for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolver._resolve_query(e,null,null,t)}}class G{store;constructor(t){this.store=t}create_entity(){return this.store.create_entity()}get_field(t,e,s){const n=this.store.get_entity_archetype(e),r=this.store.get_entity_row(e);return n.read_field(r,t,s)}set_field(t,e,s,n){const r=this.store.get_entity_archetype(e),i=this.store.get_entity_row(e),_=r.get_column(t,s);_[i]=n}destroy_entity(t){return this.store.destroy_entity_deferred(t),this}flush_destroyed(){this.store.flush_destroyed()}add_component(t,e,s){return this.store.add_component_deferred(t,e,s??nt),this}remove_component(t,e){return this.store.remove_component_deferred(t,e),this}flush(){this.store.flush_structural(),this.store.flush_destroyed()}emit(t,e){e===void 0?this.store.emit_signal(t):this.store.emit_event(t,e)}read(t){return this.store.get_event_reader(t)}}const rt=a=>S(a,k,"SystemID must be a non-negative integer"),it=Object.freeze(Object.create(null));class ot{store;schedule;ctx;systems=new Set;next_system_id=0;_fixed_timestep;_accumulator=0;_max_fixed_steps;query_cache=new Map;scratch_mask=new T;constructor(t){this.store=new et,this.schedule=new st,this.ctx=new G(this.store),this._fixed_timestep=t?.fixed_timestep??1/60,this._max_fixed_steps=t?.max_fixed_steps??4}get fixed_timestep(){return this._fixed_timestep}set fixed_timestep(t){this._fixed_timestep=t}get fixed_alpha(){return this._accumulator/this._fixed_timestep}register_component(t){return this.store.register_component(t)}register_tag(){return this.store.register_component([])}register_event(t){return this.store.register_event(t)}register_signal(){return this.store.register_event([])}create_entity(){return this.store.create_entity()}destroy_entity(t){this.store.destroy_entity_deferred(t)}is_alive(t){return this.store.is_alive(t)}get entity_count(){return this.store.entity_count}add_component(t,e,s){this.store.add_component(t,e,s??it)}add_components(t,e){this.store.add_components(t,e)}remove_component(t,e){return this.store.remove_component(t,e),this}remove_components(t,...e){this.store.remove_components(t,e)}has_component(t,e){return this.store.has_component(t,e)}get_field(t,e,s){const n=this.store.get_entity_archetype(e),r=this.store.get_entity_row(e);return n.read_field(r,t,s)}emit(t,e){e===void 0?this.store.emit_signal(t):this.store.emit_event(t,e)}query(...t){const e=this.scratch_mask;e._words.fill(0);for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolve_query(e.copy(),null,null,t)}_resolve_query(t,e,s,n){const r=t.hash(),i=e?e.hash():0,_=s?s.hash():0,h=r^Math.imul(i,2654435769)^Math.imul(_,1367130551)|0,o=this._find_cached(h,t,e,s);if(o!==void 0)return o.query;const c=this.store.register_query(t,e??void 0,s??void 0),l=new X(c,n,this,t.copy(),e?.copy()??null,s?.copy()??null);return F(this.query_cache,h,{include_mask:t.copy(),exclude_mask:e?.copy()??null,any_of_mask:s?.copy()??null,query:l}),l}_find_cached(t,e,s,n){const r=this.query_cache.get(t);if(r)for(let i=0;i<r.length;i++){const _=r[i];if(!(!_.include_mask.equals(e)||!(s===null?_.exclude_mask===null:_.exclude_mask!==null&&_.exclude_mask.equals(s))||!(n===null?_.any_of_mask===null:_.any_of_mask!==null&&_.any_of_mask.equals(n))))return _}}register_system(t,e){let s;if(typeof t=="function"){const i=e(new j(this)),_=this.ctx;s={fn:(h,o)=>t(i,_,o)}}else s=t;const n=rt(this.next_system_id++),r=Object.freeze(Object.assign({id:n},s));return this.systems.add(r),r}add_systems(t,...e){return this.schedule.add_systems(t,...e),this}remove_system(t){this.schedule.remove_system(t),t.on_removed?.(),this.systems.delete(t)}get system_count(){return this.systems.size}startup(){for(const t of this.systems.values())t.on_added?.(this.store);this.schedule.run_startup(this.ctx)}update(t){if(this.store.clear_events(),this.schedule.has_fixed_systems()){this._accumulator+=t;const e=this._max_fixed_steps*this._fixed_timestep;for(this._accumulator>e&&(this._accumulator=e);this._accumulator>=this._fixed_timestep;)this.schedule.run_fixed_update(this.ctx,this._fixed_timestep),this._accumulator-=this._fixed_timestep}this.schedule.run_update(this.ctx,t)}flush(){this.ctx.flush()}dispose(){for(const t of this.systems.values())t.dispose?.(),t.on_removed?.();this.systems.clear(),this.schedule.clear()}}exports.Query=X;exports.QueryBuilder=j;exports.SCHEDULE=C;exports.SystemContext=G;exports.World=ot;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { World } from './world';
2
2
  export { Query, QueryBuilder, SystemContext } from './query';
3
3
  export { SCHEDULE } from './schedule';
4
+ export type { WorldOptions } from './world';
4
5
  export type { EntityID } from './entity';
5
6
  export type { ComponentDef, ComponentFields, FieldValues, ColumnsForSchema, } from './component';
6
7
  export type { EventDef, EventReader } from './event';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACzC,YAAY,EACV,YAAY,EACZ,eAAe,EACf,WAAW,EACX,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACrD,YAAY,EACV,QAAQ,EACR,YAAY,EACZ,gBAAgB,GACjB,MAAM,UAAU,CAAC;AAClB,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAG5C,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACzC,YAAY,EACV,YAAY,EACZ,eAAe,EACf,WAAW,EACX,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACrD,YAAY,EACV,QAAQ,EACR,YAAY,EACZ,gBAAgB,GACjB,MAAM,UAAU,CAAC;AAClB,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}