资源加载,bgm和音效播放

This commit is contained in:
AncTe committed 2023-06-21 17:10:06 +08:00
1 parent 6f59ea51ac
commit 5aed6393a2
16 files changed
+289 -52

No files matched your search

+4 -2
View File
@@ -44,6 +44,7 @@ export class AudioPlayer extends EventEmitter<AudioPlayerEvent> {
async update(data: ArrayBuffer) {
this.data = data;
this.buffer = await ac.decodeAudioData(data);
this.emit('update', this.buffer);
}
@@ -61,9 +62,10 @@ export class AudioPlayer extends EventEmitter<AudioPlayerEvent> {
* 播放音频
*/
play(when?: number, offset?: number, duration?: number) {
if (!this.source) return;
this.ready();
this.source?.start(when, offset, duration);
if (!this.source) return;
this.source.start(when, offset, duration);
const source = this.source;
this.source?.addEventListener('ended', ev => {
+3 -9
View File
@@ -1,7 +1,7 @@
import { has } from '../../plugin/utils';
import { ResourceController } from '../loader/controller';
class BgmController extends ResourceController<HTMLAudioElement> {
export class BgmController extends ResourceController<HTMLAudioElement> {
playing?: BgmIds;
/**
@@ -27,6 +27,7 @@ class BgmController extends ResourceController<HTMLAudioElement> {
const bgm = this.get(id);
bgm.currentTime = when;
bgm.play();
this.playing = id;
}
/**
@@ -48,13 +49,6 @@ class BgmController extends ResourceController<HTMLAudioElement> {
}
get(id: BgmIds) {
return this.list[`bgm.${id}`];
return this.list[`bgms.${id}`];
}
}
declare global {
interface AncTe {
bgm: BgmController;
}
}
ancTe.bgm = new BgmController();
+4 -8
View File
@@ -132,7 +132,10 @@ export class SoundEffect extends AudioPlayer {
}
}
class SoundController extends ResourceController<ArrayBuffer, SoundEffect> {
export class SoundController extends ResourceController<
ArrayBuffer,
SoundEffect
> {
private seIndex: Record<number, SoundEffect> = {};
/**
@@ -196,10 +199,3 @@ class SoundController extends ResourceController<ArrayBuffer, SoundEffect> {
return this.list[`sounds.${sound}`];
}
}
declare global {
interface AncTe {
sound: SoundController;
}
}
ancTe.sound = new SoundController();
+25 -4
View File
@@ -1,11 +1,32 @@
import { BgmController } from './audio/bgm';
import { SoundController } from './audio/sound';
import { readyAllResource } from './loader/load';
import { ResourceStore, ResourceType } from './loader/resource';
export {};
declare global {
interface AncTe {}
interface AncTe {
sound: SoundController;
/** 游戏资源 */
resource: ResourceStore<Exclude<ResourceType, 'zip'>>;
zipResource: ResourceStore<'zip'>;
bgm: BgmController;
}
interface Window {
readonly ancTe: AncTe;
ancTe: AncTe;
}
const ancTe: AncTe;
}
// @ts-ignore
window.ancTe = {};
function ready() {
window.ancTe = {
bgm: new BgmController(),
resource: new ResourceStore(),
zipResource: new ResourceStore(),
sound: new SoundController()
};
readyAllResource();
}
ready();
+12 -2
View File
@@ -7,7 +7,7 @@ const info = resource;
* 构建游戏包后的加载
*/
export function readyAllResource() {
if (main.RESOURCE_TYPE === 'dev') return readyDevResource();
/* @__PURE__ */ if (main.RESOURCE_TYPE === 'dev') return readyDevResource();
info.resource.forEach(v => {
const type = getTypeByResource(v);
if (type === 'zip') {
@@ -21,4 +21,14 @@ export function readyAllResource() {
/**
* 开发时的加载
*/
function readyDevResource() {}
/* @__PURE__ */ async function readyDevResource() {
const loadData = (await import('../../data/resource-dev.json')).default;
loadData.forEach(v => {
const type = getTypeByResource(v);
if (type !== 'zip') {
ancTe.resource.set(v, new Resource(v, type));
}
});
ancTe.resource.forEach(v => v.active());
}
+8 -15
View File
@@ -37,12 +37,13 @@ export class Resource<
constructor(resource: string, format: T) {
super(resource);
this.data = this.resolveUrl(resource);
this.format = format;
this.uri = resource;
this.once('active', this.load);
this.once('load', this.onLoad);
this.once('loadstart', this.onLoadStart);
this.once('active', () => this.load());
this.once('load', v => this.onLoad(v));
this.once('loadstart', v => this.onLoadStart(v));
}
protected onLoadStart(v?: ResourceData[T]) {
@@ -242,7 +243,10 @@ export class ZippedResource extends EventEmitter<ZippedEvent> {
}
}
class ResourceStore<T extends ResourceType> extends Map<string, Resource<T>> {
export class ResourceStore<T extends ResourceType> extends Map<
string,
Resource<T>
> {
active(key: string[] | string) {
const keys = ensureArray(key);
keys.forEach(v => this.get(v)?.active());
@@ -301,14 +305,3 @@ export function getZipFormatByType(type: ResourceType): 'arraybuffer' | 'text' {
if (type === 'text' || type === 'json') return 'text';
else return 'arraybuffer';
}
declare global {
interface AncTe {
/** 游戏资源 */
resource: ResourceStore<Exclude<ResourceType, 'zip'>>;
zipResource: ResourceStore<'zip'>;
}
}
ancTe.resource = new ResourceStore();
ancTe.zipResource = new ResourceStore();