@jael-ecs/core 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,7 +32,8 @@ _A modern, performant, and user-friendly Entity Component System library written
32
32
 
33
33
  - **User Friendly API** - Clean, fluent api that's easy to learn
34
34
  - **High Performance** - Optimized SparseSet implementation for fast entity lookups
35
- - **Minimal Bundle size** - Compact bundle size without dependencies.(34kb 📦)
35
+ - **Query System** - Optimized cache query system for entity packets
36
+ - **Minimal Bundle size** - Compact bundle size without dependencies.(38kb 📦)
36
37
 
37
38
  ## Installation
38
39
 
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class o{_listeners=new Map;on(e,t){if(this.contains(e,t))return;const s=this._listeners.get(e);s?s.add(t):this._listeners.set(e,new Set([t]))}off(e,t){if(!this.contains(e,t))return;const s=this._listeners.get(e);s&&s.delete(t)}once(e,t){const s=(n=>{t(n),this.off(e,s)});this.on(e,s)}clearEvent(e){this._listeners.get(e)&&this._listeners.get(e)?.clear()}clearAllEvents(){this._listeners.forEach(e=>e.clear()),this._listeners.clear()}contains(e,t){return this._listeners.get(e)?this._listeners.get(e).has(t):!1}emit(e,t){this._listeners.get(e)&&this._listeners.get(e)?.forEach(s=>{s(t)})}}class d{systemList=[];addSystem(e){this.systemList.push(e),e.init?.(),this.reorder()}reorder(){this.systemList.sort((e,t)=>e.priority-t.priority)}has(e){return this.systemList.indexOf(e)>0}removeSystem(e){if(!this.has(e))return;const t=this.systemList.indexOf(e);t>=0&&(this.systemList.splice(t,1),e.exit?.(),this.reorder())}}class c{denseValues=[];sparse=new Map;[Symbol.iterator](){let e=this.values.length;const t={value:void 0,done:!1};return{next:()=>(t.value=this.values[--e],t.done=e<0,t)}}get values(){return this.denseValues}first(){return this.denseValues[0]}add(e){this.has(e)||(this.denseValues.push(e),this.sparse.set(e,this.denseValues.length-1))}indexOf(e){const t=this.sparse.get(e);return t!==void 0?t:-1}remove(e){if(!this.has(e))return;const t=this.sparse.get(e);this.sparse.delete(e);const s=this.denseValues[this.denseValues.length-1];s!==e&&(this.denseValues[t]=s,this.sparse.set(s,t)),this.denseValues.pop()}forEach(e){for(let t of this)e(t)}size(){return this.denseValues.length}clear(){for(let e of this)this.remove(e)}has(e){return this.sparse.has(e)}}class m{id;_world;constructor(e,t){this.id=t,this._world=e}add(e,t){this._world.addComponent(this.id,e,t)}remove(e){this._world.removeComponent(this.id,e)}has(e){return this._world.componentManager.hasComponent(this.id,e)}get(e){return this._world.componentManager.getComponent(this.id,e)}}class u extends o{entityMap=new c;nextId=0;_world;constructor(e){super(),this._world=e}get entities(){return this.entityMap}create(){const e=this.nextId++;return this.entities.add(e),this.emit("create",e),e}exist(e){return this.entities.has(e)}size(){return this.entities.size()}destroy(e){return this.entities.remove(e),this.emit("destroy",e),e}}class l extends o{componentSet={};world;constructor(e){super(),this.world=e}clearComponentSchema(e){this.componentSet[e]&&delete this.componentSet[e]}addComponent(e,t,s){const n=this.componentSet[e];n?n[t]=s:this.componentSet[e]={[t]:s},this.emit("add",{entityId:e,component:t})}getComponent(e,t){if(this.hasComponent(e,t))return this.componentSet[e][t]}hasComponent(e,t){const s=this.componentSet[e];return s?t in s:!1}removeComponent(e,t){if(!this.componentSet[e])return;const s=this.componentSet[e];s&&s[t]!==void 0&&(delete s[t],Object.keys(s).length===0&&delete this.componentSet[e],this.emit("remove",{entityId:e,component:t}))}}class a extends o{config;entityMap;entityInstancesCache;world;constructor(e,t){super(),this.config=e,this.world=t,this.entityInstancesCache=new Map,this.entityMap=new c,this.on("removed",s=>{this.entityInstancesCache.delete(s)})}hasComponents(e){const t=this.world.componentManager;return this.config.include?.every(s=>t.getComponent(e,s))&&this.config.exclude?.every(s=>!t.getComponent(e,s))}size(){return this.ids.size()}get hash(){return a.getHash(this.config)}get ids(){return this.entityMap}getCachedEntity(e){const t=this.entityInstancesCache.get(e);if(t)return t;{const s=this.world.getEntity(e);return s?(this.entityInstancesCache.set(e,s),s):void 0}}get entities(){const e=[],t=this.entityMap.size()>100;return this.entityMap.forEach(s=>{if(t){const n=this.getCachedEntity(s);n&&e.push(n)}else{const n=this.world.getEntity(s);n&&e.push(n)}}),e}include(...e){return this.world.include(...e)}exclude(...e){return this.world.exclude(...e)}_checkExistingEntities(){for(let e of this.ids)this.world.exist(e)||(this.emit("removed",e),this.entityMap.remove(e))}checkEntities(){this.entityInstancesCache.clear();for(let e of this.world.entityIds)this.hasComponents(e)&&(this.entityMap.add(e),this.emit("added",e));this._checkExistingEntities()}static getHash(e){const t=e.include?.map(i=>i.trim()).filter(i=>i).join("_"),s=e.exclude?.map(i=>i.trim()).filter(i=>i).join("_"),n="in_"+t+"_out_"+s;let h=0;for(const i of n)h=(h<<5)-h+i.charCodeAt(0),h|=0;return h}}class p extends o{_startTime=0;_oldTime=0;_requestId=0;running=!1;delta=0;elapsed=0;constructor(){super()}_loop(){let e=0;if(this.running){const t=performance.now();e=(t-this._oldTime)/1e3,this._oldTime=t,this.elapsed+=e}this.delta=e,this.emit("update"),this._requestId=requestAnimationFrame(this._loop.bind(this))}start(){this._startTime=performance.now(),this._oldTime=this._startTime,this.elapsed=0,this.delta=0,this.running=!0,this._loop()}stop(){this.running=!1,cancelAnimationFrame(this._requestId),this._requestId=0}}let g=new p;class f extends o{entityManager;componentManager;systemManager;queries;constructor(){super(),this.entityManager=new u(this),this.componentManager=new l(this),this.systemManager=new d,this.entityManager.on("create",e=>{const t=this.getEntity(e);t&&(this.emit("entityCreated",{entity:t}),this._updateQueries())}),this.entityManager.on("destroy",e=>{const t=this.getEntity(e);t&&(this.emit("entityCreated",{entity:t}),this._updateQueries(),this.componentManager.clearComponentSchema(e))}),this.componentManager.on("add",e=>{const t=this.getEntity(e.entityId);t&&(this.emit("componentAdded",{entity:t,component:e.component}),this._updateQueries())}),this.componentManager.on("remove",e=>{const t=this.getEntity(e.entityId);t&&(this.emit("componentRemoved",{entity:t,component:e.component}),this._updateQueries())}),this.queries=new Map}getEntity(e){if(this.entityManager.exist(e))return new m(this,e)}get entityIds(){return this.entityManager.entities}query(e){const t=a.getHash(e);let n=this.queries.get(t);return n||(n=new a(e,this),this.queries.set(t,n),this._updateQueries()),n}_updateQueries(){this.queries.forEach(e=>e.checkEntities())}exist(e){return this.entityManager.exist(e)}include(...e){return this.query({include:e,exclude:[]})}exclude(...e){return this.query({include:[],exclude:e})}create(){return this.entityManager.create()}destroy(e){this.entityManager.destroy(e)}addSystem(e){this.systemManager.addSystem(e)}removeSystem(e){this.systemManager.removeSystem(e)}addComponent(e,t,s){this.componentManager.addComponent(e,t,s)}getComponent(e,t){return this.componentManager.getComponent(e,t)}removeComponent(e,t){this.componentManager.removeComponent(e,t)}update(){this.systemManager.systemList.forEach(e=>{e.update()}),this.emit("updated")}}exports.ComponentManager=l;exports.EntityManager=u;exports.EventRegistry=o;exports.Query=a;exports.SparseSet=c;exports.SystemManager=d;exports.Time=g;exports.World=f;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class o{_listeners=new Map;on(e,t){if(this.contains(e,t))return;const s=this._listeners.get(e);s?s.add(t):this._listeners.set(e,new Set([t]))}off(e,t){if(!this.contains(e,t))return;const s=this._listeners.get(e);s&&s.delete(t)}once(e,t){const s=(n=>{t(n),this.off(e,s)});this.on(e,s)}clearEvent(e){this._listeners.get(e)&&this._listeners.get(e)?.clear()}clearAllEvents(){this._listeners.forEach(e=>e.clear()),this._listeners.clear()}contains(e,t){return this._listeners.get(e)?this._listeners.get(e).has(t):!1}emit(e,t){this._listeners.get(e)&&this._listeners.get(e)?.forEach(s=>{s(t)})}}class u{systemList=[];addSystem(e){this.systemList.push(e),e.init?.(),this.reorder()}reorder(){this.systemList.sort((e,t)=>e.priority-t.priority)}has(e){return this.systemList.indexOf(e)>0}removeSystem(e){if(!this.has(e))return;const t=this.systemList.indexOf(e);t>=0&&(this.systemList.splice(t,1),e.exit?.(),this.reorder())}}class d{denseValues=[];sparse=new Map;[Symbol.iterator](){let e=this.values.length;const t={value:void 0,done:!1};return{next:()=>(t.value=this.values[--e],t.done=e<0,t)}}get values(){return this.denseValues}first(){return this.denseValues[0]}add(e){this.has(e)||(this.denseValues.push(e),this.sparse.set(e,this.denseValues.length-1))}indexOf(e){const t=this.sparse.get(e);return t!==void 0?t:-1}remove(e){if(!this.has(e))return;const t=this.sparse.get(e);this.sparse.delete(e);const s=this.denseValues[this.denseValues.length-1];s!==e&&(this.denseValues[t]=s,this.sparse.set(s,t)),this.denseValues.pop()}forEach(e){for(let t of this)e(t)}size(){return this.denseValues.length}clear(){for(let e of this)this.remove(e)}has(e){return this.sparse.has(e)}}class a{id;_world;constructor(e,t){this.id=t,this._world=e}add(e,t){this._world.addComponent(this.id,e,t)}remove(e){this._world.removeComponent(this.id,e)}has(e){return this._world.componentManager.hasComponent(this.id,e)}get(e){return this._world.componentManager.getComponent(this.id,e)}}class l extends o{entityMap=new d;nextId=0;_world;constructor(e){super(),this._world=e}get entities(){return this.entityMap}create(){const e=this.nextId++;return this.entities.add(e),this.emit("create",e),e}exist(e){return this.entities.has(e)}size(){return this.entities.size()}destroy(e){return this.entities.remove(e),this.emit("destroy",e),e}}class m extends o{componentSet={};world;constructor(e){super(),this.world=e}clearComponentSchema(e){this.componentSet[e]&&delete this.componentSet[e]}addComponent(e,t,s){if(!this.world.exist(e))return;const n=this.componentSet[e];n?n[t]=s:this.componentSet[e]={[t]:s},this.emit("add",{entityId:e,component:t})}getComponent(e,t){if(this.hasComponent(e,t))return this.componentSet[e][t]}hasComponent(e,t){const s=this.componentSet[e];return s?t in s:!1}removeComponent(e,t){if(!this.componentSet[e])return;const s=this.componentSet[e];s&&s[t]!==void 0&&(delete s[t],Object.keys(s).length===0&&delete this.componentSet[e],this.emit("remove",{entityId:e,component:t}))}}class c extends o{config;entityMap;entityInstancesCache;world;constructor(e,t){super(),this.config=e,this.world=t,this.entityInstancesCache=new Map,this.entityMap=new d,this.on("removed",s=>{this.entityInstancesCache.delete(s)})}hasComponents(e){const t=this.world.componentManager;return this.config.include?.every(s=>t.getComponent(e,s))&&this.config.exclude?.every(s=>!t.getComponent(e,s))}size(){return this.ids.size()}get hash(){return c.getHash(this.config)}get ids(){return this.entityMap}getCachedEntity(e){const t=this.entityInstancesCache.get(e);if(t)return t;{const s=this.world.getEntity(e);return s?(this.entityInstancesCache.set(e,s),s):void 0}}get entities(){const e=[],t=this.entityMap.size()>100;return this.entityMap.forEach(s=>{if(t){const n=this.getCachedEntity(s);n&&e.push(n)}else{const n=this.world.getEntity(s);n&&e.push(n)}}),e}include(...e){return this.world.include(...e)}exclude(...e){return this.world.exclude(...e)}_checkExistingEntities(){for(let e of this.ids)this.world.exist(e)||(this.emit("removed",e),this.entityMap.remove(e))}checkEntities(){this.entityInstancesCache.clear();for(let e of this.world.entityIds)this.hasComponents(e)&&(this.entityMap.add(e),this.emit("added",e));this._checkExistingEntities()}static getHash(e){const t=e.include?.map(i=>i.trim()).filter(i=>i).join("_"),s=e.exclude?.map(i=>i.trim()).filter(i=>i).join("_"),n="in_"+t+"_out_"+s;let h=0;for(const i of n)h=(h<<5)-h+i.charCodeAt(0),h|=0;return h}}class p extends o{_startTime=0;_oldTime=0;_requestId=0;running=!1;delta=0;elapsed=0;constructor(){super()}_loop(){let e=0;if(this.running){const t=performance.now();e=(t-this._oldTime)/1e3,this._oldTime=t,this.elapsed+=e}this.delta=e,this.emit("update"),this._requestId=requestAnimationFrame(this._loop.bind(this))}start(){this._startTime=performance.now(),this._oldTime=this._startTime,this.elapsed=0,this.delta=0,this.running=!0,this._loop()}stop(){this.running=!1,cancelAnimationFrame(this._requestId),this._requestId=0}}let g=new p;class f extends o{entityManager;componentManager;systemManager;queries;constructor(){super(),this.entityManager=new l(this),this.componentManager=new m(this),this.systemManager=new u,this.entityManager.on("create",e=>{this.emit("entityCreated",{entity:new a(this,e)}),this._updateQueries()}),this.entityManager.on("destroy",e=>{this.emit("entityDestroyed",{entity:new a(this,e)}),this._updateQueries(),this.componentManager.clearComponentSchema(e)}),this.componentManager.on("add",({entityId:e,component:t})=>{this.emit("componentAdded",{entity:new a(this,e),component:t}),this._updateQueries()}),this.componentManager.on("remove",({entityId:e,component:t})=>{this.getEntity(e)&&this.emit("componentRemoved",{entity:new a(this,e),component:t}),this._updateQueries()}),this.queries=new Map}getEntity(e){return this.exist(e)?new a(this,e):void 0}get entityIds(){return this.entityManager.entities}query(e){const t=c.getHash(e);let n=this.queries.get(t);return n||(n=new c(e,this),this.queries.set(t,n),this._updateQueries()),n}_updateQueries(){this.queries.forEach(e=>e.checkEntities())}exist(e){return this.entityManager.exist(e)}include(...e){return this.query({include:e,exclude:[]})}exclude(...e){return this.query({include:[],exclude:e})}create(){return this.entityManager.create()}destroy(e){this.entityManager.destroy(e)}addSystem(e){this.systemManager.addSystem(e)}removeSystem(e){this.systemManager.removeSystem(e)}addComponent(e,t,s){this.componentManager.addComponent(e,t,s)}getComponent(e,t){return this.componentManager.getComponent(e,t)}removeComponent(e,t){this.componentManager.removeComponent(e,t)}update(){this.systemManager.systemList.forEach(e=>{e.update()}),this.emit("updated")}}exports.ComponentManager=m;exports.EntityManager=l;exports.EventRegistry=o;exports.Query=c;exports.SparseSet=d;exports.SystemManager=u;exports.Time=g;exports.World=f;
@@ -1,66 +1,66 @@
1
- class h {
1
+ class a {
2
2
  _listeners = /* @__PURE__ */ new Map();
3
- on(t, e) {
4
- if (this.contains(t, e))
3
+ on(e, t) {
4
+ if (this.contains(e, t))
5
5
  return;
6
- const s = this._listeners.get(t);
7
- s ? s.add(e) : this._listeners.set(t, /* @__PURE__ */ new Set([e]));
6
+ const s = this._listeners.get(e);
7
+ s ? s.add(t) : this._listeners.set(e, /* @__PURE__ */ new Set([t]));
8
8
  }
9
- off(t, e) {
10
- if (!this.contains(t, e))
9
+ off(e, t) {
10
+ if (!this.contains(e, t))
11
11
  return;
12
- const s = this._listeners.get(t);
13
- s && s.delete(e);
12
+ const s = this._listeners.get(e);
13
+ s && s.delete(t);
14
14
  }
15
- once(t, e) {
15
+ once(e, t) {
16
16
  const s = ((n) => {
17
- e(n), this.off(t, s);
17
+ t(n), this.off(e, s);
18
18
  });
19
- this.on(t, s);
19
+ this.on(e, s);
20
20
  }
21
- clearEvent(t) {
22
- this._listeners.get(t) && this._listeners.get(t)?.clear();
21
+ clearEvent(e) {
22
+ this._listeners.get(e) && this._listeners.get(e)?.clear();
23
23
  }
24
24
  clearAllEvents() {
25
- this._listeners.forEach((t) => t.clear()), this._listeners.clear();
25
+ this._listeners.forEach((e) => e.clear()), this._listeners.clear();
26
26
  }
27
- contains(t, e) {
28
- return this._listeners.get(t) ? this._listeners.get(t).has(e) : !1;
27
+ contains(e, t) {
28
+ return this._listeners.get(e) ? this._listeners.get(e).has(t) : !1;
29
29
  }
30
- emit(t, e) {
31
- this._listeners.get(t) && this._listeners.get(t)?.forEach((s) => {
32
- s(e);
30
+ emit(e, t) {
31
+ this._listeners.get(e) && this._listeners.get(e)?.forEach((s) => {
32
+ s(t);
33
33
  });
34
34
  }
35
35
  }
36
- class d {
36
+ class u {
37
37
  systemList = [];
38
- addSystem(t) {
39
- this.systemList.push(t), t.init?.(), this.reorder();
38
+ addSystem(e) {
39
+ this.systemList.push(e), e.init?.(), this.reorder();
40
40
  }
41
41
  reorder() {
42
- this.systemList.sort((t, e) => t.priority - e.priority);
42
+ this.systemList.sort((e, t) => e.priority - t.priority);
43
43
  }
44
- has(t) {
45
- return this.systemList.indexOf(t) > 0;
44
+ has(e) {
45
+ return this.systemList.indexOf(e) > 0;
46
46
  }
47
- removeSystem(t) {
48
- if (!this.has(t)) return;
49
- const e = this.systemList.indexOf(t);
50
- e >= 0 && (this.systemList.splice(e, 1), t.exit?.(), this.reorder());
47
+ removeSystem(e) {
48
+ if (!this.has(e)) return;
49
+ const t = this.systemList.indexOf(e);
50
+ t >= 0 && (this.systemList.splice(t, 1), e.exit?.(), this.reorder());
51
51
  }
52
52
  }
53
- class c {
53
+ class d {
54
54
  denseValues = [];
55
55
  sparse = /* @__PURE__ */ new Map();
56
56
  [Symbol.iterator]() {
57
- let t = this.values.length;
58
- const e = {
57
+ let e = this.values.length;
58
+ const t = {
59
59
  value: void 0,
60
60
  done: !1
61
61
  };
62
62
  return {
63
- next: () => (e.value = this.values[--t], e.done = t < 0, e)
63
+ next: () => (t.value = this.values[--e], t.done = e < 0, t)
64
64
  };
65
65
  }
66
66
  get values() {
@@ -69,197 +69,198 @@ class c {
69
69
  first() {
70
70
  return this.denseValues[0];
71
71
  }
72
- add(t) {
73
- this.has(t) || (this.denseValues.push(t), this.sparse.set(t, this.denseValues.length - 1));
72
+ add(e) {
73
+ this.has(e) || (this.denseValues.push(e), this.sparse.set(e, this.denseValues.length - 1));
74
74
  }
75
- indexOf(t) {
76
- const e = this.sparse.get(t);
77
- return e !== void 0 ? e : -1;
75
+ indexOf(e) {
76
+ const t = this.sparse.get(e);
77
+ return t !== void 0 ? t : -1;
78
78
  }
79
- remove(t) {
80
- if (!this.has(t)) return;
81
- const e = this.sparse.get(t);
82
- this.sparse.delete(t);
79
+ remove(e) {
80
+ if (!this.has(e)) return;
81
+ const t = this.sparse.get(e);
82
+ this.sparse.delete(e);
83
83
  const s = this.denseValues[this.denseValues.length - 1];
84
- s !== t && (this.denseValues[e] = s, this.sparse.set(s, e)), this.denseValues.pop();
84
+ s !== e && (this.denseValues[t] = s, this.sparse.set(s, t)), this.denseValues.pop();
85
85
  }
86
- forEach(t) {
87
- for (let e of this)
88
- t(e);
86
+ forEach(e) {
87
+ for (let t of this)
88
+ e(t);
89
89
  }
90
90
  size() {
91
91
  return this.denseValues.length;
92
92
  }
93
93
  clear() {
94
- for (let t of this)
95
- this.remove(t);
94
+ for (let e of this)
95
+ this.remove(e);
96
96
  }
97
- has(t) {
98
- return this.sparse.has(t);
97
+ has(e) {
98
+ return this.sparse.has(e);
99
99
  }
100
100
  }
101
- class u {
101
+ class h {
102
102
  id;
103
103
  _world;
104
- constructor(t, e) {
105
- this.id = e, this._world = t;
104
+ constructor(e, t) {
105
+ this.id = t, this._world = e;
106
106
  }
107
107
  /**
108
108
  * Add component to current entity.
109
109
  * @param compType Component name
110
110
  * @param compValue Component value
111
111
  */
112
- add(t, e) {
113
- this._world.addComponent(this.id, t, e);
112
+ add(e, t) {
113
+ this._world.addComponent(this.id, e, t);
114
114
  }
115
115
  /**
116
116
  * Remove component of current entity.
117
117
  * @param compType Component name
118
118
  */
119
- remove(t) {
120
- this._world.removeComponent(this.id, t);
119
+ remove(e) {
120
+ this._world.removeComponent(this.id, e);
121
121
  }
122
122
  /**
123
123
  * Check if current entity has a component.
124
124
  * @param compType Component name
125
125
  * @returns boolean
126
126
  */
127
- has(t) {
128
- return this._world.componentManager.hasComponent(this.id, t);
127
+ has(e) {
128
+ return this._world.componentManager.hasComponent(this.id, e);
129
129
  }
130
130
  /**
131
131
  * Get passed component schema of current entity.
132
132
  * @param compType Component name
133
133
  * @returns Return component schema with T(any as default) as type
134
134
  */
135
- get(t) {
136
- return this._world.componentManager.getComponent(this.id, t);
135
+ get(e) {
136
+ return this._world.componentManager.getComponent(this.id, e);
137
137
  }
138
138
  }
139
- class l extends h {
140
- entityMap = new c();
139
+ class l extends a {
140
+ entityMap = new d();
141
141
  nextId = 0;
142
142
  _world;
143
- constructor(t) {
144
- super(), this._world = t;
143
+ constructor(e) {
144
+ super(), this._world = e;
145
145
  }
146
146
  get entities() {
147
147
  return this.entityMap;
148
148
  }
149
149
  create() {
150
- const t = this.nextId++;
151
- return this.entities.add(t), this.emit("create", t), t;
150
+ const e = this.nextId++;
151
+ return this.entities.add(e), this.emit("create", e), e;
152
152
  }
153
- exist(t) {
154
- return this.entities.has(t);
153
+ exist(e) {
154
+ return this.entities.has(e);
155
155
  }
156
156
  size() {
157
157
  return this.entities.size();
158
158
  }
159
- destroy(t) {
160
- return this.entities.remove(t), this.emit("destroy", t), t;
159
+ destroy(e) {
160
+ return this.entities.remove(e), this.emit("destroy", e), e;
161
161
  }
162
162
  }
163
- class m extends h {
163
+ class m extends a {
164
164
  componentSet = {};
165
165
  world;
166
- constructor(t) {
167
- super(), this.world = t;
166
+ constructor(e) {
167
+ super(), this.world = e;
168
168
  }
169
- clearComponentSchema(t) {
170
- this.componentSet[t] && delete this.componentSet[t];
169
+ clearComponentSchema(e) {
170
+ this.componentSet[e] && delete this.componentSet[e];
171
171
  }
172
- addComponent(t, e, s) {
173
- const n = this.componentSet[t];
174
- n ? n[e] = s : this.componentSet[t] = { [e]: s }, this.emit("add", { entityId: t, component: e });
172
+ addComponent(e, t, s) {
173
+ if (!this.world.exist(e)) return;
174
+ const n = this.componentSet[e];
175
+ n ? n[t] = s : this.componentSet[e] = { [t]: s }, this.emit("add", { entityId: e, component: t });
175
176
  }
176
- getComponent(t, e) {
177
- if (this.hasComponent(t, e))
178
- return this.componentSet[t][e];
177
+ getComponent(e, t) {
178
+ if (this.hasComponent(e, t))
179
+ return this.componentSet[e][t];
179
180
  }
180
- hasComponent(t, e) {
181
- const s = this.componentSet[t];
182
- return s ? e in s : !1;
181
+ hasComponent(e, t) {
182
+ const s = this.componentSet[e];
183
+ return s ? t in s : !1;
183
184
  }
184
- removeComponent(t, e) {
185
- if (!this.componentSet[t]) return;
186
- const s = this.componentSet[t];
187
- s && s[e] !== void 0 && (delete s[e], Object.keys(s).length === 0 && delete this.componentSet[t], this.emit("remove", { entityId: t, component: e }));
185
+ removeComponent(e, t) {
186
+ if (!this.componentSet[e]) return;
187
+ const s = this.componentSet[e];
188
+ s && s[t] !== void 0 && (delete s[t], Object.keys(s).length === 0 && delete this.componentSet[e], this.emit("remove", { entityId: e, component: t }));
188
189
  }
189
190
  }
190
- class a extends h {
191
+ class c extends a {
191
192
  config;
192
193
  entityMap;
193
194
  entityInstancesCache;
194
195
  world;
195
- constructor(t, e) {
196
- super(), this.config = t, this.world = e, this.entityInstancesCache = /* @__PURE__ */ new Map(), this.entityMap = new c(), this.on("removed", (s) => {
196
+ constructor(e, t) {
197
+ super(), this.config = e, this.world = t, this.entityInstancesCache = /* @__PURE__ */ new Map(), this.entityMap = new d(), this.on("removed", (s) => {
197
198
  this.entityInstancesCache.delete(s);
198
199
  });
199
200
  }
200
- hasComponents(t) {
201
- const e = this.world.componentManager;
201
+ hasComponents(e) {
202
+ const t = this.world.componentManager;
202
203
  return this.config.include?.every(
203
- (s) => e.getComponent(t, s)
204
+ (s) => t.getComponent(e, s)
204
205
  ) && this.config.exclude?.every(
205
- (s) => !e.getComponent(t, s)
206
+ (s) => !t.getComponent(e, s)
206
207
  );
207
208
  }
208
209
  size() {
209
210
  return this.ids.size();
210
211
  }
211
212
  get hash() {
212
- return a.getHash(this.config);
213
+ return c.getHash(this.config);
213
214
  }
214
215
  get ids() {
215
216
  return this.entityMap;
216
217
  }
217
- getCachedEntity(t) {
218
- const e = this.entityInstancesCache.get(t);
219
- if (e)
220
- return e;
218
+ getCachedEntity(e) {
219
+ const t = this.entityInstancesCache.get(e);
220
+ if (t)
221
+ return t;
221
222
  {
222
- const s = this.world.getEntity(t);
223
- return s ? (this.entityInstancesCache.set(t, s), s) : void 0;
223
+ const s = this.world.getEntity(e);
224
+ return s ? (this.entityInstancesCache.set(e, s), s) : void 0;
224
225
  }
225
226
  }
226
227
  get entities() {
227
- const t = [], e = this.entityMap.size() > 100;
228
+ const e = [], t = this.entityMap.size() > 100;
228
229
  return this.entityMap.forEach((s) => {
229
- if (e) {
230
+ if (t) {
230
231
  const n = this.getCachedEntity(s);
231
- n && t.push(n);
232
+ n && e.push(n);
232
233
  } else {
233
234
  const n = this.world.getEntity(s);
234
- n && t.push(n);
235
+ n && e.push(n);
235
236
  }
236
- }), t;
237
+ }), e;
237
238
  }
238
- include(...t) {
239
- return this.world.include(...t);
239
+ include(...e) {
240
+ return this.world.include(...e);
240
241
  }
241
- exclude(...t) {
242
- return this.world.exclude(...t);
242
+ exclude(...e) {
243
+ return this.world.exclude(...e);
243
244
  }
244
245
  _checkExistingEntities() {
245
- for (let t of this.ids)
246
- this.world.exist(t) || (this.emit("removed", t), this.entityMap.remove(t));
246
+ for (let e of this.ids)
247
+ this.world.exist(e) || (this.emit("removed", e), this.entityMap.remove(e));
247
248
  }
248
249
  checkEntities() {
249
250
  this.entityInstancesCache.clear();
250
- for (let t of this.world.entityIds)
251
- this.hasComponents(t) && (this.entityMap.add(t), this.emit("added", t));
251
+ for (let e of this.world.entityIds)
252
+ this.hasComponents(e) && (this.entityMap.add(e), this.emit("added", e));
252
253
  this._checkExistingEntities();
253
254
  }
254
- static getHash(t) {
255
- const e = t.include?.map((i) => i.trim()).filter((i) => i).join("_"), s = t.exclude?.map((i) => i.trim()).filter((i) => i).join("_"), n = "in_" + e + "_out_" + s;
255
+ static getHash(e) {
256
+ const t = e.include?.map((i) => i.trim()).filter((i) => i).join("_"), s = e.exclude?.map((i) => i.trim()).filter((i) => i).join("_"), n = "in_" + t + "_out_" + s;
256
257
  let o = 0;
257
258
  for (const i of n)
258
259
  o = (o << 5) - o + i.charCodeAt(0), o |= 0;
259
260
  return o;
260
261
  }
261
262
  }
262
- class p extends h {
263
+ class p extends a {
263
264
  _startTime = 0;
264
265
  _oldTime = 0;
265
266
  _requestId = 0;
@@ -270,12 +271,12 @@ class p extends h {
270
271
  super();
271
272
  }
272
273
  _loop() {
273
- let t = 0;
274
+ let e = 0;
274
275
  if (this.running) {
275
- const e = performance.now();
276
- t = (e - this._oldTime) / 1e3, this._oldTime = e, this.elapsed += t;
276
+ const t = performance.now();
277
+ e = (t - this._oldTime) / 1e3, this._oldTime = t, this.elapsed += e;
277
278
  }
278
- this.delta = t, this.emit("update"), this._requestId = requestAnimationFrame(this._loop.bind(this));
279
+ this.delta = e, this.emit("update"), this._requestId = requestAnimationFrame(this._loop.bind(this));
279
280
  }
280
281
  start() {
281
282
  this._startTime = performance.now(), this._oldTime = this._startTime, this.elapsed = 0, this.delta = 0, this.running = !0, this._loop();
@@ -285,96 +286,89 @@ class p extends h {
285
286
  }
286
287
  }
287
288
  let g = new p();
288
- class f extends h {
289
+ class f extends a {
289
290
  entityManager;
290
291
  componentManager;
291
292
  systemManager;
292
293
  queries;
293
294
  constructor() {
294
- super(), this.entityManager = new l(this), this.componentManager = new m(this), this.systemManager = new d(), this.entityManager.on("create", (t) => {
295
- const e = this.getEntity(t);
296
- e && (this.emit("entityCreated", { entity: e }), this._updateQueries());
297
- }), this.entityManager.on("destroy", (t) => {
298
- const e = this.getEntity(t);
299
- e && (this.emit("entityCreated", { entity: e }), this._updateQueries(), this.componentManager.clearComponentSchema(t));
300
- }), this.componentManager.on(
301
- "add",
302
- (t) => {
303
- const e = this.getEntity(t.entityId);
304
- e && (this.emit("componentAdded", {
305
- entity: e,
306
- component: t.component
307
- }), this._updateQueries());
308
- }
309
- ), this.componentManager.on(
310
- "remove",
311
- (t) => {
312
- const e = this.getEntity(t.entityId);
313
- e && (this.emit("componentRemoved", {
314
- entity: e,
315
- component: t.component
316
- }), this._updateQueries());
317
- }
318
- ), this.queries = /* @__PURE__ */ new Map();
319
- }
320
- getEntity(t) {
321
- if (this.entityManager.exist(t))
322
- return new u(this, t);
295
+ super(), this.entityManager = new l(this), this.componentManager = new m(this), this.systemManager = new u(), this.entityManager.on("create", (e) => {
296
+ this.emit("entityCreated", {
297
+ entity: new h(this, e)
298
+ }), this._updateQueries();
299
+ }), this.entityManager.on("destroy", (e) => {
300
+ this.emit("entityDestroyed", {
301
+ entity: new h(this, e)
302
+ }), this._updateQueries(), this.componentManager.clearComponentSchema(e);
303
+ }), this.componentManager.on("add", ({ entityId: e, component: t }) => {
304
+ this.emit("componentAdded", {
305
+ entity: new h(this, e),
306
+ component: t
307
+ }), this._updateQueries();
308
+ }), this.componentManager.on("remove", ({ entityId: e, component: t }) => {
309
+ this.getEntity(e) && this.emit("componentRemoved", {
310
+ entity: new h(this, e),
311
+ component: t
312
+ }), this._updateQueries();
313
+ }), this.queries = /* @__PURE__ */ new Map();
314
+ }
315
+ getEntity(e) {
316
+ return this.exist(e) ? new h(this, e) : void 0;
323
317
  }
324
318
  get entityIds() {
325
319
  return this.entityManager.entities;
326
320
  }
327
- query(t) {
328
- const e = a.getHash(t);
329
- let n = this.queries.get(e);
330
- return n || (n = new a(t, this), this.queries.set(e, n), this._updateQueries()), n;
321
+ query(e) {
322
+ const t = c.getHash(e);
323
+ let n = this.queries.get(t);
324
+ return n || (n = new c(e, this), this.queries.set(t, n), this._updateQueries()), n;
331
325
  }
332
326
  _updateQueries() {
333
- this.queries.forEach((t) => t.checkEntities());
327
+ this.queries.forEach((e) => e.checkEntities());
334
328
  }
335
- exist(t) {
336
- return this.entityManager.exist(t);
329
+ exist(e) {
330
+ return this.entityManager.exist(e);
337
331
  }
338
- include(...t) {
339
- return this.query({ include: t, exclude: [] });
332
+ include(...e) {
333
+ return this.query({ include: e, exclude: [] });
340
334
  }
341
- exclude(...t) {
342
- return this.query({ include: [], exclude: t });
335
+ exclude(...e) {
336
+ return this.query({ include: [], exclude: e });
343
337
  }
344
338
  create() {
345
339
  return this.entityManager.create();
346
340
  }
347
- destroy(t) {
348
- this.entityManager.destroy(t);
341
+ destroy(e) {
342
+ this.entityManager.destroy(e);
349
343
  }
350
- addSystem(t) {
351
- this.systemManager.addSystem(t);
344
+ addSystem(e) {
345
+ this.systemManager.addSystem(e);
352
346
  }
353
- removeSystem(t) {
354
- this.systemManager.removeSystem(t);
347
+ removeSystem(e) {
348
+ this.systemManager.removeSystem(e);
355
349
  }
356
- addComponent(t, e, s) {
357
- this.componentManager.addComponent(t, e, s);
350
+ addComponent(e, t, s) {
351
+ this.componentManager.addComponent(e, t, s);
358
352
  }
359
- getComponent(t, e) {
360
- return this.componentManager.getComponent(t, e);
353
+ getComponent(e, t) {
354
+ return this.componentManager.getComponent(e, t);
361
355
  }
362
- removeComponent(t, e) {
363
- this.componentManager.removeComponent(t, e);
356
+ removeComponent(e, t) {
357
+ this.componentManager.removeComponent(e, t);
364
358
  }
365
359
  update() {
366
- this.systemManager.systemList.forEach((t) => {
367
- t.update();
360
+ this.systemManager.systemList.forEach((e) => {
361
+ e.update();
368
362
  }), this.emit("updated");
369
363
  }
370
364
  }
371
365
  export {
372
366
  m as ComponentManager,
373
367
  l as EntityManager,
374
- h as EventRegistry,
375
- a as Query,
376
- c as SparseSet,
377
- d as SystemManager,
368
+ a as EventRegistry,
369
+ c as Query,
370
+ d as SparseSet,
371
+ u as SystemManager,
378
372
  g as Time,
379
373
  f as World
380
374
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jael-ecs/core",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Entity Component System library with typescript",
5
5
  "keywords": [
6
6
  "ecs",