mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-01-31 23:29:27 +08:00
完成加载系统
This commit is contained in:
parent
755500f6d5
commit
87166c5ed7
@ -32,7 +32,15 @@ loader.prototype._load = function (callback) {
|
|||||||
|
|
||||||
loader.prototype._load_sync = function (callback) {
|
loader.prototype._load_sync = function (callback) {
|
||||||
this._loadAnimates_sync();
|
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) {
|
loader.prototype._load_async = function (callback) {
|
||||||
@ -108,7 +116,17 @@ loader.prototype._load_async = function (callback) {
|
|||||||
// ----- 加载资源文件 ------ //
|
// ----- 加载资源文件 ------ //
|
||||||
|
|
||||||
loader.prototype._loadMaterials_sync = 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) {
|
loader.prototype._loadMaterials_async = function (onprogress, onfinished) {
|
||||||
@ -138,7 +156,15 @@ loader.prototype._loadMaterials_afterLoad = function () {
|
|||||||
// ------ 加载使用的图片 ------ //
|
// ------ 加载使用的图片 ------ //
|
||||||
|
|
||||||
loader.prototype._loadExtraImages_sync = function (callback) {
|
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) {
|
loader.prototype._loadExtraImages_async = function (onprogress, onfinished) {
|
||||||
@ -173,7 +199,16 @@ loader.prototype._loadExtraImages_async = function (onprogress, onfinished) {
|
|||||||
// ------ 加载自动元件 ------ //
|
// ------ 加载自动元件 ------ //
|
||||||
|
|
||||||
loader.prototype._loadAutotiles_sync = function (callback) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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) {
|
loader.prototype.loadImage = function (dir, imgName, callback) {
|
||||||
@ -288,6 +363,7 @@ loader.prototype.loadImagesFromZip = function (
|
|||||||
// ------ 加载动画文件 ------ //
|
// ------ 加载动画文件 ------ //
|
||||||
|
|
||||||
loader.prototype._loadAnimates_sync = function () {
|
loader.prototype._loadAnimates_sync = function () {
|
||||||
|
if (main.mode === 'play') return;
|
||||||
this._setStartLoadTipText('正在加载动画文件...');
|
this._setStartLoadTipText('正在加载动画文件...');
|
||||||
|
|
||||||
if (main.supportBunch) {
|
if (main.supportBunch) {
|
||||||
|
@ -336,12 +336,12 @@ main.prototype.loadAsync = async function (mode, callback) {
|
|||||||
// 加载核心js代码
|
// 加载核心js代码
|
||||||
if (main.useCompress) {
|
if (main.useCompress) {
|
||||||
await main.loadScript(`libs/libs.min.js?v=${main.version}`);
|
await main.loadScript(`libs/libs.min.js?v=${main.version}`);
|
||||||
main.loading.emit('coreLoaded');
|
if (main.mode === 'play') main.loading.emit('coreLoaded');
|
||||||
} else {
|
} else {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
main.loadList.map(v =>
|
main.loadList.map(v =>
|
||||||
main.loadScript(`libs/${v}.js?v=${main.version}`).then(vv => {
|
main.loadScript(`libs/${v}.js?v=${main.version}`).then(() => {
|
||||||
if (v === 'core') {
|
if (v === 'core' && main.mode === 'play') {
|
||||||
main.loading.emit('coreLoaded');
|
main.loading.emit('coreLoaded');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -403,7 +403,7 @@ main.prototype.loadAsync = async function (mode, callback) {
|
|||||||
coreData[t] = main[t];
|
coreData[t] = main[t];
|
||||||
});
|
});
|
||||||
await core.init(coreData, callback);
|
await core.init(coreData, callback);
|
||||||
main.loading.emit('coreInit');
|
if (main.mode === 'play') main.loading.emit('coreInit');
|
||||||
|
|
||||||
core.resize();
|
core.resize();
|
||||||
|
|
||||||
|
@ -324,7 +324,6 @@ async function writeDevResource(data: string) {
|
|||||||
const icons = await fs.readFile('./public/project/icons.js', 'utf-8');
|
const icons = await fs.readFile('./public/project/icons.js', 'utf-8');
|
||||||
const iconData = JSON.parse(icons.split('\n').slice(1).join(''));
|
const iconData = JSON.parse(icons.split('\n').slice(1).join(''));
|
||||||
res.push(
|
res.push(
|
||||||
...info.main.animates.map((v: any) => `animates.${v}.animate`),
|
|
||||||
...info.main.bgms.map((v: any) => `bgms.${v}`),
|
...info.main.bgms.map((v: any) => `bgms.${v}`),
|
||||||
...info.main.fonts.map((v: any) => `fonts.${v}.ttf`),
|
...info.main.fonts.map((v: any) => `fonts.${v}.ttf`),
|
||||||
...info.main.images.map((v: any) => `images.${v}`),
|
...info.main.images.map((v: any) => `images.${v}`),
|
||||||
|
@ -40,6 +40,11 @@ export function readyAllResource() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
ancTe.resource.forEach(v => v.active());
|
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> {
|
class GameLoading extends EventEmitter<GameLoadEvent> {
|
||||||
|
@ -84,6 +84,7 @@ export class Resource<
|
|||||||
},
|
},
|
||||||
{ immediate: true }
|
{ immediate: true }
|
||||||
);
|
);
|
||||||
|
loading.addMaterialLoaded();
|
||||||
} else if (this.type === 'autotiles') {
|
} else if (this.type === 'autotiles') {
|
||||||
const name = this.name as AllIdsOf<'autotile'>;
|
const name = this.name as AllIdsOf<'autotile'>;
|
||||||
autotiles[name] = v;
|
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
|
* @returns 解析出的资源url
|
||||||
*/
|
*/
|
||||||
protected resolveUrl(resource: string) {
|
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 resolve = resource.split('.');
|
||||||
const type = (this.type = resolve[0]);
|
const type = (this.type = resolve[0]);
|
||||||
const name = (this.name = resolve.slice(1, -1).join('.'));
|
const name = (this.name = resolve.slice(1, -1).join('.'));
|
||||||
|
@ -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.beforeBoss.mp3",
|
||||||
"bgms.cave.mp3",
|
"bgms.cave.mp3",
|
||||||
"bgms.escape.mp3",
|
"bgms.escape.mp3",
|
||||||
|
2
src/types/loader.d.ts
vendored
2
src/types/loader.d.ts
vendored
@ -66,6 +66,8 @@ interface Loader {
|
|||||||
freeBgm(name: BgmIds | NameMapIn<BgmIds>): void;
|
freeBgm(name: BgmIds | NameMapIn<BgmIds>): void;
|
||||||
|
|
||||||
_loadMaterials_afterLoad(): void;
|
_loadMaterials_afterLoad(): void;
|
||||||
|
|
||||||
|
_loadAnimate(data: string): Animate;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare const loader: new () => Loader;
|
declare const loader: new () => Loader;
|
||||||
|
Loading…
Reference in New Issue
Block a user