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