@oasys/oecs 0.1.1 → 0.1.2

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";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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class F 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 q=(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))(q||{});class z extends F{constructor(t,e,s){super(e??t,!0,s),this.category=t}}function b(a,t,e){return a}const C=4;class v{_words;constructor(t){this._words=t??new Array(C).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 v(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 v(r)}copy_with_clear(t){const e=this._words.slice(),s=t>>>5;return s<e.length&&(e[s]&=~(1<<(t&31))),new v(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 A=20,P=(1<<A)-1,V=31-A,U=(1<<V)-1,G=(a,t)=>t<<A|a,f=a=>a&P,M=a=>a>>A,W=a=>b(a),$=a=>b(a);class B{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 Q=a=>b(a);class K{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],n=s.layout.field_index[e];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=f(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=f(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 y=-1,N=Object.freeze(Object.create(null));class J{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 v)}arch_get(t){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=Q(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 K(n,t,r);this.archetypes.push(i),j(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=G(t,e);return this.entity_archetype[t]=this.empty_archetype_id,this.entity_row[t]=y,s}destroy_entity(t){if(!this.is_alive(t))return;const e=f(t),s=this.entity_row[e];if(s!==y){const i=this.arch_get(this.entity_archetype[e]).remove_entity(s);i!==-1&&(this.entity_row[i]=s)}this.entity_archetype[e]=y,this.entity_row[e]=y;const n=M(t);this.entity_generations[e]=n+1&U,this.entity_free_indices.push(e),this.entity_alive_count--}is_alive(t){const e=f(t);return e<this.entity_high_water&&this.entity_generations[e]===M(t)}get entity_count(){return this.entity_alive_count}destroy_entity_deferred(t){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>>A;if(o>=i||n[o]!==c)continue;const l=s[o];if(l!==y){const u=r[e[o]],d=u.has_columns?u.remove_entity(l):u.remove_entity_tag(l);d!==-1&&(s[d]=l)}e[o]=y,s[o]=y,n[o]=c+1&U,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){this.pending_add_ids.push(t),this.pending_add_defs.push(e),this.pending_add_values.push(s??N)}remove_component_deferred(t,e){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 u=t[l],d=u&P,p=u>>A;if(d>=c||_[d]!==p)continue;const x=r[d],m=e[l],g=h[x];if(g.mask.has(m)){o[m].field_names.length>0&&g.write_fields(i[d],m,s[l]);continue}const E=this.arch_resolve_add(x,m),w=h[E],T=i[d],k=!w.has_columns&&!g.has_columns,S=k?w.add_entity_tag(u):w.add_entity(u);if(T!==y){k||w.copy_shared_from(g,T,S);const I=k?g.remove_entity_tag(T):g.remove_entity(T);I!==-1&&(i[I]=T)}o[m].field_names.length>0&&w.write_fields(S,m,s[l]),r[d]=E,i[d]=S}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,u=c>>A;if(l>=h||i[l]!==u)continue;const d=n[l],p=e[o],x=_[d];if(!x.mask.has(p))continue;const m=this.arch_resolve_remove(d,p),g=_[m],E=r[l],w=!g.has_columns&&!x.has_columns,T=w?g.add_entity_tag(c):g.add_entity(c);w||g.copy_shared_from(x,E,T);const k=w?x.remove_entity_tag(E):x.remove_entity(E);k!==-1&&(r[k]=E),n[l]=m,r[l]=T}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=W(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))return;const n=f(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!==y){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))return;const s=f(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!==y){_.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??N);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??N)}}remove_component(t,e){if(!this.is_alive(t))return;const s=f(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))return;const s=f(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))return!1;const s=f(t);return this.arch_get(this.entity_archetype[s]).has_component(e)}get_entity_archetype(t){return this.arch_get(this.entity_archetype[f(t)])}get_entity_row(t){return this.entity_row[f(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 u=c&-c>>>0,d=l+(31-Math.clz32(u));c^=u;const p=this.component_index.get(d);if(!p||p.size===0){_=!0;break}(!i||p.size<i.size)&&(i=p)}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=$(this.event_count++),s=new B(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 R=(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))(R||{});const D=["PRE_STARTUP","STARTUP","POST_STARTUP"],O=["PRE_UPDATE","UPDATE","POST_UPDATE"];class Z{label_systems=new Map;sorted_cache=new Map;system_index=new Map;next_insertion_order=0;constructor(){for(let t=0;t<D.length;t++)this.label_systems.set(D[t],[]);this.label_systems.set("FIXED_UPDATE",[]);for(let t=0;t<O.length;t++)this.label_systems.set(O[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,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 D)this.run_label(e,t,0)}run_update(t,e){for(const s of O)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 z(q.CIRCULAR_SYSTEM_DEPENDENCY,`Circular system dependency detected in ${e}: [${c.join(", ")}]`)}return h}}const H=Object.freeze(Object.create(null));class Y{_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 v;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 v;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 L{constructor(t){this._resolver=t}every(...t){const e=new v;for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolver._resolve_query(e,null,null,t)}}class X{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??H),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 tt=a=>b(a),et=Object.freeze(Object.create(null));class st{store;schedule;ctx;systems=new Set;next_system_id=0;_fixed_timestep;_accumulator=0;_max_fixed_steps;query_cache=new Map;scratch_mask=new v;constructor(t){this.store=new J,this.schedule=new Z,this.ctx=new X(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??et)}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 Y(c,n,this,t.copy(),e?.copy()??null,s?.copy()??null);return j(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 L(this)),_=this.ctx;s={fn:(h,o)=>t(i,_,o)}}else s=t;const n=tt(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=Y;exports.QueryBuilder=L;exports.SCHEDULE=R;exports.SystemContext=X;exports.World=st;
package/dist/index.js CHANGED
@@ -3,32 +3,20 @@ class R extends Error {
3
3
  super(t), this.is_operational = e, this.context = s, this.name = this.constructor.name, Error.captureStackTrace(this, this.constructor);
4
4
  }
5
5
  }
6
- var d = /* @__PURE__ */ ((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 || {});
7
- class u extends R {
6
+ var q = /* @__PURE__ */ ((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))(q || {});
7
+ class Y extends R {
8
8
  constructor(t, e, s) {
9
9
  super(e ?? t, !0, s), this.category = t;
10
10
  }
11
11
  }
12
- var Y = /* @__PURE__ */ ((a) => (a.ASSERTION_FAIL_CONDITION = "ASSERTION_FAIL_CONDITION", a.VALIDATION_FAIL_CONDITION = "VALIDATION_FAIL_CONDITION", a))(Y || {});
13
- class X extends R {
14
- constructor(t, e, s) {
15
- super(e, !1, s), this.category = t;
16
- }
17
- }
18
- const P = (a) => Number.isInteger(a) && a >= 0;
19
12
  function b(a, t, e) {
20
- if (process.env.NODE_ENV !== "production" && !t(a))
21
- throw new X(
22
- Y.VALIDATION_FAIL_CONDITION,
23
- `Expected value to meet validation: ${e}`
24
- );
25
13
  return a;
26
14
  }
27
- const C = 4;
28
- class N {
15
+ const X = 4;
16
+ class v {
29
17
  _words;
30
18
  constructor(t) {
31
- this._words = t ?? new Array(C).fill(0);
19
+ this._words = t ?? new Array(X).fill(0);
32
20
  }
33
21
  has(t) {
34
22
  const e = t >>> 5;
@@ -68,16 +56,16 @@ class N {
68
56
  return !0;
69
57
  }
70
58
  copy() {
71
- return new N(this._words.slice());
59
+ return new v(this._words.slice());
72
60
  }
73
61
  copy_with_set(t) {
74
62
  const e = t >>> 5, s = e + 1, n = this._words.length > s ? this._words.length : s, r = new Array(n).fill(0);
75
63
  for (let i = 0; i < this._words.length; i++) r[i] = this._words[i];
76
- return r[e] |= 1 << (t & 31), new N(r);
64
+ return r[e] |= 1 << (t & 31), new v(r);
77
65
  }
78
66
  copy_with_clear(t) {
79
67
  const e = this._words.slice(), s = t >>> 5;
80
- return s < e.length && (e[s] &= ~(1 << (t & 31))), new N(e);
68
+ return s < e.length && (e[s] &= ~(1 << (t & 31))), new v(e);
81
69
  }
82
70
  /** FNV-1a hash. Skips trailing zero words so differently-sized arrays with the same bits hash equally. */
83
71
  hash() {
@@ -110,24 +98,12 @@ class N {
110
98
  this._words = s;
111
99
  }
112
100
  }
113
- const I = 20, k = (1 << I) - 1, j = k, G = 31 - I, D = (1 << G) - 1, $ = (a, t) => {
114
- if (process.env.NODE_ENV !== "production") {
115
- if (a < 0 || a > j)
116
- throw new u(d.EID_MAX_INDEX_OVERFLOW);
117
- if (t < 0 || t > D)
118
- throw new u(d.EID_MAX_GEN_OVERFLOW);
119
- }
120
- return t << I | a;
121
- }, y = (a) => a & k, q = (a) => a >> I, z = (a) => b(
122
- a,
123
- P,
124
- "ComponentID must be a non-negative integer"
125
- ), W = (a) => b(
126
- a,
127
- P,
128
- "EventID must be a non-negative integer"
101
+ const A = 20, P = (1 << A) - 1, L = 31 - A, U = (1 << L) - 1, F = (a, t) => t << A | a, f = (a) => a & P, M = (a) => a >> A, z = (a) => b(
102
+ a
103
+ ), V = (a) => b(
104
+ a
129
105
  );
130
- class B {
106
+ class C {
131
107
  field_names;
132
108
  columns;
133
109
  reader;
@@ -157,12 +133,10 @@ class B {
157
133
  t[e].length = 0;
158
134
  }
159
135
  }
160
- const Q = (a) => b(
161
- a,
162
- P,
163
- "ArchetypeID must be a non-negative integer"
136
+ const G = (a) => b(
137
+ a
164
138
  );
165
- class K {
139
+ class W {
166
140
  id;
167
141
  mask;
168
142
  has_columns;
@@ -206,18 +180,7 @@ class K {
206
180
  }
207
181
  /** Get a single field's column. Valid data: indices 0..entity_count-1. */
208
182
  get_column(t, e) {
209
- const s = this.column_groups[t];
210
- if (process.env.NODE_ENV !== "production" && !s)
211
- throw new u(
212
- d.COMPONENT_NOT_REGISTERED,
213
- `Component ${t} not in archetype ${this.id}`
214
- );
215
- const n = s.layout.field_index[e];
216
- if (process.env.NODE_ENV !== "production" && n === void 0)
217
- throw new u(
218
- d.COMPONENT_NOT_REGISTERED,
219
- `Field "${e}" does not exist on component`
220
- );
183
+ const s = this.column_groups[t], n = s.layout.field_index[e];
221
184
  return s.columns[n];
222
185
  }
223
186
  /** Get all columns for a component as { fieldName: number[] }. */
@@ -274,7 +237,7 @@ class K {
274
237
  let s = -1;
275
238
  const n = this._column_ids;
276
239
  if (t !== e) {
277
- this.entity_ids[t] = this.entity_ids[e], s = y(this.entity_ids[t]);
240
+ this.entity_ids[t] = this.entity_ids[e], s = f(this.entity_ids[t]);
278
241
  for (let r = 0; r < n.length; r++) {
279
242
  const i = this.column_groups[n[r]];
280
243
  for (let _ = 0; _ < i.columns.length; _++)
@@ -297,7 +260,7 @@ class K {
297
260
  remove_entity_tag(t) {
298
261
  const e = this.length - 1;
299
262
  let s = -1;
300
- return t !== e && (this.entity_ids[t] = this.entity_ids[e], s = y(this.entity_ids[t])), this.entity_ids.pop(), this.length--, s;
263
+ return t !== e && (this.entity_ids[t] = this.entity_ids[e], s = f(this.entity_ids[t])), this.entity_ids.pop(), this.length--, s;
301
264
  }
302
265
  get_edge(t) {
303
266
  return this.edges[t];
@@ -306,12 +269,12 @@ class K {
306
269
  this.edges[t] = e;
307
270
  }
308
271
  }
309
- function F(a, t, e) {
272
+ function j(a, t, e) {
310
273
  const s = a.get(t);
311
274
  s !== void 0 ? s.push(e) : a.set(t, [e]);
312
275
  }
313
- const m = -1, V = Object.freeze(/* @__PURE__ */ Object.create(null));
314
- class J {
276
+ const y = -1, S = Object.freeze(/* @__PURE__ */ Object.create(null));
277
+ class $ {
315
278
  // --- Entity ID management ---
316
279
  // Generational slot allocator: entity_generations[index] holds the current
317
280
  // generation for that slot. Free indices are recycled via a stack.
@@ -354,17 +317,12 @@ class J {
354
317
  pending_remove_ids = [];
355
318
  pending_remove_defs = [];
356
319
  constructor() {
357
- this.empty_archetype_id = this.arch_get_or_create_from_mask(new N());
320
+ this.empty_archetype_id = this.arch_get_or_create_from_mask(new v());
358
321
  }
359
322
  // =======================================================
360
323
  // Archetype graph
361
324
  // =======================================================
362
325
  arch_get(t) {
363
- if (process.env.NODE_ENV !== "production" && (t < 0 || t >= this.archetypes.length))
364
- throw new u(
365
- d.ARCHETYPE_NOT_FOUND,
366
- `Archetype with ID ${t} not found`
367
- );
368
326
  return this.archetypes[t];
369
327
  }
370
328
  /**
@@ -378,7 +336,7 @@ class J {
378
336
  if (this.archetypes[s[h]].mask.equals(t))
379
337
  return s[h];
380
338
  }
381
- const n = Q(this.next_archetype_id++), r = [];
339
+ const n = G(this.next_archetype_id++), r = [];
382
340
  t.for_each((h) => {
383
341
  const o = h, c = this.component_metas[o];
384
342
  c && c.field_names.length > 0 && r.push({
@@ -387,8 +345,8 @@ class J {
387
345
  field_index: c.field_index
388
346
  });
389
347
  });
390
- const i = new K(n, t, r);
391
- this.archetypes.push(i), F(this.archetype_map, e, n), t.for_each((h) => {
348
+ const i = new W(n, t, r);
349
+ this.archetypes.push(i), j(this.archetype_map, e, n), t.for_each((h) => {
392
350
  const o = h;
393
351
  let c = this.component_index.get(o);
394
352
  c || (c = /* @__PURE__ */ new Set(), this.component_index.set(o, c)), c.add(n);
@@ -438,29 +396,25 @@ class J {
438
396
  create_entity() {
439
397
  let t, e;
440
398
  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++;
441
- const s = $(t, e);
442
- return this.entity_archetype[t] = this.empty_archetype_id, this.entity_row[t] = m, s;
399
+ const s = F(t, e);
400
+ return this.entity_archetype[t] = this.empty_archetype_id, this.entity_row[t] = y, s;
443
401
  }
444
402
  /** Immediately destroy an entity, removing it from its archetype. */
445
403
  destroy_entity(t) {
446
- if (!this.is_alive(t)) {
447
- if (process.env.NODE_ENV !== "production") throw new u(d.ENTITY_NOT_ALIVE);
404
+ if (!this.is_alive(t))
448
405
  return;
449
- }
450
- const e = y(t), s = this.entity_row[e];
451
- if (s !== m) {
406
+ const e = f(t), s = this.entity_row[e];
407
+ if (s !== y) {
452
408
  const i = this.arch_get(this.entity_archetype[e]).remove_entity(s);
453
409
  i !== -1 && (this.entity_row[i] = s);
454
410
  }
455
- this.entity_archetype[e] = m, this.entity_row[e] = m;
456
- const n = q(t);
457
- if (process.env.NODE_ENV !== "production" && n >= D)
458
- throw new u(d.EID_MAX_GEN_OVERFLOW);
459
- this.entity_generations[e] = n + 1 & D, this.entity_free_indices.push(e), this.entity_alive_count--;
411
+ this.entity_archetype[e] = y, this.entity_row[e] = y;
412
+ const n = M(t);
413
+ this.entity_generations[e] = n + 1 & U, this.entity_free_indices.push(e), this.entity_alive_count--;
460
414
  }
461
415
  is_alive(t) {
462
- const e = y(t);
463
- return e < this.entity_high_water && this.entity_generations[e] === q(t);
416
+ const e = f(t);
417
+ return e < this.entity_high_water && this.entity_generations[e] === M(t);
464
418
  }
465
419
  get entity_count() {
466
420
  return this.entity_alive_count;
@@ -469,8 +423,6 @@ class J {
469
423
  // Deferred destruction
470
424
  // =======================================================
471
425
  destroy_entity_deferred(t) {
472
- if (process.env.NODE_ENV !== "production" && !this.is_alive(t))
473
- throw new u(d.ENTITY_NOT_ALIVE);
474
426
  this.pending_destroy.push(t);
475
427
  }
476
428
  /** Flush all buffered entity destructions in batch. */
@@ -479,16 +431,14 @@ class J {
479
431
  if (t.length === 0) return;
480
432
  const e = this.entity_archetype, s = this.entity_row, n = this.entity_generations, r = this.archetypes, i = this.entity_high_water;
481
433
  for (let _ = 0; _ < t.length; _++) {
482
- const h = t[_], o = h & k, c = h >> I;
434
+ const h = t[_], o = h & P, c = h >> A;
483
435
  if (o >= i || n[o] !== c) continue;
484
436
  const l = s[o];
485
- if (l !== m) {
486
- const p = r[e[o]], g = p.has_columns ? p.remove_entity(l) : p.remove_entity_tag(l);
487
- g !== -1 && (s[g] = l);
437
+ if (l !== y) {
438
+ const u = r[e[o]], d = u.has_columns ? u.remove_entity(l) : u.remove_entity_tag(l);
439
+ d !== -1 && (s[d] = l);
488
440
  }
489
- if (e[o] = m, s[o] = m, process.env.NODE_ENV !== "production" && c >= D)
490
- throw new u(d.EID_MAX_GEN_OVERFLOW);
491
- n[o] = c + 1 & D, this.entity_free_indices.push(o), this.entity_alive_count--;
441
+ e[o] = y, s[o] = y, n[o] = c + 1 & U, this.entity_free_indices.push(o), this.entity_alive_count--;
492
442
  }
493
443
  t.length = 0;
494
444
  }
@@ -496,13 +446,9 @@ class J {
496
446
  return this.pending_destroy.length;
497
447
  }
498
448
  add_component_deferred(t, e, s) {
499
- if (process.env.NODE_ENV !== "production" && !this.is_alive(t))
500
- throw new u(d.ENTITY_NOT_ALIVE);
501
- this.pending_add_ids.push(t), this.pending_add_defs.push(e), this.pending_add_values.push(s ?? V);
449
+ this.pending_add_ids.push(t), this.pending_add_defs.push(e), this.pending_add_values.push(s ?? S);
502
450
  }
503
451
  remove_component_deferred(t, e) {
504
- if (process.env.NODE_ENV !== "production" && !this.is_alive(t))
505
- throw new u(d.ENTITY_NOT_ALIVE);
506
452
  this.pending_remove_ids.push(t), this.pending_remove_defs.push(e);
507
453
  }
508
454
  flush_structural() {
@@ -512,20 +458,20 @@ class J {
512
458
  _flush_adds() {
513
459
  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;
514
460
  for (let l = 0; l < n; l++) {
515
- const p = t[l], g = p & k, w = p >> I;
516
- if (g >= c || _[g] !== w) continue;
517
- const T = r[g], v = e[l], f = h[T];
518
- if (f.mask.has(v)) {
519
- o[v].field_names.length > 0 && f.write_fields(i[g], v, s[l]);
461
+ const u = t[l], d = u & P, p = u >> A;
462
+ if (d >= c || _[d] !== p) continue;
463
+ const x = r[d], m = e[l], g = h[x];
464
+ if (g.mask.has(m)) {
465
+ o[m].field_names.length > 0 && g.write_fields(i[d], m, s[l]);
520
466
  continue;
521
467
  }
522
- const O = this.arch_resolve_add(T, v), E = h[O], x = i[g], A = !E.has_columns && !f.has_columns, S = A ? E.add_entity_tag(p) : E.add_entity(p);
523
- if (x !== m) {
524
- A || E.copy_shared_from(f, x, S);
525
- const U = A ? f.remove_entity_tag(x) : f.remove_entity(x);
526
- U !== -1 && (i[U] = x);
468
+ const E = this.arch_resolve_add(x, m), w = h[E], T = i[d], k = !w.has_columns && !g.has_columns, N = k ? w.add_entity_tag(u) : w.add_entity(u);
469
+ if (T !== y) {
470
+ k || w.copy_shared_from(g, T, N);
471
+ const I = k ? g.remove_entity_tag(T) : g.remove_entity(T);
472
+ I !== -1 && (i[I] = T);
527
473
  }
528
- o[v].field_names.length > 0 && E.write_fields(S, v, s[l]), r[g] = O, i[g] = S;
474
+ o[m].field_names.length > 0 && w.write_fields(N, m, s[l]), r[d] = E, i[d] = N;
529
475
  }
530
476
  t.length = 0, e.length = 0, s.length = 0;
531
477
  }
@@ -533,14 +479,14 @@ class J {
533
479
  _flush_removes() {
534
480
  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;
535
481
  for (let o = 0; o < s; o++) {
536
- const c = t[o], l = c & k, p = c >> I;
537
- if (l >= h || i[l] !== p) continue;
538
- const g = n[l], w = e[o], T = _[g];
539
- if (!T.mask.has(w)) continue;
540
- const v = this.arch_resolve_remove(g, w), f = _[v], O = r[l], E = !f.has_columns && !T.has_columns, x = E ? f.add_entity_tag(c) : f.add_entity(c);
541
- E || f.copy_shared_from(T, O, x);
542
- const A = E ? T.remove_entity_tag(O) : T.remove_entity(O);
543
- A !== -1 && (r[A] = O), n[l] = v, r[l] = x;
482
+ const c = t[o], l = c & P, u = c >> A;
483
+ if (l >= h || i[l] !== u) continue;
484
+ const d = n[l], p = e[o], x = _[d];
485
+ if (!x.mask.has(p)) continue;
486
+ const m = this.arch_resolve_remove(d, p), g = _[m], E = r[l], w = !g.has_columns && !x.has_columns, T = w ? g.add_entity_tag(c) : g.add_entity(c);
487
+ w || g.copy_shared_from(x, E, T);
488
+ const k = w ? x.remove_entity_tag(E) : x.remove_entity(E);
489
+ k !== -1 && (r[k] = E), n[l] = m, r[l] = T;
544
490
  }
545
491
  t.length = 0, e.length = 0;
546
492
  }
@@ -557,11 +503,9 @@ class J {
557
503
  return this.component_metas.push({ field_names: s, field_index: n }), e;
558
504
  }
559
505
  add_component(t, e, s) {
560
- if (!this.is_alive(t)) {
561
- if (process.env.NODE_ENV !== "production") throw new u(d.ENTITY_NOT_ALIVE);
506
+ if (!this.is_alive(t))
562
507
  return;
563
- }
564
- const n = y(t), r = this.entity_archetype[n], i = this.arch_get(r);
508
+ const n = f(t), r = this.entity_archetype[n], i = this.arch_get(r);
565
509
  if (i.has_component(e)) {
566
510
  i.write_fields(
567
511
  this.entity_row[n],
@@ -574,7 +518,7 @@ class J {
574
518
  r,
575
519
  e
576
520
  ), h = this.arch_get(_), o = this.entity_row[n], c = h.add_entity(t);
577
- if (o !== m) {
521
+ if (o !== y) {
578
522
  h.copy_shared_from(i, o, c);
579
523
  const l = i.remove_entity(o);
580
524
  l !== -1 && (this.entity_row[l] = o);
@@ -587,11 +531,9 @@ class J {
587
531
  }
588
532
  /** Add multiple components in one transition (resolves final archetype, then moves once). */
589
533
  add_components(t, e) {
590
- if (!this.is_alive(t)) {
591
- if (process.env.NODE_ENV !== "production") throw new u(d.ENTITY_NOT_ALIVE);
534
+ if (!this.is_alive(t))
592
535
  return;
593
- }
594
- const s = y(t), n = this.entity_archetype[s];
536
+ const s = f(t), n = this.entity_archetype[s];
595
537
  let r = n;
596
538
  for (let i = 0; i < e.length; i++)
597
539
  r = this.arch_resolve_add(
@@ -600,7 +542,7 @@ class J {
600
542
  );
601
543
  if (r !== n) {
602
544
  const i = this.arch_get(n), _ = this.arch_get(r), h = this.entity_row[s], o = _.add_entity(t);
603
- if (h !== m) {
545
+ if (h !== y) {
604
546
  _.copy_shared_from(i, h, o);
605
547
  const c = i.remove_entity(h);
606
548
  c !== -1 && (this.entity_row[c] = h);
@@ -609,7 +551,7 @@ class J {
609
551
  _.write_fields(
610
552
  o,
611
553
  e[c].def,
612
- e[c].values ?? V
554
+ e[c].values ?? S
613
555
  );
614
556
  this.entity_archetype[s] = r, this.entity_row[s] = o;
615
557
  } else {
@@ -618,16 +560,14 @@ class J {
618
560
  i.write_fields(
619
561
  _,
620
562
  e[h].def,
621
- e[h].values ?? V
563
+ e[h].values ?? S
622
564
  );
623
565
  }
624
566
  }
625
567
  remove_component(t, e) {
626
- if (!this.is_alive(t)) {
627
- if (process.env.NODE_ENV !== "production") throw new u(d.ENTITY_NOT_ALIVE);
568
+ if (!this.is_alive(t))
628
569
  return;
629
- }
630
- const s = y(t), n = this.entity_archetype[s], r = this.arch_get(n);
570
+ const s = f(t), n = this.entity_archetype[s], r = this.arch_get(n);
631
571
  if (!r.has_component(e)) return;
632
572
  const i = this.arch_resolve_remove(
633
573
  n,
@@ -639,11 +579,9 @@ class J {
639
579
  }
640
580
  /** Remove multiple components in one transition (resolves final archetype, then moves once). */
641
581
  remove_components(t, e) {
642
- if (!this.is_alive(t)) {
643
- if (process.env.NODE_ENV !== "production") throw new u(d.ENTITY_NOT_ALIVE);
582
+ if (!this.is_alive(t))
644
583
  return;
645
- }
646
- const s = y(t), n = this.entity_archetype[s];
584
+ const s = f(t), n = this.entity_archetype[s];
647
585
  let r = n;
648
586
  for (let l = 0; l < e.length; l++)
649
587
  r = this.arch_resolve_remove(
@@ -657,11 +595,9 @@ class J {
657
595
  c !== -1 && (this.entity_row[c] = h), this.entity_archetype[s] = r, this.entity_row[s] = o;
658
596
  }
659
597
  has_component(t, e) {
660
- if (!this.is_alive(t)) {
661
- if (process.env.NODE_ENV !== "production") throw new u(d.ENTITY_NOT_ALIVE);
598
+ if (!this.is_alive(t))
662
599
  return !1;
663
- }
664
- const s = y(t);
600
+ const s = f(t);
665
601
  return this.arch_get(
666
602
  this.entity_archetype[s]
667
603
  ).has_component(e);
@@ -671,11 +607,11 @@ class J {
671
607
  // =======================================================
672
608
  get_entity_archetype(t) {
673
609
  return this.arch_get(
674
- this.entity_archetype[y(t)]
610
+ this.entity_archetype[f(t)]
675
611
  );
676
612
  }
677
613
  get_entity_row(t) {
678
- return this.entity_row[y(t)];
614
+ return this.entity_row[f(t)];
679
615
  }
680
616
  // =======================================================
681
617
  // Query support
@@ -703,14 +639,14 @@ class J {
703
639
  if (c === 0) continue;
704
640
  const l = o << 5;
705
641
  for (; c !== 0; ) {
706
- const p = c & -c >>> 0, g = l + (31 - Math.clz32(p));
707
- c ^= p;
708
- const w = this.component_index.get(g);
709
- if (!w || w.size === 0) {
642
+ const u = c & -c >>> 0, d = l + (31 - Math.clz32(u));
643
+ c ^= u;
644
+ const p = this.component_index.get(d);
645
+ if (!p || p.size === 0) {
710
646
  _ = !0;
711
647
  break;
712
648
  }
713
- (!i || w.size < i.size) && (i = w);
649
+ (!i || p.size < i.size) && (i = p);
714
650
  }
715
651
  if (_) break;
716
652
  }
@@ -746,7 +682,7 @@ class J {
746
682
  // Event channels
747
683
  // =======================================================
748
684
  register_event(t) {
749
- const e = W(this.event_count++), s = new B(t);
685
+ const e = V(this.event_count++), s = new C(t);
750
686
  return this.event_channels.push(s), e;
751
687
  }
752
688
  emit_event(t, e) {
@@ -764,39 +700,33 @@ class J {
764
700
  t[e].clear();
765
701
  }
766
702
  }
767
- var Z = /* @__PURE__ */ ((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))(Z || {});
768
- const M = [
703
+ var B = /* @__PURE__ */ ((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))(B || {});
704
+ const D = [
769
705
  "PRE_STARTUP",
770
706
  "STARTUP",
771
707
  "POST_STARTUP"
772
708
  /* POST_STARTUP */
773
- ], L = [
709
+ ], O = [
774
710
  "PRE_UPDATE",
775
711
  "UPDATE",
776
712
  "POST_UPDATE"
777
713
  /* POST_UPDATE */
778
714
  ];
779
- class H {
715
+ class Q {
780
716
  label_systems = /* @__PURE__ */ new Map();
781
717
  sorted_cache = /* @__PURE__ */ new Map();
782
718
  system_index = /* @__PURE__ */ new Map();
783
719
  next_insertion_order = 0;
784
720
  constructor() {
785
- for (let t = 0; t < M.length; t++)
786
- this.label_systems.set(M[t], []);
721
+ for (let t = 0; t < D.length; t++)
722
+ this.label_systems.set(D[t], []);
787
723
  this.label_systems.set("FIXED_UPDATE", []);
788
- for (let t = 0; t < L.length; t++)
789
- this.label_systems.set(L[t], []);
724
+ for (let t = 0; t < O.length; t++)
725
+ this.label_systems.set(O[t], []);
790
726
  }
791
727
  add_systems(t, ...e) {
792
728
  for (const s of e) {
793
- const n = "system" in s ? s.system : s, r = "system" in s ? s.ordering : void 0;
794
- if (process.env.NODE_ENV !== "production" && this.system_index.has(n))
795
- throw new u(
796
- d.DUPLICATE_SYSTEM,
797
- `System ${n.id} is already scheduled`
798
- );
799
- const i = {
729
+ const n = "system" in s ? s.system : s, r = "system" in s ? s.ordering : void 0, i = {
800
730
  descriptor: n,
801
731
  insertion_order: this.next_insertion_order++,
802
732
  before: new Set(r?.before ?? []),
@@ -818,11 +748,11 @@ class H {
818
748
  this.system_index.delete(t), this.sorted_cache.delete(e);
819
749
  }
820
750
  run_startup(t) {
821
- for (const e of M)
751
+ for (const e of D)
822
752
  this.run_label(e, t, 0);
823
753
  }
824
754
  run_update(t, e) {
825
- for (const s of L)
755
+ for (const s of O)
826
756
  this.run_label(s, t, e);
827
757
  }
828
758
  run_fixed_update(t, e) {
@@ -892,16 +822,16 @@ class H {
892
822
  }
893
823
  if (h.length !== t.length) {
894
824
  const o = new Set(h), c = t.filter((l) => !o.has(l.descriptor)).map((l) => `system_${l.descriptor.id}`);
895
- throw new u(
896
- d.CIRCULAR_SYSTEM_DEPENDENCY,
825
+ throw new Y(
826
+ q.CIRCULAR_SYSTEM_DEPENDENCY,
897
827
  `Circular system dependency detected in ${e}: [${c.join(", ")}]`
898
828
  );
899
829
  }
900
830
  return h;
901
831
  }
902
832
  }
903
- const tt = Object.freeze(/* @__PURE__ */ Object.create(null));
904
- class et {
833
+ const K = Object.freeze(/* @__PURE__ */ Object.create(null));
834
+ class J {
905
835
  _archetypes;
906
836
  _defs;
907
837
  _resolver;
@@ -964,7 +894,7 @@ class et {
964
894
  }
965
895
  /** Exclude archetypes that have any of these components. */
966
896
  not(...t) {
967
- const e = this._exclude ? this._exclude.copy() : new N();
897
+ const e = this._exclude ? this._exclude.copy() : new v();
968
898
  for (let s = 0; s < t.length; s++) e.set(t[s]);
969
899
  return this._resolver._resolve_query(
970
900
  this._include,
@@ -975,7 +905,7 @@ class et {
975
905
  }
976
906
  /** Require at least one of these components. */
977
907
  or(...t) {
978
- const e = this._any_of ? this._any_of.copy() : new N();
908
+ const e = this._any_of ? this._any_of.copy() : new v();
979
909
  for (let s = 0; s < t.length; s++) e.set(t[s]);
980
910
  return this._resolver._resolve_query(
981
911
  this._include,
@@ -985,17 +915,17 @@ class et {
985
915
  );
986
916
  }
987
917
  }
988
- class st {
918
+ class Z {
989
919
  constructor(t) {
990
920
  this._resolver = t;
991
921
  }
992
922
  every(...t) {
993
- const e = new N();
923
+ const e = new v();
994
924
  for (let s = 0; s < t.length; s++) e.set(t[s]);
995
925
  return this._resolver._resolve_query(e, null, null, t);
996
926
  }
997
927
  }
998
- class nt {
928
+ class H {
999
929
  store;
1000
930
  constructor(t) {
1001
931
  this.store = t;
@@ -1019,7 +949,7 @@ class nt {
1019
949
  this.store.flush_destroyed();
1020
950
  }
1021
951
  add_component(t, e, s) {
1022
- return this.store.add_component_deferred(t, e, s ?? tt), this;
952
+ return this.store.add_component_deferred(t, e, s ?? K), this;
1023
953
  }
1024
954
  remove_component(t, e) {
1025
955
  return this.store.remove_component_deferred(t, e), this;
@@ -1035,12 +965,10 @@ class nt {
1035
965
  return this.store.get_event_reader(t);
1036
966
  }
1037
967
  }
1038
- const rt = (a) => b(
1039
- a,
1040
- P,
1041
- "SystemID must be a non-negative integer"
1042
- ), it = Object.freeze(/* @__PURE__ */ Object.create(null));
1043
- class ot {
968
+ const tt = (a) => b(
969
+ a
970
+ ), et = Object.freeze(/* @__PURE__ */ Object.create(null));
971
+ class st {
1044
972
  store;
1045
973
  schedule;
1046
974
  ctx;
@@ -1054,9 +982,9 @@ class ot {
1054
982
  // Multiple queries can share the same hash (collision), so each bucket is an array.
1055
983
  query_cache = /* @__PURE__ */ new Map();
1056
984
  // Reusable BitSet for building query masks — avoids allocation per query() call
1057
- scratch_mask = new N();
985
+ scratch_mask = new v();
1058
986
  constructor(t) {
1059
- this.store = new J(), this.schedule = new H(), this.ctx = new nt(this.store), this._fixed_timestep = t?.fixed_timestep ?? 1 / 60, this._max_fixed_steps = t?.max_fixed_steps ?? 4;
987
+ this.store = new $(), this.schedule = new Q(), this.ctx = new H(this.store), this._fixed_timestep = t?.fixed_timestep ?? 1 / 60, this._max_fixed_steps = t?.max_fixed_steps ?? 4;
1060
988
  }
1061
989
  get fixed_timestep() {
1062
990
  return this._fixed_timestep;
@@ -1092,7 +1020,7 @@ class ot {
1092
1020
  return this.store.entity_count;
1093
1021
  }
1094
1022
  add_component(t, e, s) {
1095
- this.store.add_component(t, e, s ?? it);
1023
+ this.store.add_component(t, e, s ?? et);
1096
1024
  }
1097
1025
  add_components(t, e) {
1098
1026
  this.store.add_components(t, e);
@@ -1128,7 +1056,7 @@ class ot {
1128
1056
  t,
1129
1057
  e ?? void 0,
1130
1058
  s ?? void 0
1131
- ), l = new et(
1059
+ ), l = new J(
1132
1060
  c,
1133
1061
  n,
1134
1062
  this,
@@ -1136,7 +1064,7 @@ class ot {
1136
1064
  e?.copy() ?? null,
1137
1065
  s?.copy() ?? null
1138
1066
  );
1139
- return F(this.query_cache, h, {
1067
+ return j(this.query_cache, h, {
1140
1068
  include_mask: t.copy(),
1141
1069
  exclude_mask: e?.copy() ?? null,
1142
1070
  any_of_mask: s?.copy() ?? null,
@@ -1155,11 +1083,11 @@ class ot {
1155
1083
  register_system(t, e) {
1156
1084
  let s;
1157
1085
  if (typeof t == "function") {
1158
- const i = e(new st(this)), _ = this.ctx;
1086
+ const i = e(new Z(this)), _ = this.ctx;
1159
1087
  s = { fn: (h, o) => t(i, _, o) };
1160
1088
  } else
1161
1089
  s = t;
1162
- const n = rt(this.next_system_id++), r = Object.freeze(
1090
+ const n = tt(this.next_system_id++), r = Object.freeze(
1163
1091
  Object.assign({ id: n }, s)
1164
1092
  );
1165
1093
  return this.systems.add(r), r;
@@ -1197,9 +1125,9 @@ class ot {
1197
1125
  }
1198
1126
  }
1199
1127
  export {
1200
- et as Query,
1201
- st as QueryBuilder,
1202
- Z as SCHEDULE,
1203
- nt as SystemContext,
1204
- ot as World
1128
+ J as Query,
1129
+ Z as QueryBuilder,
1130
+ B as SCHEDULE,
1131
+ H as SystemContext,
1132
+ st as World
1205
1133
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oasys/oecs",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Archetype-based Entity Component System",
5
5
  "type": "module",
6
6
  "exports": {