breakout71/src/monitorLevelsUnlocks.ts

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-04-26 22:40:32 +02:00
import { GameState, PerkId } from "./types";
2025-04-08 14:03:38 +02:00
import { getSettingValue, setSettingValue } from "./settings";
import { allLevels, icons } from "./loadGameData";
import { t } from "./i18n/i18n";
import { toast } from "./toast";
import { schedulGameSound } from "./gameStateMutators";
2025-04-26 22:40:32 +02:00
import { getLevelUnlockCondition } from "./get_level_unlock_condition";
2025-04-08 14:03:38 +02:00
let list: {
minScore: number;
2025-04-26 20:07:01 +02:00
forbidden: PerkId[];
required: PerkId[];
2025-04-08 14:03:38 +02:00
}[];
2025-04-26 22:40:32 +02:00
let unlocked: Set<string> | null = null;
2025-04-08 14:03:38 +02:00
export function monitorLevelsUnlocks(gameState: GameState) {
2025-04-26 22:40:32 +02:00
if (!unlocked) {
unlocked = new Set(
2025-04-26 20:07:01 +02:00
getSettingValue("breakout_71_unlocked_levels", []) as string[],
);
}
2025-04-08 14:03:38 +02:00
if (gameState.creative) return;
if (!list) {
list = allLevels.map((l, li) => ({
name: l.name,
li,
l,
2025-04-26 20:07:01 +02:00
...getLevelUnlockCondition(li, l.name),
2025-04-08 14:03:38 +02:00
}));
}
list.forEach(({ name, minScore, forbidden, required, l }) => {
// Already unlocked
2025-04-26 20:07:01 +02:00
if (unlocked!.has(name)) return;
2025-04-08 14:03:38 +02:00
// Score not reached yet
2025-04-16 15:30:20 +02:00
if (gameState.score < minScore) return;
2025-04-16 15:30:42 +02:00
if (!minScore) return;
2025-04-16 15:30:20 +02:00
2025-04-08 14:03:38 +02:00
if (gameState.score < minScore) return;
// We are missing a required perk
if (required.find((id) => !gameState.perks[id])) return;
// We have a forbidden perk
if (forbidden.find((id) => gameState.perks[id])) return;
// Level just got unlocked
2025-04-26 20:07:01 +02:00
unlocked!.add(name);
2025-04-08 14:03:38 +02:00
setSettingValue(
"breakout_71_unlocked_levels",
getSettingValue("breakout_71_unlocked_levels", []).concat([name]),
);
toast(icons[name] + "<strong>" + t("unlocks.just_unlocked") + "</strong>");
schedulGameSound(gameState, "colorChange", 0, 1);
});
}