diff --git a/bgs/tutorial.js b/bgs/tutorial.js index bc82a4f..8864d85 100644 --- a/bgs/tutorial.js +++ b/bgs/tutorial.js @@ -13,6 +13,8 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o if (index < 0) throw Error("Unknown tutorial chapter"); const sound = createSoundDesign(); let soundOn = false, disposed = false, lastStep = -1, lastTurn = 0, lastTick = performance.now(); + let playing = false, previousStage, playbackBusy = false; + const canPlay = s => s.state.stage === "resolution" && !s.completed && !s.state.game.resolution?.outcome && !(chapter === "basics" && completedTurns(s.state) >= 3); let storage; try { storage = window.localStorage; } catch {} const tutorial = await createLessonTutorial(chapter, { locale, storage, onProgress }); @@ -65,11 +67,14 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o guide.innerHTML = `
${escape(LESSONS[index].title[locale === "fr" ? 0 : 1])}
${standalone ? `${t("Partie locale", "Local game")}` : ''}${transport("sound", soundOn ? "speaker" : "muted", soundOn ? t("Couper les sons", "Mute sounds") : t("Activer les sons", "Enable sounds"))}

${escape(s.title)}

${escape(missed ? t("Destination manquée. Revenez préparer un autre trajet.", "Destination missed. Go back and plan another route.") : s.text)}

-
${transport("restart", "rewind", t("Recommencer l’exercice", "Restart exercise"), s.busy)}${transport("previous", "left", t("Étape précédente", "Previous step"), s.step === 0 || s.busy)}${transport("replay", "restart", t("Rejouer cette étape", "Replay step"), s.busy || s.step === 0)}${s.canContinue ? `` : ''}${s.completed && (nextChapter || standalone && LESSONS[index + 1]) ? `` : ''}
+
${canPlay(s) ? transport("playback", playing ? "pause" : "right", playing ? t("Pause", "Pause") : t("Lire le programme", "Play program")) : ""}${transport("restart", "rewind", t("Recommencer l’exercice", "Restart exercise"), s.busy)}${transport("previous", "left", t("Étape précédente", "Previous step"), s.step === 0 || s.busy)}${transport("replay", "restart", t("Rejouer cette étape", "Replay step"), s.busy || s.step === 0)}${s.canContinue ? `` : ''}${s.completed && (nextChapter || standalone && LESSONS[index + 1]) ? `` : ''}
${s.error ? `

${escape(s.error)}

` : ''}`; } const unsubscribe = tutorial.subscribe(s => { if (disposed) return; + if (s.state.stage === "resolution" && previousStage === "programming") playing = true; + previousStage = s.state.stage; + if (!canPlay(s)) playing = false; if (s.step !== lastStep) { ui.resetTutorial(); @@ -99,7 +104,11 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o if (action === "sound") { soundOn = !soundOn; sound.enable(soundOn); sound.cue("select"); renderGuide(tutorial.snapshot); return; } + if (action === "playback") { + playing = !playing; renderGuide(tutorial.snapshot); return; + } if (!action && button.dataset.guideStep === undefined) return; + playing = false; ui.resetTutorial(); lastTurn = -1; sound.cue("back"); @@ -108,7 +117,7 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o while (tutorial.snapshot.step > step) await tutorial.previousStep(); } else if (action === "continue") await tutorial.continue(); else if (action === "previous") await tutorial.previousStep(); - else if (action === "replay") await tutorial.restartStep(); + else if (action === "replay") { await tutorial.restartStep(); playing = canPlay(tutorial.snapshot); renderGuide(tutorial.snapshot); } else if (action === "restart") await tutorial.restart(); else if (action === "next") { if (nextChapter) nextChapter.open(); @@ -118,11 +127,19 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o lastTick = performance.now(); }; guide.addEventListener("click", onClick); + // Playback pacing only: each tick executes the next deterministic engine step. + const playback = setInterval(async () => { + if (!playing || disposed || playbackBusy || tutorial.snapshot.busy || !canPlay(tutorial.snapshot)) return; + playbackBusy = true; + try { await play({ type: "resolve" }); } + catch { playing = false; renderGuide(tutorial.snapshot); } + finally { playbackBusy = false; } + }, 450); const ticker = setInterval(() => { if (!tutorial.snapshot.busy) void tick().catch(() => {}); }, 5000); return () => { - disposed = true; clearInterval(ticker); unsubscribe(); tutorial.destroy(); ui.destroy(); sound.destroy(); + disposed = true; clearInterval(playback); clearInterval(ticker); unsubscribe(); tutorial.destroy(); ui.destroy(); sound.destroy(); guide.removeEventListener("click", onClick); target.classList.remove("native-tutorial"); }; diff --git a/preview/style.css b/preview/style.css index aed8411..eec901a 100644 --- a/preview/style.css +++ b/preview/style.css @@ -2446,3 +2446,6 @@ input:not([type="checkbox"]):not([type="range"]) { @media (max-width: 800px) { .native-tutorial.learning-basics .flight-layout { grid-template-columns: 1fr; gap: 20px; } } + +.native-tutorial[data-tutorial-stage="resolution"] #controls, +.native-tutorial.learning-basics[data-tutorial-stage="resolution"] #controls { display: none !important; } diff --git a/src/tutorial-controller.js b/src/tutorial-controller.js index 9eac6e1..8c0788d 100644 --- a/src/tutorial-controller.js +++ b/src/tutorial-controller.js @@ -42,7 +42,7 @@ export function createLessonTutorial(id, { locale = "en", storage, onProgress } text: lesson.basics ? t("Rejoignez la salle marquée en deux actions. La troisième case reste vide : attendre.", "Reach the marked room in two actions. Leave the third slot empty: wait.") : t("Programmez les cartes sur le vaisseau, puis terminez la programmation. Le chrono est facultatif ici.", "Program cards on the ship, then finish planning. The clock is optional here."), complete: state => state.stage === "resolution" }, { id: "execute", title: t("Exécuter les tours", "Run the turns"), - text: t("Programme verrouillé. Avancez la résolution avec ▶. Les équipiers agissent ensemble, puis les menaces.", "Program locked. Advance resolution with ▶. Crew act together, then threats."), + text: t("Programme verrouillé. Les équipiers agissent ensemble, puis les menaces.", "Program locked. Crew act together, then threats."), complete: state => lesson.basics ? completedTurns(state) >= 3 && state.game.resolution.ship.crew[0].station === state.tutorial.targetStation : !!state.game.resolution?.outcome }, ], completion: { title: t("Exercice terminé", "Exercise complete"), text: lesson.basics ? t("Destination atteinte ! Votre programme vous y a conduit.", "Destination reached! Your program took you there.") : t("Rejouez pour comparer un autre programme, ou passez à la suite.", "Replay to compare another program, or move on.") },