图片加载

This commit is contained in:
AncTe committed 2023-06-28 22:48:15 +08:00
1 parent 2e0770ae71
commit e947a1e6e8
10 files changed
+164 -63

No files matched your search

+16
View File
@@ -5,16 +5,19 @@ export interface EmitableEvent {
interface Listener<T extends (...params: any) => any> {
fn: T;
once?: boolean;
immediate?: boolean;
}
interface ListenerOptions {
once: boolean;
immediate: boolean;
}
export class EventEmitter<T extends EmitableEvent = {}> {
private events: {
[P in keyof T]?: Listener<T[P]>[];
} = {};
private emitted: (keyof T)[] = [];
/**
* 监听某个事件
@@ -27,6 +30,16 @@ export class EventEmitter<T extends EmitableEvent = {}> {
fn: T[K],
options?: Partial<ListenerOptions>
) {
if (options?.immediate && this.emitted.includes(event)) {
fn();
if (!options.once) {
this.events[event] ??= [];
this.events[event]?.push({
fn
});
}
return;
}
this.events[event] ??= [];
this.events[event]?.push({
fn,
@@ -60,6 +73,9 @@ export class EventEmitter<T extends EmitableEvent = {}> {
* @param params 传入的参数
*/
emit<K extends keyof T>(event: K, ...params: Parameters<T[K]>) {
if (!this.emitted.includes(event)) {
this.emitted.push(event);
}
const events = (this.events[event] ??= []);
for (let i = 0; i < events.length; i++) {
const e = events[i];
-2
View File
@@ -3,8 +3,6 @@ import { SoundController } from './audio/sound';
import { readyAllResource } from './loader/load';
import { ResourceStore, ResourceType } from './loader/resource';
export {};
declare global {
interface AncTe {
sound: SoundController;
+85
View File
@@ -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;
+39
View File
@@ -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 }
);
}
// 资源加载类型处理
+1 -1
View File
@@ -810,7 +810,7 @@ type CoreStatusBarElements = {
readonly [key: string]: HTMLElement;
};
type Materails = [
type Materials = [
'animates',
'enemys',
'items',
+2
View File
@@ -64,6 +64,8 @@ interface Loader {
* @param name 要释放的bgm的id或名称
*/
freeBgm(name: BgmIds | NameMapIn<BgmIds>): void;
_loadMaterials_afterLoad(): void;
}
declare const loader: new () => Loader;
+2
View File
@@ -1376,6 +1376,8 @@ interface Maps {
* @param doCallback 是否执行该动画的回调函数
*/
stopAnimate(id?: number, doCallback?: boolean): void;
_makeAutotileEdges(): void;
}
declare const maps: new () => Maps;