From 80ce3b1a313fabe09c8c59bce9421185c154ed6b Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Fri, 9 Jun 2023 22:59:20 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=E5=88=86=E7=B1=BB=EF=BC=8C?= =?UTF-8?q?=E5=88=86=E7=B1=BB=E5=87=BA=E9=9F=B3=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/resource.ts | 3 ++- src/core/audio/bgm.ts | 0 src/core/audio/sound.ts | 12 +++++++++ src/core/common/disposable.ts | 3 +++ src/core/index.ts | 11 ++++++++ src/core/loader/load.ts | 27 ++++++++++++++++++- src/core/loader/resource.ts | 50 ++++++++++++++++++++++++++++------- src/data/resource.json | 7 +++++ 8 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 src/core/audio/bgm.ts create mode 100644 src/core/audio/sound.ts create mode 100644 src/core/index.ts create mode 100644 src/data/resource.json diff --git a/script/resource.ts b/script/resource.ts index 6c5e3c2..626c998 100644 --- a/script/resource.ts +++ b/script/resource.ts @@ -23,11 +23,12 @@ let totalSize = 0; type Stats = fs.Stats & { name?: string }; export async function splitResorce(compress: boolean = false) { + await fs.ensureDir('./dist-resource'); + await fs.emptyDir('./dist-resource'); const folder = await fs.stat('./dist'); totalSize = folder.size; if (totalSize < MAX_SIZE) return; - await fs.ensureDir('./dist-resource'); await doSplit(compress); } diff --git a/src/core/audio/bgm.ts b/src/core/audio/bgm.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/core/audio/sound.ts b/src/core/audio/sound.ts new file mode 100644 index 0000000..d14c1df --- /dev/null +++ b/src/core/audio/sound.ts @@ -0,0 +1,12 @@ +export class MotaAudio {} + +class SoundController { + add(uri: string, data: ArrayBuffer) {} +} + +declare global { + interface AncTe { + sound: SoundController; + } +} +ancTe.sound = new SoundController(); diff --git a/src/core/common/disposable.ts b/src/core/common/disposable.ts index 0ba1fc9..45dffcd 100644 --- a/src/core/common/disposable.ts +++ b/src/core/common/disposable.ts @@ -37,16 +37,19 @@ export class Disposable extends EventEmitter> { } active() { + if (this.activated) return; this.activated = true; this.emit('active', this._data!); } dispose() { + if (!this.activated) return; this.activated = false; this.emit('dispose', this._data!); } destroy() { + if (this.destroyed) return; this.destroyed = true; this.emit('destroy'); delete this._data; diff --git a/src/core/index.ts b/src/core/index.ts new file mode 100644 index 0000000..900acb5 --- /dev/null +++ b/src/core/index.ts @@ -0,0 +1,11 @@ +export {}; + +declare global { + interface AncTe {} + interface Window { + ancTe: AncTe; + } + const ancTe: AncTe; +} +// @ts-ignore +window.ancTe = {}; diff --git a/src/core/loader/load.ts b/src/core/loader/load.ts index 6342cee..5ee3ebb 100644 --- a/src/core/loader/load.ts +++ b/src/core/loader/load.ts @@ -1 +1,26 @@ -export function readyAllResource() {} +import resource from '../../data/resource.json'; +import { Resource, ResourceType } from './resource'; + +const info = resource as ResourceInfo[]; + +export interface ResourceInfo { + floor: [FloorIds, FloorIds][]; + includes: string[]; + zip: boolean; + zippedName?: string; +} + +export function readyAllResource() { + info.forEach(v => { + if (v.zip) { + const res = new Resource(`zip.${v.zippedName}`, 'zip'); + ancTe.resource.push([[`zip.${v.zippedName}`, res]]); + } else { + const res: [string, Resource][] = v.includes.map(v => { + const type = v.split('.')[0]; + return [v, new Resource(v, type as ResourceType)]; + }); + ancTe.resource.push(res); + } + }); +} diff --git a/src/core/loader/resource.ts b/src/core/loader/resource.ts index a448e92..0c6e62f 100644 --- a/src/core/loader/resource.ts +++ b/src/core/loader/resource.ts @@ -11,16 +11,24 @@ interface ResourceData { text: string; json: any; zip: ZippedResource; + bgm: HTMLAudioElement; } -type ResourceType = keyof ResourceData; +export type ResourceType = keyof ResourceData; export class Resource< T extends ResourceType = ResourceType > extends Disposable { format: T; - request?: Promise | '@imageLoaded'>; + request?: Promise< + AxiosResponse | '@imageLoaded' | '@bgmLoaded' + >; loaded: boolean = false; + resStr: string; + + type!: string; + name!: string; + ext!: string; /** 资源数据 */ resource?: ResourceData[T]; @@ -29,8 +37,18 @@ export class Resource< super(resource); this.data = this.resolveUrl(resource); this.format = type; + this.resStr = resource; this.on('active', this.load); + this.on('load', this.onload); + } + + protected onload(v: ResourceData[T]) { + 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); + } } /** @@ -40,9 +58,9 @@ export class Resource< */ protected resolveUrl(resource: string) { const resolve = resource.split('.'); - const type = resolve[0]; - const name = resolve.slice(1, -1).join('.'); - const ext = '.' + resolve.at(-1); + const type = (this.type = resolve[0]); + const name = (this.name = resolve.slice(1, -1).join('.')); + const ext = (this.ext = '.' + resolve.at(-1)); if (!main.USE_RESOURCE) { return `/games/${core.data.firstData.name}/project/${type}/${name}${ext}`; @@ -77,9 +95,21 @@ export class Resource< img.addEventListener('load', () => { this.resource = img; this.loaded = true; + this.emit('load', img); res('@imageLoaded'); }); }); + } else if (this.format === 'bgm') { + this.request = new Promise(res => { + const audio = new Audio(); + audio.src = data; + audio.addEventListener('load', () => { + this.resource = audio; + this.loaded = true; + this.emit('load', audio); + res('@bgmLoaded'); + }); + }); } else if ( this.format === 'json' || this.format === 'text' || @@ -90,6 +120,7 @@ export class Resource< .then(v => { this.resource = v.data; this.loaded = true; + this.emit('load', v.data); return v; }); } else if (this.format === 'zip') { @@ -98,6 +129,7 @@ export class Resource< .then(v => { this.resource = new ZippedResource(v.data); this.loaded = true; + this.emit('load', this.resource); return v; }); } @@ -172,12 +204,10 @@ class ResourceStore extends Map { } declare global { - interface Window { + interface AncTe { /** 游戏资源 */ - gameResource: ResourceStore; + resource: ResourceStore; } - /** 游戏资源 */ - const gameResource: ResourceStore; } -window.gameResource = new ResourceStore(); +ancTe.resource = new ResourceStore(); diff --git a/src/data/resource.json b/src/data/resource.json new file mode 100644 index 0000000..a89ea46 --- /dev/null +++ b/src/data/resource.json @@ -0,0 +1,7 @@ +[ + { + "floor": [], + "includes": [], + "zip": false + } +] \ No newline at end of file