@dill-pixel/plugin-matter-physics
Version:
Matter Physics
421 lines (420 loc) • 13.3 kB
JavaScript
import { Application as x, Logger as h, Plugin as G, Container as v, resolvePointLike as p, resolveSizeLike as B } from "dill-pixel";
import r, { Runner as c, World as u, Engine as g, Bodies as l, Body as C } from "matter-js";
import { Graphics as D } from "pixi.js";
const e = class e {
static get ceiling() {
return e._ceiling;
}
static get floor() {
return e._floor;
}
static get leftWall() {
return e._leftWall;
}
static get rightWall() {
return e._rightWall;
}
static get walls() {
return e._walls;
}
static get debug() {
return e._debug;
}
static set debug(t) {
var i;
e._debug = t, !e._debug && e._debugGraphics && (e._debugGraphics.destroy(), (i = e._debugGraphics.parent) == null || i.removeChild(e._debugGraphics), e._debugGraphics = null);
}
static get enabled() {
return e._enabled;
}
static set enabled(t) {
e._enabled = t, e.app.ticker.remove(e.update), e._enabled ? (e._engine && c.run(e._engine), e.app.ticker.add(e.update)) : (e._runner && c.stop(e._runner), e.app.ticker.remove(e.update));
}
static get world() {
return u;
}
static get api() {
return {
axes: r.Axes,
bodies: r.Bodies,
body: r.Body,
common: r.Common,
composite: r.Composite,
composites: r.Composites,
constraint: r.Constraint,
contact: r.Contact,
engine: r.Engine,
events: r.Events,
runner: r.Runner,
sleeping: r.Sleeping,
vector: r.Vector,
vertices: r.Vertices,
world: r.World
};
}
static get runner() {
return e._runner;
}
static get engine() {
return e._engine;
}
static get bounds() {
return e._bounds;
}
static set bounds(t) {
e._bounds = t;
}
static get app() {
return x.getInstance();
}
static destroy() {
var t, i;
h.warn("MatterPhysicsPlugin:: Destroying Matter Physics System"), e.enabled = !1;
try {
u.clear((t = e._engine) == null ? void 0 : t.world, !1);
} catch (o) {
h.warn(o);
}
try {
g.clear(e._engine);
} catch (o) {
h.warn(o);
}
try {
c.stop(e._runner);
} catch (o) {
h.warn(o);
}
try {
e._objects.clear(), (i = e._debugGraphics) == null || i.destroy({ children: !0 }), e._debugGraphics = null;
} catch (o) {
h.warn(o);
}
}
static initialize(t) {
if (e._options = { ...e.pluginOptions, ...t }, e._engine = g.create(e._options.engine), e._runner = c.create(e._options.runner), c.run(e._engine), e._options.container && (e.container = e._options.container), e._options.worldBounds && (e.bounds = e._options.worldBounds), e._options.createWalls) {
const i = e._options.createWalls.thickness ?? 10, { width: o, height: s } = e.bounds, n = [], a = {
isStatic: !0,
density: 1e3,
friction: 0,
frictionStatic: 0
};
if (e._options.createWalls.top) {
const d = l.rectangle(o / 2, -i / 2, o, i, a);
n.push(d), e._ceiling = d;
}
if (e._options.createWalls.bottom) {
const d = l.rectangle(o / 2, s + i / 2, o, i, a);
n.push(d), e._floor = d;
}
if (e._options.createWalls.left) {
const d = l.rectangle(-i / 2, s / 2, i, s + i, a);
n.push(d), e._leftWall = d;
}
if (e._options.createWalls.right) {
const d = l.rectangle(
o + i / 2,
s / 2,
i,
s + i,
a
);
n.push(d), e._rightWall = d;
}
e._walls = n, e.addToWorld(...n);
}
e._options.debug && (e.debug = !0);
}
static addToWorld(...t) {
t.forEach((i) => {
let o;
i.hasOwnProperty("body") ? (o = i.body, this._objects.add(i)) : o = i, u.add(e._engine.world, o);
});
}
static removeFromWorld(...t) {
t.forEach((i) => {
let o;
i.hasOwnProperty("body") ? (o = i.body, this._objects.add(i)) : o = i, u.remove(this._engine.world, o), e._objects.delete(i);
});
}
static drawDebug() {
var t;
e.enabled && (e._debugGraphics || (e._debugGraphics = new D(), e._debugGraphics.zIndex = 1e3, e._debugGraphics.sortableChildren = !0), e.container && !e._debugGraphics.parent && e.container.addChild(e._debugGraphics), e._debugGraphics.clear(), e._objects.forEach((i) => {
const o = i.body, s = (i == null ? void 0 : i.debugColor) || 2737654;
if (o.parts && o.parts.length > 1)
for (let n = 1; n < o.parts.length; n++) {
const a = o.parts[n];
if (e._debugGraphics && a.vertices.length > 0) {
e._debugGraphics.moveTo(a.vertices[0].x, a.vertices[0].y);
for (let d = 1; d < a.vertices.length; d++)
e._debugGraphics.lineTo(a.vertices[d].x, a.vertices[d].y);
e._debugGraphics.lineTo(a.vertices[0].x, a.vertices[0].y), e._debugGraphics.fill({ color: s, alpha: 0.25 }), e._debugGraphics.stroke({ color: 16711680, pixelLine: !0 });
}
}
else {
const n = o.vertices;
if (e._debugGraphics && n.length > 0) {
e._debugGraphics.moveTo(n[0].x, n[0].y);
for (let a = 1; a < n.length; a++)
e._debugGraphics.lineTo(n[a].x, n[a].y);
e._debugGraphics.lineTo(n[0].x, n[0].y), e._debugGraphics.fill({ color: s, alpha: 0.25 }), e._debugGraphics.stroke({ color: 16711680, pixelLine: !0 });
}
}
}), (t = e._walls) == null || t.forEach((i) => {
const s = i.vertices;
if (e._debugGraphics && s.length > 0) {
e._debugGraphics.moveTo(s[0].x, s[0].y);
for (let n = 1; n < s.length; n++)
e._debugGraphics.lineTo(s[n].x, s[n].y);
e._debugGraphics.lineTo(s[0].x, s[0].y), e._debugGraphics.fill({ color: 65280, alpha: 1 }), e._debugGraphics.stroke({ color: 16711680, pixelLine: !0 });
}
}));
}
static update(t) {
e._enabled && e._engine && (e._objects.forEach((i) => {
i.update();
}), e.debug && e.drawDebug(), g.update(e._engine, 16.666666666666668, t.deltaTime));
}
};
e._objects = /* @__PURE__ */ new Set(), e._debugGraphics = null, e._debug = !1, e._enabled = !1;
let y = e;
const W = "6.2.3", k = "0.20.0", b = {
debug: !1,
autoInit: !1,
engine: {},
runner: {
delta: 1e3 / 60,
isFixed: !1,
enabled: !0
}
};
class P extends G {
get system() {
return y;
}
get matter() {
return r;
}
pause() {
this.system.enabled = !1;
}
resume() {
this.system.enabled = !0;
}
get add() {
if (!this.system.container)
throw new Error("Container not set");
try {
return this.system.container.add;
} catch {
throw new Error("Container is not a Dill Pixel Container");
}
}
hello() {
const t = `%c Dill Pixel Matter Physics Plugin v${W} | %cMatter.js v${k}`;
console.log(
t,
"background: rgba(31, 41, 55, 1);color: #74b64c",
"background: rgba(31, 41, 55, 1);color: #e91e63"
);
}
initialize(t) {
this._options = {
...b,
...t,
runner: { ...b.runner, ...t == null ? void 0 : t.runner },
engine: { ...b.engine, ...t == null ? void 0 : t.engine }
}, this._options.autoInit && this.system.initialize(this._options), this.hello();
}
destroy() {
this.system && this.system.destroy(), super.destroy();
}
}
class A extends v {
constructor() {
super(), this.bodies = [], this.joints = [];
}
get system() {
return y;
}
createBodies() {
}
createJoints() {
}
added() {
this.createBodies(), this.createJoints(), this.system.api.composite.add(this.system.engine.world, [...this.bodies, ...this.joints]);
}
onRemoved() {
this.system.api.composite.remove(this.system.engine.world, this.bodies);
}
update() {
}
}
const f = class f extends v {
constructor(t = {}) {
super(), this.config = t, this.bodyDefinition = {}, this._isDestroyed = !1, this.isGrounded = !1, this.onLandCallbacks = [], this.groundSensorHeight = 2, this.rotationBehavior = "follow", t.view && (this.view = this.add.existing(t.view)), t.bodyType && (this.bodyType = t.bodyType), t.bodyDefinition && (this.bodyDefinition = t.bodyDefinition), t.debugColor && (this.debugColor = t.debugColor), t.rotationBehavior && (this.rotationBehavior = t.rotationBehavior), t.offset ? this.offset = t.offset : this.offset = { x: 0, y: 0 };
}
get system() {
return y;
}
get velocity() {
return this.body.velocity;
}
get matter() {
return r;
}
set offset(t) {
this._offset = p(t);
}
get offset() {
return this._offset;
}
added() {
this.createBody(), this.system.addToWorld(this), this.setupCollisionDetection();
}
onRemoved() {
this.system.removeFromWorld(this.body);
}
destroy() {
this._isDestroyed = !0, this.system.removeFromWorld(this.body), super.destroy();
}
setSize(t, i) {
this.size = [t, i];
}
set size(t) {
const i = B(t);
this.config.size = { width: i.width, height: i.height }, this.createBody();
}
createBody() {
var t, i;
if (this.config.parts && this.config.parts.length > 0) {
const o = this.config.parts.map((s) => {
switch (s.type) {
case "rectangle":
return l.rectangle(this.x + s.x, this.y + s.y, s.width, s.height, s.bodyDefinition);
case "circle":
return l.circle(this.x + s.x, this.y + s.y, s.width * 0.5, s.bodyDefinition);
case "trapezoid":
return l.trapezoid(
this.x + s.x,
this.y + s.y,
s.width,
s.height,
0.5,
s.bodyDefinition
);
default:
return l.rectangle(this.x + s.x, this.y + s.y, s.width, s.height, s.bodyDefinition);
}
});
this.body = C.create({
parts: o,
...this.bodyDefinition
});
} else {
const o = ((t = this.config.size) == null ? void 0 : t.width) || this.view.width, s = ((i = this.config.size) == null ? void 0 : i.height) || this.view.height;
switch (this.bodyType) {
case "rectangle":
this.body = l.rectangle(this.x, this.y, o, s, {
...this.bodyDefinition
});
break;
case "circle":
this.body = l.circle(this.x, this.y, o * 0.5, {
...this.bodyDefinition
});
break;
case "convex":
break;
case "trapezoid":
this.body = l.trapezoid(this.x, this.y, o, s, 0.5, {
...this.bodyDefinition
});
break;
}
}
}
setVelocity(t) {
const i = p(t);
r.Body.setVelocity(this.body, i);
}
setVelocityX(t) {
r.Body.setVelocity(this.body, { x: t, y: this.body.velocity.y });
}
setVelocityY(t) {
r.Body.setVelocity(this.body, { x: this.body.velocity.x, y: t });
}
/**
* Sets up collision detection for the entity
*/
setupCollisionDetection() {
r.Events.on(this.system.engine, "collisionStart", (t) => {
t.pairs.forEach((i) => {
if (i.bodyA === this.body || i.bodyB === this.body) {
const o = i.bodyA === this.body ? i.bodyB : i.bodyA;
this.handleCollisionStart(o, i);
}
});
}), r.Events.on(this.system.engine, "collisionEnd", (t) => {
t.pairs.forEach((i) => {
if (i.bodyA === this.body || i.bodyB === this.body) {
const o = i.bodyA === this.body ? i.bodyB : i.bodyA;
this.handleCollisionEnd(o, i);
}
});
});
}
/**
* Handles the start of a collision
*/
handleCollisionStart(t, i) {
const s = i.collision.normal;
(i.bodyA === this.body ? s.y : -s.y) < -0.5 && (this.isGrounded = !0, this.onLandCallbacks.forEach((a) => a(t)));
}
/**
* Handles the end of a collision
*/
handleCollisionEnd(t, i) {
const s = i.collision.normal;
(i.bodyA === this.body ? s.y : -s.y) < -0.5 && (this.isGrounded = !1);
}
/**
* Register a callback for when the entity lands
*/
onLand(t) {
this.onLandCallbacks.push(t);
}
/**
* Returns whether the entity is currently on the ground
*/
getIsGrounded() {
return this.isGrounded;
}
/**
* Locks the rotation of the physics body, keeping it upright
*/
lockRotation() {
this.body && (r.Body.setInertia(this.body, 1 / 0), r.Body.setAngularVelocity(this.body, 0));
}
update() {
var t;
if (!this._isDestroyed && this.view && this.body) {
const i = this.body.angle;
if (this.offset.x !== 0 || this.offset.y !== 0) {
const o = Math.cos(i), s = Math.sin(i), n = this.offset.x * o - this.offset.y * s, a = this.offset.x * s + this.offset.y * o;
this.x = this.body.position.x + n, this.y = this.body.position.y + a;
} else
this.x = this.body.position.x, this.y = this.body.position.y;
this.rotationBehavior !== "none" && (this.config.parts && this.config.parts.length > 0 ? this.rotationBehavior === "firstPart" ? this.rotation = ((t = this.body.parts[1]) == null ? void 0 : t.angle) || 0 : this.rotation = this.body.angle : this.rotation = this.body.angle);
}
}
};
f.DEFAULT_DEBUG_COLOR = 2737654;
let m = f;
export {
A as CompositeEntity,
m as Entity,
y as System,
P as default
};
//# sourceMappingURL=dill-pixel-plugin-matter-physics.mjs.map