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