From 77efd767eed9deb511a33fd1dc54c901239e01f3 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Sat, 17 Jun 2023 23:05:44 +0800 Subject: [PATCH] =?UTF-8?q?bgm=E6=8E=A7=E5=88=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/audio/bgm.ts | 56 +++++++++++++++++++++++++++++++++++ src/core/audio/sound.ts | 13 ++------ src/core/loader/controller.ts | 9 ++++++ src/core/loader/resource.ts | 7 +++-- 4 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 src/core/loader/controller.ts diff --git a/src/core/audio/bgm.ts b/src/core/audio/bgm.ts index e69de29..5b1116f 100644 --- a/src/core/audio/bgm.ts +++ b/src/core/audio/bgm.ts @@ -0,0 +1,56 @@ +import { has } from '../../plugin/utils'; +import { ResourceController } from '../loader/controller'; + +class BgmController extends ResourceController { + playing?: BgmIds; + + /** + * 添加一个bgm + * @param uri bgm的uri + * @param data bgm音频元素 + */ + add(uri: string, data: HTMLAudioElement) { + if (this.list[uri]) { + console.warn(`Repeated bgm: '${uri}'`); + } + this.list[uri] = data; + data.loop = true; + } + + /** + * 切换bgm + * @param id bgm的id + */ + play(id: BgmIds) { + const bgm = this.get(id); + } + + /** + * 加载一个bgm + * @param id 要加载的bgm + */ + load(id: BgmIds) { + const bgm = this.get(id); + bgm.load(); + } + + /** + * 停止当前的bgm播放 + */ + stop() { + if (!has(this.playing)) return; + const bgm = this.get(this.playing); + bgm.pause(); + } + + get(id: BgmIds) { + return this.list[`bgm.${id}`]; + } +} + +declare global { + interface AncTe { + bgm: BgmController; + } +} +ancTe.bgm = new BgmController(); diff --git a/src/core/audio/sound.ts b/src/core/audio/sound.ts index 4a4c694..e33a318 100644 --- a/src/core/audio/sound.ts +++ b/src/core/audio/sound.ts @@ -1,6 +1,7 @@ import { has } from '../../plugin/utils'; import { AudioParamOf, AudioPlayer } from './audio'; import resource from '../../data/resource.json'; +import { ResourceController } from '../loader/controller'; type Panner = AudioParamOf; @@ -131,9 +132,7 @@ export class SoundEffect extends AudioPlayer { } } -class SoundController { - list: Record = {}; - +class SoundController extends ResourceController { private seIndex: Record = {}; /** @@ -150,14 +149,6 @@ class SoundController { return (this.list[uri] = se); } - /** - * 移除一个音频 - * @param uri 要移除的音频的uri - */ - remove(uri: string) { - delete this.list[uri]; - } - /** * 播放音频 * @param sound 音效的名称 diff --git a/src/core/loader/controller.ts b/src/core/loader/controller.ts new file mode 100644 index 0000000..9325392 --- /dev/null +++ b/src/core/loader/controller.ts @@ -0,0 +1,9 @@ +export abstract class ResourceController { + list: Record = {}; + + abstract add(uri: string, data: D): void; + + remove(uri: string) { + delete this.list[uri]; + } +} diff --git a/src/core/loader/resource.ts b/src/core/loader/resource.ts index 47096d1..fc644f8 100644 --- a/src/core/loader/resource.ts +++ b/src/core/loader/resource.ts @@ -25,7 +25,7 @@ export class Resource< AxiosResponse | '@imageLoaded' | '@bgmLoaded' >; loaded: boolean = false; - resStr: string; + uri: string; type!: string; name!: string; @@ -38,7 +38,7 @@ export class Resource< super(resource); this.data = this.resolveUrl(resource); this.format = format; - this.resStr = resource; + this.uri = resource; this.once('active', this.load); this.once('load', this.onLoad); @@ -48,6 +48,7 @@ export class Resource< protected onLoadStart(v?: ResourceData[T]) { if (this.format === 'bgm') { // bgm 单独处理,因为它可以边播放边加载 + ancTe.bgm.add(this.uri, v!); } } @@ -56,7 +57,7 @@ export class Resource< if (this.type === 'fonts') { document.fonts.add(new FontFace(this.name, v as ArrayBuffer)); } else if (this.type === 'sounds') { - ancTe.sound.add(this.resStr, v as ArrayBuffer); + ancTe.sound.add(this.uri, v as ArrayBuffer); } // 资源加载类型处理