Refactor ball body creation
This commit is contained in:
68
src/spawn.js
68
src/spawn.js
@@ -117,23 +117,7 @@
|
|||||||
config.ballRadius = nextRadius;
|
config.ballRadius = nextRadius;
|
||||||
};
|
};
|
||||||
|
|
||||||
const createBallBodies = (x, y, color) => {
|
const createSoftBlob = (x, y, color, commonOpts) => {
|
||||||
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,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
if (scene?.config?.blobBalls === "soft") {
|
|
||||||
const cols = 3;
|
const cols = 3;
|
||||||
const rows = 2;
|
const rows = 2;
|
||||||
const radius = Math.max(10, config.ballRadius * 0.55);
|
const radius = Math.max(10, config.ballRadius * 0.55);
|
||||||
@@ -165,8 +149,9 @@
|
|||||||
c.render.type = "line";
|
c.render.type = "line";
|
||||||
});
|
});
|
||||||
return { bodies: soft.bodies, constraints: soft.constraints, blobId };
|
return { bodies: soft.bodies, constraints: soft.constraints, blobId };
|
||||||
}
|
};
|
||||||
if (scene?.config?.blobBalls === "jagged") {
|
|
||||||
|
const createJaggedBall = (x, y, color, commonOpts) => {
|
||||||
const points = [];
|
const points = [];
|
||||||
const segments = 6;
|
const segments = 6;
|
||||||
for (let i = 0; i < segments; i += 1) {
|
for (let i = 0; i < segments; i += 1) {
|
||||||
@@ -186,8 +171,9 @@
|
|||||||
shape: "jagged",
|
shape: "jagged",
|
||||||
};
|
};
|
||||||
return { bodies: [body], constraints: [], blobId: null };
|
return { bodies: [body], constraints: [], blobId: null };
|
||||||
}
|
};
|
||||||
if (scene?.config?.ballShape === "gift") {
|
|
||||||
|
const createGiftBall = (x, y, color, commonOpts, scene, debugSpawn) => {
|
||||||
const size = config.ballRadius * 2;
|
const size = config.ballRadius * 2;
|
||||||
const ribbonSize = Math.max(4, config.ballRadius * 0.35);
|
const ribbonSize = Math.max(4, config.ballRadius * 0.35);
|
||||||
const ribbonColor = scene?.config?.giftRibbonColor || "#f8fafc";
|
const ribbonColor = scene?.config?.giftRibbonColor || "#f8fafc";
|
||||||
@@ -251,8 +237,9 @@
|
|||||||
shape: "gift",
|
shape: "gift",
|
||||||
};
|
};
|
||||||
return { bodies: [body], constraints: [], blobId: null };
|
return { bodies: [body], constraints: [], blobId: null };
|
||||||
}
|
};
|
||||||
if (scene?.config?.ballShape === "rect") {
|
|
||||||
|
const createRectBall = (x, y, color, commonOpts) => {
|
||||||
const side = config.ballRadius * 2;
|
const side = config.ballRadius * 2;
|
||||||
const body = Bodies.rectangle(x, y, side, side, {
|
const body = Bodies.rectangle(x, y, side, side, {
|
||||||
...commonOpts,
|
...commonOpts,
|
||||||
@@ -265,7 +252,9 @@
|
|||||||
shape: "rect",
|
shape: "rect",
|
||||||
};
|
};
|
||||||
return { bodies: [body], constraints: [], blobId: null };
|
return { bodies: [body], constraints: [], blobId: null };
|
||||||
}
|
};
|
||||||
|
|
||||||
|
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 = {
|
body.plugin = {
|
||||||
color,
|
color,
|
||||||
@@ -276,6 +265,37 @@
|
|||||||
return { bodies: [body], constraints: [], blobId: null };
|
return { bodies: [body], constraints: [], blobId: null };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (scene?.config?.blobBalls === "soft") {
|
||||||
|
return createSoftBlob(x, y, color, commonOpts);
|
||||||
|
}
|
||||||
|
if (scene?.config?.blobBalls === "jagged") {
|
||||||
|
return createJaggedBall(x, y, color, commonOpts);
|
||||||
|
}
|
||||||
|
if (scene?.config?.ballShape === "gift") {
|
||||||
|
return createGiftBall(x, y, color, commonOpts, scene, debugSpawn);
|
||||||
|
}
|
||||||
|
if (scene?.config?.ballShape === "rect") {
|
||||||
|
return createRectBall(x, y, color, commonOpts);
|
||||||
|
}
|
||||||
|
return createCircleBall(x, y, color, commonOpts);
|
||||||
|
};
|
||||||
|
|
||||||
const spawnBall = () => {
|
const spawnBall = () => {
|
||||||
if (isGameOver()) return;
|
if (isGameOver()) return;
|
||||||
const scene = getCurrentScene();
|
const scene = getCurrentScene();
|
||||||
|
|||||||
Reference in New Issue
Block a user