大体积魔塔分块加载

This commit is contained in:
草莓 2024-11-30 15:09:02 +08:00
parent 4b1d1dd89c
commit 649bdf1121
2 changed files with 149 additions and 59 deletions

View File

@ -24,6 +24,7 @@ loader.prototype._setStartLoadTipText = function (text) {
}
loader.prototype._load = function (callback) {
this._loadMusics();
if (main.useCompress) {
this._load_async(callback);
} else {
@ -33,7 +34,7 @@ loader.prototype._load = function (callback) {
loader.prototype._load_sync = function (callback) {
this._loadAnimates_sync();
this._loadMusic_sync();
this._loadSounds_sync();
core.loader._loadMaterials_sync(function () {
core.loader._loadExtraImages_sync(function () {
core.loader._loadAutotiles_sync(function () {
@ -45,18 +46,19 @@ loader.prototype._load_sync = function (callback) {
loader.prototype._load_async = function (callback) {
core.loader._setStartLoadTipText('正在加载资源文件...');
var all = {};
const all = {};
var _makeOnProgress = function (name) {
const _makeOnProgress = function (name) {
if (!all[name]) all[name] = { loaded: 0, total: 0, finished: false };
return function (loaded, total) {
return (loaded, total) => {
all[name].loaded = loaded;
all[name].total = total;
var allLoaded = 0, allTotal = 0;
for (var one in all) {
allLoaded += all[one].loaded;
allTotal += all[one].total;
let allLoaded = 0, allTotal = 0;
for (const one of Object.values(all)) {
allLoaded += one.loaded;
allTotal += one.total;
}
console.log(allLoaded. allTotal);
if (allTotal > 0) {
if (allLoaded == allTotal) {
core.loader._setStartLoadTipText("正在处理资源文件... 请稍候...");
@ -69,9 +71,9 @@ loader.prototype._load_async = function (callback) {
}
};
}
var _makeOnFinished = function (name) {
return function () {
setTimeout(function () {
const _makeOnFinished = function (name) {
return () => {
setTimeout(() => {
all[name].finished = true;
for (var one in all) {
if (!all[one].finished) return;
@ -81,12 +83,21 @@ loader.prototype._load_async = function (callback) {
}
}
this._loadAnimates_async(_makeOnProgress('animates'), _makeOnFinished('animates'));
this._loadMusic_async(_makeOnProgress('sounds'), _makeOnFinished('sounds'));
this._loadMaterials_async(_makeOnProgress('materials'), _makeOnFinished('materials'));
this._loadExtraImages_async(_makeOnProgress('images'), _makeOnFinished('images'));
this._loadAutotiles_async(_makeOnProgress('autotiles'), _makeOnFinished('autotiles'));
this._loadTilesets_async(_makeOnProgress('tilesets'), _makeOnFinished('tilesets'));
if (main.splitChunkMap) {
this._loadAnimates_chunked(main.splitChunkMap.animates, _makeOnProgress, _makeOnFinished);
this._loadSounds_chunked(main.splitChunkMap.sounds, _makeOnProgress, _makeOnFinished);
this._loadMaterials_chunked(main.splitChunkMap.materials, _makeOnProgress, _makeOnFinished);
this._loadExtraImages_chunked(main.splitChunkMap.images, _makeOnProgress, _makeOnFinished);
this._loadAutotiles_chunked(main.splitChunkMap.autotiles, _makeOnProgress, _makeOnFinished);
this._loadTilesets_chunked(main.splitChunkMap.tilesets, _makeOnProgress, _makeOnFinished);
} else {
this._loadAnimates_async(_makeOnProgress('animates'), _makeOnFinished('animates'));
this._loadSounds_async(_makeOnProgress('sounds'), _makeOnFinished('sounds'));
this._loadMaterials_async(_makeOnProgress('materials'), _makeOnFinished('materials'));
this._loadExtraImages_async(_makeOnProgress('images'), _makeOnFinished('images'));
this._loadAutotiles_async(_makeOnProgress('autotiles'), _makeOnFinished('autotiles'));
this._loadTilesets_async(_makeOnProgress('tilesets'), _makeOnFinished('tilesets'));
}
}
// ----- 加载资源文件 ------ //
@ -106,9 +117,14 @@ loader.prototype._loadMaterials_async = function (onprogress, onfinished) {
});
}
loader.prototype._loadMaterials_chunked = async function (chunks, makeOnProgress, makeOnFinished) {
await this._loadImagesFromChunks(chunks, core.materials, core.material.images, makeOnProgress, makeOnFinished);
core.loader._loadMaterials_afterLoad();
}
loader.prototype._loadMaterials_afterLoad = function () {
var images = core.splitImage(core.material.images['icons']);
for (var key in core.statusBar.icons) {
const images = core.splitImage(core.material.images['icons']);
for (let key in core.statusBar.icons) {
if (typeof core.statusBar.icons[key] == 'number') {
core.statusBar.icons[key] = images[core.statusBar.icons[key]];
if (core.statusBar.image[key] != null)
@ -127,20 +143,38 @@ loader.prototype._loadExtraImages_sync = function (callback) {
loader.prototype._loadExtraImages_async = function (onprogress, onfinished) {
core.material.images.images = {};
var images = core.images;
// Check .gif
var gifs = images.filter(function (name) {
const gifs = images.filter(function (name) {
return name.toLowerCase().endsWith('.gif');
});
images = images.filter(function (name) {
return !name.toLowerCase().endsWith('.gif');
});
// gif没有被压缩在zip中延迟加载...
this._loadExtraImages_loadLazy(gifs);
images = images.filter((name) => !name.toLowerCase().endsWith('.gif'));
this.loadImagesFromZip('project/images/images.h5data', images, core.material.images.images, onprogress, onfinished);
}
loader.prototype._loadExtraImages_chunked = function (chunks, makeOnProgress, makeOnFinished) {
core.material.images.images = {};
let images = core.images;
// Check .gif
const gifs = images.filter(function (name) {
return name.toLowerCase().endsWith('.gif');
});
// gif没有被压缩在zip中延迟加载...
gifs.forEach(function (gif) {
this.loadImage("images", gif, function (id, image) {
this._loadExtraImages_loadLazy(gifs);
images = images.filter((name) => !name.toLowerCase().endsWith('.gif'));
this._loadImagesFromChunks(chunks, images, core.material.images.images, makeOnProgress, makeOnFinished);
}
loader.prototype._loadExtraImages_loadLazy = function (list) {
list.forEach(function (gif) {
this.loadImage("images", gif, (id, image) => {
if (image != null) {
core.material.images.images[gif] = image;
}
@ -173,6 +207,15 @@ loader.prototype._loadAutotiles_async = function (onprogress, onfinished) {
});
}
loader.prototype._loadAutotiles_chunked = async function (chunks, makeOnProgress, makeOnFinished) {
core.material.images.autotile = {};
const keys = Object.keys(core.material.icons.autotile);
const autotiles = {};
await this._loadImagesFromChunks(chunks, keys, autotiles, makeOnProgress, makeOnFinished);
core.loader._loadAutotiles_afterLoad(keys, autotiles);
}
loader.prototype._loadAutotiles_afterLoad = function (keys, autotiles) {
// autotile需要保证顺序
keys.forEach(function (v) {
@ -204,6 +247,12 @@ loader.prototype._loadTilesets_async = function (onprogress, onfinished) {
});
}
loader.prototype._loadTilesets_chunked = async function (chunks, makeOnProgress, makeOnFinished) {
core.material.images.tilesets = {};
await this._loadImagesFromChunks(chunks, core.tilesets, core.material.images.tilesets, makeOnProgress, makeOnFinished);
core.loader._loadTilesets_afterLoad();
}
loader.prototype._loadTilesets_afterLoad = function () {
// 检查宽高是32倍数如果出错在控制台报错
for (var imgName in core.material.images.tilesets) {
@ -299,6 +348,19 @@ loader.prototype.loadImagesFromZip = function (url, names, toSave, onprogress, o
}, null, false, onprogress);
}
loader.prototype._loadImagesFromChunks = async function (chunks, names, toSave, makeOnProgress, makeOnFinished) {
await Promise.all(chunks.map((chunk) => {
const onfinished = makeOnFinished(chunk);
const onprogress = makeOnProgress(chunk);
return new Promise((resolve) => {
this.loadImagesFromZip(chunk, names, toSave, onprogress, () => {
onfinished();
resolve();
});
});
}));
}
// ------ 加载动画文件 ------ //
loader.prototype._loadAnimates_sync = function () {
@ -331,16 +393,22 @@ loader.prototype._loadAnimates_sync = function () {
}
loader.prototype._loadAnimates_async = function (onprogress, onfinished) {
core.unzip('project/animates/animates.h5data?v=' + main.version, function (animates) {
for (var name in animates) {
if (name.endsWith(".animate")) {
var t = name.substring(0, name.length - 8);
if (core.animates.indexOf(t) >= 0)
core.material.animates[t] = core.loader._loadAnimate(animates[name]);
}
this._loadFileFromZip('project/animates/animates.h5data', this._saveAnimate, true, onprogress, onfinished);
}
loader.prototype._loadAnimates_chunked = function (chunks, makeOnProgress, makeOnFinished) {
this._loadFileFromChunks(chunks, this._saveAnimate, true, makeOnProgress, makeOnFinished);
}
loader.prototype._saveAnimate = function (animates, onfinished) {
for (var name in animates) {
if (name.endsWith(".animate")) {
var t = name.substring(0, name.length - 8);
if (core.animates.indexOf(t) >= 0)
core.material.animates[t] = core.loader._loadAnimate(animates[name]);
}
onfinished();
}, null, true, onprogress);
}
onfinished();
}
loader.prototype._loadAnimate = function (content) {
@ -393,36 +461,39 @@ loader.prototype._loadAnimate = function (content) {
// ------ 加载音乐和音效 ------ //
loader.prototype._loadMusic_sync = function () {
this._setStartLoadTipText("正在加载音效文件...");
loader.prototype._loadMusics = function () {
core.bgms.forEach(function (t) {
core.loader.loadOneMusic(t);
});
core.sounds.forEach(function (t) {
core.loader.loadOneSound(t);
});
// 直接开始播放
core.playBgm(main.startBgm);
}
loader.prototype._loadMusic_async = function (onprogress, onfinished) {
core.bgms.forEach(function (t) {
core.loader.loadOneMusic(t);
loader.prototype._loadSounds_sync = function () {
this._setStartLoadTipText("正在加载音效文件...");
core.sounds.forEach(function (t) {
core.loader.loadOneSound(t);
});
core.unzip('project/sounds/sounds.h5data?v=' + main.version, function (data) {
// 延迟解析
setTimeout(function () {
for (var name in data) {
if (core.sounds.indexOf(name) >= 0) {
core.loader._loadOneSound_decodeData(name, data[name]);
}
}
});
onfinished();
}, null, false, onprogress);
}
// 直接开始播放
core.playBgm(main.startBgm);
loader.prototype._loadSounds_async = function (onprogress, onfinished) {
this._loadFileFromZip('project/sounds/sounds.h5data', this._saveSounds, false, onprogress, onfinished);
}
loader.prototype._loadSounds_chunked = function (chunks, makeOnProgress, makeOnFinished) {
this._loadFileFromChunks(chunks, this._saveSounds, false, makeOnProgress, makeOnFinished);
}
loader.prototype._saveSounds = function (data, onfinished) {
// 延迟解析
setTimeout(function () {
for (var name in data) {
if (core.sounds.indexOf(name) >= 0) {
core.loader._loadOneSound_decodeData(name, data[name]);
}
}
onfinished();
});
}
loader.prototype.loadOneMusic = function (name) {
@ -513,4 +584,23 @@ loader.prototype.freeBgm = function (name) {
setTimeout(function () {
core.loader.loadOneMusic(name);
}, 3000);
}
}
loader.prototype._loadFileFromZip = function (url, save, convertToText, onprogress, onfinished) {
core.unzip(url + '?v=' + main.version, function (data) {
save(data, onfinished);
}, null, convertToText, onprogress);
}
loader.prototype._loadFileFromChunks = async function (chunks, save, convertToText, makeOnProgress, makeOnFinished) {
await Promise.all(chunks.map((chunk) => {
const onfinished = makeOnFinished(chunk);
const onprogress = makeOnProgress(chunk);
return new Promise((resolve) => {
this._loadFileFromZip(chunk, save, convertToText, onprogress, () => {
onfinished();
resolve();
});
});
}));
}

View File

@ -13,7 +13,7 @@ function main() {
this.bgmRemoteRoot = 'https://h5mota.com/music/'; // 远程BGM的根目录
this.isCompetition = false; // 是否是比赛模式
this.enableSplitChunks = true; // 允许拆分压缩包
this.savePages = 1000; // 存档页数每页可存5个默认为1000页5000个存档
this.criticalUseLoop = 1; // 循环临界的分界