From 87166c5ed76c17d786ff25a4b9e997ca35ed52e2 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Thu, 29 Jun 2023 20:24:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=8A=A0=E8=BD=BD=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/libs/loader.js | 90 ++++++++++++++++++++++++++++++++++--- public/main.js | 8 ++-- script/dev.ts | 1 - src/core/loader/load.ts | 5 +++ src/core/loader/resource.ts | 27 +++++++++++ src/data/resource-dev.json | 22 --------- src/types/loader.d.ts | 2 + 7 files changed, 121 insertions(+), 34 deletions(-) diff --git a/public/libs/loader.js b/public/libs/loader.js index f00b383..fab081d 100644 --- a/public/libs/loader.js +++ b/public/libs/loader.js @@ -32,7 +32,15 @@ loader.prototype._load = function (callback) { loader.prototype._load_sync = function (callback) { this._loadAnimates_sync(); - callback(); + if (main.mode === 'play') return callback(); + this._loadMusic_sync(); + core.loader._loadMaterials_sync(function () { + core.loader._loadExtraImages_sync(function () { + core.loader._loadAutotiles_sync(function () { + core.loader._loadTilesets_sync(callback); + }); + }); + }); }; loader.prototype._load_async = function (callback) { @@ -108,7 +116,17 @@ loader.prototype._load_async = function (callback) { // ----- 加载资源文件 ------ // loader.prototype._loadMaterials_sync = function (callback) { - callback(); + if (main.mode === 'play') return callback(); + this._setStartLoadTipText('正在加载资源文件...'); + this.loadImages( + 'materials', + core.materials, + core.material.images, + function () { + core.loader._loadMaterials_afterLoad(); + callback(); + } + ); }; loader.prototype._loadMaterials_async = function (onprogress, onfinished) { @@ -138,7 +156,15 @@ loader.prototype._loadMaterials_afterLoad = function () { // ------ 加载使用的图片 ------ // loader.prototype._loadExtraImages_sync = function (callback) { - callback(); + if (main.mode === 'play') return callback(); + core.material.images.images = {}; + this._setStartLoadTipText('正在加载图片文件...'); + core.loadImages( + 'images', + core.images, + core.material.images.images, + callback + ); }; loader.prototype._loadExtraImages_async = function (onprogress, onfinished) { @@ -173,7 +199,16 @@ loader.prototype._loadExtraImages_async = function (onprogress, onfinished) { // ------ 加载自动元件 ------ // loader.prototype._loadAutotiles_sync = function (callback) { - callback(); + if (main.mode === 'play') return callback(); + core.material.images.autotile = {}; + var keys = Object.keys(core.material.icons.autotile); + var autotiles = {}; + + this._setStartLoadTipText('正在加载自动元件...'); + this.loadImages('autotiles', keys, autotiles, function () { + core.loader._loadAutotiles_afterLoad(keys, autotiles); + callback(); + }); }; loader.prototype._loadAutotiles_async = function (onprogress, onfinished) { @@ -193,12 +228,32 @@ loader.prototype._loadAutotiles_async = function (onprogress, onfinished) { ); }; -loader.prototype._loadAutotiles_afterLoad = function (keys, autotiles) {}; +loader.prototype._loadAutotiles_afterLoad = function (keys, autotiles) { + if (main.mode === 'play') return; + // autotile需要保证顺序 + keys.forEach(function (v) { + core.material.images.autotile[v] = autotiles[v]; + }); + + setTimeout(function () { + core.maps._makeAutotileEdges(); + }); +}; // ------ 加载额外素材 ------ // loader.prototype._loadTilesets_sync = function (callback) { - callback(); + if (main.mode === 'play') return callback(); + core.material.images.tilesets = {}; + this._setStartLoadTipText('正在加载额外素材...'); + this.loadImages( + 'tilesets', + core.tilesets, + core.material.images.tilesets, + function () { + callback(); + } + ); }; loader.prototype._loadTilesets_async = function (onprogress, onfinished) { @@ -217,7 +272,27 @@ loader.prototype._loadTilesets_async = function (onprogress, onfinished) { // ------ 实际加载一系列图片 ------ // loader.prototype.loadImages = function (dir, names, toSave, callback) { - return callback(); + if (main.mode === 'play') return callback(); + if (!names || names.length == 0) { + if (callback) callback(); + return; + } + var items = 0; + for (var i = 0; i < names.length; i++) { + this.loadImage(dir, names[i], function (id, image) { + core.loader._setStartLoadTipText('正在加载图片 ' + id + '...'); + if (toSave[id] !== undefined) { + if (image != null) toSave[id] = image; + return; + } + toSave[id] = image; + items++; + core.loader._setStartProgressVal(items * (100 / names.length)); + if (items == names.length) { + if (callback) callback(); + } + }); + } }; loader.prototype.loadImage = function (dir, imgName, callback) { @@ -288,6 +363,7 @@ loader.prototype.loadImagesFromZip = function ( // ------ 加载动画文件 ------ // loader.prototype._loadAnimates_sync = function () { + if (main.mode === 'play') return; this._setStartLoadTipText('正在加载动画文件...'); if (main.supportBunch) { diff --git a/public/main.js b/public/main.js index e0dbb6e..b8f53ff 100644 --- a/public/main.js +++ b/public/main.js @@ -336,12 +336,12 @@ main.prototype.loadAsync = async function (mode, callback) { // 加载核心js代码 if (main.useCompress) { await main.loadScript(`libs/libs.min.js?v=${main.version}`); - main.loading.emit('coreLoaded'); + if (main.mode === 'play') main.loading.emit('coreLoaded'); } else { await Promise.all( main.loadList.map(v => - main.loadScript(`libs/${v}.js?v=${main.version}`).then(vv => { - if (v === 'core') { + main.loadScript(`libs/${v}.js?v=${main.version}`).then(() => { + if (v === 'core' && main.mode === 'play') { main.loading.emit('coreLoaded'); } }) @@ -403,7 +403,7 @@ main.prototype.loadAsync = async function (mode, callback) { coreData[t] = main[t]; }); await core.init(coreData, callback); - main.loading.emit('coreInit'); + if (main.mode === 'play') main.loading.emit('coreInit'); core.resize(); diff --git a/script/dev.ts b/script/dev.ts index 07caded..dfc9455 100644 --- a/script/dev.ts +++ b/script/dev.ts @@ -324,7 +324,6 @@ async function writeDevResource(data: string) { const icons = await fs.readFile('./public/project/icons.js', 'utf-8'); const iconData = JSON.parse(icons.split('\n').slice(1).join('')); res.push( - ...info.main.animates.map((v: any) => `animates.${v}.animate`), ...info.main.bgms.map((v: any) => `bgms.${v}`), ...info.main.fonts.map((v: any) => `fonts.${v}.ttf`), ...info.main.images.map((v: any) => `images.${v}`), diff --git a/src/core/loader/load.ts b/src/core/loader/load.ts index f30d19c..728679f 100644 --- a/src/core/loader/load.ts +++ b/src/core/loader/load.ts @@ -40,6 +40,11 @@ export function readyAllResource() { } }); ancTe.resource.forEach(v => v.active()); + loading.once('coreInit', () => { + const animates = new Resource('__all_animates__', 'text'); + ancTe.resource.set('__all_animates__', animates); + animates.active(); + }); } class GameLoading extends EventEmitter { diff --git a/src/core/loader/resource.ts b/src/core/loader/resource.ts index f926857..c8501f6 100644 --- a/src/core/loader/resource.ts +++ b/src/core/loader/resource.ts @@ -84,6 +84,7 @@ export class Resource< }, { immediate: true } ); + loading.addMaterialLoaded(); } else if (this.type === 'autotiles') { const name = this.name as AllIdsOf<'autotile'>; autotiles[name] = v; @@ -115,6 +116,23 @@ export class Resource< }); }); } + + if (this.name === '__all_animates__') { + if (this.format !== 'text') { + throw new Error( + `Unexpected mismatch of '__all_animates__' response type.` + + ` Expected: text. Meet: ${this.format}` + ); + } + const data = (v as string).split('@@@~~~###~~~@@@'); + data.forEach((v, i) => { + const id = main.animates[i]; + if (v === '') { + throw new Error(`Cannot find animate: '${id}'`); + } + core.material.animates[id] = core.loader._loadAnimate(v); + }); + } } /** @@ -123,6 +141,15 @@ export class Resource< * @returns 解析出的资源url */ protected resolveUrl(resource: string) { + if (resource === '__all_animates__') { + this.type = 'animates'; + this.name = '__all_animates__'; + this.ext = '.animate'; + + return `/all/__all_animates__?v=${ + main.version + }&id=${main.animates.join(',')}`; + } const resolve = resource.split('.'); const type = (this.type = resolve[0]); const name = (this.name = resolve.slice(1, -1).join('.')); diff --git a/src/data/resource-dev.json b/src/data/resource-dev.json index dd583df..abdd0aa 100644 --- a/src/data/resource-dev.json +++ b/src/data/resource-dev.json @@ -1,26 +1,4 @@ [ - "animates.amazed.animate", - "animates.angry.animate", - "animates.angry2.animate", - "animates.bulb.animate", - "animates.emm.animate", - "animates.explosion1.animate", - "animates.explosion2.animate", - "animates.explosion3.animate", - "animates.explosion4.animate", - "animates.fire.animate", - "animates.focus.animate", - "animates.fret.animate", - "animates.hand.animate", - "animates.ice.animate", - "animates.jianji.animate", - "animates.luv.animate", - "animates.magicAtk.animate", - "animates.stone.animate", - "animates.sweat.animate", - "animates.sweat2.animate", - "animates.sword.animate", - "animates.zone.animate", "bgms.beforeBoss.mp3", "bgms.cave.mp3", "bgms.escape.mp3", diff --git a/src/types/loader.d.ts b/src/types/loader.d.ts index ac25e9a..cd9f365 100644 --- a/src/types/loader.d.ts +++ b/src/types/loader.d.ts @@ -66,6 +66,8 @@ interface Loader { freeBgm(name: BgmIds | NameMapIn): void; _loadMaterials_afterLoad(): void; + + _loadAnimate(data: string): Animate; } declare const loader: new () => Loader;