refactor: inline event registration at setup level, remove registerEvents duplication across entry points

This commit is contained in:
2026-06-09 12:06:40 +08:00
parent ace5ed1fb3
commit 9e339c7c16

View File

@@ -15,18 +15,6 @@ export function useGameEngine(videoEls: () => [HTMLVideoElement | null, HTMLVide
;(window as any).__store = store ;(window as any).__store = store
} }
async function loadGame(dataUrl: string) {
const resp = await fetch(dataUrl)
const data: GameData = await resp.json()
engine.sceneManager.load(data)
engine.stateManager.init(data.variables)
store.setChapters(data.chapters || [])
const unlocked = await saveSystem.getUnlockedChapters()
store.setUnlockedChapters(unlocked)
}
function registerEvents() {
engine.setChapterUnlockHandler(async (chapterId) => { engine.setChapterUnlockHandler(async (chapterId) => {
await saveSystem.unlockChapter(chapterId) await saveSystem.unlockChapter(chapterId)
store.addUnlockedChapter(chapterId) store.addUnlockedChapter(chapterId)
@@ -75,7 +63,9 @@ export function useGameEngine(videoEls: () => [HTMLVideoElement | null, HTMLVide
lastThumbnail = canvas.toDataURL('image/jpeg', 0.6) lastThumbnail = canvas.toDataURL('image/jpeg', 0.6)
} }
} }
} catch { /* ignore */ } } catch {
/* ignore */
}
}) })
engine.on('gameEnd', () => { engine.on('gameEnd', () => {
@@ -98,22 +88,39 @@ export function useGameEngine(videoEls: () => [HTMLVideoElement | null, HTMLVide
engine.videoManager.onTimeUpdate((t: number) => { engine.videoManager.onTimeUpdate((t: number) => {
store.setVideoTime(t) store.setVideoTime(t)
}) })
async function loadGame(dataUrl: string) {
const resp = await fetch(dataUrl)
const data: GameData = await resp.json()
engine.sceneManager.load(data)
engine.stateManager.init(data.variables)
store.setChapters(data.chapters || [])
const unlocked = await saveSystem.getUnlockedChapters()
store.setUnlockedChapters(unlocked)
}
function ensureVideo() {
const [elA, elB] = videoEls()
if (elA && elB) engine.videoManager.attach(elA, elB)
} }
function start() { function start() {
const [elA, elB] = videoEls() ensureVideo()
if (elA && elB) engine.videoManager.attach(elA, elB)
registerEvents()
engine.start() engine.start()
} }
async function resumeAutoSave(): Promise<boolean> { async function resumeAutoSave(): Promise<boolean> {
const [elA, elB] = videoEls() ensureVideo()
if (elA && elB) engine.videoManager.attach(elA, elB) store.setGameEnded(false)
registerEvents()
return await loadGameFromSlot(0) return await loadGameFromSlot(0)
} }
function startChapter(chapterId: string) {
ensureVideo()
store.setGameEnded(false)
engine.startChapter(chapterId)
}
function makeChoice(index: number) { function makeChoice(index: number) {
const scene = store.currentScene const scene = store.currentScene
if (!scene?.choices) return if (!scene?.choices) return
@@ -131,17 +138,9 @@ export function useGameEngine(videoEls: () => [HTMLVideoElement | null, HTMLVide
} }
} }
function startChapter(chapterId: string) {
const [elA, elB] = videoEls()
if (elA && elB) engine.videoManager.attach(elA, elB)
store.setGameEnded(false)
engine.startChapter(chapterId)
}
async function saveGame(slot: number) { async function saveGame(slot: number) {
const state = engine.stateManager const state = engine.stateManager
const currentScene = store.currentScene const currentScene = store.currentScene
await saveSystem.save(slot, { await saveSystem.save(slot, {
timestamp: Date.now(), timestamp: Date.now(),
currentScene: currentScene?.id ?? '', currentScene: currentScene?.id ?? '',
@@ -156,7 +155,6 @@ export function useGameEngine(videoEls: () => [HTMLVideoElement | null, HTMLVide
async function loadGameFromSlot(slot: number): Promise<boolean> { async function loadGameFromSlot(slot: number): Promise<boolean> {
const data = await saveSystem.load(slot) const data = await saveSystem.load(slot)
if (!data) return false if (!data) return false
store.setGameEnded(false) store.setGameEnded(false)
engine.resumeScene(data.currentScene, { engine.resumeScene(data.currentScene, {
variables: data.variables, variables: data.variables,
@@ -179,6 +177,17 @@ export function useGameEngine(videoEls: () => [HTMLVideoElement | null, HTMLVide
destroy() destroy()
}) })
return { loadGame, start, resumeAutoSave, makeChoice, clickHotspot, startChapter, return {
saveGame, loadGameFromSlot, refreshSaves, engine, saveSystem } loadGame,
start,
resumeAutoSave,
makeChoice,
clickHotspot,
startChapter,
saveGame,
loadGameFromSlot,
refreshSaves,
engine,
saveSystem,
}
} }