feat: global assetBase for scene JSON, convert demo to relative paths
This commit is contained in:
@@ -111,9 +111,43 @@ export function useGameEngine(videoEls: () => [HTMLVideoElement | null, HTMLVide
|
||||
store.setVideoTime(t)
|
||||
})
|
||||
|
||||
function resolveAsset(base: string, path: string): string {
|
||||
if (!path || path.startsWith('http://') || path.startsWith('https://') || path.startsWith('data:')) return path
|
||||
const b = base.endsWith('/') ? base.slice(0, -1) : base
|
||||
const p = path.startsWith('/') ? path : '/' + path
|
||||
return b + p
|
||||
}
|
||||
|
||||
function applyAssetBase(data: GameData) {
|
||||
const base = data.assetBase || ''
|
||||
if (!base) return
|
||||
for (const scene of Object.values(data.scenes)) {
|
||||
if (scene.videoUrl) scene.videoUrl = resolveAsset(base, scene.videoUrl)
|
||||
if (scene.subtitleUrl) scene.subtitleUrl = resolveAsset(base, scene.subtitleUrl)
|
||||
if (scene.imageUrl) scene.imageUrl = resolveAsset(base, scene.imageUrl)
|
||||
if (scene.bgmUrl) scene.bgmUrl = resolveAsset(base, scene.bgmUrl)
|
||||
if (scene.subtitles) {
|
||||
for (const k of Object.keys(scene.subtitles)) {
|
||||
scene.subtitles[k] = resolveAsset(base, scene.subtitles[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
if (data.endings) {
|
||||
for (const e of data.endings) {
|
||||
if (e.thumbnail) e.thumbnail = resolveAsset(base, e.thumbnail)
|
||||
}
|
||||
}
|
||||
if (data.chapters) {
|
||||
for (const c of data.chapters) {
|
||||
if (c.thumbnail) c.thumbnail = resolveAsset(base, c.thumbnail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function loadGame(dataUrl: string) {
|
||||
const resp = await fetch(dataUrl)
|
||||
const data: GameData = await resp.json()
|
||||
applyAssetBase(data)
|
||||
engine.sceneManager.load(data)
|
||||
engine.stateManager.init(data.variables)
|
||||
store.setChapters(data.chapters || [])
|
||||
|
||||
Reference in New Issue
Block a user