@oasys/oecs 0.1.2 → 0.2.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/README.md +172 -164
- package/dist/archetype.d.ts +50 -13
- package/dist/archetype.d.ts.map +1 -1
- package/dist/component.d.ts +27 -10
- package/dist/component.d.ts.map +1 -1
- package/dist/ecs.d.ts +104 -0
- package/dist/ecs.d.ts.map +1 -0
- package/dist/entity.d.ts.map +1 -1
- package/dist/event.d.ts +2 -2
- package/dist/event.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +8 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +673 -321
- package/dist/query.d.ts +28 -31
- package/dist/query.d.ts.map +1 -1
- package/dist/ref.d.ts +19 -0
- package/dist/ref.d.ts.map +1 -0
- package/dist/resource.d.ts +23 -0
- package/dist/resource.d.ts.map +1 -0
- package/dist/schedule.d.ts +3 -3
- package/dist/schedule.d.ts.map +1 -1
- package/dist/store.d.ts +48 -29
- package/dist/store.d.ts.map +1 -1
- package/dist/system.d.ts +2 -2
- package/dist/system.d.ts.map +1 -1
- package/dist/type_primitives/assertions.d.ts +1 -0
- package/dist/type_primitives/assertions.d.ts.map +1 -1
- package/dist/type_primitives/bitset/bitset.d.ts +2 -2
- package/dist/type_primitives/bitset/bitset.d.ts.map +1 -1
- package/dist/type_primitives/sparse_map/sparse_map.d.ts.map +1 -1
- package/dist/type_primitives/sparse_set/sparse_set.d.ts.map +1 -1
- package/dist/type_primitives/typed_arrays/typed_arrays.d.ts +9 -0
- package/dist/type_primitives/typed_arrays/typed_arrays.d.ts.map +1 -1
- package/dist/utils/constants.d.ts +20 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/error.d.ts +2 -9
- package/dist/utils/error.d.ts.map +1 -1
- package/package.json +3 -2
- package/dist/world.d.ts +0 -73
- package/dist/world.d.ts.map +0 -1
package/dist/ecs.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { SCHEDULE, SystemEntry } from './schedule';
|
|
2
|
+
import { Archetype } from './archetype';
|
|
3
|
+
import { SystemContext, Query, QueryBuilder, QueryResolver } from './query';
|
|
4
|
+
import { EntityID } from './entity';
|
|
5
|
+
import { ComponentDef, ComponentSchema, ComponentFields, FieldValues } from './component';
|
|
6
|
+
import { EventDef } from './event';
|
|
7
|
+
import { ResourceDef, ResourceReader } from './resource';
|
|
8
|
+
import { SystemFn, SystemConfig, SystemDescriptor } from './system';
|
|
9
|
+
import { BitSet, TypedArrayTag } from './type_primitives';
|
|
10
|
+
export interface WorldOptions {
|
|
11
|
+
fixed_timestep?: number;
|
|
12
|
+
max_fixed_steps?: number;
|
|
13
|
+
initial_capacity?: number;
|
|
14
|
+
}
|
|
15
|
+
export declare class ECS implements QueryResolver {
|
|
16
|
+
private readonly store;
|
|
17
|
+
private readonly schedule;
|
|
18
|
+
private readonly ctx;
|
|
19
|
+
private readonly systems;
|
|
20
|
+
private next_system_id;
|
|
21
|
+
private _fixed_timestep;
|
|
22
|
+
private _accumulator;
|
|
23
|
+
private _max_fixed_steps;
|
|
24
|
+
private readonly query_cache;
|
|
25
|
+
private readonly scratch_mask;
|
|
26
|
+
constructor(options?: WorldOptions);
|
|
27
|
+
get fixed_timestep(): number;
|
|
28
|
+
set fixed_timestep(value: number);
|
|
29
|
+
get fixed_alpha(): number;
|
|
30
|
+
register_component<S extends Record<string, TypedArrayTag>>(schema: S): ComponentDef<S>;
|
|
31
|
+
register_component<const F extends readonly string[], T extends TypedArrayTag = "f64">(fields: F, type?: T): ComponentDef<{
|
|
32
|
+
readonly [K in F[number]]: T;
|
|
33
|
+
}>;
|
|
34
|
+
register_tag(): ComponentDef<Record<string, never>>;
|
|
35
|
+
register_event<F extends readonly string[]>(fields: F): EventDef<F>;
|
|
36
|
+
register_signal(): EventDef<readonly []>;
|
|
37
|
+
register_resource<F extends readonly string[]>(fields: F, initial: {
|
|
38
|
+
readonly [K in F[number]]: number;
|
|
39
|
+
}): ResourceDef<F>;
|
|
40
|
+
resource<F extends ComponentFields>(def: ResourceDef<F>): ResourceReader<F>;
|
|
41
|
+
set_resource<F extends ComponentFields>(def: ResourceDef<F>, values: {
|
|
42
|
+
readonly [K in F[number]]: number;
|
|
43
|
+
}): void;
|
|
44
|
+
create_entity(): EntityID;
|
|
45
|
+
destroy_entity_deferred(id: EntityID): void;
|
|
46
|
+
is_alive(id: EntityID): boolean;
|
|
47
|
+
get entity_count(): number;
|
|
48
|
+
add_component(entity_id: EntityID, def: ComponentDef<Record<string, never>>): this;
|
|
49
|
+
add_component<S extends ComponentSchema>(entity_id: EntityID, def: ComponentDef<S>, values: FieldValues<S>): this;
|
|
50
|
+
add_components(entity_id: EntityID, entries: {
|
|
51
|
+
def: ComponentDef;
|
|
52
|
+
values?: Record<string, number>;
|
|
53
|
+
}[]): void;
|
|
54
|
+
remove_component(entity_id: EntityID, def: ComponentDef): this;
|
|
55
|
+
remove_components(entity_id: EntityID, ...defs: ComponentDef[]): void;
|
|
56
|
+
has_component(entity_id: EntityID, def: ComponentDef): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Bulk add a component to ALL entities in the given archetype.
|
|
59
|
+
* O(columns) via TypedArray.set() instead of O(N×columns).
|
|
60
|
+
*/
|
|
61
|
+
batch_add_component(src_arch: Archetype, def: ComponentDef<Record<string, never>>): void;
|
|
62
|
+
batch_add_component<S extends ComponentSchema>(src_arch: Archetype, def: ComponentDef<S>, values: FieldValues<S>): void;
|
|
63
|
+
/**
|
|
64
|
+
* Bulk remove a component from ALL entities in the given archetype.
|
|
65
|
+
* O(columns) via TypedArray.set() instead of O(N×columns).
|
|
66
|
+
*/
|
|
67
|
+
batch_remove_component(src_arch: Archetype, def: ComponentDef): void;
|
|
68
|
+
get_field<S extends ComponentSchema>(entity_id: EntityID, def: ComponentDef<S>, field: string & keyof S): number;
|
|
69
|
+
set_field<S extends ComponentSchema>(entity_id: EntityID, def: ComponentDef<S>, field: string & keyof S, value: number): void;
|
|
70
|
+
emit(def: EventDef<readonly []>): void;
|
|
71
|
+
emit<F extends ComponentFields>(def: EventDef<F>, values: {
|
|
72
|
+
readonly [K in F[number]]: number;
|
|
73
|
+
}): void;
|
|
74
|
+
query<T extends ComponentDef[]>(...defs: T): Query<T>;
|
|
75
|
+
/** QueryResolver implementation — creates or retrieves a cached Query. */
|
|
76
|
+
_resolve_query(include: BitSet, exclude: BitSet | null, any_of: BitSet | null, defs: readonly ComponentDef[]): Query<any>;
|
|
77
|
+
private _find_cached;
|
|
78
|
+
/**
|
|
79
|
+
* Register a system.
|
|
80
|
+
*
|
|
81
|
+
* // Bare function (no query, no lifecycle hooks)
|
|
82
|
+
* world.register_system((ctx, dt) => { ... });
|
|
83
|
+
*
|
|
84
|
+
* // Function + query builder
|
|
85
|
+
* world.register_system(
|
|
86
|
+
* (q, ctx, dt) => { for (const arch of q) { ... } },
|
|
87
|
+
* (qb) => qb.every(Pos, Vel),
|
|
88
|
+
* );
|
|
89
|
+
*
|
|
90
|
+
* // Full config (for lifecycle hooks)
|
|
91
|
+
* world.register_system({ fn(ctx, dt) { ... } });
|
|
92
|
+
*/
|
|
93
|
+
register_system(fn: SystemFn): SystemDescriptor;
|
|
94
|
+
register_system<Defs extends readonly ComponentDef[]>(fn: (q: Query<Defs>, ctx: SystemContext, dt: number) => void, query_fn: (qb: QueryBuilder) => Query<Defs>): SystemDescriptor;
|
|
95
|
+
register_system(config: SystemConfig): SystemDescriptor;
|
|
96
|
+
add_systems(label: SCHEDULE, ...entries: (SystemDescriptor | SystemEntry)[]): this;
|
|
97
|
+
remove_system(system: SystemDescriptor): void;
|
|
98
|
+
get system_count(): number;
|
|
99
|
+
startup(): void;
|
|
100
|
+
update(dt: number): void;
|
|
101
|
+
flush(): void;
|
|
102
|
+
dispose(): void;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=ecs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ecs.d.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,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,MAAM,KAAK,IAAI,EAC5D,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,KAAK,KAAK,CAAC,IAAI,CAAC,GAC1C,gBAAgB;IACZ,eAAe,CAAC,MAAM,EAAE,YAAY,GAAG,gBAAgB;IAkCvD,WAAW,CAChB,KAAK,EAAE,QAAQ,EACf,GAAG,OAAO,EAAE,CAAC,gBAAgB,GAAG,WAAW,CAAC,EAAE,GAC7C,IAAI;IAKA,aAAa,CAAC,MAAM,EAAE,gBAAgB,GAAG,IAAI;IAMpD,IAAW,YAAY,IAAI,MAAM,CAEhC;IAEM,OAAO,IAAI,IAAI;IAOf,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAiBxB,KAAK,IAAI,IAAI;IAIb,OAAO,IAAI,IAAI;CAQvB"}
|
package/dist/entity.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entity.d.ts","sourceRoot":"","sources":["../src/entity.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;KAkBK;AAEL,OAAO,EAAE,KAAK,EAAe,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"entity.d.ts","sourceRoot":"","sources":["../src/entity.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;KAkBK;AAEL,OAAO,EAAE,KAAK,EAAe,MAAM,iBAAiB,CAAC;AAIrD,MAAM,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAElD,eAAO,MAAM,UAAU,KAAK,CAAC;AAC7B,eAAO,MAAM,UAAU,QAAwB,CAAC;AAChD,eAAO,MAAM,SAAS,QAAa,CAAC;AACpC,eAAO,MAAM,eAAe,QAAiC,CAAC;AAC9D,eAAO,MAAM,cAAc,QAA6B,CAAC;AAEzD,eAAO,MAAM,gBAAgB,UACpB,MAAM,cACD,MAAM,KACjB,QAWF,CAAC;AAEF,eAAO,MAAM,gBAAgB,OAAQ,QAAQ,KAAG,MAAyB,CAAC;AAE1E,eAAO,MAAM,qBAAqB,OAAQ,QAAQ,KAAG,MACvB,CAAC"}
|
package/dist/event.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Brand } from './type_primitives';
|
|
2
|
-
import { ComponentFields,
|
|
2
|
+
import { ComponentFields, ColumnsForFields } from './component';
|
|
3
3
|
export type EventID = Brand<number, "event_id">;
|
|
4
4
|
export declare const as_event_id: (value: number) => EventID;
|
|
5
5
|
declare const __event_schema: unique symbol;
|
|
@@ -9,7 +9,7 @@ export type EventDef<F extends ComponentFields = ComponentFields> = EventID & {
|
|
|
9
9
|
/** Reader view over an event channel's SoA columns. */
|
|
10
10
|
export type EventReader<F extends ComponentFields> = {
|
|
11
11
|
length: number;
|
|
12
|
-
} &
|
|
12
|
+
} & ColumnsForFields<F>;
|
|
13
13
|
export declare class EventChannel {
|
|
14
14
|
readonly field_names: string[];
|
|
15
15
|
readonly columns: number[][];
|
package/dist/event.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../src/event.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;KAyBK;AAEL,OAAO,EACL,KAAK,EAGN,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAErE,MAAM,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAChD,eAAO,MAAM,WAAW,UAAW,MAAM,YAKtC,CAAC;AAGJ,OAAO,CAAC,MAAM,cAAc,EAAE,OAAO,MAAM,CAAC;AAE5C,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,eAAe,GAAG,eAAe,
|
|
1
|
+
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../src/event.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;KAyBK;AAEL,OAAO,EACL,KAAK,EAGN,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAErE,MAAM,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAChD,eAAO,MAAM,WAAW,UAAW,MAAM,YAKtC,CAAC;AAGJ,OAAO,CAAC,MAAM,cAAc,EAAE,OAAO,MAAM,CAAC;AAE5C,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,eAAe,GAAG,eAAe,IAAI,OAAO,GAAG;IAC5E,QAAQ,CAAC,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;CAC9B,CAAC;AAEF,uDAAuD;AACvD,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,eAAe,IAAI;IACnD,MAAM,EAAE,MAAM,CAAC;CAChB,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;AAExB,qBAAa,YAAY;IACvB,SAAgB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtC,SAAgB,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC;IAEpC,SAAgB,MAAM,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;gBAE7B,WAAW,EAAE,MAAM,EAAE;IAgB1B,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IASjD,wCAAwC;IACjC,WAAW,IAAI,IAAI;IAInB,KAAK,IAAI,IAAI;CAOrB"}
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
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;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class tt 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 z=(l=>(l.EID_MAX_INDEX_OVERFLOW="EID_MAX_INDEX_OVERFLOW",l.EID_MAX_GEN_OVERFLOW="EID_MAX_GEN_OVERFLOW",l.COMPONENT_NOT_REGISTERED="COMPONENT_NOT_REGISTERED",l.ENTITY_NOT_ALIVE="ENTITY_NOT_ALIVE",l.CIRCULAR_SYSTEM_DEPENDENCY="CIRCULAR_SYSTEM_DEPENDENCY",l.DUPLICATE_SYSTEM="DUPLICATE_SYSTEM",l.ARCHETYPE_NOT_FOUND="ARCHETYPE_NOT_FOUND",l.RESOURCE_NOT_REGISTERED="RESOURCE_NOT_REGISTERED",l))(z||{});class et extends tt{constructor(t,e,s){super(e??t,!0,s),this.category=t}}function N(l,t,e){return l}const v=-1,y=-1,D=Object.freeze(Object.create(null)),k=5,I=31,st=2166136261,nt=16777619,rt=2654435769,it=1367130551,x=16,G=2,V=1024,R=0,j=0,ot=31,_t=1/60,ct=4,ht=0,lt=4;class b{_words;constructor(t){this._words=t??new Array(lt).fill(0)}has(t){const e=t>>>k;return e>=this._words.length?!1:(this._words[e]&1<<(t&I))!==0}set(t){const e=t>>>k;e>=this._words.length&&this.grow(e+1),this._words[e]|=1<<(t&I)}clear(t){const e=t>>>k;e>=this._words.length||(this._words[e]&=~(1<<(t&I)))}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 b(this._words.slice())}copy_with_set(t){const e=t>>>k,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&I),new b(r)}copy_with_clear(t){const e=this._words.slice(),s=t>>>k;return s<e.length&&(e[s]&=~(1<<(t&I))),new b(e)}hash(){let t=st;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,nt);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<<k;for(;n!==0;){const i=n&-n>>>0,_=I-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}}class A{constructor(t,e=x){this._ctor=t,this._buf=new t(e)}_buf;_len=0;get length(){return this._len}push(t){this._len>=this._buf.length&&this._grow(),this._buf[this._len++]=t}pop(){return this._buf[--this._len]}get(t){return this._buf[t]}set_at(t,e){this._buf[t]=e}swap_remove(t){const e=this._buf[t];return this._buf[t]=this._buf[--this._len],e}clear(){this._len=0}get buf(){return this._buf}view(){return this._buf.subarray(0,this._len)}[Symbol.iterator](){let t=0;const e=this._buf,s=this._len;return{next(){return t<s?{value:e[t++],done:!1}:{value:0,done:!0}}}}ensure_capacity(t){if(t<=this._buf.length)return;let e=this._buf.length||1;for(;e<t;)e*=G;const s=new this._ctor(e);s.set(this._buf.subarray(0,this._len)),this._buf=s}bulk_append(t,e,s){this.ensure_capacity(this._len+s),this._buf.set(t.subarray(e,e+s),this._len),this._len+=s}bulk_append_zeroes(t){this.ensure_capacity(this._len+t),this._buf.fill(0,this._len,this._len+t),this._len+=t}_grow(){const t=new this._ctor(this._buf.length*G);t.set(this._buf),this._buf=t}}class at extends A{constructor(t=x){super(Float32Array,t)}}class dt extends A{constructor(t=x){super(Float64Array,t)}}class ut extends A{constructor(t=x){super(Int8Array,t)}}class ft extends A{constructor(t=x){super(Int16Array,t)}}class gt extends A{constructor(t=x){super(Int32Array,t)}}class mt extends A{constructor(t=x){super(Uint8Array,t)}}class yt extends A{constructor(t=x){super(Uint16Array,t)}}class B extends A{constructor(t=x){super(Uint32Array,t)}}const pt={f32:at,f64:dt,i8:ut,i16:ft,i32:gt,u8:mt,u16:yt,u32:B},S=20,F=(1<<S)-1,wt=ot-S,X=(1<<wt)-1,vt=(l,t)=>t<<S|l,m=l=>l&F,Y=l=>l>>S,bt=l=>N(l),xt=l=>N(l);class Tt{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 At=l=>N(l);class Et{field_names;field_index;columns;reader;constructor(t,e){this.field_names=t,this.field_index=Object.create(null),this.columns=[];for(let r=0;r<t.length;r++)this.field_index[t[r]]=r,this.columns.push([e[t[r]]??0]);const s=Object.create(null),n=this.columns;for(let r=0;r<t.length;r++){const i=n[r];Object.defineProperty(s,t[r],{get(){return i[R]},enumerable:!0})}this.reader=s}write(t){const e=this.field_names,s=this.columns;for(let n=0;n<e.length;n++)e[n]in t&&(s[n][R]=t[e[n]])}read_field(t){return this.columns[t][R]}write_field(t,e){this.columns[t][R]=e}}const kt=l=>N(l);class It{id;mask;has_columns;_entity_ids;length=0;edges=[];_flat_columns=[];_col_offset=[];_field_count=[];_field_index=[];_field_names=[];column_groups=[];_column_ids=[];constructor(t,e,s,n=V){if(this.id=t,this.mask=e,this._entity_ids=new B(n),s){let r=0;for(let i=0;i<s.length;i++){const _=s[i],c=_.component_id,o=new Array(_.field_names.length);this._col_offset[c]=r,this._field_count[c]=_.field_names.length,this._field_index[c]=_.field_index,this._field_names[c]=_.field_names;for(let h=0;h<_.field_names.length;h++){const a=new pt[_.field_types[h]](n);o[h]=a,this._flat_columns[r++]=a}this.column_groups[c]={layout:_,columns:o},this._column_ids.push(c)}}this.has_columns=this._column_ids.length>0}get entity_count(){return this.length}get entity_ids(){return this._entity_ids.buf}get entity_list(){return this._entity_ids.view()}has_component(t){return this.mask.has(t)}matches(t){return this.mask.contains(t)}get_column(t,e){const s=t,n=this._field_index[s][e];return this._flat_columns[this._col_offset[s]+n].buf}write_fields(t,e,s){const n=e,r=this._col_offset[n];if(r===void 0)return;const i=this._field_names[n],_=this._flat_columns;for(let c=0;c<i.length;c++)_[r+c].buf[t]=s[i[c]]}write_fields_positional(t,e,s){const n=e,r=this._col_offset[n];if(r===void 0)return;const i=this._flat_columns;for(let _=0;_<s.length;_++)i[r+_].buf[t]=s[_]}read_field(t,e,s){const n=e,r=this._col_offset[n];if(r===void 0)return NaN;const i=this._field_index[n][s];return i===void 0?NaN:this._flat_columns[r+i].buf[t]}copy_shared_from(t,e,s){const n=t._col_offset,r=t._field_count,i=t._flat_columns,_=this._flat_columns,c=this._column_ids;for(let o=0;o<c.length;o++){const h=c[o],a=n[h];if(a===void 0)continue;const u=this._col_offset[h],d=r[h];for(let g=0;g<d;g++)_[u+g].buf[s]=i[a+g].buf[e]}}add_entity(t){const e=this.length;this._entity_ids.push(t);const s=this._flat_columns;for(let n=0;n<s.length;n++)s[n].push(0);return this.length++,e}remove_entity(t){const e=this.length-1;let s=y;const n=this._flat_columns,r=this._entity_ids.buf;if(t!==e){r[t]=r[e],s=m(r[t]);for(let i=0;i<n.length;i++)n[i].swap_remove(t)}else for(let i=0;i<n.length;i++)n[i].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=y;const n=this._entity_ids.buf;return t!==e&&(n[t]=n[e],s=m(n[t])),this._entity_ids.pop(),this.length--,s}move_entity_from(t,e,s,n){const r=this.length;this._entity_ids.push(s);const i=this._flat_columns,_=t._flat_columns;for(let o=0;o<i.length;o++){const h=n[o];i[o].push(h>=0?_[h].buf[e]:0)}this.length++;const c=t.has_columns?t.remove_entity(e):t.remove_entity_tag(e);f[0]=r,f[1]=c}move_entity_from_tag(t,e,s){const n=this.length;this._entity_ids.push(s),this.length++;const r=t.remove_entity_tag(e);f[0]=n,f[1]=r}bulk_move_all_from(t,e){const s=t.length;if(s===0)return this.length;const n=this.length,r=this._flat_columns,i=t._flat_columns;this._entity_ids.bulk_append(t._entity_ids.buf,0,s);for(let _=0;_<r.length;_++){const c=e[_];c>=0?r[_].bulk_append(i[c].buf,0,s):r[_].bulk_append_zeroes(s)}this.length+=s,t.length=0,t._entity_ids.clear();for(let _=0;_<i.length;_++)i[_].clear();return n}get_edge(t){return this.edges[t]}set_edge(t,e){this.edges[t]=e}}const f=[0,y];function M(l,t){const e=t._flat_columns,s=new Int16Array(e.length),n=t._column_ids,r=l._col_offset,i=t._col_offset,_=t._field_count;for(let c=0;c<n.length;c++){const o=n[c],h=i[o],a=_[o],u=r[o];if(u!==void 0)for(let d=0;d<a;d++)s[h+d]=u+d;else for(let d=0;d<a;d++)s[h+d]=-1}return s}function Q(l,t,e){const s=l.get(t);s!==void 0?s.push(e):l.set(t,[e])}class St{entity_generations=[];entity_high_water=0;entity_free_indices=[];entity_alive_count=0;component_metas=[];component_count=0;event_channels=[];event_count=0;resource_channels=[];resource_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=[];initial_capacity;constructor(t){this.initial_capacity=t??V,this.empty_archetype_id=this.arch_get_or_create_from_mask(new b)}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 c=0;c<s.length;c++)if(this.archetypes[s[c]].mask.equals(t))return s[c]}const n=kt(this.next_archetype_id++),r=[];t.for_each(c=>{const o=c,h=this.component_metas[o];h&&h.field_names.length>0&&r.push({component_id:o,field_names:h.field_names,field_index:h.field_index,field_types:h.field_types})});const i=new It(n,t,r,this.initial_capacity);this.archetypes.push(i),Q(this.archetype_map,e,n),t.for_each(c=>{const o=c;let h=this.component_index.get(o);h||(h=new Set,this.component_index.set(o,h)),h.add(n)});const _=this.registered_queries;for(let c=0;c<_.length;c++){const o=_[c];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,add_map:null,remove_map:null};n.add=e.id,n.add_map=M(t,e),t.set_edge(s,n);const r=e.get_edge(s)??{add:null,remove:null,add_map:null,remove_map:null};r.remove=t.id,r.remove_map=M(e,t),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]=j,e=j),this.entity_alive_count++;const s=vt(t,e);return this.entity_archetype[t]=this.empty_archetype_id,this.entity_row[t]=v,s}destroy_entity(t){if(!this.is_alive(t))return;const e=m(t),s=this.entity_row[e];if(s!==v){const i=this.arch_get(this.entity_archetype[e]).remove_entity(s);i!==y&&(this.entity_row[i]=s)}this.entity_archetype[e]=v,this.entity_row[e]=v;const n=Y(t);this.entity_generations[e]=n+1&X,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]===Y(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 c=t[_],o=c&F,h=c>>S;if(o>=i||n[o]!==h)continue;const a=s[o];if(a!==v){const u=r[e[o]],d=u.has_columns?u.remove_entity(a):u.remove_entity_tag(a);d!==y&&(s[d]=a)}e[o]=v,s[o]=v,n[o]=h+1&X,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??D)}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,c=this.archetypes,o=this.component_metas,h=this.entity_high_water;for(let a=0;a<n;a++){const u=t[a],d=u&F,g=u>>S;if(d>=h||_[d]!==g)continue;const T=r[d],p=e[a],w=c[T];if(w.mask.has(p)){o[p].field_names.length>0&&w.write_fields(i[d],p,s[a]);continue}const P=this.arch_resolve_add(T,p),E=c[P],O=i[d],L=!E.has_columns&&!w.has_columns;let U;if(O!==v){if(L)E.move_entity_from_tag(w,O,u);else{const Z=w.get_edge(p);E.move_entity_from(w,O,u,Z.add_map)}U=f[0],f[1]!==y&&(i[f[1]]=O)}else U=L?E.add_entity_tag(u):E.add_entity(u);o[p].field_names.length>0&&E.write_fields(U,p,s[a]),r[d]=P,i[d]=U}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,c=this.entity_high_water;for(let o=0;o<s;o++){const h=t[o],a=h&F,u=h>>S;if(a>=c||i[a]!==u)continue;const d=n[a],g=e[o],T=_[d];if(!T.mask.has(g))continue;const p=this.arch_resolve_remove(d,g),w=_[p],P=r[a];if(!w.has_columns&&!T.has_columns)w.move_entity_from_tag(T,P,h);else{const O=T.get_edge(g);w.move_entity_from(T,P,h,O.remove_map)}f[1]!==y&&(r[f[1]]=P),n[a]=p,r[a]=f[0]}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=bt(this.component_count++),s=Object.keys(t),n=new Array(s.length),r=Object.create(null);for(let i=0;i<s.length;i++)r[s[i]]=i,n[i]=t[s[i]];return this.component_metas.push({field_names:s,field_index:r,field_types:n}),e}add_component(t,e,s){if(!this.is_alive(t))return;const n=m(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),c=this.arch_get(_),o=this.entity_row[n];let h;if(o!==v){const a=i.get_edge(e);!c.has_columns&&!i.has_columns?c.move_entity_from_tag(i,o,t):c.move_entity_from(i,o,t,a.add_map),h=f[0],f[1]!==y&&(this.entity_row[f[1]]=o)}else h=c.has_columns?c.add_entity(t):c.add_entity_tag(t);c.write_fields(h,e,s),this.entity_archetype[n]=_,this.entity_row[n]=h}add_components(t,e){if(!this.is_alive(t))return;const s=m(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),c=this.entity_row[s];let o;if(c!==v){const h=M(i,_);_.move_entity_from(i,c,t,h),o=f[0],f[1]!==y&&(this.entity_row[f[1]]=c)}else o=_.add_entity(t);for(let h=0;h<e.length;h++)_.write_fields(o,e[h].def,e[h].values??D);this.entity_archetype[s]=r,this.entity_row[s]=o}else{const i=this.arch_get(n),_=this.entity_row[s];for(let c=0;c<e.length;c++)i.write_fields(_,e[c].def,e[c].values??D)}}remove_component(t,e){if(!this.is_alive(t))return;const s=m(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),c=this.entity_row[s],o=r.get_edge(e);!_.has_columns&&!r.has_columns?_.move_entity_from_tag(r,c,t):_.move_entity_from(r,c,t,o.remove_map),f[1]!==y&&(this.entity_row[f[1]]=c),this.entity_archetype[s]=i,this.entity_row[s]=f[0]}remove_components(t,e){if(!this.is_alive(t))return;const s=m(t),n=this.entity_archetype[s];let r=n;for(let h=0;h<e.length;h++)r=this.arch_resolve_remove(r,e[h]);if(r===n)return;const i=this.arch_get(n),_=this.arch_get(r),c=this.entity_row[s],o=M(i,_);_.move_entity_from(i,c,t,o),f[1]!==y&&(this.entity_row[f[1]]=c),this.entity_archetype[s]=r,this.entity_row[s]=f[0]}has_component(t,e){if(!this.is_alive(t))return!1;const s=m(t);return this.arch_get(this.entity_archetype[s]).has_component(e)}batch_add_component(t,e,s){if(t.length===0)return;const n=e;if(t.mask.has(n))return;const r=this.arch_resolve_add(t.id,n),i=this.arch_get(r),_=t.get_edge(n),c=t.length,o=this.entity_archetype,h=this.entity_row,a=i.bulk_move_all_from(t,_.add_map);for(let d=0;d<c;d++){const g=m(i.entity_ids[a+d]);o[g]=r,h[g]=a+d}if(this.component_metas[n].field_names.length>0&&s)for(let d=0;d<c;d++)i.write_fields(a+d,n,s)}batch_remove_component(t,e){if(t.length===0)return;const s=e;if(!t.mask.has(s))return;const n=this.arch_resolve_remove(t.id,s),r=this.arch_get(n),i=t.get_edge(s),_=t.length,c=r.bulk_move_all_from(t,i.remove_map),o=this.entity_archetype,h=this.entity_row;for(let a=0;a<_;a++){const u=m(r.entity_ids[c+a]);o[u]=n,h[u]=c+a}}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 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 h=n[o];if(h===0)continue;const a=o<<k;for(;h!==0;){const u=h&-h>>>0,d=a+(I-Math.clz32(u));h^=u;const g=this.component_index.get(d);if(!g||g.size===0){_=!0;break}(!i||g.size<i.size)&&(i=g)}if(_)break}if(_||!i)return[];const c=[];for(const o of i){const h=this.arch_get(o);h.matches(t)&&(!e||!h.mask.overlaps(e))&&(!s||h.mask.overlaps(s))&&c.push(h)}return c}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=xt(this.event_count++),s=new Tt(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()}register_resource(t,e){const s=At(this.resource_count++),n=new Et(t,e);return this.resource_channels.push(n),s}get_resource_reader(t){return this.resource_channels[t].reader}get_resource_channel(t){return this.resource_channels[t]}}var H=(l=>(l.PRE_STARTUP="PRE_STARTUP",l.STARTUP="STARTUP",l.POST_STARTUP="POST_STARTUP",l.FIXED_UPDATE="FIXED_UPDATE",l.PRE_UPDATE="PRE_UPDATE",l.UPDATE="UPDATE",l.POST_UPDATE="POST_UPDATE",l))(H||{});const q=["PRE_STARTUP","STARTUP","POST_STARTUP"],C=["PRE_UPDATE","UPDATE","POST_UPDATE"];class Pt{label_systems=new Map;sorted_cache=new Map;system_index=new Map;next_insertion_order=0;constructor(){for(let t=0;t<q.length;t++)this.label_systems.set(q[t],[]);this.label_systems.set("FIXED_UPDATE",[]);for(let t=0;t<C.length;t++)this.label_systems.set(C[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 q)this.run_label(e,t,ht)}run_update(t,e){for(const s of C)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 h of o.before)i.has(h)&&(s.get(o.descriptor).add(h),n.set(h,n.get(h)+1));for(const h of o.after)i.has(h)&&(s.get(h).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,h)=>r.get(h)-r.get(o));const c=[];for(;_.length>0;){const o=_.pop();c.push(o);for(const h of s.get(o)){const a=n.get(h)-1;n.set(h,a),a===0&&_.push(h)}_.sort((h,a)=>r.get(a)-r.get(h))}if(c.length!==t.length){const o=new Set(c),h=t.filter(a=>!o.has(a.descriptor)).map(a=>a.descriptor.name??`system_${a.descriptor.id}`);throw new et(z.CIRCULAR_SYSTEM_DEPENDENCY,`Circular system dependency detected in ${e}: [${h.join(", ")}]`)}return c}}const W=new WeakMap;function Ot(l,t){let e=W.get(l);if(!e){e=Object.create(null);const{field_names:r}=l.layout;for(let i=0;i<r.length;i++){const _=i;Object.defineProperty(e,r[i],{get(){return this._columns[_][this._row]},set(c){this._columns[_][this._row]=c},enumerable:!0,configurable:!1})}W.set(l,e)}const s=Object.create(e),n=new Array(l.columns.length);for(let r=0;r<l.columns.length;r++)n[r]=l.columns[r].buf;return s._columns=n,s._row=t,s}class K{_archetypes;_defs;_resolver;_include;_exclude;_any_of;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}get archetype_count(){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])}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 b;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)}any_of(...t){const e=this._any_of?this._any_of.copy():new b;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 ${constructor(t){this._resolver=t}every(...t){const e=new b;for(let s=0;s<t.length;s++)e.set(t[s]);return this._resolver._resolve_query(e,null,null,t)}}class J{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(t),r=this.store.get_entity_row(t);return n.read_field(r,e,s)}set_field(t,e,s,n){const r=this.store.get_entity_archetype(t),i=this.store.get_entity_row(t),_=r.get_column(e,s);_[i]=n}ref(t,e){const s=this.store.get_entity_archetype(e),n=this.store.get_entity_row(e);return Ot(s.column_groups[t],n)}destroy_entity(t){return this.store.destroy_entity_deferred(t),this}add_component(t,e,s){return this.store.add_component_deferred(t,e,s??D),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)}resource(t){return this.store.get_resource_reader(t)}set_resource(t,e){this.store.get_resource_channel(t).write(e)}}const Dt=l=>N(l);class Nt{store;schedule;ctx;systems=new Set;next_system_id=0;_fixed_timestep;_accumulator=0;_max_fixed_steps;query_cache=new Map;scratch_mask=new b;constructor(t){this.store=new St(t?.initial_capacity),this.schedule=new Pt,this.ctx=new J(this.store),this._fixed_timestep=t?.fixed_timestep??_t,this._max_fixed_steps=t?.max_fixed_steps??ct}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,e){if(Array.isArray(t)){const s=e??"f64",n=Object.create(null);for(const r of t)n[r]=s;return this.store.register_component(n)}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([])}register_resource(t,e){return this.store.register_resource(t,e)}resource(t){return this.store.get_resource_reader(t)}set_resource(t,e){this.store.get_resource_channel(t).write(e)}create_entity(){return this.store.create_entity()}destroy_entity_deferred(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){return this.store.add_component(t,e,s??D),this}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)}batch_add_component(t,e,s){this.store.batch_add_component(t,e,s)}batch_remove_component(t,e){this.store.batch_remove_component(t,e)}get_field(t,e,s){const n=this.store.get_entity_archetype(t),r=this.store.get_entity_row(t);return n.read_field(r,e,s)}set_field(t,e,s,n){const r=this.store.get_entity_archetype(t),i=this.store.get_entity_row(t),_=r.get_column(e,s);_[i]=n}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,c=r^Math.imul(i,rt)^Math.imul(_,it)|0,o=this._find_cached(c,t,e,s);if(o!==void 0)return o.query;const h=this.store.register_query(t,e??void 0,s??void 0),a=new K(h,n,this,t.copy(),e?.copy()??null,s?.copy()??null);return Q(this.query_cache,c,{include_mask:t.copy(),exclude_mask:e?.copy()??null,any_of_mask:s?.copy()??null,query:a}),a}_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")if(e!==void 0){const i=e(new $(this)),_=this.ctx,c=t;s={fn:(o,h)=>c(i,_,h)}}else s={fn:t};else s=t;const n=Dt(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.ctx);this.schedule.run_startup(this.ctx)}update(t){if(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),this.store.clear_events()}flush(){this.ctx.flush()}dispose(){for(const t of this.systems.values())t.dispose?.(),t.on_removed?.();this.systems.clear(),this.schedule.clear()}}exports.ECS=Nt;exports.Query=K;exports.QueryBuilder=$;exports.SCHEDULE=H;exports.SystemContext=J;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export type {
|
|
1
|
+
export { ECS, type WorldOptions } from './ecs';
|
|
2
|
+
export { SCHEDULE, type SystemEntry, type SystemOrdering } from './schedule';
|
|
3
|
+
export { SystemContext } from './query';
|
|
4
|
+
export type { SystemConfig, SystemDescriptor } from './system';
|
|
5
|
+
export type { ComponentRef } from './ref';
|
|
6
|
+
export { Query, QueryBuilder } from './query';
|
|
5
7
|
export type { EntityID } from './entity';
|
|
6
|
-
export type { ComponentDef, ComponentFields, FieldValues, ColumnsForSchema, } from './component';
|
|
8
|
+
export type { ComponentDef, ComponentSchema, ComponentFields, FieldValues, TagToTypedArray, ColumnsForSchema, } from './component';
|
|
7
9
|
export type { EventDef, EventReader } from './event';
|
|
8
|
-
export type {
|
|
9
|
-
export type { SystemEntry, SystemOrdering } from './schedule';
|
|
10
|
+
export type { ResourceDef, ResourceReader } from './resource';
|
|
10
11
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,KAAK,YAAY,EAAE,MAAM,OAAO,CAAC;AAG/C,OAAO,EAAE,QAAQ,EAAE,KAAK,WAAW,EAAE,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAG7E,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,YAAY,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAG/D,YAAY,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAG1C,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAG9C,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAGzC,YAAY,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,WAAW,EACX,eAAe,EACf,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAGrB,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAGrD,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
|