mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-02-28 09:27:07 +08:00
资源分类,分类出音频
This commit is contained in:
parent
820dc5bf4c
commit
80ce3b1a31
@ -23,11 +23,12 @@ let totalSize = 0;
|
|||||||
type Stats = fs.Stats & { name?: string };
|
type Stats = fs.Stats & { name?: string };
|
||||||
|
|
||||||
export async function splitResorce(compress: boolean = false) {
|
export async function splitResorce(compress: boolean = false) {
|
||||||
|
await fs.ensureDir('./dist-resource');
|
||||||
|
await fs.emptyDir('./dist-resource');
|
||||||
const folder = await fs.stat('./dist');
|
const folder = await fs.stat('./dist');
|
||||||
totalSize = folder.size;
|
totalSize = folder.size;
|
||||||
if (totalSize < MAX_SIZE) return;
|
if (totalSize < MAX_SIZE) return;
|
||||||
|
|
||||||
await fs.ensureDir('./dist-resource');
|
|
||||||
await doSplit(compress);
|
await doSplit(compress);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
0
src/core/audio/bgm.ts
Normal file
0
src/core/audio/bgm.ts
Normal file
12
src/core/audio/sound.ts
Normal file
12
src/core/audio/sound.ts
Normal 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();
|
@ -37,16 +37,19 @@ export class Disposable<T> extends EventEmitter<DisposableEvent<T>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
active() {
|
active() {
|
||||||
|
if (this.activated) return;
|
||||||
this.activated = true;
|
this.activated = true;
|
||||||
this.emit('active', this._data!);
|
this.emit('active', this._data!);
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
|
if (!this.activated) return;
|
||||||
this.activated = false;
|
this.activated = false;
|
||||||
this.emit('dispose', this._data!);
|
this.emit('dispose', this._data!);
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
if (this.destroyed) return;
|
||||||
this.destroyed = true;
|
this.destroyed = true;
|
||||||
this.emit('destroy');
|
this.emit('destroy');
|
||||||
delete this._data;
|
delete this._data;
|
||||||
|
11
src/core/index.ts
Normal file
11
src/core/index.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export {};
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface AncTe {}
|
||||||
|
interface Window {
|
||||||
|
ancTe: AncTe;
|
||||||
|
}
|
||||||
|
const ancTe: AncTe;
|
||||||
|
}
|
||||||
|
// @ts-ignore
|
||||||
|
window.ancTe = {};
|
@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -11,16 +11,24 @@ interface ResourceData {
|
|||||||
text: string;
|
text: string;
|
||||||
json: any;
|
json: any;
|
||||||
zip: ZippedResource;
|
zip: ZippedResource;
|
||||||
|
bgm: HTMLAudioElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourceType = keyof ResourceData;
|
export type ResourceType = keyof ResourceData;
|
||||||
|
|
||||||
export class Resource<
|
export class Resource<
|
||||||
T extends ResourceType = ResourceType
|
T extends ResourceType = ResourceType
|
||||||
> extends Disposable<string> {
|
> extends Disposable<string> {
|
||||||
format: T;
|
format: T;
|
||||||
request?: Promise<AxiosResponse<ResourceData[T]> | '@imageLoaded'>;
|
request?: Promise<
|
||||||
|
AxiosResponse<ResourceData[T]> | '@imageLoaded' | '@bgmLoaded'
|
||||||
|
>;
|
||||||
loaded: boolean = false;
|
loaded: boolean = false;
|
||||||
|
resStr: string;
|
||||||
|
|
||||||
|
type!: string;
|
||||||
|
name!: string;
|
||||||
|
ext!: string;
|
||||||
|
|
||||||
/** 资源数据 */
|
/** 资源数据 */
|
||||||
resource?: ResourceData[T];
|
resource?: ResourceData[T];
|
||||||
@ -29,8 +37,18 @@ export class Resource<
|
|||||||
super(resource);
|
super(resource);
|
||||||
this.data = this.resolveUrl(resource);
|
this.data = this.resolveUrl(resource);
|
||||||
this.format = type;
|
this.format = type;
|
||||||
|
this.resStr = resource;
|
||||||
|
|
||||||
this.on('active', this.load);
|
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) {
|
protected resolveUrl(resource: string) {
|
||||||
const resolve = resource.split('.');
|
const resolve = resource.split('.');
|
||||||
const type = resolve[0];
|
const type = (this.type = resolve[0]);
|
||||||
const name = resolve.slice(1, -1).join('.');
|
const name = (this.name = resolve.slice(1, -1).join('.'));
|
||||||
const ext = '.' + resolve.at(-1);
|
const ext = (this.ext = '.' + resolve.at(-1));
|
||||||
|
|
||||||
if (!main.USE_RESOURCE) {
|
if (!main.USE_RESOURCE) {
|
||||||
return `/games/${core.data.firstData.name}/project/${type}/${name}${ext}`;
|
return `/games/${core.data.firstData.name}/project/${type}/${name}${ext}`;
|
||||||
@ -77,9 +95,21 @@ export class Resource<
|
|||||||
img.addEventListener('load', () => {
|
img.addEventListener('load', () => {
|
||||||
this.resource = img;
|
this.resource = img;
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
|
this.emit('load', img);
|
||||||
res('@imageLoaded');
|
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 (
|
} else if (
|
||||||
this.format === 'json' ||
|
this.format === 'json' ||
|
||||||
this.format === 'text' ||
|
this.format === 'text' ||
|
||||||
@ -90,6 +120,7 @@ export class Resource<
|
|||||||
.then(v => {
|
.then(v => {
|
||||||
this.resource = v.data;
|
this.resource = v.data;
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
|
this.emit('load', v.data);
|
||||||
return v;
|
return v;
|
||||||
});
|
});
|
||||||
} else if (this.format === 'zip') {
|
} else if (this.format === 'zip') {
|
||||||
@ -98,6 +129,7 @@ export class Resource<
|
|||||||
.then(v => {
|
.then(v => {
|
||||||
this.resource = new ZippedResource(v.data);
|
this.resource = new ZippedResource(v.data);
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
|
this.emit('load', this.resource);
|
||||||
return v;
|
return v;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -172,12 +204,10 @@ class ResourceStore extends Map<string, Resource> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
declare global {
|
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
7
src/data/resource.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"floor": [],
|
||||||
|
"includes": [],
|
||||||
|
"zip": false
|
||||||
|
}
|
||||||
|
]
|
Loading…
Reference in New Issue
Block a user