Extract scene registry helpers
This commit is contained in:
@@ -105,6 +105,7 @@
|
|||||||
<script src="./src/scenes/scene-storm-grid.js"></script>
|
<script src="./src/scenes/scene-storm-grid.js"></script>
|
||||||
<script src="./src/scenes/scene-stack-blocks.js"></script>
|
<script src="./src/scenes/scene-stack-blocks.js"></script>
|
||||||
<script src="./src/scenes/index.js"></script>
|
<script src="./src/scenes/index.js"></script>
|
||||||
|
<script src="./src/scene-registry.js"></script>
|
||||||
<script src="./src/ui.js"></script>
|
<script src="./src/ui.js"></script>
|
||||||
<script src="./src/storage.js"></script>
|
<script src="./src/storage.js"></script>
|
||||||
<script src="./src/spawn.js"></script>
|
<script src="./src/spawn.js"></script>
|
||||||
|
|||||||
60
src/main.js
60
src/main.js
@@ -2,30 +2,13 @@
|
|||||||
const { Engine, Render, Runner, World, Body, Constraint, Events, Vector } =
|
const { Engine, Render, Runner, World, Body, Constraint, Events, Vector } =
|
||||||
Matter;
|
Matter;
|
||||||
|
|
||||||
const { scenes = [], defaultSceneId } = window.PhysilinksScenes || {};
|
const {
|
||||||
const getSceneById = (sceneId) =>
|
scenes = [],
|
||||||
scenes.find((candidate) => candidate.id === sceneId) || null;
|
defaultSceneId,
|
||||||
|
order: sceneOrder = [],
|
||||||
const getSceneIdFromUrl = () => {
|
} = window.PhysilinksScenes || {};
|
||||||
try {
|
const { getSceneById, getSceneIdFromUrl, setSceneIdInUrl, getNextSceneId } =
|
||||||
const params = new URLSearchParams(window.location.search);
|
window.PhysilinksSceneRegistry || {};
|
||||||
const urlScene = params.get("scene");
|
|
||||||
return getSceneById(urlScene) ? urlScene : null;
|
|
||||||
} catch (err) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const setSceneIdInUrl = (sceneId) => {
|
|
||||||
if (!sceneId) return;
|
|
||||||
try {
|
|
||||||
const url = new URL(window.location.href);
|
|
||||||
url.searchParams.set("scene", sceneId);
|
|
||||||
history.replaceState({}, "", `${url.pathname}${url.search}${url.hash}`);
|
|
||||||
} catch (err) {
|
|
||||||
// ignore history failures (blocked or unsupported)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const defaultMessageConfig = {
|
const defaultMessageConfig = {
|
||||||
durationMs: 4200,
|
durationMs: 4200,
|
||||||
@@ -206,28 +189,6 @@
|
|||||||
updateHud();
|
updateHud();
|
||||||
};
|
};
|
||||||
|
|
||||||
const getNextSceneId = () => {
|
|
||||||
const order = window.PhysilinksScenes?.order || [];
|
|
||||||
const currentId = currentScene?.id;
|
|
||||||
const orderedExisting = order.filter((id) =>
|
|
||||||
scenes.some((s) => s.id === id),
|
|
||||||
);
|
|
||||||
if (orderedExisting.length > 0 && currentId) {
|
|
||||||
const idx = orderedExisting.indexOf(currentId);
|
|
||||||
if (idx >= 0 && idx < orderedExisting.length - 1) {
|
|
||||||
return orderedExisting[idx + 1];
|
|
||||||
}
|
|
||||||
if (idx === orderedExisting.length - 1) {
|
|
||||||
return orderedExisting[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const fallbackIdx = scenes.findIndex((s) => s.id === currentId);
|
|
||||||
if (fallbackIdx >= 0 && fallbackIdx < scenes.length - 1) {
|
|
||||||
return scenes[fallbackIdx + 1].id;
|
|
||||||
}
|
|
||||||
return scenes[0]?.id || defaultSceneId;
|
|
||||||
};
|
|
||||||
|
|
||||||
const chain = {
|
const chain = {
|
||||||
active: false,
|
active: false,
|
||||||
color: null,
|
color: null,
|
||||||
@@ -782,7 +743,12 @@
|
|||||||
onPauseToggle: () => setPaused(!isPaused),
|
onPauseToggle: () => setPaused(!isPaused),
|
||||||
onRestart: restartGame,
|
onRestart: restartGame,
|
||||||
onSceneChange: (id) => applyScene(id),
|
onSceneChange: (id) => applyScene(id),
|
||||||
onWinNext: () => applyScene(getNextSceneId()),
|
onWinNext: () =>
|
||||||
|
applyScene(
|
||||||
|
getNextSceneId
|
||||||
|
? getNextSceneId(scenes, sceneOrder, currentScene) || currentScene?.id
|
||||||
|
: currentScene?.id,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
ui.setSceneOptions(
|
ui.setSceneOptions(
|
||||||
scenes,
|
scenes,
|
||||||
|
|||||||
53
src/scene-registry.js
Normal file
53
src/scene-registry.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
(() => {
|
||||||
|
const getSceneById = (scenes, sceneId) =>
|
||||||
|
scenes.find((candidate) => candidate.id === sceneId) || null;
|
||||||
|
|
||||||
|
const getSceneIdFromUrl = (scenes) => {
|
||||||
|
try {
|
||||||
|
const params = new URLSearchParams(window.location.search);
|
||||||
|
const urlScene = params.get("scene");
|
||||||
|
return getSceneById(scenes, urlScene) ? urlScene : null;
|
||||||
|
} catch (err) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const setSceneIdInUrl = (sceneId) => {
|
||||||
|
if (!sceneId) return;
|
||||||
|
try {
|
||||||
|
const url = new URL(window.location.href);
|
||||||
|
url.searchParams.set("scene", sceneId);
|
||||||
|
history.replaceState({}, "", `${url.pathname}${url.search}${url.hash}`);
|
||||||
|
} catch (err) {
|
||||||
|
// ignore history failures (blocked or unsupported)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getNextSceneId = (scenes, order, currentScene) => {
|
||||||
|
const currentId = currentScene?.id;
|
||||||
|
const orderedExisting = order.filter((id) =>
|
||||||
|
scenes.some((s) => s.id === id),
|
||||||
|
);
|
||||||
|
if (orderedExisting.length > 0 && currentId) {
|
||||||
|
const idx = orderedExisting.indexOf(currentId);
|
||||||
|
if (idx >= 0 && idx < orderedExisting.length - 1) {
|
||||||
|
return orderedExisting[idx + 1];
|
||||||
|
}
|
||||||
|
if (idx === orderedExisting.length - 1) {
|
||||||
|
return orderedExisting[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const fallbackIdx = scenes.findIndex((s) => s.id === currentId);
|
||||||
|
if (fallbackIdx >= 0 && fallbackIdx < scenes.length - 1) {
|
||||||
|
return scenes[fallbackIdx + 1].id;
|
||||||
|
}
|
||||||
|
return scenes[0]?.id || null;
|
||||||
|
};
|
||||||
|
|
||||||
|
window.PhysilinksSceneRegistry = {
|
||||||
|
getSceneById,
|
||||||
|
getSceneIdFromUrl,
|
||||||
|
setSceneIdInUrl,
|
||||||
|
getNextSceneId,
|
||||||
|
};
|
||||||
|
})();
|
||||||
Reference in New Issue
Block a user