@caperjs/plugin-crunch 0.1.0
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 +22 -0
- package/README.md +205 -0
- package/lib/Actor.d.ts +227 -0
- package/lib/Actor.d.ts.map +1 -0
- package/lib/CrunchPhysicsPlugin.d.ts +508 -0
- package/lib/CrunchPhysicsPlugin.d.ts.map +1 -0
- package/lib/Entity.d.ts +377 -0
- package/lib/Entity.d.ts.map +1 -0
- package/lib/Group.d.ts +122 -0
- package/lib/Group.d.ts.map +1 -0
- package/lib/Sensor.d.ts +161 -0
- package/lib/Sensor.d.ts.map +1 -0
- package/lib/Solid.d.ts +109 -0
- package/lib/Solid.d.ts.map +1 -0
- package/lib/System.d.ts +265 -0
- package/lib/System.d.ts.map +1 -0
- package/lib/caper-plugin-crunch.mjs +1331 -0
- package/lib/caper-plugin-crunch.mjs.map +1 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/interfaces.d.ts +32 -0
- package/lib/interfaces.d.ts.map +1 -0
- package/lib/types.d.ts +246 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/utils.d.ts +63 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/version.d.ts +2 -0
- package/lib/version.d.ts.map +1 -0
- package/package.json +42 -0
- package/src/Actor.ts +726 -0
- package/src/CrunchPhysicsPlugin.ts +718 -0
- package/src/Entity.ts +762 -0
- package/src/Group.ts +181 -0
- package/src/Sensor.ts +427 -0
- package/src/Solid.ts +380 -0
- package/src/System.ts +1546 -0
- package/src/collision-layers.d.ts +87 -0
- package/src/index.ts +18 -0
- package/src/interfaces.ts +33 -0
- package/src/types.ts +336 -0
- package/src/utils.ts +72 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,1331 @@
|
|
|
1
|
+
import { Application as e, Logger as t, Plugin as n, SignalConnections as r, bindAllMethods as i, isDev as a, randomUUID as o, resolvePointLike as s, resolveSizeLike as c, version as l } from "@caperjs/core";
|
|
2
|
+
import { Container as u, Graphics as d, Rectangle as f } from "pixi.js";
|
|
3
|
+
//#region src/types.ts
|
|
4
|
+
var p = /* @__PURE__ */ function(e) {
|
|
5
|
+
return e[e.NONE = 0] = "NONE", e[e.DEFAULT = 1] = "DEFAULT", e[e.PLAYER = 2] = "PLAYER", e[e.ENEMY = 4] = "ENEMY", e[e.PROJECTILE = 8] = "PROJECTILE", e[e.PLATFORM = 16] = "PLATFORM", e[e.TRIGGER = 32] = "TRIGGER", e[e.ITEM = 64] = "ITEM", e[e.WALL = 128] = "WALL", e[e.FX = 256] = "FX", e[e.ALL = 4294967295] = "ALL", e;
|
|
6
|
+
}({}), m = class e {
|
|
7
|
+
constructor() {
|
|
8
|
+
this._layers = /* @__PURE__ */ new Map(), this._usedIndices = /* @__PURE__ */ new Set();
|
|
9
|
+
}
|
|
10
|
+
static get instance() {
|
|
11
|
+
return e._instance ||= new e(), e._instance;
|
|
12
|
+
}
|
|
13
|
+
register(e, t, n) {
|
|
14
|
+
if (t < 0 || t > 15) throw Error("Custom collision layer index must be between 0 and 15");
|
|
15
|
+
if (this._usedIndices.has(t)) throw Error(`Collision layer index ${t} is already in use`);
|
|
16
|
+
if (this._layers.has(e)) throw Error(`Collision layer with name "${e}" already exists`);
|
|
17
|
+
let r = {
|
|
18
|
+
name: e,
|
|
19
|
+
value: 1 << t + 16,
|
|
20
|
+
description: n
|
|
21
|
+
};
|
|
22
|
+
return this._layers.set(e, r), this._usedIndices.add(t), r;
|
|
23
|
+
}
|
|
24
|
+
get(e) {
|
|
25
|
+
return this._layers.get(e);
|
|
26
|
+
}
|
|
27
|
+
getAll() {
|
|
28
|
+
return Array.from(this._layers.values());
|
|
29
|
+
}
|
|
30
|
+
has(e) {
|
|
31
|
+
return this._layers.has(e);
|
|
32
|
+
}
|
|
33
|
+
remove(e) {
|
|
34
|
+
let t = this._layers.get(e);
|
|
35
|
+
if (!t) return !1;
|
|
36
|
+
let n = t.value, r = Math.log2(n) - 16;
|
|
37
|
+
return this._usedIndices.delete(r), this._layers.delete(e);
|
|
38
|
+
}
|
|
39
|
+
clear() {
|
|
40
|
+
this._layers.clear(), this._usedIndices.clear();
|
|
41
|
+
}
|
|
42
|
+
getNextAvailableIndex() {
|
|
43
|
+
for (let e = 0; e < 16; e++) if (!this._usedIndices.has(e)) return e;
|
|
44
|
+
return -1;
|
|
45
|
+
}
|
|
46
|
+
}, h = {
|
|
47
|
+
createLayer(e) {
|
|
48
|
+
if (e < 0 || e > 15) throw Error("Custom collision layer index must be between 0 and 15");
|
|
49
|
+
return 1 << e + 16;
|
|
50
|
+
},
|
|
51
|
+
createMask(e) {
|
|
52
|
+
return e.reduce((e, t) => e | t, 0);
|
|
53
|
+
},
|
|
54
|
+
isLayerInMask(e, t) {
|
|
55
|
+
return (e & t) !== 0;
|
|
56
|
+
},
|
|
57
|
+
getRegistry() {
|
|
58
|
+
return m.instance;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/utils.ts
|
|
63
|
+
function g(e) {
|
|
64
|
+
let { x: t, y: n } = e.position === void 0 ? {
|
|
65
|
+
x: e?.x ?? 0,
|
|
66
|
+
y: e?.y ?? 0
|
|
67
|
+
} : s(e.position);
|
|
68
|
+
return {
|
|
69
|
+
x: t,
|
|
70
|
+
y: n
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function _(e) {
|
|
74
|
+
let { width: t, height: n } = e.size === void 0 ? {
|
|
75
|
+
width: e?.width ?? 32,
|
|
76
|
+
height: e?.height ?? 32
|
|
77
|
+
} : c(e.size);
|
|
78
|
+
return {
|
|
79
|
+
width: t,
|
|
80
|
+
height: n
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/Entity.ts
|
|
85
|
+
var v = class {
|
|
86
|
+
set active(e) {
|
|
87
|
+
this._active = e;
|
|
88
|
+
}
|
|
89
|
+
get active() {
|
|
90
|
+
return this._active;
|
|
91
|
+
}
|
|
92
|
+
set id(e) {
|
|
93
|
+
this._id = e;
|
|
94
|
+
}
|
|
95
|
+
get id() {
|
|
96
|
+
return this._id || this.type;
|
|
97
|
+
}
|
|
98
|
+
get make() {
|
|
99
|
+
return this.app.make;
|
|
100
|
+
}
|
|
101
|
+
set data(e) {
|
|
102
|
+
this._data = e;
|
|
103
|
+
}
|
|
104
|
+
get data() {
|
|
105
|
+
return this._data;
|
|
106
|
+
}
|
|
107
|
+
setFollowing(e, t = {
|
|
108
|
+
x: 0,
|
|
109
|
+
y: 0
|
|
110
|
+
}) {
|
|
111
|
+
this._followOffset = s(t), this._following && this.system.removeFollower(this), this._following = e, e && this.system.addFollower(e, this);
|
|
112
|
+
}
|
|
113
|
+
get followOffset() {
|
|
114
|
+
return this._followOffset || {
|
|
115
|
+
x: 0,
|
|
116
|
+
y: 0
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
get following() {
|
|
120
|
+
return this._following;
|
|
121
|
+
}
|
|
122
|
+
get followers() {
|
|
123
|
+
return this.system.getFollowersOf(this);
|
|
124
|
+
}
|
|
125
|
+
get group() {
|
|
126
|
+
return this._group;
|
|
127
|
+
}
|
|
128
|
+
set groupOffset(e) {
|
|
129
|
+
this._groupOffset = s(e);
|
|
130
|
+
}
|
|
131
|
+
get groupOffset() {
|
|
132
|
+
return this._groupOffset || {
|
|
133
|
+
x: 0,
|
|
134
|
+
y: 0
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
setGroup(e, t = {
|
|
138
|
+
x: 0,
|
|
139
|
+
y: 0
|
|
140
|
+
}) {
|
|
141
|
+
this._groupOffset = s(t), this._group && (this.system.removeFromGroup(this), this.onRemovedFromGroup()), this._group = e, e && (this.system.addToGroup(e, this), this.onAddedToGroup());
|
|
142
|
+
}
|
|
143
|
+
set position(e) {
|
|
144
|
+
let { x: t, y: n } = s(e);
|
|
145
|
+
this.setPosition(t, n);
|
|
146
|
+
}
|
|
147
|
+
get position() {
|
|
148
|
+
return {
|
|
149
|
+
x: this.x,
|
|
150
|
+
y: this.y
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
set x(e) {
|
|
154
|
+
this._x = e;
|
|
155
|
+
}
|
|
156
|
+
get x() {
|
|
157
|
+
return this._group ? Math.round(this._x + this._group.getChildOffset(this).x) : Math.round(this._x);
|
|
158
|
+
}
|
|
159
|
+
set y(e) {
|
|
160
|
+
this._y = e;
|
|
161
|
+
}
|
|
162
|
+
get y() {
|
|
163
|
+
return this._group ? Math.round(this._y + this._group.getChildOffset(this).y) : Math.round(this._y);
|
|
164
|
+
}
|
|
165
|
+
get isCulled() {
|
|
166
|
+
return this._isCulled;
|
|
167
|
+
}
|
|
168
|
+
get isDestroyed() {
|
|
169
|
+
return this._isDestroyed;
|
|
170
|
+
}
|
|
171
|
+
get app() {
|
|
172
|
+
return e.getInstance();
|
|
173
|
+
}
|
|
174
|
+
get physics() {
|
|
175
|
+
return this.app.getPlugin("crunch");
|
|
176
|
+
}
|
|
177
|
+
constructor(e) {
|
|
178
|
+
this.collisionLayer = p.NONE, this.collisionMask = p.NONE, this._active = !0, this.updatedFollowPosition = !1, this.updatedGroupPosition = !1, i(this), this._config = e, this.signalConnections = new r(), this.shouldRemoveOnCull = !1, this.width = 0, this.height = 0, this._data = {}, this._group = null, this._isCulled = !1, this._isDestroyed = !1, this._isInitialized = !1, this._xRemainder = 0, this._yRemainder = 0, this._x = 0, this._y = 0, e && this.init(e), this.initialize(), this.addView();
|
|
179
|
+
}
|
|
180
|
+
initialize() {}
|
|
181
|
+
preUpdate() {}
|
|
182
|
+
update(e) {}
|
|
183
|
+
postUpdate() {}
|
|
184
|
+
excludeCollisionType() {
|
|
185
|
+
console.warn("excludeCollisionType is deprecated. Use setCollisionMask instead.");
|
|
186
|
+
}
|
|
187
|
+
includeCollisionType() {
|
|
188
|
+
console.warn("includeCollisionType is deprecated. Use setCollisionMask instead.");
|
|
189
|
+
}
|
|
190
|
+
removeCollisionType() {
|
|
191
|
+
console.warn("removeCollisionType is deprecated. Use removeCollisionMask instead.");
|
|
192
|
+
}
|
|
193
|
+
addCollisionType() {
|
|
194
|
+
console.warn("addCollisionType is deprecated. Use addCollisionMask instead.");
|
|
195
|
+
}
|
|
196
|
+
canCollideWith() {
|
|
197
|
+
return !0;
|
|
198
|
+
}
|
|
199
|
+
addView() {
|
|
200
|
+
this.view && (this.view.visible = !0, this.view.label = this.id || this.type, this.system.container && (this.system.container.addChild(this.view), this.updateView()));
|
|
201
|
+
}
|
|
202
|
+
init(e) {
|
|
203
|
+
if (!e) return;
|
|
204
|
+
this._config = e, e.id ? this._id = e.id : this._id = o(), e.type && (this.type = e.type), e.data && (this._data = e.data);
|
|
205
|
+
let t = g(e);
|
|
206
|
+
this._x = t.x, this._y = t.y;
|
|
207
|
+
let n = _(e);
|
|
208
|
+
this.width = n.width, this.height = n.height, e.collisionLayer !== void 0 && this.setCollisionLayer(e.collisionLayer), e.collisionMask !== void 0 && this.setCollisionMask(e.collisionMask), this._xRemainder = 0, this._yRemainder = 0, e.view && this.setView(e.view), e.group && this.setGroup(e.group ?? null, e.groupOffset ? s(e.groupOffset) : {
|
|
209
|
+
x: 0,
|
|
210
|
+
y: 0
|
|
211
|
+
}), e.follows && this.setFollowing(e.follows ?? null, e.followOffset ? s(e.followOffset) : {
|
|
212
|
+
x: 0,
|
|
213
|
+
y: 0
|
|
214
|
+
}), this.addView();
|
|
215
|
+
}
|
|
216
|
+
reset() {
|
|
217
|
+
this._isCulled = !1, this._isDestroyed = !1, this._xRemainder = 0, this._yRemainder = 0, this._followOffset = {
|
|
218
|
+
x: 0,
|
|
219
|
+
y: 0
|
|
220
|
+
}, this._following = null, this._groupOffset = {
|
|
221
|
+
x: 0,
|
|
222
|
+
y: 0
|
|
223
|
+
}, this._group = null, this._data = {}, this._x = -(2 ** 53 - 1), this._y = -(2 ** 53 - 1), this.view && (this.view.visible = !1), this.system.removeEntity(this);
|
|
224
|
+
}
|
|
225
|
+
get system() {
|
|
226
|
+
return this.physics.system;
|
|
227
|
+
}
|
|
228
|
+
onAddedToGroup() {}
|
|
229
|
+
onRemovedFromGroup() {}
|
|
230
|
+
updatePosition() {
|
|
231
|
+
this.x = this._x, this.y = this._y, this.updateView();
|
|
232
|
+
}
|
|
233
|
+
onCull() {
|
|
234
|
+
this._isCulled = !0, this.view && (this.view.visible = !1);
|
|
235
|
+
}
|
|
236
|
+
onUncull() {
|
|
237
|
+
this._isCulled = !1, this.view && (this.view.visible = !0);
|
|
238
|
+
}
|
|
239
|
+
destroy() {
|
|
240
|
+
this._isDestroyed || (this._isDestroyed = !0, this._isCulled = !1, this.signalConnections.disconnectAll(), this.view && (this.view.visible = !1, this.view.removeFromParent()), this.system.removeFollower(this));
|
|
241
|
+
}
|
|
242
|
+
onRemoved() {
|
|
243
|
+
this._isDestroyed || this.destroy();
|
|
244
|
+
}
|
|
245
|
+
setView(e) {
|
|
246
|
+
this.view = e, this.updateView();
|
|
247
|
+
}
|
|
248
|
+
updateView() {
|
|
249
|
+
this.view && this.view.visible && this.view.position && this.view.position.set(this.x, this.y);
|
|
250
|
+
}
|
|
251
|
+
getBounds() {
|
|
252
|
+
return {
|
|
253
|
+
x: this.x,
|
|
254
|
+
y: this.y,
|
|
255
|
+
width: this.width,
|
|
256
|
+
height: this.height
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
setPosition(e, t) {
|
|
260
|
+
this._x = e, this._y = t, this._xRemainder = 0, this._yRemainder = 0, this.updateView();
|
|
261
|
+
}
|
|
262
|
+
moveTo(e, t) {
|
|
263
|
+
this.setPosition(e, t);
|
|
264
|
+
}
|
|
265
|
+
addSignalConnection(...e) {
|
|
266
|
+
for (let t of e) this.signalConnections.add(t);
|
|
267
|
+
}
|
|
268
|
+
connectSignal(...e) {
|
|
269
|
+
for (let t of e) this.signalConnections.add(t);
|
|
270
|
+
}
|
|
271
|
+
connectAction(...e) {
|
|
272
|
+
for (let t of e) this.signalConnections.add(t);
|
|
273
|
+
}
|
|
274
|
+
canCollideWithEntity(e) {
|
|
275
|
+
return (this.collisionLayer & e.collisionMask) !== 0 && (e.collisionLayer & this.collisionMask) !== 0;
|
|
276
|
+
}
|
|
277
|
+
setCollisionLayer(e) {
|
|
278
|
+
this.collisionLayer = e;
|
|
279
|
+
}
|
|
280
|
+
addCollisionLayer(e) {
|
|
281
|
+
this.collisionLayer |= e;
|
|
282
|
+
}
|
|
283
|
+
removeCollisionLayer(e) {
|
|
284
|
+
this.collisionLayer &= ~e;
|
|
285
|
+
}
|
|
286
|
+
setCollisionMask(...e) {
|
|
287
|
+
this.collisionMask = this.physics.createCollisionMask(...e);
|
|
288
|
+
}
|
|
289
|
+
addCollisionMask(e) {
|
|
290
|
+
this.collisionMask |= e;
|
|
291
|
+
}
|
|
292
|
+
removeCollisionMask(e) {
|
|
293
|
+
this.collisionMask &= ~e;
|
|
294
|
+
}
|
|
295
|
+
hasCollisionLayer(e) {
|
|
296
|
+
return (this.collisionLayer & e) !== 0;
|
|
297
|
+
}
|
|
298
|
+
canCollideWithLayer(e) {
|
|
299
|
+
return (this.collisionMask & e) !== 0;
|
|
300
|
+
}
|
|
301
|
+
}, y = class extends v {
|
|
302
|
+
constructor(...e) {
|
|
303
|
+
super(...e), this.entityType = "Actor", this.velocity = {
|
|
304
|
+
x: 0,
|
|
305
|
+
y: 0
|
|
306
|
+
}, this.disableActorCollisions = !1, this.shouldRemoveOnCull = !0, this.collisions = [], this.actorCollisions = [], this._isRidingSolidCache = null, this._carriedBy = null, this._carriedByOverlap = 0, this._currentGridCells = [];
|
|
307
|
+
}
|
|
308
|
+
init(e) {
|
|
309
|
+
super.init(e), this.velocity = {
|
|
310
|
+
x: 0,
|
|
311
|
+
y: 0
|
|
312
|
+
}, this._isRidingSolidCache = null, this._carriedBy = null, this._carriedByOverlap = 0, this.actorCollisions = [], this._currentGridCells = [], e.disableActorCollisions !== void 0 && (this.disableActorCollisions = e.disableActorCollisions), this.system.enableActorCollisions && !this.disableActorCollisions && this.updateGridCells();
|
|
313
|
+
}
|
|
314
|
+
preUpdate() {
|
|
315
|
+
this.active && (this.collisions = [], this.actorCollisions = [], this._isRidingSolidCache = null, this._carriedBy = null, this._carriedByOverlap = 0);
|
|
316
|
+
}
|
|
317
|
+
update(e) {
|
|
318
|
+
this.active && (this.isRidingSolid() || (this.velocity.y += this.system.gravity * e), this.velocity.x = Math.min(Math.max(this.velocity.x, -this.system.maxVelocity), this.system.maxVelocity), this.velocity.y = Math.min(Math.max(this.velocity.y, -this.system.maxVelocity), this.system.maxVelocity), this.velocity.x !== 0 && this.moveX(this.velocity.x * e), this.velocity.y !== 0 && this.moveY(this.velocity.y * e), this.system.enableActorCollisions && this.updateGridCells(), this.updateView());
|
|
319
|
+
}
|
|
320
|
+
postUpdate() {
|
|
321
|
+
this.active && this.isRidingSolid() && (this.velocity.y = 0);
|
|
322
|
+
}
|
|
323
|
+
reset() {
|
|
324
|
+
super.reset(), this._isRidingSolidCache = null, this._carriedBy = null, this._carriedByOverlap = 0, this.velocity = {
|
|
325
|
+
x: 0,
|
|
326
|
+
y: 0
|
|
327
|
+
}, this.updatePosition();
|
|
328
|
+
}
|
|
329
|
+
onCull() {
|
|
330
|
+
this.view?.destroy();
|
|
331
|
+
}
|
|
332
|
+
onCollide(e) {}
|
|
333
|
+
onActorCollide(e) {}
|
|
334
|
+
isRiding(e) {
|
|
335
|
+
if (!e.collideable || (this.collisionLayer & e.collisionMask) === 0 || (e.collisionLayer & this.collisionMask) === 0 || this._carriedBy && this._carriedBy !== e) return !1;
|
|
336
|
+
let t = this.y + this.height, n = Math.abs(t - e.y) <= 1, r = this.x + this.width > e.x && this.x < e.x + e.width, i = Math.min(this.x + this.width, e.x + e.width) - Math.max(this.x, e.x), a = n && r;
|
|
337
|
+
return a && i > this._carriedByOverlap && (this._carriedBy = e, this._carriedByOverlap = i), a;
|
|
338
|
+
}
|
|
339
|
+
isRidingSolid() {
|
|
340
|
+
return this._isRidingSolidCache === null && (this._isRidingSolidCache = this.getSolidsAt(this.x, this.y + 1).some((e) => this.isRiding(e))), this._isRidingSolidCache;
|
|
341
|
+
}
|
|
342
|
+
squish(e) {}
|
|
343
|
+
updateGridCells() {
|
|
344
|
+
this.disableActorCollisions || this.system.updateActorInGrid(this);
|
|
345
|
+
}
|
|
346
|
+
get currentGridCells() {
|
|
347
|
+
return this._currentGridCells;
|
|
348
|
+
}
|
|
349
|
+
set currentGridCells(e) {
|
|
350
|
+
this._currentGridCells = e;
|
|
351
|
+
}
|
|
352
|
+
moveX(e, t, n) {
|
|
353
|
+
if (!this.active || e === 0) return [];
|
|
354
|
+
this._xRemainder += e;
|
|
355
|
+
let r = Math.round(this._xRemainder);
|
|
356
|
+
if (r === 0) return [];
|
|
357
|
+
let i = [], a = this.collisionLayer, o = this.collisionMask;
|
|
358
|
+
if (o === 0) return this._xRemainder -= r, this._x += r, this.updateView(), [];
|
|
359
|
+
this._xRemainder -= r;
|
|
360
|
+
let s = Math.sign(r), c = Math.abs(r), l = s;
|
|
361
|
+
for (n && (n.collideable = !1); c > 0;) {
|
|
362
|
+
let e = this._x + l, r = this.getSolidsAt(e, this._y), u = !1;
|
|
363
|
+
for (let e of r) {
|
|
364
|
+
if (!e.canCollide || (a & e.collisionMask) === 0 || (e.collisionLayer & o) === 0) continue;
|
|
365
|
+
let r = {
|
|
366
|
+
collided: !0,
|
|
367
|
+
solid: e,
|
|
368
|
+
normal: {
|
|
369
|
+
x: -s,
|
|
370
|
+
y: 0
|
|
371
|
+
},
|
|
372
|
+
penetration: l > 0 ? this.x + this.width - e.x : e.x + e.width - this.x,
|
|
373
|
+
pushingSolid: n
|
|
374
|
+
};
|
|
375
|
+
i.push(r), t && t(r), this.onCollide(r), u = !0;
|
|
376
|
+
}
|
|
377
|
+
if (u) break;
|
|
378
|
+
this._x = e, c--, (c % 4 == 0 || c === 0) && this.updateView();
|
|
379
|
+
}
|
|
380
|
+
return n && (n.collideable = !0), Math.abs(r) - c > 0 && this.updateView(), i;
|
|
381
|
+
}
|
|
382
|
+
moveY(e, t, n) {
|
|
383
|
+
if (!this.active || e === 0) return [];
|
|
384
|
+
this._yRemainder += e;
|
|
385
|
+
let r = Math.round(this._yRemainder);
|
|
386
|
+
if (r === 0) return [];
|
|
387
|
+
let i = [], a = this.collisionLayer, o = this.collisionMask;
|
|
388
|
+
if (o === 0) return this._yRemainder -= r, this._y += r, this.updateView(), [];
|
|
389
|
+
this._yRemainder -= r;
|
|
390
|
+
let s = Math.sign(r), c = Math.abs(r), l = s;
|
|
391
|
+
for (n && (n.collideable = !1); c > 0;) {
|
|
392
|
+
let e = this._y + l, r = this.getSolidsAt(this._x, e), u = !1;
|
|
393
|
+
for (let e of r) {
|
|
394
|
+
if (!e.canCollide || (a & e.collisionMask) === 0 || (e.collisionLayer & o) === 0) continue;
|
|
395
|
+
let r = {
|
|
396
|
+
collided: !0,
|
|
397
|
+
solid: e,
|
|
398
|
+
normal: {
|
|
399
|
+
x: 0,
|
|
400
|
+
y: -s
|
|
401
|
+
},
|
|
402
|
+
penetration: l > 0 ? this.y + this.height - e.y : e.y + e.height - this.y,
|
|
403
|
+
pushingSolid: n
|
|
404
|
+
};
|
|
405
|
+
i.push(r), t && t(r), this.onCollide(r), u = !0;
|
|
406
|
+
}
|
|
407
|
+
if (u) break;
|
|
408
|
+
this._y = e, c--, (c % 4 == 0 || c === 0) && this.updateView();
|
|
409
|
+
}
|
|
410
|
+
return n && (n.collideable = !0), Math.abs(r) - c > 0 && this.updateView(), i;
|
|
411
|
+
}
|
|
412
|
+
updateView() {
|
|
413
|
+
this.view && this.view.visible && (this.view.x = this._x, this.view.y = this._y);
|
|
414
|
+
}
|
|
415
|
+
getSolidsAt(e, t) {
|
|
416
|
+
return this.system.getSolidsAt(e, t, this);
|
|
417
|
+
}
|
|
418
|
+
checkActorCollision(e) {
|
|
419
|
+
if (!this.active || !e.active || this.disableActorCollisions || e.disableActorCollisions || (this.collisionLayer & e.collisionMask) === 0 || (e.collisionLayer & this.collisionMask) === 0) return {
|
|
420
|
+
collided: !1,
|
|
421
|
+
actor: e
|
|
422
|
+
};
|
|
423
|
+
let t = this.x, n = this.x + this.width, r = this.y, i = this.y + this.height, a = e.x, o = e.x + e.width, s = e.y, c = e.y + e.height;
|
|
424
|
+
if (n > a && t < o && i > s && r < c) {
|
|
425
|
+
let l = Math.min(n - a, o - t), u = Math.min(i - s, c - r), d, f;
|
|
426
|
+
return l < u ? (f = l, d = {
|
|
427
|
+
x: t < a ? -1 : 1,
|
|
428
|
+
y: 0
|
|
429
|
+
}) : (f = u, d = {
|
|
430
|
+
x: 0,
|
|
431
|
+
y: r < s ? -1 : 1
|
|
432
|
+
}), {
|
|
433
|
+
collided: !0,
|
|
434
|
+
actor: e,
|
|
435
|
+
normal: d,
|
|
436
|
+
penetration: f
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
return {
|
|
440
|
+
collided: !1,
|
|
441
|
+
actor: e
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
resolveActorCollision(e) {
|
|
445
|
+
return !e.collided || !e.normal || !e.penetration || this.onActorCollide(e), e;
|
|
446
|
+
}
|
|
447
|
+
setSize(e, t) {
|
|
448
|
+
let n = this.width !== e || this.height !== t;
|
|
449
|
+
this.width = e, this.height = t, n && this.system.enableActorCollisions && this.updateGridCells();
|
|
450
|
+
}
|
|
451
|
+
setWidth(e) {
|
|
452
|
+
this.width !== e && (this.width = e, this.system.enableActorCollisions && this.updateGridCells());
|
|
453
|
+
}
|
|
454
|
+
setHeight(e) {
|
|
455
|
+
this.height !== e && (this.height = e, this.system.enableActorCollisions && this.updateGridCells());
|
|
456
|
+
}
|
|
457
|
+
}, b = class extends v {
|
|
458
|
+
constructor(...e) {
|
|
459
|
+
super(...e), this._visible = !0, this.entityType = "Group", this.childOffsets = /* @__PURE__ */ new Map(), this.isStatic = !0;
|
|
460
|
+
}
|
|
461
|
+
getChildOffset(e) {
|
|
462
|
+
return this.childOffsets.get(e) ?? {
|
|
463
|
+
x: 0,
|
|
464
|
+
y: 0
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
set visible(e) {
|
|
468
|
+
this.system.getEntitiesInGroup(this).forEach((t) => {
|
|
469
|
+
t.view.visible = e;
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
get visible() {
|
|
473
|
+
return this._visible;
|
|
474
|
+
}
|
|
475
|
+
set x(e) {
|
|
476
|
+
this._x = e;
|
|
477
|
+
}
|
|
478
|
+
get x() {
|
|
479
|
+
return this._x;
|
|
480
|
+
}
|
|
481
|
+
set y(e) {
|
|
482
|
+
this._y = e;
|
|
483
|
+
}
|
|
484
|
+
get y() {
|
|
485
|
+
return this._y;
|
|
486
|
+
}
|
|
487
|
+
add(e, t = {
|
|
488
|
+
x: 0,
|
|
489
|
+
y: 0
|
|
490
|
+
}) {
|
|
491
|
+
return e.setGroup(this, t), this;
|
|
492
|
+
}
|
|
493
|
+
remove(e) {
|
|
494
|
+
return e.setGroup(null), this;
|
|
495
|
+
}
|
|
496
|
+
move(e, t) {
|
|
497
|
+
this._x += e, this._y += t, this.updateView();
|
|
498
|
+
}
|
|
499
|
+
moveTo(e, t) {
|
|
500
|
+
let n = e - this.x, r = t - this.y;
|
|
501
|
+
this.move(n, r);
|
|
502
|
+
}
|
|
503
|
+
getChildrenByType(e) {
|
|
504
|
+
return this.system.getEntitiesInGroup(this).filter((t) => t.entityType === e || t.type === e);
|
|
505
|
+
}
|
|
506
|
+
getActors() {
|
|
507
|
+
return this.getChildrenByType("Actor");
|
|
508
|
+
}
|
|
509
|
+
getSolids() {
|
|
510
|
+
return this.getChildrenByType("Solid");
|
|
511
|
+
}
|
|
512
|
+
getSensors() {
|
|
513
|
+
return this.getChildrenByType("Sensor");
|
|
514
|
+
}
|
|
515
|
+
getGroups() {
|
|
516
|
+
return this.getChildrenByType("Group");
|
|
517
|
+
}
|
|
518
|
+
destroy() {
|
|
519
|
+
this.system.removeGroup(this), super.destroy();
|
|
520
|
+
}
|
|
521
|
+
}, x = class extends v {
|
|
522
|
+
constructor(...e) {
|
|
523
|
+
super(...e), this.entityType = "Sensor", this.shouldRemoveOnCull = !1, this.collidableTypes = [], this.velocity = {
|
|
524
|
+
x: 0,
|
|
525
|
+
y: 0
|
|
526
|
+
}, this.isStatic = !1, this.overlappingActors = /* @__PURE__ */ new Set(), this._isRidingSolidCache = null, this._currentSensorOverlaps = /* @__PURE__ */ new Set(), this._currentOverlaps = /* @__PURE__ */ new Set();
|
|
527
|
+
}
|
|
528
|
+
setPosition(e, t) {
|
|
529
|
+
this.isStatic ? (this._x = e, this._y = t, this._xRemainder = 0, this._yRemainder = 0, this.updateView(), this.checkActorOverlaps()) : super.setPosition(e, t);
|
|
530
|
+
}
|
|
531
|
+
set x(e) {
|
|
532
|
+
super.x = e, this.isStatic && (this._xRemainder = 0, this.updateView(), this.checkActorOverlaps());
|
|
533
|
+
}
|
|
534
|
+
get x() {
|
|
535
|
+
return super.x;
|
|
536
|
+
}
|
|
537
|
+
set y(e) {
|
|
538
|
+
super.y = e, this.isStatic && (this._yRemainder = 0, this.updateView(), this.checkActorOverlaps());
|
|
539
|
+
}
|
|
540
|
+
get y() {
|
|
541
|
+
return super.y;
|
|
542
|
+
}
|
|
543
|
+
init(e) {
|
|
544
|
+
super.init(e), this.velocity ||= {
|
|
545
|
+
x: 0,
|
|
546
|
+
y: 0
|
|
547
|
+
}, this.velocity.x = 0, this.velocity.y = 0, this.overlappingActors ||= /* @__PURE__ */ new Set(), this._isRidingSolidCache = null;
|
|
548
|
+
}
|
|
549
|
+
isRiding(e) {
|
|
550
|
+
if (Math.sign(this.system.gravity) > 0) {
|
|
551
|
+
let t = this.y + this.height, n = Math.abs(t - e.y) <= 1, r = this.x + this.width > e.x && this.x < e.x + e.width;
|
|
552
|
+
return n && r;
|
|
553
|
+
} else {
|
|
554
|
+
let t = this.y, n = Math.abs(t - (e.y + e.height)) <= 1, r = this.x + this.width > e.x && this.x < e.x + e.width;
|
|
555
|
+
return n && r;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
isRidingSolid() {
|
|
559
|
+
return this._isRidingSolidCache === null && (this._isRidingSolidCache = this.getSolidsAt(this.x, this.system.gravity > 0 ? this.y + 1 : this.y - 1).some((e) => this.isRiding(e))), this._isRidingSolidCache;
|
|
560
|
+
}
|
|
561
|
+
moveStatic(e, t) {
|
|
562
|
+
this._x = e, this._y = t, this._xRemainder = 0, this._yRemainder = 0, this.updateView(), this.checkActorOverlaps();
|
|
563
|
+
}
|
|
564
|
+
moveX(e) {
|
|
565
|
+
if (this.isStatic) return;
|
|
566
|
+
this._xRemainder += e;
|
|
567
|
+
let t = Math.round(this._xRemainder);
|
|
568
|
+
if (t !== 0) {
|
|
569
|
+
this._xRemainder -= t;
|
|
570
|
+
let e = Math.sign(t), n = Math.abs(t);
|
|
571
|
+
for (; n > 0;) {
|
|
572
|
+
let t = e, r = this.x + t, i = !1;
|
|
573
|
+
for (let e of this.getSolidsAt(r, this.y)) if (e.canCollide) {
|
|
574
|
+
i = !0;
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
if (!i) this._x = r, n--, this.updateView(), this.checkActorOverlaps();
|
|
578
|
+
else {
|
|
579
|
+
this.velocity.x = 0;
|
|
580
|
+
break;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
moveY(e) {
|
|
586
|
+
if (this.isStatic) return;
|
|
587
|
+
this._yRemainder += e;
|
|
588
|
+
let t = Math.round(this._yRemainder);
|
|
589
|
+
if (t !== 0) {
|
|
590
|
+
this._yRemainder -= t;
|
|
591
|
+
let e = Math.sign(t), n = Math.abs(t);
|
|
592
|
+
for (; n > 0;) {
|
|
593
|
+
let t = e, r = this.y + t, i = !1;
|
|
594
|
+
if (Math.sign(this.system.gravity) === e) {
|
|
595
|
+
for (let e of this.getSolidsAt(this.x, r)) if (e.canCollide) {
|
|
596
|
+
i = !0;
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
if (!i) this._y = r, n--, this.updateView(), this.checkActorOverlaps();
|
|
601
|
+
else {
|
|
602
|
+
this.velocity.y = 0;
|
|
603
|
+
break;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
update(e) {
|
|
609
|
+
this._isRidingSolidCache = null, this.active && (!this.isStatic && !this.isRidingSolid() && (this.velocity.y += this.system.gravity * e), this.velocity.x !== 0 && this.moveX(this.velocity.x * e), this.velocity.y !== 0 && this.moveY(this.velocity.y * e));
|
|
610
|
+
}
|
|
611
|
+
checkActorOverlaps() {
|
|
612
|
+
if (this.collisionMask === 0 || !this.active) return /* @__PURE__ */ new Set();
|
|
613
|
+
this._currentSensorOverlaps.clear(), this._currentOverlaps.clear();
|
|
614
|
+
let e = this.collisionLayer, t = this.collisionMask, n = this.system.getActorsByType(this.collidableTypes);
|
|
615
|
+
for (let r of n) r.active && ((e & r.collisionMask) === 0 || (r.collisionLayer & t) === 0 || this.system.aabbOverlap(this, r) && (this._currentOverlaps.add(r), this.overlappingActors.has(r) || (this._currentSensorOverlaps.add({
|
|
616
|
+
actor: r,
|
|
617
|
+
sensor: this,
|
|
618
|
+
type: `${r.type}|${this.type}`
|
|
619
|
+
}), this.onActorEnter(r))));
|
|
620
|
+
for (let e of this.overlappingActors) this._currentOverlaps.has(e) || this.onActorExit(e);
|
|
621
|
+
let r = this.overlappingActors;
|
|
622
|
+
return this.overlappingActors = this._currentOverlaps, this._currentOverlaps = r, this._currentOverlaps.clear(), this._currentSensorOverlaps;
|
|
623
|
+
}
|
|
624
|
+
reset() {
|
|
625
|
+
super.reset(), this._currentSensorOverlaps.clear(), this._currentOverlaps.clear(), this._isRidingSolidCache = null, this.velocity = {
|
|
626
|
+
x: 0,
|
|
627
|
+
y: 0
|
|
628
|
+
}, this.overlappingActors = /* @__PURE__ */ new Set();
|
|
629
|
+
}
|
|
630
|
+
onActorEnter(e) {}
|
|
631
|
+
onActorExit(e) {}
|
|
632
|
+
getSolidsAt(e, t) {
|
|
633
|
+
return this.system.getSolidsAt(e, t, this);
|
|
634
|
+
}
|
|
635
|
+
}, S = class extends v {
|
|
636
|
+
constructor(...e) {
|
|
637
|
+
super(...e), this.entityType = "Solid", this.shouldRemoveOnCull = !1, this._canCollide = !0, this.collideable = !0, this.moving = !1;
|
|
638
|
+
}
|
|
639
|
+
get canCollide() {
|
|
640
|
+
return this._canCollide;
|
|
641
|
+
}
|
|
642
|
+
setPosition(e, t) {
|
|
643
|
+
this.x = e, this.y = t;
|
|
644
|
+
}
|
|
645
|
+
set x(e) {
|
|
646
|
+
this._nextX = Math.round(e), this.moving = !0;
|
|
647
|
+
}
|
|
648
|
+
get x() {
|
|
649
|
+
return this._x;
|
|
650
|
+
}
|
|
651
|
+
set y(e) {
|
|
652
|
+
this._nextY = Math.round(e), this.moving = !0;
|
|
653
|
+
}
|
|
654
|
+
get y() {
|
|
655
|
+
return this._y;
|
|
656
|
+
}
|
|
657
|
+
init(e) {
|
|
658
|
+
super.init(e), e && (this._nextX = this._x, this._nextY = this._y);
|
|
659
|
+
}
|
|
660
|
+
get right() {
|
|
661
|
+
return this.x + this.width;
|
|
662
|
+
}
|
|
663
|
+
get left() {
|
|
664
|
+
return this.x;
|
|
665
|
+
}
|
|
666
|
+
get top() {
|
|
667
|
+
return this.y;
|
|
668
|
+
}
|
|
669
|
+
get bottom() {
|
|
670
|
+
return this.y + this.height;
|
|
671
|
+
}
|
|
672
|
+
canCollideWith() {
|
|
673
|
+
return !!this.collideable;
|
|
674
|
+
}
|
|
675
|
+
move(e, t, n = this.system.actors, r = this.system.sensors, i = !1) {
|
|
676
|
+
if (!this.active) return;
|
|
677
|
+
let a = e + (this._nextX - this._x), o = t + (this._nextY - this._y);
|
|
678
|
+
this._xRemainder += a, this._yRemainder += o;
|
|
679
|
+
let s = Math.round(this._xRemainder), c = Math.round(this._yRemainder);
|
|
680
|
+
if (s !== 0 || c !== 0 || i) {
|
|
681
|
+
if (this.collideable) {
|
|
682
|
+
let e = /* @__PURE__ */ new Set(), t = /* @__PURE__ */ new Set();
|
|
683
|
+
for (let t of n) (t.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & t.collisionMask) !== 0 && t.isRiding(this) && e.add(t);
|
|
684
|
+
for (let e of r) (e.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & e.collisionMask) !== 0 && e.isRiding(this) && t.add(e);
|
|
685
|
+
if (this._canCollide = !1, s !== 0) if (this._xRemainder -= s, this._x += s, s > 0) {
|
|
686
|
+
for (let t of n) this.overlaps(t) && (t.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & t.collisionMask) !== 0 ? t.moveX(this.right - t.x, t.squish, this) : e.has(t) && t.moveX(s);
|
|
687
|
+
for (let e of r) this.overlaps(e) && (e.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & e.collisionMask) !== 0 ? e.moveX(this.right - e.x) : t.has(e) && e.moveX(s);
|
|
688
|
+
} else {
|
|
689
|
+
for (let t of n) this.overlaps(t) && (t.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & t.collisionMask) !== 0 ? t.moveX(this.left - (t.x + t.width), t.squish, this) : e.has(t) && t.moveX(s);
|
|
690
|
+
for (let e of r) this.overlaps(e) && (e.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & e.collisionMask) !== 0 ? e.moveX(this.left - (e.x + e.width)) : t.has(e) && e.moveX(s);
|
|
691
|
+
}
|
|
692
|
+
if (c !== 0) if (this._yRemainder -= c, this._y += c, c > 0) {
|
|
693
|
+
for (let t of n) this.overlaps(t) && (t.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & t.collisionMask) !== 0 ? t.moveY(this.bottom - t.y, t.squish, this) : e.has(t) && t.moveY(c);
|
|
694
|
+
for (let e of r) this.overlaps(e) && (e.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & e.collisionMask) !== 0 ? e.moveY(this.bottom - e.y) : t.has(e) && e.moveY(c);
|
|
695
|
+
} else {
|
|
696
|
+
for (let t of n) this.overlaps(t) && (t.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & t.collisionMask) !== 0 ? t.moveY(this.top - (t.y + t.height), t.squish, this) : e.has(t) && t.moveY(c);
|
|
697
|
+
for (let e of r) this.overlaps(e) && (e.collisionLayer & this.collisionMask) !== 0 && (this.collisionLayer & e.collisionMask) !== 0 ? e.moveY(this.top - (e.y + e.height)) : t.has(e) && e.moveY(c);
|
|
698
|
+
}
|
|
699
|
+
this._canCollide = !0;
|
|
700
|
+
} else s !== 0 && (this._xRemainder -= s, this._x += s), c !== 0 && (this._yRemainder -= c, this._y += c);
|
|
701
|
+
this._nextX = this._x, this._nextY = this._y, this.updateView();
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
overlaps(e) {
|
|
705
|
+
return (this.collisionLayer & e.collisionMask) === 0 || (e.collisionLayer & this.collisionMask) === 0 ? !1 : this.system.aabbOverlap(this, e);
|
|
706
|
+
}
|
|
707
|
+
}, C = class {
|
|
708
|
+
set debug(e) {
|
|
709
|
+
this._debug = e, this._debug ? (this._debugContainer ||= this.options.plugin.container.addChild(new u()), this._debugGfx ||= new d(), this._debugContainer.addChild(this._debugGfx)) : (this._debugGfx?.clear(), this._debugContainer?.removeChildren());
|
|
710
|
+
}
|
|
711
|
+
set gridSize(e) {
|
|
712
|
+
this.options.gridSize = e, this.grid.clear();
|
|
713
|
+
for (let e of this.solids) this.addSolidToGrid(e);
|
|
714
|
+
}
|
|
715
|
+
set gravity(e) {
|
|
716
|
+
this.options.gravity = e;
|
|
717
|
+
}
|
|
718
|
+
get gravity() {
|
|
719
|
+
return this.options.gravity;
|
|
720
|
+
}
|
|
721
|
+
set maxVelocity(e) {
|
|
722
|
+
this.options.maxVelocity = e;
|
|
723
|
+
}
|
|
724
|
+
get maxVelocity() {
|
|
725
|
+
return this.options.maxVelocity;
|
|
726
|
+
}
|
|
727
|
+
set boundary(e) {
|
|
728
|
+
this.options.boundary = e;
|
|
729
|
+
}
|
|
730
|
+
get boundary() {
|
|
731
|
+
return this.options.boundary;
|
|
732
|
+
}
|
|
733
|
+
get container() {
|
|
734
|
+
return this.options.plugin.container;
|
|
735
|
+
}
|
|
736
|
+
addView(e) {
|
|
737
|
+
this.container.addChild(e);
|
|
738
|
+
}
|
|
739
|
+
constructor(e) {
|
|
740
|
+
this.entities = /* @__PURE__ */ new Set(), this._flaggedEntities = /* @__PURE__ */ new Set(), this.actors = /* @__PURE__ */ new Set(), this.solids = /* @__PURE__ */ new Set(), this.sensors = /* @__PURE__ */ new Set(), this.groups = /* @__PURE__ */ new Set(), this.actorsByType = /* @__PURE__ */ new Map(), this.solidsByType = /* @__PURE__ */ new Map(), this.sensorsByType = /* @__PURE__ */ new Map(), this.groupsByType = /* @__PURE__ */ new Map(), this.followers = /* @__PURE__ */ new Map(), this.groupWithEntities = /* @__PURE__ */ new Map(), this.grid = /* @__PURE__ */ new Map(), this.collisions = [], this.sensorOverlaps = [], this.actorCollisions = [], this._checkedPairs = /* @__PURE__ */ new Set(), this._collisionResultPool = [], this._collisionResultPoolIndex = 0, this._actorCollisionPool = [], this._actorCollisionPoolIndex = 0, this._debugGfx = null, this._debug = !1, this._movedActors = /* @__PURE__ */ new Set(), this._activeGridCells = /* @__PURE__ */ new Set(), this._potentialCollisions = /* @__PURE__ */ new Map(), this.options = {
|
|
741
|
+
...e,
|
|
742
|
+
culling: e.culling ?? !1
|
|
743
|
+
}, this.debug = e.debug ?? !1, this._debugContainer = new u(), e.plugin.container.addChild(this._debugContainer);
|
|
744
|
+
}
|
|
745
|
+
_resetPositionFlags(e) {
|
|
746
|
+
e.updatedFollowPosition = !1, e.updatedGroupPosition = !1;
|
|
747
|
+
}
|
|
748
|
+
update(e) {
|
|
749
|
+
if (!this.options.plugin.enabled) return;
|
|
750
|
+
this.collisions.length = 0, this.sensorOverlaps.length = 0, this.actorCollisions.length = 0, this.resetCollisionPools();
|
|
751
|
+
let t = e / 60;
|
|
752
|
+
for (let e of this.groups) e.update(t);
|
|
753
|
+
this.updateSolids(t);
|
|
754
|
+
for (let e of this.sensors) this.updateSensor(e, t);
|
|
755
|
+
for (let e of this.actors) e.actorCollisions.length > 0 && (e.actorCollisions.length = 0);
|
|
756
|
+
this.updateActors(t), this.updateEntityPositions(), this.processCollisionsAndOverlaps(), this.options.culling && this.options.boundary && this.cullOutOfBounds(), this._debug && this.debugRender();
|
|
757
|
+
}
|
|
758
|
+
updateSolids(e) {
|
|
759
|
+
let t = !1;
|
|
760
|
+
for (let n of this.solids) n.preUpdate(), n.update(e), n.postUpdate(), n.moving && (t = !0);
|
|
761
|
+
if (!t) return;
|
|
762
|
+
let n = /* @__PURE__ */ new Set();
|
|
763
|
+
for (let e of this.solids) e.moving && (this.removeSolidFromGrid(e), e.move(0, 0, this.actors, this.sensors, !0), n.add(e));
|
|
764
|
+
for (let e of n) this.addSolidToGrid(e);
|
|
765
|
+
}
|
|
766
|
+
updateActors(e) {
|
|
767
|
+
if (this.actors.size !== 0) {
|
|
768
|
+
for (let t of this.actors) if (t.active) {
|
|
769
|
+
t.preUpdate(), t.update(e), t.postUpdate();
|
|
770
|
+
let n = t.collisions;
|
|
771
|
+
if (n.length > 0) for (let e of n) this.collisions.push({
|
|
772
|
+
type: `${t.type}|${e.solid.type}`,
|
|
773
|
+
entity1: t,
|
|
774
|
+
entity2: e.solid,
|
|
775
|
+
result: {
|
|
776
|
+
collided: e.collided,
|
|
777
|
+
normal: e.normal,
|
|
778
|
+
penetration: e.penetration,
|
|
779
|
+
solid: e.solid
|
|
780
|
+
}
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
updateEntityPositions() {
|
|
786
|
+
this._flaggedEntities.clear();
|
|
787
|
+
for (let [e, t] of this.followers) {
|
|
788
|
+
let n = e.x, r = e.y;
|
|
789
|
+
for (let i of t) if (!i.updatedFollowPosition) {
|
|
790
|
+
let t = i.group !== null;
|
|
791
|
+
if (t && i.group === e.group) {
|
|
792
|
+
let t = e.groupOffset, n = i.group.x, r = i.group.y;
|
|
793
|
+
i.setPosition(n + t.x + i.followOffset.x, r + t.y + i.followOffset.y);
|
|
794
|
+
} else t ? i.setPosition(n + i.followOffset.x + i.group.x + i.groupOffset.x, r + i.followOffset.y + i.group.y + i.groupOffset.y) : i.setPosition(n + i.followOffset.x, r + i.followOffset.y);
|
|
795
|
+
i.updatedFollowPosition = !0, this._flaggedEntities.add(i), t && (i.updatedGroupPosition = !0);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
for (let [e, t] of this.groupWithEntities) {
|
|
799
|
+
let n = e.x, r = e.y;
|
|
800
|
+
for (let e of t) !e.updatedGroupPosition && !e.updatedFollowPosition && (e.setPosition(n + e.groupOffset.x, r + e.groupOffset.y), e.updatedGroupPosition = !0, this._flaggedEntities.add(e));
|
|
801
|
+
}
|
|
802
|
+
this._flaggedEntities.forEach(this._resetPositionFlags);
|
|
803
|
+
}
|
|
804
|
+
processCollisionsAndOverlaps() {
|
|
805
|
+
this.options.overlapResolver && this.sensorOverlaps.length > 0 && this.options.overlapResolver(this.sensorOverlaps), this.options.enableActorCollisions && this.actors.size > 1 && this.checkActorCollisions(), this.collisions.length > 0 && this.options.collisionResolver && this.options.collisionResolver(this.collisions), this.actorCollisions.length > 0 && this.options.actorCollisionResolver && this.options.actorCollisionResolver(this.actorCollisions);
|
|
806
|
+
}
|
|
807
|
+
cullOutOfBounds() {
|
|
808
|
+
if (!this.options.boundary) return;
|
|
809
|
+
let e = this.options.boundary, t = [], n = [], r = [], i = [], a = e.x, o = e.y, s = a + e.width, c = o + e.height;
|
|
810
|
+
for (let e of this.actors) {
|
|
811
|
+
let n = e.x + e.width, r = e.y + e.height;
|
|
812
|
+
e.x >= s || n <= a || e.y >= c || r <= o ? (e.isCulled || e.onCull(), e.shouldRemoveOnCull && t.push(e)) : e.isCulled && e.onUncull();
|
|
813
|
+
}
|
|
814
|
+
for (let e of this.solids) {
|
|
815
|
+
let t = e.x + e.width, r = e.y + e.height;
|
|
816
|
+
e.x >= s || t <= a || e.y >= c || r <= o ? (e.isCulled || e.onCull(), e.shouldRemoveOnCull && n.push(e)) : e.isCulled && e.onUncull();
|
|
817
|
+
}
|
|
818
|
+
for (let e of this.sensors) {
|
|
819
|
+
let t = e.x + e.width, n = e.y + e.height;
|
|
820
|
+
e.x >= s || t <= a || e.y >= c || n <= o ? (e.isCulled || e.onCull(), e.shouldRemoveOnCull && r.push(e)) : e.isCulled && e.onUncull();
|
|
821
|
+
}
|
|
822
|
+
for (let e of this.groups) {
|
|
823
|
+
let t = e.x + e.width, n = e.y + e.height;
|
|
824
|
+
e.x >= s || t <= a || e.y >= c || n <= o ? (e.isCulled || e.onCull(), e.shouldRemoveOnCull && i.push(e)) : e.isCulled && e.onUncull();
|
|
825
|
+
}
|
|
826
|
+
let l = t.length, u = n.length, d = r.length, f = i.length;
|
|
827
|
+
if (l + u + d + f !== 0) {
|
|
828
|
+
for (let e = 0; e < l; e++) this.removeActor(t[e]);
|
|
829
|
+
for (let e = 0; e < u; e++) this.removeSolid(n[e]);
|
|
830
|
+
for (let e = 0; e < d; e++) this.removeSensor(r[e]);
|
|
831
|
+
for (let e = 0; e < f; e++) this.removeGroup(i[e]);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
updateSensor(e, t) {
|
|
835
|
+
e.preUpdate(), e.update(t), e.postUpdate();
|
|
836
|
+
let n = e.checkActorOverlaps();
|
|
837
|
+
this.sensorOverlaps.push(...n);
|
|
838
|
+
}
|
|
839
|
+
createEntity(e) {
|
|
840
|
+
if (e.type === "actor") return this.createActor(e);
|
|
841
|
+
if (e.type === "solid") return this.createSolid(e);
|
|
842
|
+
if (e.type === "sensor") return this.createSensor(e);
|
|
843
|
+
if (e.type === "group") return this.createGroup(e);
|
|
844
|
+
throw Error(`Invalid entity type: ${e.type}`);
|
|
845
|
+
}
|
|
846
|
+
addEntity(e) {
|
|
847
|
+
if (!e || !e.entityType) throw Error("Entity is required");
|
|
848
|
+
if (e.entityType === "Actor") return this.addActor(e);
|
|
849
|
+
if (e.entityType === "Solid") return this.addSolid(e);
|
|
850
|
+
if (e.entityType === "Sensor") return this.addSensor(e);
|
|
851
|
+
if (e.entityType === "Group") return this.addGroup(e);
|
|
852
|
+
throw Error(`Invalid entity type: ${e.entityType}`);
|
|
853
|
+
}
|
|
854
|
+
removeEntity(e) {
|
|
855
|
+
e.entityType === "Actor" ? this.removeActor(e) : e.entityType === "Solid" ? this.removeSolid(e) : e.entityType === "Sensor" ? this.removeSensor(e) : e.entityType === "Group" && this.removeGroup(e);
|
|
856
|
+
}
|
|
857
|
+
createActor(e) {
|
|
858
|
+
this.options.defaultCollisionLayer !== void 0 && e.collisionLayer === void 0 && (e.collisionLayer = this.options.defaultCollisionLayer), this.options.defaultCollisionMask !== void 0 && e.collisionMask === void 0 && (e.collisionMask = this.options.defaultCollisionMask);
|
|
859
|
+
let t = e.class ? new e.class(e) : new y(e);
|
|
860
|
+
return this.addActor(t);
|
|
861
|
+
}
|
|
862
|
+
addActor(e) {
|
|
863
|
+
return this.entities.add(e), this.actors.add(e), this.actorsByType.has(e.type) || this.actorsByType.set(e.type, /* @__PURE__ */ new Set()), this.actorsByType.get(e.type).add(e), this.options.enableActorCollisions && e.updateGridCells(), e;
|
|
864
|
+
}
|
|
865
|
+
createSensor(e) {
|
|
866
|
+
this.options.defaultCollisionLayer !== void 0 && e.collisionLayer === void 0 && (e.collisionLayer = this.options.defaultCollisionLayer), this.options.defaultCollisionMask !== void 0 && e.collisionMask === void 0 && (e.collisionMask = this.options.defaultCollisionMask);
|
|
867
|
+
let t = e.class ? new e.class(e) : new x(e);
|
|
868
|
+
return this.addSensor(t);
|
|
869
|
+
}
|
|
870
|
+
addSensor(e) {
|
|
871
|
+
return this.entities.add(e), this.sensors.add(e), this.sensorsByType.has(e.type) || this.sensorsByType.set(e.type, /* @__PURE__ */ new Set()), this.sensorsByType.get(e.type).add(e), e;
|
|
872
|
+
}
|
|
873
|
+
createSolid(e) {
|
|
874
|
+
this.options.defaultCollisionLayer !== void 0 && e.collisionLayer === void 0 && (e.collisionLayer = this.options.defaultCollisionLayer), this.options.defaultCollisionMask !== void 0 && e.collisionMask === void 0 && (e.collisionMask = this.options.defaultCollisionMask);
|
|
875
|
+
let t = e.class ? new e.class(e) : new S(e);
|
|
876
|
+
return this.addSolid(t);
|
|
877
|
+
}
|
|
878
|
+
addSolid(e) {
|
|
879
|
+
return this.entities.add(e), this.solids.add(e), this.solidsByType.has(e.type) || this.solidsByType.set(e.type, /* @__PURE__ */ new Set()), this.solidsByType.get(e.type).add(e), this.addSolidToGrid(e), e;
|
|
880
|
+
}
|
|
881
|
+
removeActor(e, t = !0) {
|
|
882
|
+
this.entities.delete(e), this.actors.delete(e);
|
|
883
|
+
let n = this.actorsByType.get(e.type);
|
|
884
|
+
n && (n.delete(e), n.size === 0 && this.actorsByType.delete(e.type)), e.onRemoved(), t && e.view?.removeFromParent(), this.options.enableActorCollisions && this.removeActorFromGrid(e);
|
|
885
|
+
}
|
|
886
|
+
removeSolid(e, t = !0) {
|
|
887
|
+
this.entities.delete(e), this.solids.delete(e);
|
|
888
|
+
let n = this.solidsByType.get(e.type);
|
|
889
|
+
n && (n.delete(e), n.size === 0 && this.solidsByType.delete(e.type)), this.removeSolidFromGrid(e), t && e.view?.removeFromParent(), e.onRemoved();
|
|
890
|
+
}
|
|
891
|
+
removeSensor(e, t = !0) {
|
|
892
|
+
this.entities.delete(e), this.sensors.delete(e);
|
|
893
|
+
let n = this.sensorsByType.get(e.type);
|
|
894
|
+
n && (n.delete(e), n.size === 0 && this.sensorsByType.delete(e.type)), e.onRemoved(), t && e.view?.removeFromParent();
|
|
895
|
+
}
|
|
896
|
+
moveSolid(e, t, n) {
|
|
897
|
+
this.removeSolidFromGrid(e), e.move(t, n, this.actors, this.sensors), this.addSolidToGrid(e), e.updateView();
|
|
898
|
+
}
|
|
899
|
+
getSolidsAt(e, t, n) {
|
|
900
|
+
if (n.collisionMask === 0) return [];
|
|
901
|
+
let r = {
|
|
902
|
+
x: e,
|
|
903
|
+
y: t,
|
|
904
|
+
width: n.width,
|
|
905
|
+
height: n.height
|
|
906
|
+
}, i = e - n.x, a = t - n.y, o = this.getCells(r);
|
|
907
|
+
if (i !== 0) {
|
|
908
|
+
let t = i > 0 ? Math.ceil((e + r.width) / this.options.gridSize) : Math.floor(e / this.options.gridSize) - 1;
|
|
909
|
+
for (let e = Math.floor(r.y / this.options.gridSize); e < Math.ceil((r.y + r.height) / this.options.gridSize); e++) o.push(`${t},${e}`);
|
|
910
|
+
}
|
|
911
|
+
if (a !== 0) {
|
|
912
|
+
let e = a > 0 ? Math.ceil((t + r.height) / this.options.gridSize) : Math.floor(t / this.options.gridSize) - 1;
|
|
913
|
+
for (let t = Math.floor(r.x / this.options.gridSize); t < Math.ceil((r.x + r.width) / this.options.gridSize); t++) o.push(`${t},${e}`);
|
|
914
|
+
}
|
|
915
|
+
let s = [], c = /* @__PURE__ */ new Set(), l = n.collisionLayer, u = n.collisionMask;
|
|
916
|
+
for (let e of o) {
|
|
917
|
+
let t = this.grid.get(e);
|
|
918
|
+
if (t) for (let e of t.solids) c.has(e) || (l & e.collisionMask) === 0 || (e.collisionLayer & u) === 0 || (c.add(e), this.overlaps(r, {
|
|
919
|
+
x: e.x,
|
|
920
|
+
y: e.y,
|
|
921
|
+
width: e.width,
|
|
922
|
+
height: e.height
|
|
923
|
+
}) && s.push(e));
|
|
924
|
+
}
|
|
925
|
+
return s;
|
|
926
|
+
}
|
|
927
|
+
addFollower(e, t) {
|
|
928
|
+
this.followers.has(e) || this.followers.set(e, /* @__PURE__ */ new Set()), this.followers.get(e).add(t);
|
|
929
|
+
}
|
|
930
|
+
removeFollower(e) {
|
|
931
|
+
for (let t of this.followers.values()) t.delete(e);
|
|
932
|
+
}
|
|
933
|
+
getFollowersOf(e) {
|
|
934
|
+
return Array.from(this.followers.get(e) || []);
|
|
935
|
+
}
|
|
936
|
+
removeFollowersOf(e) {
|
|
937
|
+
let t = this.followers.get(e);
|
|
938
|
+
t && (t.forEach((e) => {
|
|
939
|
+
e.destroy();
|
|
940
|
+
}), t.clear(), this.followers.delete(e));
|
|
941
|
+
}
|
|
942
|
+
addToGroup(e, t) {
|
|
943
|
+
return this.groupWithEntities.has(e) || this.groupWithEntities.set(e, /* @__PURE__ */ new Set()), this.groupWithEntities.get(e).add(t), t;
|
|
944
|
+
}
|
|
945
|
+
removeFromGroup(e) {
|
|
946
|
+
for (let t of this.groupWithEntities.values()) t.delete(e);
|
|
947
|
+
return e;
|
|
948
|
+
}
|
|
949
|
+
getEntitiesInGroup(e) {
|
|
950
|
+
return Array.from(this.groupWithEntities.get(e) || []);
|
|
951
|
+
}
|
|
952
|
+
removeEntitiesOfGroup(e) {
|
|
953
|
+
let t = Array.from(this.groupWithEntities.get(e) || []) || [];
|
|
954
|
+
return t.forEach((e) => {
|
|
955
|
+
e.setGroup(null);
|
|
956
|
+
}), this.groupWithEntities.delete(e), t;
|
|
957
|
+
}
|
|
958
|
+
overlaps(e, t) {
|
|
959
|
+
return e.x < t.x + t.width && e.x + e.width > t.x && e.y < t.y + t.height && e.y + e.height > t.y;
|
|
960
|
+
}
|
|
961
|
+
getCells(e) {
|
|
962
|
+
let t = [], { gridSize: n } = this.options, r = Math.floor(e.x / n), i = Math.floor(e.y / n), a = Math.ceil((e.x + e.width) / n), o = Math.ceil((e.y + e.height) / n);
|
|
963
|
+
t.length = (a - r) * (o - i);
|
|
964
|
+
let s = 0;
|
|
965
|
+
for (let e = r; e < a; e++) for (let n = i; n < o; n++) t[s++] = `${e},${n}`;
|
|
966
|
+
return t;
|
|
967
|
+
}
|
|
968
|
+
addSolidToGrid(e) {
|
|
969
|
+
let t = {
|
|
970
|
+
x: e.x,
|
|
971
|
+
y: e.y,
|
|
972
|
+
width: e.width,
|
|
973
|
+
height: e.height
|
|
974
|
+
}, n = this.getCells(t);
|
|
975
|
+
for (let t of n) this.grid.has(t) || this.grid.set(t, {
|
|
976
|
+
solids: /* @__PURE__ */ new Set(),
|
|
977
|
+
actors: /* @__PURE__ */ new Set()
|
|
978
|
+
}), this.grid.get(t).solids.add(e);
|
|
979
|
+
}
|
|
980
|
+
removeSolidFromGrid(e) {
|
|
981
|
+
let t = {
|
|
982
|
+
x: e.x,
|
|
983
|
+
y: e.y,
|
|
984
|
+
width: e.width,
|
|
985
|
+
height: e.height
|
|
986
|
+
}, n = this.getCells(t);
|
|
987
|
+
for (let t of n) {
|
|
988
|
+
let n = this.grid.get(t);
|
|
989
|
+
n && (n.solids.delete(e), n.solids.size === 0 && n.actors.size === 0 && this.grid.delete(t));
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
debugRender() {
|
|
993
|
+
if (!this._debugGfx) return;
|
|
994
|
+
let e = this._debugGfx;
|
|
995
|
+
if (e.clear(), this.options.boundary) {
|
|
996
|
+
let t = this.options.boundary;
|
|
997
|
+
e.rect(t.x, t.y, t.width, t.height), e.stroke({
|
|
998
|
+
color: 16711680,
|
|
999
|
+
width: 2,
|
|
1000
|
+
alignment: .5
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
for (let t of this.grid.keys()) {
|
|
1004
|
+
let [n, r] = t.split(",").map(Number);
|
|
1005
|
+
e.rect(n * this.options.gridSize, r * this.options.gridSize, this.options.gridSize, this.options.gridSize);
|
|
1006
|
+
}
|
|
1007
|
+
e.stroke({
|
|
1008
|
+
color: 65280,
|
|
1009
|
+
width: 1,
|
|
1010
|
+
join: "miter",
|
|
1011
|
+
cap: "butt"
|
|
1012
|
+
});
|
|
1013
|
+
for (let t of this.solids) e.rect(t.x, t.y, t.width, t.height), e.stroke({
|
|
1014
|
+
color: t.debugColor ?? 65280,
|
|
1015
|
+
alpha: 1
|
|
1016
|
+
});
|
|
1017
|
+
for (let t of this.actors) e.rect(t.x, t.y, t.width, t.height), e.stroke({
|
|
1018
|
+
color: t.debugColor ?? 16711680,
|
|
1019
|
+
alpha: 1
|
|
1020
|
+
});
|
|
1021
|
+
for (let t of this.sensors) e.rect(t.x, t.y, t.width, t.height), e.stroke({
|
|
1022
|
+
color: t.debugColor ?? 16776960,
|
|
1023
|
+
alpha: 1
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
getByType(e) {
|
|
1027
|
+
let t = this.actorsByType.get(e) || /* @__PURE__ */ new Set(), n = this.solidsByType.get(e) || /* @__PURE__ */ new Set();
|
|
1028
|
+
return [...t, ...n];
|
|
1029
|
+
}
|
|
1030
|
+
getActorsByType(e) {
|
|
1031
|
+
return Array.isArray(e) ? e.flatMap((e) => Array.from(this.actorsByType.get(e) || /* @__PURE__ */ new Set())) : Array.from(this.actorsByType.get(e) || /* @__PURE__ */ new Set());
|
|
1032
|
+
}
|
|
1033
|
+
getSolidsByType(e) {
|
|
1034
|
+
return Array.isArray(e) ? e.flatMap((e) => Array.from(this.solidsByType.get(e) || /* @__PURE__ */ new Set())) : Array.from(this.solidsByType.get(e) || /* @__PURE__ */ new Set());
|
|
1035
|
+
}
|
|
1036
|
+
getSensorsByType(e) {
|
|
1037
|
+
return Array.isArray(e) ? e.flatMap((e) => Array.from(this.sensorsByType.get(e) || /* @__PURE__ */ new Set())) : Array.from(this.sensorsByType.get(e) || /* @__PURE__ */ new Set());
|
|
1038
|
+
}
|
|
1039
|
+
clearGrid() {
|
|
1040
|
+
this.grid.clear();
|
|
1041
|
+
}
|
|
1042
|
+
clearAll(e = !0) {
|
|
1043
|
+
this.grid.clear(), this.entities.clear(), e && (this.solids.forEach((e) => {
|
|
1044
|
+
e.destroy();
|
|
1045
|
+
}), this.actors.forEach((e) => {
|
|
1046
|
+
e.destroy();
|
|
1047
|
+
}), this.sensors.forEach((e) => e.destroy())), this.solids.clear(), this.actors.clear(), this.sensors.clear(), this.solidsByType.clear(), this.actorsByType.clear(), this.sensorsByType.clear();
|
|
1048
|
+
}
|
|
1049
|
+
destroy() {
|
|
1050
|
+
this.debug = !1, this.gravity = 0, this.maxVelocity = 0, this.clearAll();
|
|
1051
|
+
}
|
|
1052
|
+
getCollisionResult() {
|
|
1053
|
+
if (this._collisionResultPoolIndex < this._collisionResultPool.length) {
|
|
1054
|
+
let e = this._collisionResultPool[this._collisionResultPoolIndex++];
|
|
1055
|
+
return e.collided = !1, e.actor = null, e.normal = void 0, e.penetration = 0, e;
|
|
1056
|
+
}
|
|
1057
|
+
let e = {
|
|
1058
|
+
collided: !1,
|
|
1059
|
+
actor: null,
|
|
1060
|
+
normal: void 0,
|
|
1061
|
+
penetration: 0
|
|
1062
|
+
};
|
|
1063
|
+
return this._collisionResultPool.push(e), this._collisionResultPoolIndex++, e;
|
|
1064
|
+
}
|
|
1065
|
+
getActorCollision() {
|
|
1066
|
+
if (this._actorCollisionPoolIndex < this._actorCollisionPool.length) return this._actorCollisionPool[this._actorCollisionPoolIndex++];
|
|
1067
|
+
let e = {
|
|
1068
|
+
type: "unknown|unknown",
|
|
1069
|
+
actor1: null,
|
|
1070
|
+
actor2: null,
|
|
1071
|
+
result: null
|
|
1072
|
+
};
|
|
1073
|
+
return this._actorCollisionPool.push(e), this._actorCollisionPoolIndex++, e;
|
|
1074
|
+
}
|
|
1075
|
+
resetCollisionPools() {
|
|
1076
|
+
this._collisionResultPoolIndex = 0, this._actorCollisionPoolIndex = 0;
|
|
1077
|
+
}
|
|
1078
|
+
updateActorInGrid(e) {
|
|
1079
|
+
if (!this.options.enableActorCollisions) return;
|
|
1080
|
+
if (!e.active || e.collisionLayer === 0 || e.collisionMask === 0) {
|
|
1081
|
+
e.currentGridCells.length > 0 && this.removeActorFromGrid(e);
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
this.removeActorFromGrid(e);
|
|
1085
|
+
let t = {
|
|
1086
|
+
x: e.x,
|
|
1087
|
+
y: e.y,
|
|
1088
|
+
width: e.width,
|
|
1089
|
+
height: e.height
|
|
1090
|
+
}, n = this.getCells(t);
|
|
1091
|
+
e.currentGridCells = n;
|
|
1092
|
+
for (let t of n) this.grid.has(t) || this.grid.set(t, {
|
|
1093
|
+
solids: /* @__PURE__ */ new Set(),
|
|
1094
|
+
actors: /* @__PURE__ */ new Set()
|
|
1095
|
+
}), this.grid.get(t).actors.add(e);
|
|
1096
|
+
}
|
|
1097
|
+
removeActorFromGrid(e) {
|
|
1098
|
+
let t = e.currentGridCells;
|
|
1099
|
+
for (let n of t) {
|
|
1100
|
+
let t = this.grid.get(n);
|
|
1101
|
+
t && (t.actors.delete(e), t.actors.size === 0 && t.solids.size === 0 && this.grid.delete(n));
|
|
1102
|
+
}
|
|
1103
|
+
e.currentGridCells = [];
|
|
1104
|
+
}
|
|
1105
|
+
checkActorCollisions() {
|
|
1106
|
+
if (!(this.actors.size <= 1 || !this.options.enableActorCollisions)) {
|
|
1107
|
+
this._checkedPairs.clear(), this._potentialCollisions.clear(), this._activeGridCells.clear();
|
|
1108
|
+
for (let [e, t] of this.grid.entries()) for (let n of t.actors) if (n.active && (n.velocity.x !== 0 || n.velocity.y !== 0)) {
|
|
1109
|
+
this._activeGridCells.add(e);
|
|
1110
|
+
break;
|
|
1111
|
+
}
|
|
1112
|
+
for (let e of this._activeGridCells) {
|
|
1113
|
+
let t = this.grid.get(e);
|
|
1114
|
+
if (!t || t.actors.size <= 1) continue;
|
|
1115
|
+
let n = Array.from(t.actors), r = n.length;
|
|
1116
|
+
for (let e = 0; e < r; e++) {
|
|
1117
|
+
let t = n[e];
|
|
1118
|
+
if (t.active) for (let i = e + 1; i < r; i++) {
|
|
1119
|
+
let e = n[i];
|
|
1120
|
+
if (!e.active) continue;
|
|
1121
|
+
let r = t.id < e.id ? `${t.id}|${e.id}` : `${e.id}|${t.id}`;
|
|
1122
|
+
this._checkedPairs.has(r) || (this._checkedPairs.add(r), !((t.collisionLayer & e.collisionMask) === 0 && (e.collisionLayer & t.collisionMask) === 0) && t.x < e.x + e.width && t.x + t.width > e.x && t.y < e.y + e.height && t.y + t.height > e.y && this._potentialCollisions.set(r, [t, e]));
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
for (let [e, t] of this._potentialCollisions.values()) {
|
|
1127
|
+
let n = e.checkActorCollision(t);
|
|
1128
|
+
if (n.collided) {
|
|
1129
|
+
e.actorCollisions.push(n);
|
|
1130
|
+
let r = this.getCollisionResult();
|
|
1131
|
+
r.collided = !0, r.actor = e, n.normal ? (r.normal ||= {
|
|
1132
|
+
x: 0,
|
|
1133
|
+
y: 0
|
|
1134
|
+
}, r.normal.x = -n.normal.x, r.normal.y = -n.normal.y) : r.normal = void 0, r.penetration = n.penetration, t.actorCollisions.push(r);
|
|
1135
|
+
let i = this.getActorCollision();
|
|
1136
|
+
i.type = `${e.type}|${t.type}`, i.actor1 = e, i.actor2 = t, i.result = n, this.actorCollisions[this._actorCollisionPoolIndex++] = i, e.onActorCollide(n), t.onActorCollide(r), e.resolveActorCollision(n), t.resolveActorCollision(r);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
this._actorCollisionPoolIndex > 0 && this.options.actorCollisionResolver && this.options.actorCollisionResolver(this.actorCollisions.slice(0, this._actorCollisionPoolIndex));
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
setCollisionResolver(e) {
|
|
1143
|
+
this.options.collisionResolver = e;
|
|
1144
|
+
}
|
|
1145
|
+
setActorCollisionResolver(e) {
|
|
1146
|
+
this.options.actorCollisionResolver = e;
|
|
1147
|
+
}
|
|
1148
|
+
get enableActorCollisions() {
|
|
1149
|
+
return !!this.options.enableActorCollisions;
|
|
1150
|
+
}
|
|
1151
|
+
setActorCollisionsEnabled(e) {
|
|
1152
|
+
let t = this.options.enableActorCollisions;
|
|
1153
|
+
if (this.options.enableActorCollisions = e, e && !t) for (let e of this.actors) e.updateGridCells();
|
|
1154
|
+
else if (!e && t) {
|
|
1155
|
+
this.grid.clear();
|
|
1156
|
+
for (let e of this.actors) e.currentGridCells = [];
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
createGroup(e) {
|
|
1160
|
+
this.options.defaultCollisionLayer !== void 0 && e.collisionLayer === void 0 && (e.collisionLayer = this.options.defaultCollisionLayer), this.options.defaultCollisionMask !== void 0 && e.collisionMask === void 0 && (e.collisionMask = this.options.defaultCollisionMask);
|
|
1161
|
+
let t = e.class ? new e.class(e) : new b(e);
|
|
1162
|
+
return this.addGroup(t);
|
|
1163
|
+
}
|
|
1164
|
+
addGroup(e) {
|
|
1165
|
+
return this.entities.add(e), this.groups.add(e), this.groupsByType.has(e.type) || this.groupsByType.set(e.type, /* @__PURE__ */ new Set()), this.groupsByType.get(e.type).add(e), e;
|
|
1166
|
+
}
|
|
1167
|
+
removeGroup(e, t = !0) {
|
|
1168
|
+
if (!this.groups.has(e)) return;
|
|
1169
|
+
this.entities.delete(e), this.groups.delete(e);
|
|
1170
|
+
let n = this.groupsByType.get(e.type);
|
|
1171
|
+
n && (n.delete(e), n.size === 0 && this.groupsByType.delete(e.type));
|
|
1172
|
+
let r = this.groupWithEntities.get(e);
|
|
1173
|
+
if (r && r.size > 0) {
|
|
1174
|
+
let e = [], n = [], i = [];
|
|
1175
|
+
for (let t of r) t.entityType === "Actor" ? e.push(t) : t.entityType === "Solid" ? n.push(t) : t.entityType === "Sensor" && i.push(t);
|
|
1176
|
+
for (let n of e) this.removeActor(n, t);
|
|
1177
|
+
for (let e of n) this.removeSolid(e, t);
|
|
1178
|
+
for (let e of i) this.removeSensor(e, t);
|
|
1179
|
+
}
|
|
1180
|
+
this.groupWithEntities.delete(e), e.onRemoved();
|
|
1181
|
+
}
|
|
1182
|
+
getGroupsByType(e) {
|
|
1183
|
+
if (Array.isArray(e)) {
|
|
1184
|
+
let t = 0;
|
|
1185
|
+
for (let n of e) {
|
|
1186
|
+
let e = this.groupsByType.get(n);
|
|
1187
|
+
e && (t += e.size);
|
|
1188
|
+
}
|
|
1189
|
+
let n = Array(t), r = 0;
|
|
1190
|
+
for (let t of e) {
|
|
1191
|
+
let e = this.groupsByType.get(t);
|
|
1192
|
+
if (e) for (let t of e) n[r++] = t;
|
|
1193
|
+
}
|
|
1194
|
+
return n;
|
|
1195
|
+
}
|
|
1196
|
+
return Array.from(this.groupsByType.get(e) || /* @__PURE__ */ new Set());
|
|
1197
|
+
}
|
|
1198
|
+
aabbOverlap(e, t) {
|
|
1199
|
+
return e.x < t.x + t.width && e.x + e.width > t.x && e.y < t.y + t.height && e.y + e.height > t.y;
|
|
1200
|
+
}
|
|
1201
|
+
}, w = {
|
|
1202
|
+
gridSize: 128,
|
|
1203
|
+
gravity: 900,
|
|
1204
|
+
maxVelocity: 400,
|
|
1205
|
+
debug: !1,
|
|
1206
|
+
culling: !0,
|
|
1207
|
+
boundary: new f(0, 0, 800, 600)
|
|
1208
|
+
}, T = class extends n {
|
|
1209
|
+
constructor() {
|
|
1210
|
+
super("crunch"), this.enabled = !1;
|
|
1211
|
+
}
|
|
1212
|
+
hello() {
|
|
1213
|
+
let e = `%c Caper Crunch Physics Plugin v${l}`;
|
|
1214
|
+
console.log(e, "background: rgba(31, 41, 55, 1);color: #74b64c");
|
|
1215
|
+
}
|
|
1216
|
+
async initialize(e, n) {
|
|
1217
|
+
if (this.hello(), !e) return;
|
|
1218
|
+
this._options = {
|
|
1219
|
+
...w,
|
|
1220
|
+
...e
|
|
1221
|
+
}, this.enabled = !0, this.container = e.container || this.app.stage, a && t.log(this._options);
|
|
1222
|
+
let r = e.gridSize ?? 32, i = e.gravity ?? 900, o = e.maxVelocity ?? 400, s = e.debug ?? !1, c = e.enableActorCollisions ?? !1;
|
|
1223
|
+
this.system = new C({
|
|
1224
|
+
gridSize: r,
|
|
1225
|
+
gravity: i,
|
|
1226
|
+
maxVelocity: o,
|
|
1227
|
+
boundary: e.boundary,
|
|
1228
|
+
culling: e.culling,
|
|
1229
|
+
plugin: this,
|
|
1230
|
+
collisionResolver: this.collisionResolver,
|
|
1231
|
+
overlapResolver: this.overlapResolver,
|
|
1232
|
+
actorCollisionResolver: this.actorCollisionResolver,
|
|
1233
|
+
enableActorCollisions: c,
|
|
1234
|
+
defaultCollisionLayer: p.NONE,
|
|
1235
|
+
defaultCollisionMask: p.NONE
|
|
1236
|
+
}), s && (this.system.debug = !0), this.app.ticker.add(this.update);
|
|
1237
|
+
}
|
|
1238
|
+
setCollisionResolver(e) {
|
|
1239
|
+
this.collisionResolver = e, this.system.setCollisionResolver(e);
|
|
1240
|
+
}
|
|
1241
|
+
setActorCollisionResolver(e) {
|
|
1242
|
+
this.actorCollisionResolver = e, this.system.setActorCollisionResolver(e);
|
|
1243
|
+
}
|
|
1244
|
+
setActorCollisionsEnabled(e) {
|
|
1245
|
+
this.system.setActorCollisionsEnabled(e);
|
|
1246
|
+
}
|
|
1247
|
+
createEntity(e) {
|
|
1248
|
+
return this.system.createEntity(e);
|
|
1249
|
+
}
|
|
1250
|
+
addEntity(e) {
|
|
1251
|
+
return this.system.addEntity(e);
|
|
1252
|
+
}
|
|
1253
|
+
createActor(e) {
|
|
1254
|
+
return this.system.createActor(e);
|
|
1255
|
+
}
|
|
1256
|
+
createSolid(e) {
|
|
1257
|
+
return this.system.createSolid(e);
|
|
1258
|
+
}
|
|
1259
|
+
createSensor(e) {
|
|
1260
|
+
return this.system.createSensor(e);
|
|
1261
|
+
}
|
|
1262
|
+
createGroup(e) {
|
|
1263
|
+
return this.system.createGroup(e);
|
|
1264
|
+
}
|
|
1265
|
+
addActor(e) {
|
|
1266
|
+
return this.system.addActor(e);
|
|
1267
|
+
}
|
|
1268
|
+
addSolid(e) {
|
|
1269
|
+
return this.system.addSolid(e);
|
|
1270
|
+
}
|
|
1271
|
+
addSensor(e) {
|
|
1272
|
+
return this.system.addSensor(e);
|
|
1273
|
+
}
|
|
1274
|
+
addGroup(e) {
|
|
1275
|
+
return this.system.addGroup(e);
|
|
1276
|
+
}
|
|
1277
|
+
removeActor(e) {
|
|
1278
|
+
this.system.removeActor(e);
|
|
1279
|
+
}
|
|
1280
|
+
removeSolid(e) {
|
|
1281
|
+
this.system.removeSolid(e);
|
|
1282
|
+
}
|
|
1283
|
+
removeSensor(e) {
|
|
1284
|
+
this.system.removeSensor(e);
|
|
1285
|
+
}
|
|
1286
|
+
removeGroup(e) {
|
|
1287
|
+
this.system.removeGroup(e);
|
|
1288
|
+
}
|
|
1289
|
+
destroy() {
|
|
1290
|
+
this.enabled = !1, this.app.ticker.remove(this.update), this.system &&= (this.system.destroy(), null), super.destroy();
|
|
1291
|
+
}
|
|
1292
|
+
update(e) {
|
|
1293
|
+
this.enabled && this.system.update(e.deltaTime);
|
|
1294
|
+
}
|
|
1295
|
+
createCollisionLayer(e) {
|
|
1296
|
+
return h.createLayer(e);
|
|
1297
|
+
}
|
|
1298
|
+
createCollisionMask(...e) {
|
|
1299
|
+
return h.createMask(e);
|
|
1300
|
+
}
|
|
1301
|
+
registerCollisionLayer(e, t, n) {
|
|
1302
|
+
let r = h.getRegistry();
|
|
1303
|
+
if (typeof t == "number") {
|
|
1304
|
+
let i = t;
|
|
1305
|
+
return r.register(e, i, n).value;
|
|
1306
|
+
} else {
|
|
1307
|
+
let n = t, i = r.getNextAvailableIndex();
|
|
1308
|
+
if (i === -1) throw Error("No more collision layer indices available (maximum 16)");
|
|
1309
|
+
return r.register(e, i, n).value;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
getCollisionLayer(e) {
|
|
1313
|
+
return h.getRegistry().get(e)?.value;
|
|
1314
|
+
}
|
|
1315
|
+
getCollisionLayers() {
|
|
1316
|
+
return h.getRegistry().getAll();
|
|
1317
|
+
}
|
|
1318
|
+
removeCollisionLayer(e) {
|
|
1319
|
+
return h.getRegistry().remove(e);
|
|
1320
|
+
}
|
|
1321
|
+
clearCollisionLayers() {
|
|
1322
|
+
h.getRegistry().clear();
|
|
1323
|
+
}
|
|
1324
|
+
aabbOverlap(e, t) {
|
|
1325
|
+
return this.system.aabbOverlap(e, t);
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
//#endregion
|
|
1329
|
+
export { y as Actor, p as CollisionLayer, m as CollisionLayerRegistry, h as CollisionLayers, v as Entity, b as Group, x as Sensor, S as Solid, C as System, T as default };
|
|
1330
|
+
|
|
1331
|
+
//# sourceMappingURL=caper-plugin-crunch.mjs.map
|