@skewedaspect/sage-core 0.10.0-beta.2 → 0.10.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/behavior.d.ts +7 -4
- package/dist/computedState.d.ts +11 -0
- package/dist/eventBus.d.ts +9 -1
- package/dist/events.d.ts +6 -1
- package/dist/gameEntity.d.ts +24 -8
- package/dist/index.d.ts +17 -15
- package/dist/namedBehavior.d.ts +1 -1
- package/dist/sage-core.js +308 -103
- package/dist/sage-core.js.map +1 -1
- package/dist/simulation.d.ts +48 -11
- package/dist/simulationBus.d.ts +13 -0
- package/dist/simulationContext.d.ts +2 -2
- package/dist/stateMachineBehavior.d.ts +3 -2
- package/package.json +5 -2
package/dist/sage-core.js
CHANGED
|
@@ -1,38 +1,96 @@
|
|
|
1
|
+
//#region src/computedState.ts
|
|
2
|
+
var e = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
function t(t) {
|
|
4
|
+
let n = e.get(t);
|
|
5
|
+
return n === void 0 && (n = [], e.set(t, n)), n;
|
|
6
|
+
}
|
|
7
|
+
function n(e, n, r) {
|
|
8
|
+
let i = Object.getOwnPropertyDescriptor(e, n);
|
|
9
|
+
i === void 0 && (i = {
|
|
10
|
+
value: void 0,
|
|
11
|
+
writable: !0,
|
|
12
|
+
enumerable: !0,
|
|
13
|
+
configurable: !0
|
|
14
|
+
}), t(e).push({
|
|
15
|
+
field: n,
|
|
16
|
+
beneath: i
|
|
17
|
+
}), Object.defineProperty(e, n, {
|
|
18
|
+
get: r.get,
|
|
19
|
+
set: r.set,
|
|
20
|
+
enumerable: !0,
|
|
21
|
+
configurable: !0
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function r(e) {
|
|
25
|
+
if (Array.isArray(e)) {
|
|
26
|
+
let t = [];
|
|
27
|
+
for (let n of e) t.push(r(n));
|
|
28
|
+
return t;
|
|
29
|
+
}
|
|
30
|
+
if (typeof e == "object" && e) {
|
|
31
|
+
let t = Object.create(null);
|
|
32
|
+
for (let [n, i] of Object.entries(e)) t[n] = r(i);
|
|
33
|
+
return t;
|
|
34
|
+
}
|
|
35
|
+
return e;
|
|
36
|
+
}
|
|
37
|
+
function i(t) {
|
|
38
|
+
let n = e.get(t);
|
|
39
|
+
if (n !== void 0) {
|
|
40
|
+
for (let e = n.length - 1; e >= 0; e--) {
|
|
41
|
+
let i = n[e], a = r(t[i.field]);
|
|
42
|
+
"value" in i.beneath ? Object.defineProperty(t, i.field, {
|
|
43
|
+
value: a,
|
|
44
|
+
writable: !0,
|
|
45
|
+
enumerable: !0,
|
|
46
|
+
configurable: !0
|
|
47
|
+
}) : Object.defineProperty(t, i.field, i.beneath);
|
|
48
|
+
}
|
|
49
|
+
n.length = 0;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
1
53
|
//#region src/behavior.ts
|
|
2
|
-
var
|
|
54
|
+
var a = class {
|
|
55
|
+
computed(e, t) {
|
|
56
|
+
n(this.state, e, t);
|
|
57
|
+
}
|
|
58
|
+
}, o = class extends a {
|
|
3
59
|
defaults = { displayName: "" };
|
|
4
60
|
};
|
|
5
61
|
//#endregion
|
|
6
62
|
//#region src/stateMachineBehavior.ts
|
|
7
|
-
function
|
|
63
|
+
function s(e, t) {
|
|
8
64
|
return t;
|
|
9
65
|
}
|
|
10
|
-
var
|
|
66
|
+
var c = class extends a {
|
|
11
67
|
static ops = ["transitionTo"];
|
|
12
68
|
onUpdate(e) {
|
|
13
69
|
let t = this.state.currentState;
|
|
70
|
+
if (!Object.hasOwn(this.states, t)) throw Error(`StateMachineBehavior on entity "${this.entity.id}": currentState is "${t}", which states does not declare.`);
|
|
14
71
|
this.states[t].update?.call(this, e);
|
|
15
72
|
}
|
|
16
73
|
transitionTo(e) {
|
|
17
|
-
if (!(
|
|
74
|
+
if (!Object.hasOwn(this.states, e)) throw Error(`StateMachineBehavior: no state named "${e}" is declared.`);
|
|
18
75
|
if (e === this.state.currentState) return;
|
|
19
76
|
let t = this.state.currentState;
|
|
20
77
|
this.states[t].exit?.call(this, e), this.state.currentState = e, this.states[e].enter?.call(this, t);
|
|
21
78
|
}
|
|
22
|
-
},
|
|
79
|
+
}, l = 32, u = 64, d = 5;
|
|
23
80
|
//#endregion
|
|
24
81
|
//#region src/events.ts
|
|
25
|
-
function
|
|
82
|
+
function f(e) {
|
|
26
83
|
let t = e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replace(/\\\*/g, ".*");
|
|
27
84
|
return RegExp(`^${t}$`);
|
|
28
85
|
}
|
|
29
86
|
//#endregion
|
|
30
87
|
//#region src/eventBus.ts
|
|
31
|
-
var
|
|
88
|
+
var p = class e {
|
|
32
89
|
#e = [];
|
|
33
90
|
#t = [];
|
|
34
91
|
#n = /* @__PURE__ */ new Map();
|
|
35
92
|
#r = /* @__PURE__ */ new Map();
|
|
93
|
+
#i = 0;
|
|
36
94
|
register(e, t) {
|
|
37
95
|
if (this.#n.has(e.id)) throw Error(`A subscriber is already registered under the ID "${e.id}".`);
|
|
38
96
|
this.#n.set(e.id, e);
|
|
@@ -41,13 +99,26 @@ var c = class e {
|
|
|
41
99
|
let t = i;
|
|
42
100
|
if (t.endsWith(":*")) {
|
|
43
101
|
let n = {
|
|
44
|
-
pattern:
|
|
102
|
+
pattern: f(t),
|
|
45
103
|
subscriber: e
|
|
46
104
|
};
|
|
47
105
|
r.push(n), this.#t.push(n);
|
|
48
|
-
} else n.includes(t) || (n.push(t), this.#
|
|
106
|
+
} else n.includes(t) || (n.push(t), this.#a(t, e));
|
|
49
107
|
}
|
|
50
|
-
return this.#
|
|
108
|
+
return this.#s(e, n, r);
|
|
109
|
+
}
|
|
110
|
+
on(e, t) {
|
|
111
|
+
this.#i += 1;
|
|
112
|
+
let n = {
|
|
113
|
+
id: this.mintSubscriberID(this.#i),
|
|
114
|
+
receive(e) {
|
|
115
|
+
return t(e), !1;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
return this.register(n, [e]);
|
|
119
|
+
}
|
|
120
|
+
mintSubscriberID(e) {
|
|
121
|
+
return `subscriber-${e}`;
|
|
51
122
|
}
|
|
52
123
|
emit(e) {
|
|
53
124
|
this.#e.push(e);
|
|
@@ -61,45 +132,54 @@ var c = class e {
|
|
|
61
132
|
}
|
|
62
133
|
let n = this.#e;
|
|
63
134
|
this.#e = [];
|
|
64
|
-
for (let t of n) this.#
|
|
135
|
+
for (let t of n) this.#c(t, e);
|
|
65
136
|
}
|
|
66
137
|
if (e.length > 0) throw AggregateError(e, "The drain raised.");
|
|
67
138
|
}
|
|
68
|
-
|
|
139
|
+
takeQueuedEvents() {
|
|
140
|
+
let e = this.#e;
|
|
141
|
+
return this.#e = [], e;
|
|
142
|
+
}
|
|
143
|
+
static reclaimQueuedEvents(e, t) {
|
|
144
|
+
let n = [], r = [];
|
|
145
|
+
for (let i of e.#e) t.includes(i) ? n.push(i) : r.push(i);
|
|
146
|
+
return e.#e = r, n;
|
|
147
|
+
}
|
|
148
|
+
#a(e, t) {
|
|
69
149
|
let n = this.#r.get(e) ?? [];
|
|
70
150
|
n.push(t), this.#r.set(e, n);
|
|
71
151
|
}
|
|
72
|
-
#
|
|
152
|
+
#o(e, t) {
|
|
73
153
|
let n = this.#r.get(e);
|
|
74
154
|
if (n === void 0) return;
|
|
75
155
|
let r = n.indexOf(t);
|
|
76
156
|
r >= 0 && n.splice(r, 1), n.length === 0 && this.#r.delete(e);
|
|
77
157
|
}
|
|
78
|
-
#
|
|
158
|
+
#s(e, t, n) {
|
|
79
159
|
let r = !0;
|
|
80
160
|
return () => {
|
|
81
161
|
if (!r) return;
|
|
82
162
|
r = !1, this.#n.get(e.id) === e && this.#n.delete(e.id);
|
|
83
|
-
for (let n of t) this.#
|
|
163
|
+
for (let n of t) this.#o(n, e);
|
|
84
164
|
let i = [];
|
|
85
165
|
for (let e of this.#t) n.includes(e) || i.push(e);
|
|
86
166
|
this.#t = i;
|
|
87
167
|
};
|
|
88
168
|
}
|
|
89
|
-
#
|
|
169
|
+
#c(t, n) {
|
|
90
170
|
if (t.targetID !== void 0) {
|
|
91
171
|
let r = this.#n.get(t.targetID);
|
|
92
|
-
r !== void 0 && e.#
|
|
172
|
+
r !== void 0 && e.#u(r, t, n);
|
|
93
173
|
return;
|
|
94
174
|
}
|
|
95
175
|
let r = /* @__PURE__ */ new Set();
|
|
96
|
-
for (let i of [...this.#r.get(t.type) ?? []]) e.#
|
|
97
|
-
for (let i of [...this.#t]) i.pattern.test(t.type) && e.#
|
|
176
|
+
for (let i of [...this.#r.get(t.type) ?? []]) e.#l(i, t, r, n);
|
|
177
|
+
for (let i of [...this.#t]) i.pattern.test(t.type) && e.#l(i.subscriber, t, r, n);
|
|
98
178
|
}
|
|
99
|
-
static #
|
|
100
|
-
r.has(t) || (r.add(t), e.#
|
|
179
|
+
static #l(t, n, r, i) {
|
|
180
|
+
r.has(t) || (r.add(t), e.#u(t, n, i));
|
|
101
181
|
}
|
|
102
|
-
static #
|
|
182
|
+
static #u(e, t, n) {
|
|
103
183
|
try {
|
|
104
184
|
e.receive(t);
|
|
105
185
|
} catch (e) {
|
|
@@ -109,57 +189,60 @@ var c = class e {
|
|
|
109
189
|
};
|
|
110
190
|
//#endregion
|
|
111
191
|
//#region src/gameEntity.ts
|
|
112
|
-
function
|
|
192
|
+
function m(e, t) {
|
|
113
193
|
throw TypeError(`Entity "${e}" state holds a value that is not finite JSON-compatible data at "${t}".`);
|
|
114
194
|
}
|
|
115
|
-
function
|
|
195
|
+
function h(e, t, n, r) {
|
|
116
196
|
if (e === null || typeof e == "string" || typeof e == "boolean") return e;
|
|
117
|
-
if (typeof e == "number") return Number.isFinite(e) ||
|
|
197
|
+
if (typeof e == "number") return Number.isFinite(e) || m(t, n), e;
|
|
118
198
|
if (Array.isArray(e)) {
|
|
119
|
-
r.has(e) &&
|
|
199
|
+
r.has(e) && m(t, n), r.add(e);
|
|
120
200
|
let i = [];
|
|
121
|
-
for (let [a, o] of e.entries()) i.push(
|
|
201
|
+
for (let [a, o] of e.entries()) i.push(h(o, t, `${n}[${a}]`, r));
|
|
122
202
|
return r.delete(e), i;
|
|
123
203
|
}
|
|
124
204
|
if (typeof e == "object") {
|
|
125
205
|
let i = Object.getPrototypeOf(e);
|
|
126
|
-
(i !== Object.prototype && i !== null || r.has(e)) &&
|
|
206
|
+
(i !== Object.prototype && i !== null || r.has(e)) && m(t, n), r.add(e);
|
|
127
207
|
let a = Object.create(null);
|
|
128
|
-
for (let [i, o] of Object.entries(e))
|
|
208
|
+
for (let [i, o] of Object.entries(e)) {
|
|
209
|
+
let e = `${n}.${i}`;
|
|
210
|
+
n === "" && (e = i), a[i] = h(o, t, e, r);
|
|
211
|
+
}
|
|
129
212
|
return r.delete(e), a;
|
|
130
213
|
}
|
|
131
|
-
return
|
|
214
|
+
return m(t, n);
|
|
132
215
|
}
|
|
133
|
-
function
|
|
134
|
-
return
|
|
216
|
+
function g(e, t) {
|
|
217
|
+
return h(e, t, "", /* @__PURE__ */ new Set());
|
|
135
218
|
}
|
|
136
|
-
var
|
|
219
|
+
var _ = class e {
|
|
137
220
|
id;
|
|
138
221
|
name;
|
|
139
222
|
context;
|
|
140
223
|
state;
|
|
141
224
|
ops;
|
|
142
|
-
eventSubscriptions;
|
|
143
225
|
static behaviors = [];
|
|
144
226
|
#e = [];
|
|
145
227
|
#t = [];
|
|
146
228
|
#n;
|
|
147
229
|
#r = 0;
|
|
148
230
|
#i;
|
|
231
|
+
#a = !1;
|
|
149
232
|
static create(t, n, r, i) {
|
|
150
|
-
return e.#
|
|
233
|
+
return e.#o(this, t, n, r ?? {}, !0, i);
|
|
151
234
|
}
|
|
152
235
|
static restore(t, n, r, i) {
|
|
153
|
-
return e.#
|
|
236
|
+
return e.#o(this, t, n, r, !1, i);
|
|
154
237
|
}
|
|
155
|
-
static #
|
|
238
|
+
static #o(e, t, n, r, i, a) {
|
|
156
239
|
return new e(t, n, r, i, a);
|
|
157
240
|
}
|
|
158
241
|
constructor(t, n, r, i, a) {
|
|
159
242
|
this.id = t, this.name = a ?? new.target.name, this.context = n;
|
|
160
243
|
let o = new.target.behaviors;
|
|
161
244
|
for (let e of o) this.#e.push(new e());
|
|
162
|
-
this.state = e.#
|
|
245
|
+
this.state = e.#d(this.#e, r, t);
|
|
163
246
|
let s = Object.create(null);
|
|
164
247
|
this.ops = s;
|
|
165
248
|
let c = [];
|
|
@@ -169,9 +252,11 @@ var f = class e {
|
|
|
169
252
|
state: this.state,
|
|
170
253
|
ops: s,
|
|
171
254
|
entity: this
|
|
172
|
-
}), this.#
|
|
255
|
+
}), this.#s(n, t, s), this.#c(n, t, c));
|
|
173
256
|
}
|
|
174
|
-
this.
|
|
257
|
+
this.declareComputedState(r);
|
|
258
|
+
for (let e of this.#e) e.onStateReady?.();
|
|
259
|
+
this.#n = n.bus.register(this, c), i && this.#u();
|
|
175
260
|
}
|
|
176
261
|
hasBehavior(e) {
|
|
177
262
|
for (let t of this.#e) if (t instanceof e) return !0;
|
|
@@ -194,32 +279,39 @@ var f = class e {
|
|
|
194
279
|
this.context.bus.emit(r);
|
|
195
280
|
}
|
|
196
281
|
update(e) {
|
|
197
|
-
for (let t of this.#e) t.onUpdate?.(e);
|
|
282
|
+
for (let t of this.#e) if (t.onUpdate?.(e), this.#a) return;
|
|
198
283
|
}
|
|
199
284
|
receive(t) {
|
|
200
285
|
let n = t;
|
|
201
|
-
for (let r of this.#t) if (e.#
|
|
286
|
+
for (let r of this.#t) if (e.#p(r, n.type) && r.instance.onEvent?.(t) === !0) return !0;
|
|
202
287
|
return !1;
|
|
203
288
|
}
|
|
204
289
|
destroy() {
|
|
205
|
-
this.#n();
|
|
290
|
+
this.#a = !0, this.removeComputedState(), this.#n();
|
|
291
|
+
}
|
|
292
|
+
declareComputedState(e) {}
|
|
293
|
+
computed(e, t) {
|
|
294
|
+
n(this.state, e, t);
|
|
295
|
+
}
|
|
296
|
+
removeComputedState() {
|
|
297
|
+
i(this.state);
|
|
206
298
|
}
|
|
207
299
|
attachedBehaviors() {
|
|
208
300
|
return [...this.#e];
|
|
209
301
|
}
|
|
210
|
-
#
|
|
302
|
+
#s(e, t, n) {
|
|
211
303
|
for (let r of t.ops ?? []) {
|
|
212
304
|
if (Object.hasOwn(n, r)) throw Error(`Two behaviors on entity "${this.id}" expose the operation "${String(r)}".`);
|
|
213
305
|
let i = e[r];
|
|
214
306
|
if (typeof i != "function") throw Error(`Behavior "${t.name}" declares the operation "${String(r)}" with no method behind it.`);
|
|
215
|
-
n[r] = this.#
|
|
307
|
+
n[r] = this.#l(r, i, e);
|
|
216
308
|
}
|
|
217
309
|
}
|
|
218
|
-
#
|
|
310
|
+
#c(e, t, n) {
|
|
219
311
|
let r = [], i = [];
|
|
220
312
|
for (let e of t.events ?? []) {
|
|
221
313
|
let t = e;
|
|
222
|
-
t.endsWith(":*") ? i.push(
|
|
314
|
+
t.endsWith(":*") ? i.push(f(t)) : r.push(t), n.includes(e) || n.push(e);
|
|
223
315
|
}
|
|
224
316
|
(r.length > 0 || i.length > 0) && this.#t.push({
|
|
225
317
|
instance: e,
|
|
@@ -227,7 +319,7 @@ var f = class e {
|
|
|
227
319
|
patterns: i
|
|
228
320
|
});
|
|
229
321
|
}
|
|
230
|
-
#
|
|
322
|
+
#l(e, t, n) {
|
|
231
323
|
return (...r) => {
|
|
232
324
|
this.#r += 1;
|
|
233
325
|
try {
|
|
@@ -238,7 +330,7 @@ var f = class e {
|
|
|
238
330
|
}
|
|
239
331
|
};
|
|
240
332
|
}
|
|
241
|
-
#
|
|
333
|
+
#u() {
|
|
242
334
|
this.#i = [];
|
|
243
335
|
try {
|
|
244
336
|
for (let e of this.#e) e.onCreate?.();
|
|
@@ -249,13 +341,13 @@ var f = class e {
|
|
|
249
341
|
this.#i = void 0;
|
|
250
342
|
for (let t of e) this.context.bus.emit(t);
|
|
251
343
|
}
|
|
252
|
-
static #
|
|
344
|
+
static #d(t, n, r) {
|
|
253
345
|
let i = Object.create(null);
|
|
254
346
|
i.tags = [];
|
|
255
|
-
for (let e of t) Object.assign(i,
|
|
256
|
-
return Object.assign(i,
|
|
347
|
+
for (let e of t) Object.assign(i, g(e.defaults, r));
|
|
348
|
+
return Object.assign(i, g(n, r)), e.#f(i, r), i;
|
|
257
349
|
}
|
|
258
|
-
static #
|
|
350
|
+
static #f(e, t) {
|
|
259
351
|
let n = e.tags;
|
|
260
352
|
if (!Array.isArray(n)) throw TypeError(`Entity "${t}" tags must be a list of strings.`);
|
|
261
353
|
let r = [];
|
|
@@ -265,120 +357,233 @@ var f = class e {
|
|
|
265
357
|
r.push(e);
|
|
266
358
|
}
|
|
267
359
|
}
|
|
268
|
-
static #
|
|
360
|
+
static #p(e, t) {
|
|
269
361
|
if (e.exact.includes(t)) return !0;
|
|
270
362
|
for (let n of e.patterns) if (n.test(t)) return !0;
|
|
271
363
|
return !1;
|
|
272
364
|
}
|
|
273
|
-
},
|
|
274
|
-
#e
|
|
365
|
+
}, v = class extends p {
|
|
366
|
+
#e;
|
|
367
|
+
#t;
|
|
368
|
+
#n = [];
|
|
369
|
+
#r;
|
|
370
|
+
#i = [];
|
|
371
|
+
constructor(e, t) {
|
|
372
|
+
super(), this.#e = e, this.#t = t;
|
|
373
|
+
}
|
|
374
|
+
get attached() {
|
|
375
|
+
return this.#r !== void 0;
|
|
376
|
+
}
|
|
377
|
+
mintSubscriberID(e) {
|
|
378
|
+
return `${this.#e}-subscriber-${e}`;
|
|
379
|
+
}
|
|
380
|
+
register(e, t) {
|
|
381
|
+
let n = this.#a(), r;
|
|
382
|
+
r = n === void 0 ? super.register(e, t) : n.register(e, t);
|
|
383
|
+
let i = {
|
|
384
|
+
subscriber: e,
|
|
385
|
+
subscriptions: t,
|
|
386
|
+
teardown: r
|
|
387
|
+
};
|
|
388
|
+
return this.#n.push(i), () => {
|
|
389
|
+
let e = this.#n.indexOf(i);
|
|
390
|
+
e < 0 || (this.#n.splice(e, 1), i.teardown());
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
emit(e) {
|
|
394
|
+
let t = this.#a();
|
|
395
|
+
t === void 0 ? super.emit(e) : (this.#i.push(e), t.emit(e));
|
|
396
|
+
}
|
|
397
|
+
drain() {
|
|
398
|
+
let e = this.#a();
|
|
399
|
+
if (e !== void 0) try {
|
|
400
|
+
e.drain();
|
|
401
|
+
} finally {
|
|
402
|
+
this.#i = [];
|
|
403
|
+
}
|
|
404
|
+
else super.drain();
|
|
405
|
+
}
|
|
406
|
+
attach() {
|
|
407
|
+
if (this.#t === void 0) throw Error(`The simulation "${this.#e}" was built with no engine bus to attach to.`);
|
|
408
|
+
if (this.#r === void 0) {
|
|
409
|
+
this.#r = this.#t.register({
|
|
410
|
+
id: this.#e,
|
|
411
|
+
receive: () => !1
|
|
412
|
+
}, []);
|
|
413
|
+
for (let e of this.#n) e.teardown(), e.teardown = this.#t.register(e.subscriber, e.subscriptions);
|
|
414
|
+
for (let e of this.takeQueuedEvents()) this.#t.emit(e);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
detach() {
|
|
418
|
+
if (this.#r === void 0) return;
|
|
419
|
+
for (let e of this.#n) e.teardown(), e.teardown = super.register(e.subscriber, e.subscriptions);
|
|
420
|
+
let e = this.#t;
|
|
421
|
+
if (e !== void 0) for (let t of p.reclaimQueuedEvents(e, this.#i)) super.emit(t);
|
|
422
|
+
this.#i = [], this.#r(), this.#r = void 0;
|
|
423
|
+
}
|
|
424
|
+
#a() {
|
|
425
|
+
if (this.#r !== void 0) return this.#t;
|
|
426
|
+
}
|
|
427
|
+
}, y = "global", b = 128 * 2 ** -52, x = class e {
|
|
428
|
+
#e;
|
|
275
429
|
#t;
|
|
276
430
|
#n;
|
|
277
431
|
#r;
|
|
278
|
-
#i
|
|
279
|
-
#a
|
|
432
|
+
#i;
|
|
433
|
+
#a;
|
|
280
434
|
#o = 0;
|
|
281
|
-
#s =
|
|
435
|
+
#s = 0;
|
|
436
|
+
#c = 0;
|
|
437
|
+
#l = [];
|
|
438
|
+
#u = /* @__PURE__ */ new Map();
|
|
439
|
+
#d = /* @__PURE__ */ new Set();
|
|
282
440
|
constructor(e) {
|
|
283
|
-
if (this.#
|
|
441
|
+
if (this.#e = e.id, this.#r = e.fixedStepDuration, this.#i = e.entityClasses, this.#a = e.allocator, this.#n = e.bus, this.#t = new v(e.id, e.bus), this.#e === y) throw Error(`The simulation name "${y}" is reserved for the persistent simulation.`);
|
|
442
|
+
if (!Number.isFinite(this.#r) || this.#r <= 0) throw RangeError("A fixed step duration must be a finite number greater than zero.");
|
|
284
443
|
}
|
|
285
|
-
get
|
|
444
|
+
get id() {
|
|
286
445
|
return this.#e;
|
|
287
446
|
}
|
|
447
|
+
get bus() {
|
|
448
|
+
return this.#t;
|
|
449
|
+
}
|
|
450
|
+
get attached() {
|
|
451
|
+
return this.#t.attached;
|
|
452
|
+
}
|
|
288
453
|
get tick() {
|
|
289
|
-
return this.#
|
|
454
|
+
return this.#o;
|
|
290
455
|
}
|
|
291
456
|
get entities() {
|
|
292
|
-
return [...this.#
|
|
457
|
+
return [...this.#l];
|
|
458
|
+
}
|
|
459
|
+
attach() {
|
|
460
|
+
this.#t.attach();
|
|
461
|
+
}
|
|
462
|
+
detach() {
|
|
463
|
+
this.#t.detach();
|
|
293
464
|
}
|
|
294
465
|
spawn(e, t, n) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
466
|
+
return this.spawnByName(e, t, n);
|
|
467
|
+
}
|
|
468
|
+
spawnByName(e, t, n) {
|
|
469
|
+
let r = this.#b(e), i = n;
|
|
470
|
+
if (i === void 0 && (i = this.#_()), this.#u.has(i)) throw Error(`The simulation already holds an entity under the ID "${i}".`);
|
|
471
|
+
if (this.#d.has(i)) throw Error(`The simulation already retired the ID "${i}"; an ID is never reused.`);
|
|
472
|
+
let a = r.create(i, this.#y(), t ?? {}, e);
|
|
473
|
+
return this.#l.push(a), this.#u.set(i, a), a;
|
|
299
474
|
}
|
|
300
475
|
despawn(e) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
n
|
|
476
|
+
return this.#f(e, /* @__PURE__ */ new Set());
|
|
477
|
+
}
|
|
478
|
+
#f(e, t) {
|
|
479
|
+
let n = this.#u.get(e);
|
|
480
|
+
if (n === void 0) return !1;
|
|
481
|
+
t.add(e);
|
|
482
|
+
for (let n of this.#g(e)) t.has(n.id) || this.#f(n.id, t);
|
|
483
|
+
return n.destroy(), this.#l.splice(this.#l.indexOf(n), 1), this.#u.delete(e), this.#d.add(e), !0;
|
|
305
484
|
}
|
|
306
485
|
getEntity(e) {
|
|
307
|
-
return this.#
|
|
486
|
+
return this.#u.get(e);
|
|
308
487
|
}
|
|
309
488
|
getEntitiesByTag(t) {
|
|
310
489
|
let n = [];
|
|
311
|
-
for (let r of this.#
|
|
490
|
+
for (let r of this.#l) e.#x(r).tags.includes(t) && n.push(r);
|
|
312
491
|
return n;
|
|
313
492
|
}
|
|
493
|
+
getEntitiesOfClass(e) {
|
|
494
|
+
let t = [];
|
|
495
|
+
for (let n of this.#l) n instanceof e && t.push(n);
|
|
496
|
+
return t;
|
|
497
|
+
}
|
|
314
498
|
runStepsForElapsedTime(e) {
|
|
315
499
|
if (!Number.isFinite(e) || e < 0) throw RangeError("Elapsed time must be a finite number of zero or more.");
|
|
316
|
-
this.#
|
|
500
|
+
this.#c += e;
|
|
317
501
|
let t = 0;
|
|
318
|
-
for (; this.#
|
|
319
|
-
t === 5 && this.#
|
|
502
|
+
for (; this.#m() && t < 5;) this.#h(), t += 1;
|
|
503
|
+
t === 5 && this.#m() && (this.#c %= this.#r);
|
|
320
504
|
}
|
|
321
505
|
snapshot() {
|
|
322
506
|
let e = [];
|
|
323
|
-
for (let t of this.#
|
|
507
|
+
for (let t of this.#l) e.push({
|
|
324
508
|
id: t.id,
|
|
325
509
|
name: t.name,
|
|
326
|
-
state:
|
|
510
|
+
state: g(t.state, t.id)
|
|
327
511
|
});
|
|
328
512
|
return {
|
|
329
|
-
|
|
330
|
-
|
|
513
|
+
id: this.#e,
|
|
514
|
+
tick: this.#o,
|
|
515
|
+
idCounter: this.#s,
|
|
331
516
|
entities: e
|
|
332
517
|
};
|
|
333
518
|
}
|
|
334
|
-
|
|
519
|
+
buildFromSnapshot(e) {
|
|
335
520
|
let t = this.buildReplacementSimulation({
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
521
|
+
id: e.id,
|
|
522
|
+
fixedStepDuration: this.#r,
|
|
523
|
+
entityClasses: this.#i,
|
|
524
|
+
allocator: this.#a,
|
|
525
|
+
bus: this.#n
|
|
339
526
|
});
|
|
340
|
-
return t.#
|
|
527
|
+
return t.#p(e), t;
|
|
341
528
|
}
|
|
342
529
|
buildReplacementSimulation(t) {
|
|
343
530
|
return new e(t);
|
|
344
531
|
}
|
|
345
|
-
#
|
|
532
|
+
#p(e) {
|
|
346
533
|
let t = [];
|
|
347
534
|
try {
|
|
348
535
|
for (let n of e.entities) {
|
|
349
|
-
let e = this.#
|
|
536
|
+
let e = this.#b(n.name).restore(n.id, this.#y(), n.state, n.name);
|
|
350
537
|
t.push(e);
|
|
351
538
|
}
|
|
352
539
|
} catch (e) {
|
|
353
540
|
for (let e of t) e.destroy();
|
|
354
541
|
throw e;
|
|
355
542
|
}
|
|
356
|
-
this.#
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
543
|
+
this.#l = t, this.#u = /* @__PURE__ */ new Map();
|
|
544
|
+
for (let e of t) this.#u.set(e.id, e);
|
|
545
|
+
this.#o = e.tick, this.#s = e.idCounter;
|
|
546
|
+
}
|
|
547
|
+
#m() {
|
|
548
|
+
let e = b * this.#r;
|
|
549
|
+
return this.#c + e >= this.#r;
|
|
550
|
+
}
|
|
551
|
+
advanceFixedStep(e) {}
|
|
552
|
+
#h() {
|
|
553
|
+
for (let e of [...this.#l]) this.#u.get(e.id) === e && e.update(this.#r);
|
|
554
|
+
this.advanceFixedStep(this.#r);
|
|
363
555
|
try {
|
|
364
|
-
this.#
|
|
556
|
+
this.#t.drain();
|
|
365
557
|
} finally {
|
|
366
|
-
this.#
|
|
558
|
+
this.#c -= this.#r, Math.abs(this.#c) <= b * this.#r && (this.#c = 0), this.#o += 1;
|
|
367
559
|
}
|
|
368
560
|
}
|
|
369
|
-
#
|
|
370
|
-
|
|
561
|
+
#g(e) {
|
|
562
|
+
let t = [];
|
|
563
|
+
for (let n of this.#l) n.state.parentID === e && t.push(n);
|
|
564
|
+
return t;
|
|
565
|
+
}
|
|
566
|
+
#_() {
|
|
567
|
+
if (this.#a !== void 0) return this.#a();
|
|
568
|
+
for (this.#s += 1; this.#v(`${this.#e}-${this.#s}`);) this.#s += 1;
|
|
569
|
+
return `${this.#e}-${this.#s}`;
|
|
570
|
+
}
|
|
571
|
+
#v(e) {
|
|
572
|
+
return this.#u.has(e) || this.#d.has(e);
|
|
573
|
+
}
|
|
574
|
+
#y() {
|
|
575
|
+
return this;
|
|
371
576
|
}
|
|
372
|
-
#
|
|
373
|
-
let t = this.#
|
|
374
|
-
if (t === void 0) throw Error(`The entity
|
|
577
|
+
#b(e) {
|
|
578
|
+
let t = this.#i[e];
|
|
579
|
+
if (t === void 0) throw Error(`The entity registry holds no class named "${e}".`);
|
|
375
580
|
return t;
|
|
376
581
|
}
|
|
377
|
-
static #
|
|
582
|
+
static #x(e) {
|
|
378
583
|
return e.state;
|
|
379
584
|
}
|
|
380
585
|
};
|
|
381
586
|
//#endregion
|
|
382
|
-
export {
|
|
587
|
+
export { a as Behavior, p as EventBus, _ as GameEntity, x as GameSimulationManager, o as NamedBehavior, c as StateMachineBehavior, s as defineStates, d as dilationLimit, u as drainPassLimit, l as operationCallDepthLimit };
|
|
383
588
|
|
|
384
589
|
//# sourceMappingURL=sage-core.js.map
|