bgm控制器

This commit is contained in:
unanmed 2023-06-17 23:05:44 +08:00
parent a835e6d4ac
commit 77efd767ee
4 changed files with 71 additions and 14 deletions

View File

@ -0,0 +1,56 @@
import { has } from '../../plugin/utils';
import { ResourceController } from '../loader/controller';
class BgmController extends ResourceController<HTMLAudioElement> {
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();

View File

@ -1,6 +1,7 @@
import { has } from '../../plugin/utils'; import { has } from '../../plugin/utils';
import { AudioParamOf, AudioPlayer } from './audio'; import { AudioParamOf, AudioPlayer } from './audio';
import resource from '../../data/resource.json'; import resource from '../../data/resource.json';
import { ResourceController } from '../loader/controller';
type Panner = AudioParamOf<PannerNode>; type Panner = AudioParamOf<PannerNode>;
@ -131,9 +132,7 @@ export class SoundEffect extends AudioPlayer {
} }
} }
class SoundController { class SoundController extends ResourceController<ArrayBuffer, SoundEffect> {
list: Record<string, SoundEffect> = {};
private seIndex: Record<number, SoundEffect> = {}; private seIndex: Record<number, SoundEffect> = {};
/** /**
@ -150,14 +149,6 @@ class SoundController {
return (this.list[uri] = se); return (this.list[uri] = se);
} }
/**
*
* @param uri uri
*/
remove(uri: string) {
delete this.list[uri];
}
/** /**
* *
* @param sound * @param sound

View File

@ -0,0 +1,9 @@
export abstract class ResourceController<D, T = D> {
list: Record<string, T> = {};
abstract add(uri: string, data: D): void;
remove(uri: string) {
delete this.list[uri];
}
}

View File

@ -25,7 +25,7 @@ export class Resource<
AxiosResponse<ResourceData[T]> | '@imageLoaded' | '@bgmLoaded' AxiosResponse<ResourceData[T]> | '@imageLoaded' | '@bgmLoaded'
>; >;
loaded: boolean = false; loaded: boolean = false;
resStr: string; uri: string;
type!: string; type!: string;
name!: string; name!: string;
@ -38,7 +38,7 @@ export class Resource<
super(resource); super(resource);
this.data = this.resolveUrl(resource); this.data = this.resolveUrl(resource);
this.format = format; this.format = format;
this.resStr = resource; this.uri = resource;
this.once('active', this.load); this.once('active', this.load);
this.once('load', this.onLoad); this.once('load', this.onLoad);
@ -48,6 +48,7 @@ export class Resource<
protected onLoadStart(v?: ResourceData[T]) { protected onLoadStart(v?: ResourceData[T]) {
if (this.format === 'bgm') { if (this.format === 'bgm') {
// bgm 单独处理,因为它可以边播放边加载 // bgm 单独处理,因为它可以边播放边加载
ancTe.bgm.add(this.uri, v!);
} }
} }
@ -56,7 +57,7 @@ export class Resource<
if (this.type === 'fonts') { if (this.type === 'fonts') {
document.fonts.add(new FontFace(this.name, v as ArrayBuffer)); document.fonts.add(new FontFace(this.name, v as ArrayBuffer));
} else if (this.type === 'sounds') { } else if (this.type === 'sounds') {
ancTe.sound.add(this.resStr, v as ArrayBuffer); ancTe.sound.add(this.uri, v as ArrayBuffer);
} }
// 资源加载类型处理 // 资源加载类型处理