Refactor ball creation helpers

This commit is contained in:
Daddy32
2025-12-29 21:59:11 +01:00
parent a663aa3fbc
commit bbea27a3f5

View File

@@ -117,6 +117,32 @@
config.ballRadius = nextRadius;
};
const applyBallPlugin = (body, { color, shape, blobId } = {}) => {
body.plugin = {
color,
hasEntered: false,
entryCheckId: null,
};
if (shape) body.plugin.shape = shape;
if (blobId) body.plugin.blobId = blobId;
};
const getCommonBallOpts = (scene, color) => {
const ballPhysics = scene?.config?.ballPhysics || {};
return {
restitution: ballPhysics.restitution ?? 0.72,
friction: ballPhysics.friction ?? 0.01,
frictionAir: ballPhysics.frictionAir ?? 0.012,
frictionStatic: ballPhysics.frictionStatic ?? 0,
density: ballPhysics.density ?? 0.001,
render: {
fillStyle: color,
strokeStyle: "#0b1222",
lineWidth: 2,
},
};
};
const createSoftBlob = (x, y, color, commonOpts) => {
const cols = 3;
const rows = 2;
@@ -135,14 +161,7 @@
const blobId = `blob-${Date.now()}-${Math.random()
.toString(16)
.slice(2)}`;
soft.bodies.forEach((b) => {
b.plugin = {
color,
hasEntered: false,
entryCheckId: null,
blobId,
};
});
soft.bodies.forEach((b) => applyBallPlugin(b, { color, blobId }));
soft.constraints.forEach((c) => {
c.plugin = { blobId, blobConstraint: true };
c.render = c.render || {};
@@ -164,12 +183,7 @@
});
}
const body = Bodies.fromVertices(x, y, [points], commonOpts, true);
body.plugin = {
color,
hasEntered: false,
entryCheckId: null,
shape: "jagged",
};
applyBallPlugin(body, { color, shape: "jagged" });
return { bodies: [body], constraints: [], blobId: null };
};
@@ -230,12 +244,7 @@
...commonOpts.render,
visible: true,
};
body.plugin = {
color,
hasEntered: false,
entryCheckId: null,
shape: "gift",
};
applyBallPlugin(body, { color, shape: "gift" });
return { bodies: [body], constraints: [], blobId: null };
};
@@ -245,23 +254,13 @@
...commonOpts,
chamfer: 0,
});
body.plugin = {
color,
hasEntered: false,
entryCheckId: null,
shape: "rect",
};
applyBallPlugin(body, { color, shape: "rect" });
return { bodies: [body], constraints: [], blobId: null };
};
const createCircleBall = (x, y, color, commonOpts) => {
const body = Bodies.circle(x, y, config.ballRadius, commonOpts);
body.plugin = {
color,
hasEntered: false,
entryCheckId: null,
shape: "circle",
};
applyBallPlugin(body, { color, shape: "circle" });
return { bodies: [body], constraints: [], blobId: null };
};
@@ -273,20 +272,8 @@
const createBallBodies = (x, y, color) => {
const scene = getCurrentScene();
const ballPhysics = scene?.config?.ballPhysics || {};
const debugSpawn = !!scene?.config?.debugSpawn;
const commonOpts = {
restitution: ballPhysics.restitution ?? 0.72,
friction: ballPhysics.friction ?? 0.01,
frictionAir: ballPhysics.frictionAir ?? 0.012,
frictionStatic: ballPhysics.frictionStatic ?? 0,
density: ballPhysics.density ?? 0.001,
render: {
fillStyle: color,
strokeStyle: "#0b1222",
lineWidth: 2,
},
};
const commonOpts = getCommonBallOpts(scene, color);
if (scene?.config?.blobBalls === "soft") {
return createSoftBlob(x, y, color, commonOpts);
}