Simplify loader helpers
This commit is contained in:
52
src/main.js
52
src/main.js
@@ -1,22 +1,24 @@
|
|||||||
(() => {
|
(() => {
|
||||||
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", {
|
||||||
config: {},
|
create: "create",
|
||||||
defaultMessageConfig: {
|
fallback: () => ({
|
||||||
durationMs: 4200,
|
config: {},
|
||||||
position: { xPercent: 50, yPercent: 10 },
|
defaultMessageConfig: {
|
||||||
text: null,
|
durationMs: 4200,
|
||||||
colors: null,
|
position: { xPercent: 50, yPercent: 10 },
|
||||||
},
|
text: 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,
|
||||||
|
|||||||
Reference in New Issue
Block a user