Extract helpers for chain finish flow

This commit is contained in:
Daddy32
2025-12-15 21:15:23 +01:00
parent ec6ea8aa1d
commit 6410a98f5d

View File

@@ -435,11 +435,8 @@
updateHud(); updateHud();
}; };
const finishChain = (releasePoint) => { const updateLongestChain = (chainLength) => {
if (!chain.active || gameOver || isPaused) return; if (chainLength <= longestChainRecord) return;
if (chain.bodies.length >= config.minChain) {
const chainLength = chain.bodies.length;
if (chainLength > longestChainRecord) {
longestChainRecord = chainLength; longestChainRecord = chainLength;
saveLongestChain(currentScene.id, longestChainRecord); saveLongestChain(currentScene.id, longestChainRecord);
console.log( console.log(
@@ -452,7 +449,9 @@
durationMs: 3600, durationMs: 3600,
position: config.messages.position, position: config.messages.position,
}); });
} };
const getChainScoreState = () => {
const baseGain = 10 * Math.pow(chain.bodies.length, 2); const baseGain = 10 * Math.pow(chain.bodies.length, 2);
const negativeColors = ( const negativeColors = (
currentScene?.config?.negativeScoreColors || [] currentScene?.config?.negativeScoreColors || []
@@ -460,21 +459,22 @@
const negativeProgressColors = ( const negativeProgressColors = (
currentScene?.config?.negativeProgressColors || [] currentScene?.config?.negativeProgressColors || []
).map(normalizeColor); ).map(normalizeColor);
const normalizedColor = chain.color
? normalizeColor(chain.color || "")
: null;
const isNegative = const isNegative =
chain.color && normalizedColor && negativeColors.includes(normalizedColor);
negativeColors.includes(normalizeColor(chain.color || ""));
const isNegativeProgress = const isNegativeProgress =
chain.color && normalizedColor && negativeProgressColors.includes(normalizedColor);
negativeProgressColors.includes(normalizeColor(chain.color || ""));
const gain = isNegative ? -baseGain : baseGain; const gain = isNegative ? -baseGain : baseGain;
score += gain; return { gain, isNegativeProgress };
clearedCount += chain.bodies.length; };
if (score > highScore) {
highScore = score; const removeChainConstraints = () => {
saveHighScore(currentScene.id, highScore);
}
ui.spawnScorePopup(releasePoint || chain.pointer, gain, chain.color);
chain.constraints.forEach((c) => World.remove(world, c)); chain.constraints.forEach((c) => World.remove(world, c));
};
const removeChainBodies = () => {
const blobIds = new Set(); const blobIds = new Set();
chain.bodies.forEach((body) => { chain.bodies.forEach((body) => {
if (body.plugin?.blobId) { if (body.plugin?.blobId) {
@@ -499,7 +499,6 @@
}); });
spawnSystem.removeBlob(id); spawnSystem.removeBlob(id);
}); });
// Remove cleared balls from tracking list.
for (let i = balls.length - 1; i >= 0; i -= 1) { for (let i = balls.length - 1; i >= 0; i -= 1) {
if ( if (
chain.bodies.includes(balls[i]) || chain.bodies.includes(balls[i]) ||
@@ -508,20 +507,21 @@
balls.splice(i, 1); balls.splice(i, 1);
} }
} }
if (isNegativeProgress) { };
const applyNegativeProgressPenalty = (chainLength) => {
const winCond = currentScene?.config?.winCondition; const winCond = currentScene?.config?.winCondition;
if (winCond?.type === "colorClear" && Array.isArray(winCond.targets)) { if (winCond?.type !== "colorClear" || !Array.isArray(winCond.targets)) {
return;
}
winCond.targets.forEach((target) => { winCond.targets.forEach((target) => {
const key = normalizeColor(target.color); const key = normalizeColor(target.color);
const current = clearedByColor[key] || 0; const current = clearedByColor[key] || 0;
clearedByColor[key] = Math.max(0, current - chain.bodies.length); clearedByColor[key] = Math.max(0, current - chainLength);
}); });
} };
}
} else { const resetChainState = () => {
chain.constraints.forEach((c) => World.remove(world, c));
chain.bodies.forEach((b) => setHighlight(b, false));
}
chain.active = false; chain.active = false;
chain.color = null; chain.color = null;
chain.bodies = []; chain.bodies = [];
@@ -531,6 +531,31 @@
checkWinCondition(); checkWinCondition();
}; };
const finishChain = (releasePoint) => {
if (!chain.active || gameOver || isPaused) return;
const chainLength = chain.bodies.length;
if (chainLength >= config.minChain) {
updateLongestChain(chainLength);
const { gain, isNegativeProgress } = getChainScoreState();
score += gain;
clearedCount += chainLength;
if (score > highScore) {
highScore = score;
saveHighScore(currentScene.id, highScore);
}
ui.spawnScorePopup(releasePoint || chain.pointer, gain, chain.color);
removeChainConstraints();
removeChainBodies();
if (isNegativeProgress) {
applyNegativeProgressPenalty(chainLength);
}
} else {
removeChainConstraints();
chain.bodies.forEach((b) => setHighlight(b, false));
}
resetChainState();
};
const updateHud = () => { const updateHud = () => {
ui.updateHud({ ui.updateHud({
spawnIntervalMs: config.spawnIntervalMs, spawnIntervalMs: config.spawnIntervalMs,