const App = (function () { let state = Storage.load(); let currentLevelId = null; let levelHandle = null; let levelFinishing = false; function validateConfig() { const errors = []; const games = GAME_CONFIG.games; const expected = Levels.count; if (!Array.isArray(games) || games.length !== expected) { errors.push("er moeten precies " + expected + " games in GAME_CONFIG.games staan."); } else { games.forEach(function (game, index) { if (!game || !game.title) { errors.push("game " + (index + 1) + " heeft geen titel."); return; } if (!game.appId && !game.url && !game.key) { errors.push("game " + (index + 1) + " heeft geen appId, url of key."); } }); } if (!GAME_CONFIG.herName) errors.push("de naam van de jarige ontbreekt."); return errors; } function init() { UI.init(); UI.setState(state); UI.renderMap(); UI.showScreen("welcome"); const errors = validateConfig(); if (errors.length > 0) { UI.showConfigError(errors); } } function destroyLevel() { if (levelHandle) { levelHandle.destroy(); levelHandle = null; } currentLevelId = null; levelFinishing = false; UI.setHint(""); } function goToMap() { destroyLevel(); UI.renderMap(); UI.showScreen("map"); } function goHome() { if (currentLevelId !== null) { destroyLevel(); UI.renderMap(); UI.toast("Je kunt dit level later opnieuw doen."); } UI.showScreen("map"); } function startLevel(id) { if (id < 1 || id > Levels.count) return; const completed = state.completed.indexOf(id) !== -1; if (id > state.unlocked && !completed) { UI.toast("Dit level is nog niet open."); return; } destroyLevel(); currentLevelId = id; UI.showLevel(id); levelHandle = Levels.mount(id, document.getElementById("level-stage"), { hint: function (message) { UI.setHint(message); }, onComplete: function () { finishLevel(id); } }); } function finishLevel(id) { if (currentLevelId !== id || levelFinishing) return; levelFinishing = true; const firstTime = state.completed.indexOf(id) === -1; if (firstTime) { state.completed.push(id); state.completed.sort(function (a, b) { return a - b; }); state.unlocked = Math.max(state.unlocked, Math.min(Levels.count, id + 1)); Storage.save(state); UI.setState(state); } const gameIndex = id - 1; if (state.revealed.indexOf(gameIndex) === -1) { const markRevealed = function () { if (state.revealed.indexOf(gameIndex) !== -1) return; state.revealed.push(gameIndex); state.revealed.sort(function (a, b) { return a - b; }); Storage.save(state); UI.setState(state); }; UI.showReveal(gameIndex, function () { markRevealed(); destroyLevel(); UI.renderMap(); if (state.completed.length === Levels.count && state.revealed.length === Levels.count) { UI.showFinale(); } else { UI.showScreen("map"); } }, { onSteamOpen: markRevealed }); return; } destroyLevel(); UI.renderMap(); UI.toast(firstTime ? "Level voltooid!" : "Mooi opnieuw gedaan!"); UI.showScreen("map"); } function resetGame() { state = Storage.reset(); destroyLevel(); UI.setState(state); UI.renderMap(); UI.showScreen("welcome"); UI.toast("De tuin is opnieuw begonnen."); } return { init: init, goToMap: goToMap, goHome: goHome, startLevel: startLevel, resetGame: resetGame }; })(); App.init();