mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-01-19 20:59:37 +08:00
bgm控制器
This commit is contained in:
parent
a835e6d4ac
commit
77efd767ee
@ -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();
|
@ -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 音效的名称
|
||||||
|
9
src/core/loader/controller.ts
Normal file
9
src/core/loader/controller.ts
Normal 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];
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 资源加载类型处理
|
// 资源加载类型处理
|
||||||
|
Loading…
Reference in New Issue
Block a user