diff --git a/public/js/game-loop-module.js b/public/js/game-loop-module.js index 98e50f1..bf1cdd4 100644 --- a/public/js/game-loop-module.js +++ b/public/js/game-loop-module.js @@ -43,6 +43,8 @@ class GameLoopModule extends BaseModule { 'queueUnrenderedHistoryBlocks', 'autoSaveCurrentSession', 'restoreBrowserSave', + 'restoreInputStateFromSave', + 'hasUnrenderedHistory', 'resumeAutosaveIfAvailable', 'requestStartGame', 'requestSaveGame', @@ -214,17 +216,14 @@ class GameLoopModule extends BaseModule { return false; } - await this.restoreBrowserSave(browserSave, 'autosave-resume', { resetDisplay: true }); this.gameState.started = Boolean(response.running); this.gameState.startedOnce = true; this.gameState.ended = !response.running && browserSave.inputMode === 'end'; this.gameState.canSave = this.gameState.started; this.gameState.canLoad = true; - this.currentChoices = Array.isArray(browserSave.choices) ? browserSave.choices : []; - this.currentInputMode = browserSave.inputMode || 'none'; - document.dispatchEvent(new CustomEvent('story:choices', { detail: this.currentChoices })); - document.dispatchEvent(new CustomEvent('story:input-mode', { detail: this.currentInputMode })); this.updateUIState(); + await this.restoreBrowserSave(browserSave, 'autosave-resume', { resetDisplay: true }); + this.restoreInputStateFromSave(browserSave, 'autosave-resume'); return true; } @@ -393,8 +392,7 @@ class GameLoopModule extends BaseModule { if (browserSave?.musicState && audioManager?.restoreMusicState) { await audioManager.restoreMusicState(browserSave.musicState); } - const hasUnrenderedHistory = browserSave && - Number(browserSave.latestBlockId || 0) > Number(browserSave.latestRenderedBlockId || 0); + const hasUnrenderedHistory = this.hasUnrenderedHistory(browserSave); if (hasUnrenderedHistory) { const sentenceQueue = this.getModule('sentence-queue'); sentenceQueue?.pauseBeforeNext?.('load-resume'); @@ -420,6 +418,30 @@ class GameLoopModule extends BaseModule { } } + restoreInputStateFromSave(browserSave, reason = 'load-game') { + const choices = Array.isArray(browserSave?.choices) ? browserSave.choices : []; + const savedMode = ['text', 'choice', 'end', 'none'].includes(browserSave?.inputMode) + ? browserSave.inputMode + : null; + const inputMode = savedMode || (choices.length > 0 ? 'choice' : 'none'); + + this.currentChoices = choices; + this.currentInputMode = inputMode; + document.dispatchEvent(new CustomEvent('story:choices', { detail: choices })); + document.dispatchEvent(new CustomEvent('story:input-mode', { detail: inputMode })); + + if (!this.hasUnrenderedHistory(browserSave)) { + document.dispatchEvent(new CustomEvent('story:process-state', { + detail: { state: 'ready', reason: `${reason}-input-restored`, inputMode } + })); + } + } + + hasUnrenderedHistory(browserSave) { + return Boolean(browserSave) && + Number(browserSave.latestBlockId || 0) > Number(browserSave.latestRenderedBlockId || 0); + } + async hasSaveGame(slot = 1) { const storyHistory = this.getModule('story-history'); if (storyHistory && typeof storyHistory.hasSaveSlot === 'function') {