125 lines
4.6 KiB
TypeScript
125 lines
4.6 KiB
TypeScript
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 = `
|
|
<h1>ESCAPE</h1>
|
|
<p class="tagline">Prototype · moteur point & click</p>
|
|
<div class="chapters"></div>
|
|
<p class="keys">Clic : examiner · flèches : tourner / reculer (ou clic droit, Échap) · objet en main + autre objet : combiner · H : indice · D : mode debug</p>
|
|
<p class="keys"><b>Facile</b> : objets surlignés et nommés au survol · <b>Difficile</b> : seul le curseur vous guide</p>`;
|
|
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 = `
|
|
<div class="chapter-head">
|
|
<h2>${escapeHtml(scenario.title)}</h2>
|
|
<span class="difficulty" title="Difficulté">${stars(scenario.difficulty)}</span>
|
|
</div>
|
|
${scenario.subtitle ? `<p>${escapeHtml(scenario.subtitle)}</p>` : ''}
|
|
<div class="chapter-actions"></div>`;
|
|
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 = `
|
|
<h1>Évadé !</h1>
|
|
<p class="outro">${escapeHtml(entry.scenario.outro ?? '')}</p>
|
|
<div class="stats">
|
|
<div><span>${formatTime(state.elapsedMs)}</span>temps</div>
|
|
<div><span>${state.hintsUsed}</span>indice${state.hintsUsed > 1 ? 's' : ''}</div>
|
|
<div><span>${state.difficulty === 'hard' ? 'Difficile' : 'Facile'}</span>mode</div>
|
|
</div>
|
|
<button class="primary">Retour au menu</button>`;
|
|
screen.querySelector('button')!.onclick = () => {
|
|
clearSave(entry.scenario.id);
|
|
showMenu();
|
|
};
|
|
renderer?.root.appendChild(screen);
|
|
}
|
|
|
|
showMenu();
|