refactor; level reorder

This commit is contained in:
Daddy32
2025-12-15 16:43:13 +01:00
parent e197a02fd0
commit 9a68214c8d
7 changed files with 123 additions and 70 deletions

View File

@@ -151,55 +151,12 @@
let lastTimerDisplay = null;
let dragConstraint = null;
const makeStorageKey = (sceneId) => `physilinks-highscore-${sceneId}`;
const makeChainKey = (sceneId) => `physilinks-longestchain-${sceneId}`;
const loadHighScore = (sceneId) => {
try {
const raw = localStorage.getItem(makeStorageKey(sceneId));
const parsed = parseInt(raw, 10);
return Number.isFinite(parsed) ? parsed : 0;
} catch (err) {
console.error("Failed to load high score", { sceneId, err });
return 0;
}
};
const loadLongestChain = (sceneId) => {
try {
const raw = localStorage.getItem(makeChainKey(sceneId));
const parsed = parseInt(raw, 10);
return Number.isFinite(parsed) ? parsed : 0;
} catch (err) {
console.error("Failed to load longest chain", { sceneId, err });
return 0;
}
};
const saveHighScore = () => {
try {
localStorage.setItem(makeStorageKey(currentScene.id), String(highScore));
} catch (err) {
console.error("Failed to save high score", {
sceneId: currentScene.id,
err,
});
}
};
const saveLongestChain = () => {
try {
localStorage.setItem(
makeChainKey(currentScene.id),
String(longestChainRecord),
);
} catch (err) {
console.error("Failed to save longest chain", {
sceneId: currentScene.id,
err,
});
}
};
const {
loadHighScore = () => 0,
loadLongestChain = () => 0,
saveHighScore = () => {},
saveLongestChain = () => {},
} = window.PhysilinksStorage || {};
const applyScene = (sceneId) => {
const next = getSceneById(sceneId) || scenes[0];
@@ -234,6 +191,13 @@
: defaultMessageConfig.colors,
};
ui.setMessageDefaults(config.messages);
world.plugin = world.plugin || {};
world.plugin.stormSteps = Array.isArray(next.config?.spawnIntervals)
? next.config.spawnIntervals
: null;
world.plugin.squareOffset = 0;
engine.plugin = engine.plugin || {};
engine.plugin.stormState = null;
engine.gravity.scale =
typeof next.config.gravityScale === "number"
? next.config.gravityScale
@@ -432,8 +396,9 @@
const getSquarePlayArea = () => {
if (!currentScene?.config?.squarePlayArea) return null;
const size = Math.min(width, height);
const left = (width - size) / 2;
const top = (height - size) / 2;
const offset = world?.plugin?.squareOffset || 0;
const left = (width - size) / 2 + offset;
const top = (height - size) / 2 + offset;
return { size, left, top };
};
@@ -447,7 +412,7 @@
}
if (typeof currentScene?.config?.spawnInsets === "function") {
try {
const res = currentScene.config.spawnInsets({ width, height });
const res = currentScene.config.spawnInsets({ width, height, world });
if (res && Number.isFinite(res.left)) left = res.left;
if (res && Number.isFinite(res.right)) right = res.right;
} catch (err) {
@@ -764,7 +729,7 @@
const chainLength = chain.bodies.length;
if (chainLength > longestChainRecord) {
longestChainRecord = chainLength;
saveLongestChain();
saveLongestChain(currentScene.id, longestChainRecord);
console.log(
"New longest chain record",
chainLength,
@@ -794,7 +759,7 @@
clearedCount += chain.bodies.length;
if (score > highScore) {
highScore = score;
saveHighScore();
saveHighScore(currentScene.id, highScore);
}
ui.spawnScorePopup(releasePoint || chain.pointer, gain, chain.color);
chain.constraints.forEach((c) => World.remove(world, c));
@@ -1135,7 +1100,7 @@
const side = config.ballRadius * 2;
const body = Bodies.rectangle(x, y, side, side, {
...commonOpts,
chamfer: 4,
chamfer: 0,
});
body.plugin = {
color,