HumanBreak/src/core/loader/load.ts

124 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-06-09 22:59:20 +08:00
import resource from '../../data/resource.json';
2023-06-28 22:48:15 +08:00
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
2023-06-14 19:20:24 +08:00
import { Resource, getTypeByResource } from './resource';
2023-06-09 22:59:20 +08:00
2023-06-28 22:48:15 +08:00
interface GameLoadEvent extends EmitableEvent {
coreLoaded: () => void;
autotileLoaded: () => void;
coreInit: () => void;
materialLoaded: () => void;
}
2023-06-14 19:20:24 +08:00
const info = resource;
2023-06-09 22:59:20 +08:00
2023-06-20 22:35:51 +08:00
/**
*
*/
2023-06-09 22:59:20 +08:00
export function readyAllResource() {
2023-06-21 17:10:06 +08:00
/* @__PURE__ */ if (main.RESOURCE_TYPE === 'dev') return readyDevResource();
2023-06-15 12:35:43 +08:00
info.resource.forEach(v => {
2023-06-14 19:20:24 +08:00
const type = getTypeByResource(v);
if (type === 'zip') {
2023-09-06 11:59:44 +08:00
mota.zipResource.set(v, new Resource(v, 'zip'));
2023-06-09 22:59:20 +08:00
} else {
2023-09-06 11:59:44 +08:00
mota.resource.set(v, new Resource(v, type));
2023-06-09 22:59:20 +08:00
}
});
}
2023-06-20 22:35:51 +08:00
/**
*
*/
2023-06-21 17:10:06 +08:00
/* @__PURE__ */ async function readyDevResource() {
const loadData = (await import('../../data/resource-dev.json')).default;
loadData.forEach(v => {
const type = getTypeByResource(v);
if (type !== 'zip') {
2023-09-06 11:59:44 +08:00
mota.resource.set(v, new Resource(v, type));
2023-06-21 17:10:06 +08:00
}
});
2023-09-06 11:59:44 +08:00
mota.resource.forEach(v => v.active());
2023-06-29 20:24:21 +08:00
loading.once('coreInit', () => {
const animates = new Resource('__all_animates__', 'text');
2023-09-06 11:59:44 +08:00
mota.resource.set('__all_animates__', animates);
2023-06-29 20:24:21 +08:00
animates.active();
});
2023-06-21 17:10:06 +08:00
}
2023-06-28 22:48:15 +08:00
class GameLoading extends EventEmitter<GameLoadEvent> {
private autotileLoaded: number = 0;
private autotileNum?: number;
private autotileListened: boolean = false;
private materialsNum: number = main.materials.length;
private materialsLoaded: number = 0;
constructor() {
super();
this.on(
'coreInit',
() => {
this.autotileNum = Object.keys(
core.material.icons.autotile
).length;
},
{ immediate: true }
);
this.on('materialLoaded', () => {
core.loader._loadMaterials_afterLoad();
});
}
addMaterialLoaded() {
this.once('coreInit', () => {
this.materialsLoaded++;
if (this.materialsLoaded === this.materialsNum) {
this.emit('materialLoaded');
}
});
}
addAutotileLoaded() {
this.once('coreInit', () => {
this.autotileLoaded++;
if (this.autotileLoaded === this.autotileNum) {
this.emit('autotileLoaded');
}
});
}
/**
*
* @param autotiles
*/
onAutotileLoaded(
autotiles: Partial<Record<AllIdsOf<'autotile'>, HTMLImageElement>>
) {
if (this.autotileListened) return;
this.autotileListened = true;
this.on('autotileLoaded', () => {
const keys = Object.keys(
core.material.icons.autotile
) as AllIdsOf<'autotile'>[];
keys.forEach(v => {
core.material.images.autotile[v] = autotiles[v]!;
});
setTimeout(() => {
core.maps._makeAutotileEdges();
});
});
}
}
export const loading = new GameLoading();
declare global {
interface Main {
2023-08-01 12:22:05 +08:00
loading: GameLoading;
2023-06-28 22:48:15 +08:00
}
}
main.loading = loading;