资源分类,分类出音频

This commit is contained in:
unanmed 2023-06-09 22:59:20 +08:00
parent 820dc5bf4c
commit 80ce3b1a31
8 changed files with 101 additions and 12 deletions

View File

@ -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);
}

0
src/core/audio/bgm.ts Normal file
View File

12
src/core/audio/sound.ts Normal file
View File

@ -0,0 +1,12 @@
export class MotaAudio {}
class SoundController {
add(uri: string, data: ArrayBuffer) {}
}
declare global {
interface AncTe {
sound: SoundController;
}
}
ancTe.sound = new SoundController();

View File

@ -37,16 +37,19 @@ export class Disposable<T> extends EventEmitter<DisposableEvent<T>> {
}
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;

11
src/core/index.ts Normal file
View File

@ -0,0 +1,11 @@
export {};
declare global {
interface AncTe {}
interface Window {
ancTe: AncTe;
}
const ancTe: AncTe;
}
// @ts-ignore
window.ancTe = {};

View File

@ -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);
}
});
}

View File

@ -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<string> {
format: T;
request?: Promise<AxiosResponse<ResourceData[T]> | '@imageLoaded'>;
request?: Promise<
AxiosResponse<ResourceData[T]> | '@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<string, Resource> {
}
declare global {
interface Window {
interface AncTe {
/** 游戏资源 */
gameResource: ResourceStore;
resource: ResourceStore;
}
/** 游戏资源 */
const gameResource: ResourceStore;
}
window.gameResource = new ResourceStore();
ancTe.resource = new ResourceStore();

7
src/data/resource.json Normal file
View File

@ -0,0 +1,7 @@
[
{
"floor": [],
"includes": [],
"zip": false
}
]