loader v2.6
This commit is contained in:
parent
6b058c7be0
commit
e499082407
@ -10,7 +10,7 @@ HTML5 canvas制作的魔塔样板,支持全平台游戏!
|
||||
* [Docs / 使用文档说明](https://ckcz123.github.io/mota-js/)
|
||||
* [Video / 视频教程](https://www.bilibili.com/video/av32781473/)
|
||||
|
||||

|
||||

|
||||
|
||||
## 目录结构
|
||||
|
||||
|
||||
@ -195,7 +195,7 @@ editor.prototype.mapInit = function () {
|
||||
}
|
||||
|
||||
editor.prototype.fetchMapFromCore = function(){
|
||||
var mapArray = core.maps.save(core.status.maps, core.status.floorId);
|
||||
var mapArray = core.maps.saveMap(core.status.maps, core.status.floorId);
|
||||
editor.map = mapArray.map(function (v) {
|
||||
return v.map(function (v) {
|
||||
var x = parseInt(v), y = editor.indexs[x];
|
||||
|
||||
207
libs/loader.js
207
libs/loader.js
@ -23,62 +23,20 @@ loader.prototype.setStartLoadTipText = function (text) {
|
||||
}
|
||||
|
||||
loader.prototype._load = function (callback) {
|
||||
this._loadIcons();
|
||||
this._loadAnimates();
|
||||
this._loadMusic();
|
||||
|
||||
// 加载icons
|
||||
core.loader.loadIcons();
|
||||
|
||||
// 加载图片
|
||||
core.loader.loadImages(core.materials, core.material.images, function () {
|
||||
// 加载png图片
|
||||
core.material.images.images = {};
|
||||
|
||||
var images = core.clone(core.images);
|
||||
if (images.indexOf("hero.png")<0)
|
||||
images.push("hero.png");
|
||||
|
||||
core.loader.loadImages(images, core.material.images.images, function () {
|
||||
// 加载autotile
|
||||
core.material.images.autotile = {};
|
||||
var keys = Object.keys(core.material.icons.autotile);
|
||||
var autotiles = {};
|
||||
core.loader.loadImages(keys, autotiles, function () {
|
||||
|
||||
keys.forEach(function (v) {
|
||||
core.material.images.autotile[v] = autotiles[v];
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
core.maps.makeAutotileEdges();
|
||||
});
|
||||
|
||||
// 加载tilesets
|
||||
core.material.images.tilesets = {};
|
||||
if (!core.isset(core.tilesets)) core.tilesets = [];
|
||||
core.loader.loadImages(core.clone(core.tilesets), core.material.images.tilesets, function () {
|
||||
|
||||
// 检查宽高是32倍数,如果出错在控制台报错
|
||||
for (var imgName in core.material.images.tilesets) {
|
||||
var img = core.material.images.tilesets[imgName];
|
||||
if (img.width%32!=0 || img.height%32!=0) {
|
||||
console.warn("警告!"+imgName+"的宽或高不是32的倍数!");
|
||||
}
|
||||
if (img.width * img.height > 32*32*3000) {
|
||||
console.warn("警告!"+imgName+"上的图块素材个数大于3000!");
|
||||
}
|
||||
}
|
||||
|
||||
core.loader.loadAnimates();
|
||||
core.loader.loadMusic();
|
||||
if (core.isset(callback))
|
||||
callback();
|
||||
})
|
||||
core.loader._loadMaterialImages(function () {
|
||||
core.loader._loadExtraImages(function () {
|
||||
core.loader._loadAutotiles(function () {
|
||||
core.loader._loadTilesets(callback);
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
loader.prototype.loadIcons = function () {
|
||||
|
||||
loader.prototype._loadIcons = function () {
|
||||
this.loadImage("icons.png", function (id, image) {
|
||||
var images = core.cropImage(image);
|
||||
for (var key in core.statusBar.icons) {
|
||||
@ -91,6 +49,55 @@ loader.prototype.loadIcons = function () {
|
||||
});
|
||||
}
|
||||
|
||||
loader.prototype._loadMaterialImages = function (callback) {
|
||||
this.loadImages(core.materials, core.material.images, callback);
|
||||
}
|
||||
|
||||
loader.prototype._loadExtraImages = function (callback) {
|
||||
core.material.images.images = {};
|
||||
|
||||
var images = core.clone(core.images);
|
||||
if (images.indexOf("hero.png")<0)
|
||||
images.push("hero.png");
|
||||
|
||||
this.loadImages(images, core.material.images.images, callback);
|
||||
}
|
||||
|
||||
loader.prototype._loadAutotiles = function (callback) {
|
||||
core.material.images.autotile = {};
|
||||
var keys = Object.keys(core.material.icons.autotile);
|
||||
var autotiles = {};
|
||||
this.loadImages(keys, autotiles, function () {
|
||||
keys.forEach(function (v) {
|
||||
core.material.images.autotile[v] = autotiles[v];
|
||||
});
|
||||
|
||||
setTimeout(function () {
|
||||
core.maps.makeAutotileEdges();
|
||||
});
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
loader.prototype._loadTilesets = function (callback) {
|
||||
core.material.images.tilesets = {};
|
||||
if (!core.isset(core.tilesets)) core.tilesets = [];
|
||||
core.loader.loadImages(core.clone(core.tilesets), core.material.images.tilesets, function () {
|
||||
// 检查宽高是32倍数,如果出错在控制台报错
|
||||
for (var imgName in core.material.images.tilesets) {
|
||||
var img = core.material.images.tilesets[imgName];
|
||||
if (img.width%32!=0 || img.height%32!=0) {
|
||||
console.warn("警告!"+imgName+"的宽或高不是32的倍数!");
|
||||
}
|
||||
if (img.width * img.height > 32*32*3000) {
|
||||
console.warn("警告!"+imgName+"上的图块素材个数大于3000!");
|
||||
}
|
||||
}
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
loader.prototype.loadImages = function (names, toSave, callback) {
|
||||
if (!core.isset(names) || names.length==0) {
|
||||
if (core.isset(callback)) callback();
|
||||
@ -126,7 +133,7 @@ loader.prototype.loadImage = function (imgName, callback) {
|
||||
}
|
||||
}
|
||||
|
||||
loader.prototype.loadAnimates = function () {
|
||||
loader.prototype._loadAnimates = function () {
|
||||
core.animates.forEach(function (t) {
|
||||
core.http('GET', 'project/animates/' + t + ".animate", null, function (content) {
|
||||
try {
|
||||
@ -182,79 +189,14 @@ loader.prototype.loadAnimates = function () {
|
||||
}
|
||||
|
||||
////// 加载音频 //////
|
||||
loader.prototype.loadMusic = function () {
|
||||
|
||||
loader.prototype._loadMusic = function () {
|
||||
core.bgms.forEach(function (t) {
|
||||
core.loader.loadOneMusic(t);
|
||||
/*
|
||||
// 判断是不是mid
|
||||
if (/^.*\.mid$/i.test(t)) {
|
||||
|
||||
if (core.musicStatus.audioContext!=null) {
|
||||
core.material.bgms[t] = 'loading';
|
||||
|
||||
core.http('GET', 'project/sounds/'+t, null, function (data) {
|
||||
try {
|
||||
var ff = [];
|
||||
var mx = data.length;
|
||||
for (var z = 0; z < mx; z++)
|
||||
ff[z] = String.fromCharCode(data.charCodeAt(z) & 255);
|
||||
var shouldStart = core.material.bgms[t] == 'starting';
|
||||
core.material.bgms[t] = AudioPlayer(core.musicStatus.audioContext, Replayer(MidiFile(ff.join("")), Synth(44100)), true);
|
||||
|
||||
if (shouldStart)
|
||||
core.playBgm(t);
|
||||
}
|
||||
catch (e) {
|
||||
main.log(e);
|
||||
core.material.bgms[t] = null;
|
||||
}
|
||||
}, function (e) {
|
||||
main.log(e);
|
||||
core.material.bgms[t] = null;
|
||||
}, "text/plain; charset=x-user-defined")
|
||||
|
||||
}
|
||||
else {
|
||||
core.material.bgms[t] = null;
|
||||
}
|
||||
}
|
||||
else {
|
||||
core.loader.loadOneMusic(t);
|
||||
}
|
||||
*/
|
||||
});
|
||||
|
||||
core.sounds.forEach(function (t) {
|
||||
|
||||
if (core.musicStatus.audioContext != null) {
|
||||
|
||||
core.http('GET', 'project/sounds/'+t, null, function (data) {
|
||||
try {
|
||||
core.musicStatus.audioContext.decodeAudioData(data, function (buffer) {
|
||||
core.material.sounds[t] = buffer;
|
||||
}, function (e) {
|
||||
main.log(e);
|
||||
core.material.sounds[t] = null;
|
||||
})
|
||||
}
|
||||
catch (ee) {
|
||||
main.log(ee);
|
||||
core.material.sounds[t] = null;
|
||||
}
|
||||
}, function (e) {
|
||||
main.log(e);
|
||||
core.material.sounds[t] = null;
|
||||
}, null, 'arraybuffer');
|
||||
}
|
||||
else {
|
||||
var music = new Audio();
|
||||
music.src = 'project/sounds/'+t;
|
||||
core.material.sounds[t] = music;
|
||||
}
|
||||
|
||||
core.loader.loadOneSound(t);
|
||||
});
|
||||
|
||||
// 直接开始播放
|
||||
core.playBgm(main.startBgm);
|
||||
}
|
||||
@ -268,6 +210,33 @@ loader.prototype.loadOneMusic = function (name) {
|
||||
core.material.bgms[name] = music;
|
||||
}
|
||||
|
||||
loader.prototype.loadOneSound = function (name) {
|
||||
if (core.musicStatus.audioContext != null) {
|
||||
core.http('GET', 'project/sounds/'+name, null, function (data) {
|
||||
try {
|
||||
core.musicStatus.audioContext.decodeAudioData(data, function (buffer) {
|
||||
core.material.sounds[name] = buffer;
|
||||
}, function (e) {
|
||||
main.log(e);
|
||||
core.material.sounds[name] = null;
|
||||
})
|
||||
}
|
||||
catch (e) {
|
||||
main.log(e);
|
||||
core.material.sounds[name] = null;
|
||||
}
|
||||
}, function (e) {
|
||||
main.log(e);
|
||||
core.material.sounds[name] = null;
|
||||
}, null, 'arraybuffer');
|
||||
}
|
||||
else {
|
||||
var music = new Audio();
|
||||
music.src = 'project/sounds/'+name;
|
||||
core.material.sounds[name] = music;
|
||||
}
|
||||
}
|
||||
|
||||
loader.prototype.freeBgm = function (name) {
|
||||
if (!core.isset(core.material.bgms[name])) return;
|
||||
// 从cachedBgms中删除
|
||||
|
||||
10
main.js
10
main.js
@ -195,7 +195,7 @@ main.prototype.init = function (mode, callback) {
|
||||
if (mode === 'editor')main.editor = {'disableGlobalAnimate':true};
|
||||
}
|
||||
|
||||
main.loaderJs('project', main.pureData, function(){
|
||||
main.loadJs('project', main.pureData, function(){
|
||||
var mainData = data_a1e2fb4a_e986_4524_b0da_9b7ba7c0874d.main;
|
||||
for(var ii in mainData)main[ii]=mainData[ii];
|
||||
|
||||
@ -213,7 +213,7 @@ main.prototype.init = function (mode, callback) {
|
||||
main.dom.levelChooseButtons.appendChild(span);
|
||||
});
|
||||
|
||||
main.loaderJs('libs', main.loadList, function () {
|
||||
main.loadJs('libs', main.loadList, function () {
|
||||
main.core = core;
|
||||
|
||||
for (i = 0; i < main.loadList.length; i++) {
|
||||
@ -222,7 +222,7 @@ main.prototype.init = function (mode, callback) {
|
||||
main.core[name] = new (eval(name))();
|
||||
}
|
||||
|
||||
main.loaderFloors(function() {
|
||||
main.loadFloors(function() {
|
||||
var coreData = {};
|
||||
["dom", "statusBar", "canvas", "images", "tilesets", "materials",
|
||||
"animates", "bgms", "sounds", "floorIds", "floors"].forEach(function (t) {
|
||||
@ -236,7 +236,7 @@ main.prototype.init = function (mode, callback) {
|
||||
}
|
||||
|
||||
////// 动态加载所有核心JS文件 //////
|
||||
main.prototype.loaderJs = function (dir, loadList, callback) {
|
||||
main.prototype.loadJs = function (dir, loadList, callback) {
|
||||
|
||||
// 加载js
|
||||
main.setMainTipsText('正在加载核心js文件...')
|
||||
@ -272,7 +272,7 @@ main.prototype.loadMod = function (dir, modName, callback) {
|
||||
}
|
||||
|
||||
////// 动态加载所有楼层(剧本) //////
|
||||
main.prototype.loaderFloors = function (callback) {
|
||||
main.prototype.loadFloors = function (callback) {
|
||||
|
||||
// 加载js
|
||||
main.setMainTipsText('正在加载楼层文件...')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user