breakout71/src/settings.ts

36 lines
920 B
TypeScript
Raw Normal View History

2025-03-15 21:29:38 +01:00
// Settings
2025-03-16 17:45:29 +01:00
import { GameState } from "./types";
2025-03-15 21:29:38 +01:00
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);
}
export function addToTotalScore(gameState: GameState, points: number) {
2025-03-16 17:45:29 +01:00
if (gameState.isCreativeModeRun) return;
setSettingValue("breakout_71_total_score", getTotalScore() + points);
}