Simplify loader helpers

This commit is contained in:
Daddy32
2025-12-15 21:43:05 +01:00
parent c5c4c25d61
commit 2a00974d44

View File

@@ -1,14 +1,15 @@
(() => { (() => {
const { World, Body, Constraint, Events, Vector } = Matter; const { World, Body, Constraint, Events, Vector } = Matter;
const fromWindow = (key, fallback = {}) => window[key] ?? fallback; const load = (key, { create, fallback = {} } = {}) => {
const mod = window[key] ?? fallback;
const getFactory = (key, fallbackFactory) => { if (!create) return mod;
const mod = fromWindow(key); return typeof mod[create] === "function" ? mod[create].bind(mod) : fallback;
return typeof mod.create === "function" ? mod.create : fallbackFactory;
}; };
const createConfig = getFactory("PhysilinksConfig", () => ({ const createConfig = load("PhysilinksConfig", {
create: "create",
fallback: () => ({
config: {}, config: {},
defaultMessageConfig: { defaultMessageConfig: {
durationMs: 4200, durationMs: 4200,
@@ -16,7 +17,8 @@
text: null, text: null,
colors: null, colors: null,
}, },
})); }),
});
const { config: baseConfig, defaultMessageConfig } = createConfig(); const { config: baseConfig, defaultMessageConfig } = createConfig();
@@ -64,16 +66,14 @@
config.messages = normalized.messages; config.messages = normalized.messages;
}; };
const { const scenesMod = load("PhysilinksScenes", { fallback: {} });
scenes = [], const { scenes = [], defaultSceneId, order: sceneOrder = [] } = scenesMod;
defaultSceneId,
order: sceneOrder = [],
} = fromWindow("PhysilinksScenes", {});
const registry = load("PhysilinksSceneRegistry", { fallback: {} });
const { getSceneById, getSceneIdFromUrl, setSceneIdInUrl, getNextSceneId } = const { getSceneById, getSceneIdFromUrl, setSceneIdInUrl, getNextSceneId } =
fromWindow("PhysilinksSceneRegistry", {}); registry;
const createUI = getFactory("PhysilinksUI"); const createUI = load("PhysilinksUI", { create: "create" });
const ui = createUI(); const ui = createUI();
const { sceneEl } = ui; const { sceneEl } = ui;
@@ -99,7 +99,7 @@
const BALL_BASELINE = 680; // reference height used for relative ball sizing const BALL_BASELINE = 680; // reference height used for relative ball sizing
const createEngine = getFactory("PhysilinksEngine"); const createEngine = load("PhysilinksEngine", { create: "create" });
const { engine, render, runner, startRunner, stopRunner, setRenderSize } = const { engine, render, runner, startRunner, stopRunner, setRenderSize } =
createEngine({ createEngine({
sceneEl, sceneEl,
@@ -143,7 +143,7 @@
let spawnSystem = null; let spawnSystem = null;
let input = null; let input = null;
const storage = fromWindow("PhysilinksStorage", {}); const storage = load("PhysilinksStorage", { fallback: {} });
const { const {
loadHighScore = () => 0, loadHighScore = () => 0,
loadLongestChain = () => 0, loadLongestChain = () => 0,
@@ -233,7 +233,7 @@
ui.showGameOver(state.score); ui.showGameOver(state.score);
}; };
const createSpawn = getFactory("PhysilinksSpawn"); const createSpawn = load("PhysilinksSpawn", { create: "create" });
spawnSystem = createSpawn({ spawnSystem = createSpawn({
config, config,
world, world,
@@ -246,7 +246,7 @@
isGameOver: () => state.gameOver, isGameOver: () => state.gameOver,
ballBaseline: BALL_BASELINE, ballBaseline: BALL_BASELINE,
}); });
const createGoals = getFactory("PhysilinksGoals"); const createGoals = load("PhysilinksGoals", { create: "create" });
const goals = createGoals({ const goals = createGoals({
config, config,
getCurrentScene: () => currentScene, getCurrentScene: () => currentScene,
@@ -580,7 +580,7 @@
goals.maybeAnnounceGoalProgress(goal); goals.maybeAnnounceGoalProgress(goal);
}; };
const createInput = getFactory("PhysilinksInput"); const createInput = load("PhysilinksInput", { create: "create" });
input = createInput({ input = createInput({
render, render,
world, world,