Simplify loader helpers

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

View File

@@ -1,22 +1,24 @@
(() => {
const { World, Body, Constraint, Events, Vector } = Matter;
const fromWindow = (key, fallback = {}) => window[key] ?? fallback;
const getFactory = (key, fallbackFactory) => {
const mod = fromWindow(key);
return typeof mod.create === "function" ? mod.create : fallbackFactory;
const load = (key, { create, fallback = {} } = {}) => {
const mod = window[key] ?? fallback;
if (!create) return mod;
return typeof mod[create] === "function" ? mod[create].bind(mod) : fallback;
};
const createConfig = getFactory("PhysilinksConfig", () => ({
config: {},
defaultMessageConfig: {
durationMs: 4200,
position: { xPercent: 50, yPercent: 10 },
text: null,
colors: null,
},
}));
const createConfig = load("PhysilinksConfig", {
create: "create",
fallback: () => ({
config: {},
defaultMessageConfig: {
durationMs: 4200,
position: { xPercent: 50, yPercent: 10 },
text: null,
colors: null,
},
}),
});
const { config: baseConfig, defaultMessageConfig } = createConfig();
@@ -64,16 +66,14 @@
config.messages = normalized.messages;
};
const {
scenes = [],
defaultSceneId,
order: sceneOrder = [],
} = fromWindow("PhysilinksScenes", {});
const scenesMod = load("PhysilinksScenes", { fallback: {} });
const { scenes = [], defaultSceneId, order: sceneOrder = [] } = scenesMod;
const registry = load("PhysilinksSceneRegistry", { fallback: {} });
const { getSceneById, getSceneIdFromUrl, setSceneIdInUrl, getNextSceneId } =
fromWindow("PhysilinksSceneRegistry", {});
registry;
const createUI = getFactory("PhysilinksUI");
const createUI = load("PhysilinksUI", { create: "create" });
const ui = createUI();
const { sceneEl } = ui;
@@ -99,7 +99,7 @@
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 } =
createEngine({
sceneEl,
@@ -143,7 +143,7 @@
let spawnSystem = null;
let input = null;
const storage = fromWindow("PhysilinksStorage", {});
const storage = load("PhysilinksStorage", { fallback: {} });
const {
loadHighScore = () => 0,
loadLongestChain = () => 0,
@@ -233,7 +233,7 @@
ui.showGameOver(state.score);
};
const createSpawn = getFactory("PhysilinksSpawn");
const createSpawn = load("PhysilinksSpawn", { create: "create" });
spawnSystem = createSpawn({
config,
world,
@@ -246,7 +246,7 @@
isGameOver: () => state.gameOver,
ballBaseline: BALL_BASELINE,
});
const createGoals = getFactory("PhysilinksGoals");
const createGoals = load("PhysilinksGoals", { create: "create" });
const goals = createGoals({
config,
getCurrentScene: () => currentScene,
@@ -580,7 +580,7 @@
goals.maybeAnnounceGoalProgress(goal);
};
const createInput = getFactory("PhysilinksInput");
const createInput = load("PhysilinksInput", { create: "create" });
input = createInput({
render,
world,