Files
Battle-for-Tismo/client/js/menu_background.js
T

26 lines
1.1 KiB
JavaScript

// The main menu's key art. Two pieces ship and one is picked at launch; the
// chosen piece also stays behind the boot log while a world loads. The URL
// itself lives in css/style.css, keyed by the class the choice adds to <body>.
export const MENU_BACKGROUNDS = [
{ class: "menu-bg-0", source: "assets/main_menu_background.png" },
{ class: "menu-bg-1", source: "assets/main_menu_background2.png" },
];
// Picks one of the backgrounds. `random` is injectable so a test can pin it.
export function pickMenuBackground(random = Math.random) {
const index = Math.floor(random() * MENU_BACKGROUNDS.length);
return MENU_BACKGROUNDS[Math.min(index, MENU_BACKGROUNDS.length - 1)];
}
// Swaps the background class on <body>, removing whichever one was set before.
export function applyMenuBackground(choice, body = document.body) {
for (const entry of MENU_BACKGROUNDS) body.classList.remove(entry.class);
body.classList.add(choice.class);
return choice;
}
export function chooseMenuBackground(random = Math.random, body = document.body) {
return applyMenuBackground(pickMenuBackground(random), body);
}