soyoo-cocos/single-html/loader-and-starter.js

127 lines
3.9 KiB
JavaScript

window.startGame = function() {
window.boot()
}
window.pauseGame = function() {
cc.game.pause()
}
window.resumeGame = function() {
cc.game.resume()
}
window.muteAudio = function() {
cc.audioEngine.pauseAll()
}
window.unmuteAudio = function() {
cc.audioEngine.resumeAll()
}
console.log("entry file loaded")
function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91
? nChr - 65 : nChr > 96 && nChr < 123
? nChr - 71 : nChr > 47 && nChr < 58
? nChr + 4 : nChr === 43
? 62 : nChr === 47
? 63 : 0
}
/** 官网范例+1,看不懂+1,作用是将base64编码的字符串转为ArrayBuffer */
function base64DecToArr(sBase64, nBlockSize) {
var sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length
var nOutLen = nBlockSize ? Math.ceil((nInLen * 3 + 1 >>> 2) / nBlockSize) * nBlockSize : nInLen * 3 + 1 >>> 2
var aBytes = new Uint8Array(nOutLen)
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++ , nOutIdx++) {
aBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0
}
}
return aBytes
}
/**
* 修改部分资源的载入方式,可以根据项目中实际用到的资源进行修改
* - [注意] assetMap 是自己定义的,名称可以修改
*/
function loadJs(item, _, callback) {
if (!item.startsWith("assets")) {
item = "assets/" + item
}
eval(assetMap[item])
callback(null, null)
}
function loadJson(item, _, callback) {
if (!item.startsWith("assets")) {
item = "assets/" + item
}
callback(null, JSON.parse(assetMap[item]))
}
var assetMap = window.assetMap;
cc.assetManager.downloader.register({
"bundle": function (item, _, callback) {
var bundleJsKey = Object.keys(assetMap).find(key => key.includes(item + "/index") && key.endsWith(".js"))
if (!bundleJsKey) {
console.error(`bundle js ${item} not found`)
}
loadJs(bundleJsKey, _, (a1, data) => {
console.log("bundle js loaded:", bundleJsKey)
})
var bundleJsonKey = Object.keys(assetMap).find(key => key.includes(item + "/config") && key.endsWith(".json"))
if (!bundleJsonKey) {
console.error(`bundle ${item} not found`)
}
loadJson(bundleJsonKey, _, (a1, data) =>
{
var completeData = data;
completeData.base = "assets/" + item + "/"
callback(null, completeData)
})
},
".json": loadJson,
".plist": function (item, _, callback) {
callback(null, assetMap[item])
},
".png": function (item, _, callback) {
var img = new Image()
img.src = "data:image/png;base64," + assetMap[item] // 注意需要给base64编码添加前缀
callback(null, img)
},
".jpg": function (item, _, callback) {
var img = new Image()
img.src = "data:image/jpeg;base64," + assetMap[item]
callback(null, img)
},
".webp": function (item, _, callback) {
var img = new Image()
img.src = "data:image/webp;base64," + assetMap[item]
callback(null, img)
},
".mp3": function (item, _, callback) {
// 只支持以webAudio形式播放的声音
// 将base64编码的声音文件转化为ArrayBuffer
cc.sys.__audioSupport.context.decodeAudioData(
base64DecToArr(assetMap[item]).buffer,
// success
function (buffer) {
callback(null, buffer)
},
// fail
function (buffer) {
callback(new Error("mp3-res-fail"), null)
}
)
},
})