完成加载系统

This commit is contained in:
unanmed 2023-06-29 20:24:21 +08:00
parent 755500f6d5
commit 87166c5ed7
7 changed files with 121 additions and 34 deletions

View File

@ -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) {
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) {
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) {
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) {

View File

@ -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();

View File

@ -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}`),

View File

@ -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<GameLoadEvent> {

View File

@ -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('.'));

View File

@ -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",

View File

@ -66,6 +66,8 @@ interface Loader {
freeBgm(name: BgmIds | NameMapIn<BgmIds>): void;
_loadMaterials_afterLoad(): void;
_loadAnimate(data: string): Animate;
}
declare const loader: new () => Loader;