mirror of
https://github.com/unanmed/HumanBreak.git
synced 2026-11-18 13:24:30 +08:00
图片加载
This commit is contained in:
1 parent
2e0770ae71
commit
e947a1e6e8
10 files changed
+164
-63
No files matched your search
@@ -1,6 +1,15 @@
|
||||
import resource from '../../data/resource.json';
|
||||
import { has } from '../../plugin/utils';
|
||||
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
|
||||
import { Resource, getTypeByResource } from './resource';
|
||||
|
||||
interface GameLoadEvent extends EmitableEvent {
|
||||
coreLoaded: () => void;
|
||||
autotileLoaded: () => void;
|
||||
coreInit: () => void;
|
||||
materialLoaded: () => void;
|
||||
}
|
||||
|
||||
const info = resource;
|
||||
|
||||
/**
|
||||
@@ -32,3 +41,79 @@ export function readyAllResource() {
|
||||
});
|
||||
ancTe.resource.forEach(v => v.active());
|
||||
}
|
||||
|
||||
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 {
|
||||
loading: EventEmitter<GameLoadEvent>;
|
||||
}
|
||||
}
|
||||
main.loading = loading;
|
||||
@@ -4,6 +4,7 @@ import { ensureArray } from '../../plugin/utils';
|
||||
import { has } from '../../plugin/utils';
|
||||
import JSZip from 'jszip';
|
||||
import { EmitableEvent, EventEmitter } from '../common/eventEmitter';
|
||||
import { loading } from './load';
|
||||
|
||||
interface ResourceData {
|
||||
image: HTMLImageElement;
|
||||
@@ -17,6 +18,8 @@ interface ResourceData {
|
||||
export type ResourceType = keyof ResourceData;
|
||||
export type NonZipResource = Exclude<ResourceType, 'zip'>;
|
||||
|
||||
const autotiles: Partial<Record<AllIdsOf<'autotile'>, HTMLImageElement>> = {};
|
||||
|
||||
export class Resource<
|
||||
T extends ResourceType = ResourceType
|
||||
> extends Disposable<string> {
|
||||
@@ -59,6 +62,42 @@ export class Resource<
|
||||
document.fonts.add(new FontFace(this.name, v as ArrayBuffer));
|
||||
} else if (this.type === 'sounds') {
|
||||
ancTe.sound.add(this.uri, v as ArrayBuffer);
|
||||
} else if (this.type === 'images') {
|
||||
const name = `${this.name}${this.ext}` as ImageIds;
|
||||
loading.on(
|
||||
'coreLoaded',
|
||||
() => {
|
||||
core.material.images.images[name] = v as HTMLImageElement;
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
} else if (this.type === 'materials') {
|
||||
const name = this.name as SelectKey<
|
||||
MaterialImages,
|
||||
HTMLImageElement
|
||||
>;
|
||||
|
||||
loading.on(
|
||||
'coreLoaded',
|
||||
() => {
|
||||
core.material.images[name] = v;
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
} else if (this.type === 'autotiles') {
|
||||
const name = this.name as AllIdsOf<'autotile'>;
|
||||
autotiles[name] = v;
|
||||
loading.addAutotileLoaded();
|
||||
loading.onAutotileLoaded(autotiles);
|
||||
} else if (this.type === 'tilesets') {
|
||||
const name = `${this.name}${this.ext}`;
|
||||
loading.on(
|
||||
'coreLoaded',
|
||||
() => {
|
||||
core.material.images.tilesets[name] = v;
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
}
|
||||
|
||||
// 资源加载类型处理
|
||||
|
||||
Reference in new issue
Block a user