import './style.css'; // Chaque module de puzzle s'enregistre dans le registre à l'import. import './puzzles/colorCode'; import './puzzles/caesar'; import { formatTime, type RendererOptions } from './engine/BaseRenderer'; import { ThreeRenderer } from './engine/three/ThreeRenderer'; import { Engine } from './engine/Engine'; import { listScenarios, type LoadedScenario } from './engine/scenarioLoader'; import { clearSave, loadState, newState } from './engine/state'; import type { Difficulty } from './engine/types'; const app = document.getElementById('app')!; let renderer: ThreeRenderer | null = null; let engine: Engine | null = null; function stars(n: number): string { return '★'.repeat(n) + '☆'.repeat(Math.max(0, 5 - n)); } function escapeHtml(s: string): string { return s.replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]!); } function showMenu(): void { engine?.stop(); renderer?.destroy(); engine = null; renderer = null; const screen = document.createElement('div'); screen.className = 'screen menu'; screen.innerHTML = `

ESCAPE

Prototype · moteur point & click

Clic : examiner · flèches : tourner / reculer (ou clic droit, Échap) · objet en main + autre objet : combiner · H : indice · D : mode debug

Facile : objets surlignés et nommés au survol · Difficile : seul le curseur vous guide

`; const list = screen.querySelector('.chapters')!; listScenarios().forEach((entry) => { const { scenario } = entry; const save = loadState(scenario.id); const card = document.createElement('div'); card.className = 'chapter'; card.dataset.chapter = entry.folder; card.innerHTML = `

${escapeHtml(scenario.title)}

${stars(scenario.difficulty)}
${scenario.subtitle ? `

${escapeHtml(scenario.subtitle)}

` : ''}
`; const actions = card.querySelector('.chapter-actions')!; if (save && !save.won) { const cont = document.createElement('button'); cont.className = 'primary'; cont.textContent = `Continuer (${formatTime(save.elapsedMs)} · ${save.difficulty === 'hard' ? 'difficile' : 'facile'})`; cont.onclick = () => startGame(entry, false); actions.appendChild(cont); } const label = document.createElement('span'); label.className = 'new-game'; label.textContent = 'Nouvelle partie :'; actions.appendChild(label); for (const [diff, text] of [ ['easy', 'Facile'], ['hard', 'Difficile'], ] as const) { const play = document.createElement('button'); play.className = !save || save.won ? (diff === 'easy' ? 'primary' : '') : ''; play.dataset.difficulty = diff; play.textContent = text; play.onclick = () => startGame(entry, true, diff); actions.appendChild(play); } list.appendChild(card); }); app.replaceChildren(screen); } function startGame(entry: LoadedScenario, fresh: boolean, difficulty: Difficulty = 'easy'): void { const { scenario } = entry; if (fresh) clearSave(scenario.id); const state = (!fresh && loadState(scenario.id)) || newState(scenario.id, scenario.startScene, difficulty); engine = new Engine(scenario, state); const opts: RendererOptions = { resolveImage: entry.resolveImage, onMenu: showMenu, onWin: () => window.setTimeout(() => showWin(entry), 1800), }; renderer = new ThreeRenderer(engine, opts); engine.attach(renderer); // Accès console / tests automatisés : window.__escape.engine.state, etc. (window as unknown as { __escape: unknown }).__escape = { engine, renderer }; app.replaceChildren(renderer.root); engine.start(); } function showWin(entry: LoadedScenario): void { if (!engine) return; const { state } = engine; engine.stop(); const screen = document.createElement('div'); screen.className = 'screen win'; screen.innerHTML = `

Évadé !

${escapeHtml(entry.scenario.outro ?? '')}

${formatTime(state.elapsedMs)}temps
${state.hintsUsed}indice${state.hintsUsed > 1 ? 's' : ''}
${state.difficulty === 'hard' ? 'Difficile' : 'Facile'}mode
`; screen.querySelector('button')!.onclick = () => { clearSave(entry.scenario.id); showMenu(); }; renderer?.root.appendChild(screen); } showMenu();