diff --git a/src/engine/BaseRenderer.ts b/src/engine/BaseRenderer.ts index 69613e8..8e9fa0b 100644 --- a/src/engine/BaseRenderer.ts +++ b/src/engine/BaseRenderer.ts @@ -54,6 +54,7 @@ export abstract class BaseRenderer implements View { private uvHaloEl: HTMLElement; private notebookBtn: HTMLButtonElement; private notebookEl: HTMLElement | null = null; + private hintBtn: HTMLButtonElement; /** Dernier objet obtenu : sa case clignote dans l'inventaire. */ private freshItem: string | null = null; /** Indice porté par le message affiché (noté via le bouton en mode difficile). */ @@ -100,7 +101,7 @@ export abstract class BaseRenderer implements View { this.notebookBtn = el('button', 'hud-btn notebook-btn', right); this.notebookBtn.title = 'Carnet d’indices (C)'; this.notebookBtn.onclick = () => this.toggleNotebook(); - const hintBtn = el('button', 'hud-btn', right); + const hintBtn = (this.hintBtn = el('button', 'hud-btn', right)); hintBtn.innerHTML = '? Indice'; hintBtn.title = 'Obtenir un indice (H)'; hintBtn.onclick = () => engine.nextHint(); @@ -179,7 +180,7 @@ export abstract class BaseRenderer implements View { else this.engine.goBack(); } else if ((e.key === 'c' || e.key === 'C') && !this.debug) { this.toggleNotebook(); - } else if (e.key === 'h' || e.key === 'H') { + } else if ((e.key === 'h' || e.key === 'H') && this.engine.state.difficulty === 'easy') { this.engine.nextHint(); } else if (e.key === 'd' || e.key === 'D') { this.debug = !this.debug; @@ -203,6 +204,8 @@ export abstract class BaseRenderer implements View { this.renderInventory(); const n = this.engine.state.notebook.length; this.notebookBtn.innerHTML = `Carnet${n ? ` ${n}` : ''}`; + // Indices à la demande : mode facile uniquement. + this.hintBtn.style.display = this.engine.state.difficulty === 'easy' ? '' : 'none'; if (this.notebookEl) this.renderNotebook(); } @@ -458,7 +461,47 @@ export abstract class BaseRenderer implements View { return; } const list = el('ol', '', book); - for (const id of ids) el('li', '', list).textContent = this.engine.getClue(id)?.text ?? id; + const struck = this.engine.state.struck; + for (const id of ids) { + const li = el('li', 'notebook-entry', list); + li.title = struck.includes(id) ? 'Cliquer pour rétablir' : 'Cliquer pour barrer'; + const text = el('span', '', li); + text.textContent = this.engine.getClue(id)?.text ?? id; + li.onclick = () => this.engine.toggleStrike(id); + if (struck.includes(id)) this.drawStrike(li, text, id); + } + } + + /** Trait rouge tracé à la main sur chaque ligne du texte (légèrement penché et ondulé, stable d'un rendu à l'autre). */ + private drawStrike(li: HTMLElement, text: HTMLElement, seed: string): void { + let h = 2166136261; + for (let i = 0; i < seed.length; i++) h = Math.imul(h ^ seed.charCodeAt(i), 16777619); + const rnd = () => ((h = Math.imul(h ^ (h >>> 15), 2246822507) ^ (h >>> 13)) >>> 0) / 4294967296; + const box = li.getBoundingClientRect(); + const NS = 'http://www.w3.org/2000/svg'; + const svg = document.createElementNS(NS, 'svg'); + svg.setAttribute('class', 'notebook-strike'); + svg.setAttribute('width', String(box.width)); + svg.setAttribute('height', String(box.height)); + for (const r of Array.from(text.getClientRects())) { + const x0 = r.left - box.left - 3 - rnd() * 4; + const x1 = r.right - box.left + 2 + rnd() * 5; + const y = r.top - box.top + r.height * 0.55; + const tilt = (rnd() - 0.5) * 5; + const steps = Math.max(2, Math.round((x1 - x0) / 40)); + let d = `M${x0.toFixed(1)},${(y - tilt / 2).toFixed(1)}`; + for (let i = 1; i <= steps; i++) { + const t = i / steps; + const x = x0 + (x1 - x0) * t; + const cy = y - tilt / 2 + tilt * (t - 0.5 / steps) + (rnd() - 0.5) * 3; + d += ` Q${(x - (x1 - x0) / steps / 2).toFixed(1)},${cy.toFixed(1)} ${x.toFixed(1)},${(y - tilt / 2 + tilt * t + (rnd() - 0.5) * 1.2).toFixed(1)}`; + } + const path = document.createElementNS(NS, 'path'); + path.setAttribute('d', d); + path.setAttribute('stroke-width', (2 + rnd() * 0.8).toFixed(1)); + svg.appendChild(path); + } + li.appendChild(svg); } // --- Puzzles ------------------------------------------------------------------------ diff --git a/src/engine/Engine.ts b/src/engine/Engine.ts index 9883902..3650606 100644 --- a/src/engine/Engine.ts +++ b/src/engine/Engine.ts @@ -193,7 +193,20 @@ export class Engine { this.view.refresh(); } + /** Barre ou rétablit une entrée du carnet. Renvoie l'état barré, ou null si l'entrée n'existe pas. */ + toggleStrike(entry: string): boolean | null { + if (!this.state.notebook.includes(entry)) return null; + const i = this.state.struck.indexOf(entry); + if (i >= 0) this.state.struck.splice(i, 1); + else this.state.struck.push(entry); + saveState(this.state); + this.view.refresh(); + return i < 0; + } + + /** Indices à la demande : mode facile uniquement. */ nextHint(): void { + if (this.state.difficulty !== 'easy') return; const idx = this.scenario.hints.findIndex((g) => checkAll(this.state, g.when)); if (idx < 0) { this.view.showMessage('Pas d’indice pour le moment.', 'hint'); diff --git a/src/engine/state.ts b/src/engine/state.ts index 2e14fc4..c91eee0 100644 --- a/src/engine/state.ts +++ b/src/engine/state.ts @@ -21,6 +21,8 @@ export interface GameState { difficulty: Difficulty; /** Indices notés dans le carnet (identifiants, dans l'ordre). */ notebook: string[]; + /** Entrées du carnet barrées par le joueur. */ + struck: string[]; } export function newState(scenarioId: string, startScene: string, difficulty: Difficulty = 'easy'): GameState { @@ -29,6 +31,7 @@ export function newState(scenarioId: string, startScene: string, difficulty: Dif vars: {}, fired: {}, notebook: [], + struck: [], difficulty, scene: startScene, flags: {}, diff --git a/src/style.css b/src/style.css index ca70aab..24700b5 100644 --- a/src/style.css +++ b/src/style.css @@ -307,6 +307,10 @@ button.primary:hover { background: #dbb55c; } } .notebook h3 { margin: 0 0 8px; font-family: var(--serif); font-size: 24px; color: #3a2a1a; } .notebook ol { margin: 0; padding-left: 18px; line-height: 32px; font-size: 16px; } +.notebook-entry { position: relative; cursor: pointer; } +.notebook-entry:hover > span { color: #4a3a6a; } +.notebook-strike { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; } +.notebook-strike path { fill: none; stroke: #c0281e; stroke-linecap: round; opacity: .85; } .notebook-empty { font-family: var(--serif); font-style: italic; color: #6a5a4a; line-height: 1.6; } .notebook .puzzle-close { top: 10px; right: 10px; color: var(--text); } diff --git a/tests/engine.test.ts b/tests/engine.test.ts index 23332ab..f5653ec 100644 --- a/tests/engine.test.ts +++ b/tests/engine.test.ts @@ -131,6 +131,16 @@ describe('moteur', () => { expect(state.notebook).toEqual(['un message']); }); + it('carnet : barrer puis rétablir une entrée', () => { + const { engine, state } = makeEngine(); + engine.noteText('note'); + expect(engine.toggleStrike('note')).toBe(true); + expect(state.struck).toEqual(['note']); + expect(engine.toggleStrike('note')).toBe(false); + expect(state.struck).toEqual([]); + expect(engine.toggleStrike('absente')).toBe(null); + }); + it('objet inutilisable en l\'état : son propre message', () => { const { engine, state, hs, messages } = makeEngine({ items: [{ id: 'x', name: 'X', icon: '', unusable: 'éteint' }] }); state.inventory = ['x'];