From d4c03cdaec487b09c41adea45ce1144a0198ad8c Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Sun, 18 Jun 2023 22:42:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9F=B3=E4=B9=90=E4=BB=8E=E6=9F=90=E4=B8=AA?= =?UTF-8?q?=E6=97=B6=E5=88=BB=E5=BC=80=E5=A7=8B=E6=92=AD=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/audio/bgm.ts | 8 ++++++-- src/core/loader/controller.ts | 5 +++++ src/core/loader/resource.ts | 24 +++++++++++------------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/core/audio/bgm.ts b/src/core/audio/bgm.ts index 5b1116f..921cdf2 100644 --- a/src/core/audio/bgm.ts +++ b/src/core/audio/bgm.ts @@ -11,7 +11,7 @@ class BgmController extends ResourceController { */ add(uri: string, data: HTMLAudioElement) { if (this.list[uri]) { - console.warn(`Repeated bgm: '${uri}'`); + console.warn(`Repeated bgm: '${uri}'.`); } this.list[uri] = data; data.loop = true; @@ -21,8 +21,12 @@ class BgmController extends ResourceController { * 切换bgm * @param id bgm的id */ - play(id: BgmIds) { + play(id: BgmIds, when: number = 0) { + if (this.playing === id) return; + this.stop(); const bgm = this.get(id); + bgm.currentTime = when; + bgm.play(); } /** diff --git a/src/core/loader/controller.ts b/src/core/loader/controller.ts index 9325392..d3b0049 100644 --- a/src/core/loader/controller.ts +++ b/src/core/loader/controller.ts @@ -1,6 +1,11 @@ export abstract class ResourceController { list: Record = {}; + /** + * 添加一个资源 + * @param uri 资源uri + * @param data 资源数据 + */ abstract add(uri: string, data: D): void; remove(uri: string) { diff --git a/src/core/loader/resource.ts b/src/core/loader/resource.ts index fc644f8..763bbb3 100644 --- a/src/core/loader/resource.ts +++ b/src/core/loader/resource.ts @@ -271,19 +271,6 @@ class ResourceStore extends Map> { } } -declare global { - interface AncTe { - /** 游戏资源 */ - resource: ResourceStore>; - zipResource: ResourceStore<'zip'>; - } -} - -ancTe.resource = new ResourceStore(); -ancTe.zipResource = new ResourceStore(); - -console.log(JSZip.external.Promise); - export function getTypeByResource(resource: string): ResourceType { const type = resource.split('.')[0]; @@ -301,3 +288,14 @@ export function getZipFormatByType(type: ResourceType): 'arraybuffer' | 'text' { if (type === 'text' || type === 'json') return 'text'; else return 'arraybuffer'; } + +declare global { + interface AncTe { + /** 游戏资源 */ + resource: ResourceStore>; + zipResource: ResourceStore<'zip'>; + } +} + +ancTe.resource = new ResourceStore(); +ancTe.zipResource = new ResourceStore();