@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 +2 -1
- package/dist/jael-build.cjs +1 -1
- package/dist/jael-build.js +177 -183
- package/package.json +1 -1
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
|
-
- **
|
|
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
|
|
package/dist/jael-build.cjs
CHANGED
|
@@ -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
|
|
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;
|
package/dist/jael-build.js
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
class
|
|
1
|
+
class a {
|
|
2
2
|
_listeners = /* @__PURE__ */ new Map();
|
|
3
|
-
on(
|
|
4
|
-
if (this.contains(
|
|
3
|
+
on(e, t) {
|
|
4
|
+
if (this.contains(e, t))
|
|
5
5
|
return;
|
|
6
|
-
const s = this._listeners.get(
|
|
7
|
-
s ? s.add(
|
|
6
|
+
const s = this._listeners.get(e);
|
|
7
|
+
s ? s.add(t) : this._listeners.set(e, /* @__PURE__ */ new Set([t]));
|
|
8
8
|
}
|
|
9
|
-
off(
|
|
10
|
-
if (!this.contains(
|
|
9
|
+
off(e, t) {
|
|
10
|
+
if (!this.contains(e, t))
|
|
11
11
|
return;
|
|
12
|
-
const s = this._listeners.get(
|
|
13
|
-
s && s.delete(
|
|
12
|
+
const s = this._listeners.get(e);
|
|
13
|
+
s && s.delete(t);
|
|
14
14
|
}
|
|
15
|
-
once(
|
|
15
|
+
once(e, t) {
|
|
16
16
|
const s = ((n) => {
|
|
17
|
-
|
|
17
|
+
t(n), this.off(e, s);
|
|
18
18
|
});
|
|
19
|
-
this.on(
|
|
19
|
+
this.on(e, s);
|
|
20
20
|
}
|
|
21
|
-
clearEvent(
|
|
22
|
-
this._listeners.get(
|
|
21
|
+
clearEvent(e) {
|
|
22
|
+
this._listeners.get(e) && this._listeners.get(e)?.clear();
|
|
23
23
|
}
|
|
24
24
|
clearAllEvents() {
|
|
25
|
-
this._listeners.forEach((
|
|
25
|
+
this._listeners.forEach((e) => e.clear()), this._listeners.clear();
|
|
26
26
|
}
|
|
27
|
-
contains(
|
|
28
|
-
return this._listeners.get(
|
|
27
|
+
contains(e, t) {
|
|
28
|
+
return this._listeners.get(e) ? this._listeners.get(e).has(t) : !1;
|
|
29
29
|
}
|
|
30
|
-
emit(
|
|
31
|
-
this._listeners.get(
|
|
32
|
-
s(
|
|
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
|
|
36
|
+
class u {
|
|
37
37
|
systemList = [];
|
|
38
|
-
addSystem(
|
|
39
|
-
this.systemList.push(
|
|
38
|
+
addSystem(e) {
|
|
39
|
+
this.systemList.push(e), e.init?.(), this.reorder();
|
|
40
40
|
}
|
|
41
41
|
reorder() {
|
|
42
|
-
this.systemList.sort((
|
|
42
|
+
this.systemList.sort((e, t) => e.priority - t.priority);
|
|
43
43
|
}
|
|
44
|
-
has(
|
|
45
|
-
return this.systemList.indexOf(
|
|
44
|
+
has(e) {
|
|
45
|
+
return this.systemList.indexOf(e) > 0;
|
|
46
46
|
}
|
|
47
|
-
removeSystem(
|
|
48
|
-
if (!this.has(
|
|
49
|
-
const
|
|
50
|
-
|
|
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
|
|
53
|
+
class d {
|
|
54
54
|
denseValues = [];
|
|
55
55
|
sparse = /* @__PURE__ */ new Map();
|
|
56
56
|
[Symbol.iterator]() {
|
|
57
|
-
let
|
|
58
|
-
const
|
|
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: () => (
|
|
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(
|
|
73
|
-
this.has(
|
|
72
|
+
add(e) {
|
|
73
|
+
this.has(e) || (this.denseValues.push(e), this.sparse.set(e, this.denseValues.length - 1));
|
|
74
74
|
}
|
|
75
|
-
indexOf(
|
|
76
|
-
const
|
|
77
|
-
return
|
|
75
|
+
indexOf(e) {
|
|
76
|
+
const t = this.sparse.get(e);
|
|
77
|
+
return t !== void 0 ? t : -1;
|
|
78
78
|
}
|
|
79
|
-
remove(
|
|
80
|
-
if (!this.has(
|
|
81
|
-
const
|
|
82
|
-
this.sparse.delete(
|
|
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 !==
|
|
84
|
+
s !== e && (this.denseValues[t] = s, this.sparse.set(s, t)), this.denseValues.pop();
|
|
85
85
|
}
|
|
86
|
-
forEach(
|
|
87
|
-
for (let
|
|
88
|
-
t
|
|
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
|
|
95
|
-
this.remove(
|
|
94
|
+
for (let e of this)
|
|
95
|
+
this.remove(e);
|
|
96
96
|
}
|
|
97
|
-
has(
|
|
98
|
-
return this.sparse.has(
|
|
97
|
+
has(e) {
|
|
98
|
+
return this.sparse.has(e);
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
class
|
|
101
|
+
class h {
|
|
102
102
|
id;
|
|
103
103
|
_world;
|
|
104
|
-
constructor(
|
|
105
|
-
this.id =
|
|
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(
|
|
113
|
-
this._world.addComponent(this.id,
|
|
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(
|
|
120
|
-
this._world.removeComponent(this.id,
|
|
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(
|
|
128
|
-
return this._world.componentManager.hasComponent(this.id,
|
|
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(
|
|
136
|
-
return this._world.componentManager.getComponent(this.id,
|
|
135
|
+
get(e) {
|
|
136
|
+
return this._world.componentManager.getComponent(this.id, e);
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
|
-
class l extends
|
|
140
|
-
entityMap = new
|
|
139
|
+
class l extends a {
|
|
140
|
+
entityMap = new d();
|
|
141
141
|
nextId = 0;
|
|
142
142
|
_world;
|
|
143
|
-
constructor(
|
|
144
|
-
super(), this._world =
|
|
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
|
|
151
|
-
return this.entities.add(
|
|
150
|
+
const e = this.nextId++;
|
|
151
|
+
return this.entities.add(e), this.emit("create", e), e;
|
|
152
152
|
}
|
|
153
|
-
exist(
|
|
154
|
-
return this.entities.has(
|
|
153
|
+
exist(e) {
|
|
154
|
+
return this.entities.has(e);
|
|
155
155
|
}
|
|
156
156
|
size() {
|
|
157
157
|
return this.entities.size();
|
|
158
158
|
}
|
|
159
|
-
destroy(
|
|
160
|
-
return this.entities.remove(
|
|
159
|
+
destroy(e) {
|
|
160
|
+
return this.entities.remove(e), this.emit("destroy", e), e;
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
|
-
class m extends
|
|
163
|
+
class m extends a {
|
|
164
164
|
componentSet = {};
|
|
165
165
|
world;
|
|
166
|
-
constructor(
|
|
167
|
-
super(), this.world =
|
|
166
|
+
constructor(e) {
|
|
167
|
+
super(), this.world = e;
|
|
168
168
|
}
|
|
169
|
-
clearComponentSchema(
|
|
170
|
-
this.componentSet[
|
|
169
|
+
clearComponentSchema(e) {
|
|
170
|
+
this.componentSet[e] && delete this.componentSet[e];
|
|
171
171
|
}
|
|
172
|
-
addComponent(
|
|
173
|
-
|
|
174
|
-
|
|
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(
|
|
177
|
-
if (this.hasComponent(
|
|
178
|
-
return this.componentSet[
|
|
177
|
+
getComponent(e, t) {
|
|
178
|
+
if (this.hasComponent(e, t))
|
|
179
|
+
return this.componentSet[e][t];
|
|
179
180
|
}
|
|
180
|
-
hasComponent(
|
|
181
|
-
const s = this.componentSet[
|
|
182
|
-
return s ?
|
|
181
|
+
hasComponent(e, t) {
|
|
182
|
+
const s = this.componentSet[e];
|
|
183
|
+
return s ? t in s : !1;
|
|
183
184
|
}
|
|
184
|
-
removeComponent(
|
|
185
|
-
if (!this.componentSet[
|
|
186
|
-
const s = this.componentSet[
|
|
187
|
-
s && s[
|
|
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
|
|
191
|
+
class c extends a {
|
|
191
192
|
config;
|
|
192
193
|
entityMap;
|
|
193
194
|
entityInstancesCache;
|
|
194
195
|
world;
|
|
195
|
-
constructor(
|
|
196
|
-
super(), this.config =
|
|
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(
|
|
201
|
-
const
|
|
201
|
+
hasComponents(e) {
|
|
202
|
+
const t = this.world.componentManager;
|
|
202
203
|
return this.config.include?.every(
|
|
203
|
-
(s) =>
|
|
204
|
+
(s) => t.getComponent(e, s)
|
|
204
205
|
) && this.config.exclude?.every(
|
|
205
|
-
(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
|
|
213
|
+
return c.getHash(this.config);
|
|
213
214
|
}
|
|
214
215
|
get ids() {
|
|
215
216
|
return this.entityMap;
|
|
216
217
|
}
|
|
217
|
-
getCachedEntity(
|
|
218
|
-
const
|
|
219
|
-
if (
|
|
220
|
-
return
|
|
218
|
+
getCachedEntity(e) {
|
|
219
|
+
const t = this.entityInstancesCache.get(e);
|
|
220
|
+
if (t)
|
|
221
|
+
return t;
|
|
221
222
|
{
|
|
222
|
-
const s = this.world.getEntity(
|
|
223
|
-
return s ? (this.entityInstancesCache.set(
|
|
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
|
|
228
|
+
const e = [], t = this.entityMap.size() > 100;
|
|
228
229
|
return this.entityMap.forEach((s) => {
|
|
229
|
-
if (
|
|
230
|
+
if (t) {
|
|
230
231
|
const n = this.getCachedEntity(s);
|
|
231
|
-
n &&
|
|
232
|
+
n && e.push(n);
|
|
232
233
|
} else {
|
|
233
234
|
const n = this.world.getEntity(s);
|
|
234
|
-
n &&
|
|
235
|
+
n && e.push(n);
|
|
235
236
|
}
|
|
236
|
-
}),
|
|
237
|
+
}), e;
|
|
237
238
|
}
|
|
238
|
-
include(...
|
|
239
|
-
return this.world.include(...
|
|
239
|
+
include(...e) {
|
|
240
|
+
return this.world.include(...e);
|
|
240
241
|
}
|
|
241
|
-
exclude(...
|
|
242
|
-
return this.world.exclude(...
|
|
242
|
+
exclude(...e) {
|
|
243
|
+
return this.world.exclude(...e);
|
|
243
244
|
}
|
|
244
245
|
_checkExistingEntities() {
|
|
245
|
-
for (let
|
|
246
|
-
this.world.exist(
|
|
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
|
|
251
|
-
this.hasComponents(
|
|
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(
|
|
255
|
-
const
|
|
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
|
|
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
|
|
274
|
+
let e = 0;
|
|
274
275
|
if (this.running) {
|
|
275
|
-
const
|
|
276
|
-
|
|
276
|
+
const t = performance.now();
|
|
277
|
+
e = (t - this._oldTime) / 1e3, this._oldTime = t, this.elapsed += e;
|
|
277
278
|
}
|
|
278
|
-
this.delta =
|
|
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
|
|
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
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
(
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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(
|
|
328
|
-
const
|
|
329
|
-
let n = this.queries.get(
|
|
330
|
-
return n || (n = new
|
|
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((
|
|
327
|
+
this.queries.forEach((e) => e.checkEntities());
|
|
334
328
|
}
|
|
335
|
-
exist(
|
|
336
|
-
return this.entityManager.exist(
|
|
329
|
+
exist(e) {
|
|
330
|
+
return this.entityManager.exist(e);
|
|
337
331
|
}
|
|
338
|
-
include(...
|
|
339
|
-
return this.query({ include:
|
|
332
|
+
include(...e) {
|
|
333
|
+
return this.query({ include: e, exclude: [] });
|
|
340
334
|
}
|
|
341
|
-
exclude(...
|
|
342
|
-
return this.query({ include: [], exclude:
|
|
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(
|
|
348
|
-
this.entityManager.destroy(
|
|
341
|
+
destroy(e) {
|
|
342
|
+
this.entityManager.destroy(e);
|
|
349
343
|
}
|
|
350
|
-
addSystem(
|
|
351
|
-
this.systemManager.addSystem(
|
|
344
|
+
addSystem(e) {
|
|
345
|
+
this.systemManager.addSystem(e);
|
|
352
346
|
}
|
|
353
|
-
removeSystem(
|
|
354
|
-
this.systemManager.removeSystem(
|
|
347
|
+
removeSystem(e) {
|
|
348
|
+
this.systemManager.removeSystem(e);
|
|
355
349
|
}
|
|
356
|
-
addComponent(
|
|
357
|
-
this.componentManager.addComponent(
|
|
350
|
+
addComponent(e, t, s) {
|
|
351
|
+
this.componentManager.addComponent(e, t, s);
|
|
358
352
|
}
|
|
359
|
-
getComponent(
|
|
360
|
-
return this.componentManager.getComponent(
|
|
353
|
+
getComponent(e, t) {
|
|
354
|
+
return this.componentManager.getComponent(e, t);
|
|
361
355
|
}
|
|
362
|
-
removeComponent(
|
|
363
|
-
this.componentManager.removeComponent(
|
|
356
|
+
removeComponent(e, t) {
|
|
357
|
+
this.componentManager.removeComponent(e, t);
|
|
364
358
|
}
|
|
365
359
|
update() {
|
|
366
|
-
this.systemManager.systemList.forEach((
|
|
367
|
-
|
|
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
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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
|
};
|