breakout71/src/settings.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-03-15 21:29:38 +01:00
// Settings
let cachedSettings: { [key: string]: unknown } = {};
export function getSettingValue<T>(key: string, defaultValue: T) {
2025-03-16 17:45:29 +01:00
if (typeof cachedSettings[key] == "undefined") {
try {
const ls = localStorage.getItem(key);
if (ls) cachedSettings[key] = JSON.parse(ls) as T;
} catch (e) {
console.warn(e);
2025-03-15 21:29:38 +01:00
}
2025-03-16 17:45:29 +01:00
}
return (cachedSettings[key] as T) ?? defaultValue;
2025-03-15 21:29:38 +01:00
}
export function setSettingValue<T>(key: string, value: T) {
2025-03-16 17:45:29 +01:00
cachedSettings[key] = value;
try {
localStorage.setItem(key, JSON.stringify(value));
} catch (e) {
console.warn(e);
}
2025-03-15 21:29:38 +01:00
}
export function getTotalScore() {
2025-03-16 17:45:29 +01:00
return getSettingValue("breakout_71_total_score", 0);
}
2025-03-23 16:11:12 +01:00
export function getCurrentMaxCoins() {
return Math.pow(2, getSettingValue("max_coins", 1)) * 200;
}
export function getCurrentMaxParticles() {
return Math.pow(2, getSettingValue("max_particles", 1)) * 200;
}
2025-03-23 16:11:12 +01:00
export function cycleMaxCoins() {
setSettingValue("max_coins", (getSettingValue("max_coins", 1) + 1) % 6);
}
2025-03-23 16:11:12 +01:00
export function cycleMaxParticles() {
setSettingValue(
"max_particles",
(getSettingValue("max_particles", 1) + 1) % 6,
);
}