"use strict"; cc._RF.push(module, 'a1dd7qMiCpOLL3FDXJd9DGy', 'GameTool'); // Script/tool/GameTool.js "use strict"; var _window$GameTool; var StorageMessage = require("Storage").StorageMessage; window.GameTool = (_window$GameTool = { _startTime: 0, _endTime: 0, _totalTime: 0, getSeedRandom: function getSeedRandom(min, max) { //包含min 不包含max max = max || 1; min = min || 0; GM_INFO.currSeed = (GM_INFO.currSeed * 9301 + 49297) % 233280; var rnd = GM_INFO.currSeed / 233280.0; var tmp = min + rnd * (max - min); return parseInt(tmp); }, getSetScreenResolutionFlag: function getSetScreenResolutionFlag() { var size = cc.winSize; var width = size.width; var height = size.height; if (height / width > 16.2 / 9) return false; return true; }, //判断全面屏适配 setFit: function setFit(canvas) { var flag = GameTool.getSetScreenResolutionFlag(); if (flag) {// console.log("不是全面屏"); // canvas.fitWidth = false; // canvas.fitHeight = true; } else {// console.log("是全面屏"); // canvas.fitWidth = true; // canvas.fitHeight = false; } return flag; }, // submitScoreButtonFunc: function(score1,fen1){ // console.log("上传分数:",score1,fen1) // // var score = score1; // // var fen = fen1; // // if (CC_WECHATGAME) { // // window.wx.postMessage({ // // messageType: 3, // // MAIN_MENU_NUM: "guan", // // score: score, // // MAIN_MENU_FEN: "score", // // fen: fen, // // }); // // } else { // // cc.log("提交得分: x1 : " + score) // // } // // return true; // }, submitScoreButtonFunc: function submitScoreButtonFunc(fen2, number) { console.log("number=", number); if (number == 0) { console.log("上传分数:", fen2); var fen = fen2; if (CC_WECHATGAME) { window.wx.postMessage({ messageType: 3, MAIN_MENU_FEN: "score", MAIN_MENU_NUM: "fen", score: fen, fen: 0 }); } else { cc.log("提交得分: x1 : " + fen); } } else { console.log("上传特殊分数:", fen2); var fen = fen2; if (CC_WECHATGAME) { window.wx.postMessage({ messageType: 7, MAIN_MENU_FEN: "score", MAIN_MENU_NUM: "fen", score: 0, fen: fen }); } else { cc.log("提交得分: x1 : " + fen); } } return true; }, getTip: function getTip() { var index = Math.floor(Math.random() * 4) + 1; switch (index) { case 1: { return "根据小球的位置,合理晃动手机,确保小球不掉出木板。"; } case 2: { return "根据小球位置控制晃动幅度,过大或过小都不是个好选择。"; } case 3: { return "不要被天上掉落的障碍吓到,有时候它反而会帮你保持平衡。"; } case 4: { return "尝试着顶开掉落的障碍,是个很酷的玩法。"; } } }, beginTiming: function beginTiming() { this._startTime = new Date().getTime(); }, endTiming: function endTiming() { this._endTime = new Date().getTime(); // this._totalTime = endTime - this._startTime; }, //如果是判断时常的游戏 调用此方法 updateScoreTime: function updateScoreTime() { var endTime = new Date().getTime(); var tempTime = endTime - this._startTime; if (tempTime < 0 || tempTime > 500) { tempTime = Math.round(cc.director.getAnimationInterval() * 1000); } this._endTime += tempTime / 1000; this._totalTime = this._endTime.toFixed(2); GM_INFO.currScore = this._totalTime; this._startTime = endTime; }, //倒计时调用此方法 countDown: function countDown() { this._endTime = new Date().getTime(); this._totalTime = parseInt((this._endTime - this._startTime) / 1000); GM_INFO.remainingTime = GM_INFO.totalTime - this._totalTime; }, httpRequest: function httpRequest(url, data, _success, _fail) { console.log("进入网络请求:"); wx.getNetworkType({ success: function success(res) { var networkType = res.networkType; // console.log(networkType); //有网络状态下 if (networkType == "wifi" || networkType == "4g" || networkType == "3g" || networkType == "unknown") { wx.request({ url: window.globalData.requestAPI + url, data: data, header: { 'content-type': 'application/json', // 默认值 'Authorization': window.globalData.BASE64_JWT_TOKEN ? 'Bearer ' + window.globalData.BASE64_JWT_TOKEN : '' }, method: 'POST', success: function success(res) { console.log('httpRequest:::::success', res); _success(res); }, fail: function fail(res) { console.log('httpRequest:::::fail', res, "失败url为", url); _fail(res); } }); } else { _fail(res); } }, fail: function fail(res) { _fail(res); } }); // console.log('httpRequest:::::', url, data, success, fail) }, httpRequest_Notoken: function httpRequest_Notoken(url, data, _success2, _fail2) { // console.log('httpRequest:::::', url, data, success, fail) wx.getNetworkType({ success: function success(res) { var networkType = res.networkType; // console.log(networkType); //有网络状态下 if (networkType == "wifi" || networkType == "4g" || networkType == "3g" || networkType == "unknown") { wx.request({ url: window.globalData.requestAPI + url, data: data, header: { 'content-type': 'application/json' // 默认值 }, method: 'POST', success: function success(res) { console.log('httpRequest:::::success', res); _success2(res); }, fail: function fail(res) { console.log('httpRequest:::::fail', res, "失败url为", url); _fail2(res); } }); } else { _fail2(res); } }, fail: function fail(res) { _fail2(res); } }); }, getGameInfo: function getGameInfo(node) { var jg = false; var nodes = node; var data = { 'appid': window.globalData.appid }; window.GameTool.httpRequest(window.globalData.getUserInfoUrl, data, function (res) { window.globalData.gameUserInfo = res.data.king_user; //user_id在这里 console.log("拿到服务器的信息", res.data); if (window.globalData.gameUserInfo) { console.log("获得服务器游戏信息成功:", window.globalData.gameUserInfo); if (window.globalData.gameUserInfo.unlock_1) window.GM_INFO.unlock_1 = window.globalData.gameUserInfo.unlock_1; if (window.globalData.gameUserInfo.unlock_2) window.GM_INFO.unlock_2 = window.globalData.gameUserInfo.unlock_2; //如果服务器有金币信息 if (window.globalData.gameUserInfo.coin) { //本地也有金币信息, 以本地为主,把本地金币信息同步到服务器 if (StorageMessage.getStorage("Coin")) { if (window.globalData.gameUserInfo.coin != StorageMessage.getStorage("Coin")) { window.GM_INFO.coin = StorageMessage.getStorage("Coin"); StorageMessage.setStorage("Coin", window.GM_INFO.coin); } } //如果本地没有金币信息 , 以服务器金币信息为主 else { window.GM_INFO.coin = window.globalData.gameUserInfo.coin; StorageMessage.setStorage("Coin", window.GM_INFO.coin); } } else { //本地也有金币信息, 以本地为主,把本地金币信息同步到服务器 if (StorageMessage.getStorage("Coin")) { window.GM_INFO.coin = StorageMessage.getStorage("Coin"); StorageMessage.setStorage("Coin", window.GM_INFO.coin); } //如果本地没有金币信息 , 以服务器金币信息为主 else { window.GM_INFO.coin = 200; StorageMessage.setStorage("Coin", window.GM_INFO.coin); } } cc.director.loadScene('GameBegin'); // console.log("即将进入这里"); // if(StorageMessage.getStorage("level")){ // // console.log("本地有等级信息;"); // // console.log("服务器信息等级为:",window.globalData.gameUserInfo.level); // window.GM_INFO.level = StorageMessage.getStorage("level"); // var temp = window.GameTool.submitScoreButtonFunc(window.GM_INFO.level,window.GM_INFO.coin); // // console.log("本地等级信息为:",window.GM_INFO.level); // //本地存储等级 小于服务器等级, 等于本地数据丢失,这时以服务器数据为主重新构建游戏 // if(window.GM_INFO.level < window.globalData.gameUserInfo.level){ // // nodes.active = true; // window.GM_INFO.level = window.globalData.gameUserInfo.level; // var temp = window.GameTool.submitScoreButtonFunc(window.GM_INFO.level,window.GM_INFO.coin); // window.GameTool.reset(); // } // //正常进入游戏,以本地数据为主,服务器数据为辅 // else{ // console.log("本地数据大以本地主数据为主"); // // window.GameTool.setGameInfo(true); // } // } // else{ // console.log("笨地没等级数据"); // // nodes.active = true; // window.GM_INFO.level = window.globalData.gameUserInfo.level; // var temp = window.GameTool.submitScoreButtonFunc(window.GM_INFO.level,window.GM_INFO.coin); // window.GameTool.reset(); // } if (CC_WECHATGAME) { wx.onShow(function (res) { if (res) { if (res.query) { console.log("分享中带的信息", res.query); if (res.query.key1) { window.GameTool.setFrindHelp(res.query.key1); } if (res.query.key2) { window.GameTool.setFrindBlock(res.query.key2); } if (res.query.key3) { window.GameTool.setFrindBlockMax(res.query.key3); } } } }); wx.onHide(function (res) { console.log("检测退出游戏"); // window.arm = []; // for(var i=0; i<12; i++){ // window.arm[i] = null; // window.arm[i] = {error:"error"}; // } // window.arm = StorageMessage.setStorage("arm",window.arm); var temp = window.GameTool.submitScoreButtonFunc(window.GM_INFO.level, window.GM_INFO.coin); window.GameTool.setGameInfo(); }); } jg = true; } else { console.log("拿到服务器信息失败"); cc.director.loadScene("LoadScene"); } }, function () { jg = false; console.log("get用户信息失败", res); }); return jg; }, //根据服务器数据重置所需要所有数据 reset: function reset() { // if(window.globalData.gameUserInfo.arm) // StorageMessage.setStorage("arm",window.globalData.gameUserInfo.arm); // if(window.globalData.gameUserInfo.richest_coin) // StorageMessage.setStorage("coin",parseInt(window.globalData.gameUserInfo.richest_coin)); // if(window.globalData.gameUserInfo.dimond) // StorageMessage.setStorage("dimond",window.globalData.gameUserInfo.dimond); // if(window.globalData.gameUserInfo.buy_time) // StorageMessage.setStorage("buy_Time",window.globalData.gameUserInfo.buy_time); // if(window.globalData.gameUserInfo.dimond_time) // StorageMessage.setStorage("dimond_Time",window.globalData.gameUserInfo.dimond_time); // if(window.globalData.gameUserInfo.level) // StorageMessage.setStorage("level",window.globalData.gameUserInfo.level); // if(window.globalData.gameUserInfo.total_Speed) // StorageMessage.setStorage("total_Speed",parseInt(window.globalData.gameUserInfo.total_Speed)); // if(window.globalData.gameUserInfo.leave_time) // StorageMessage.setStorage("leave_time",window.globalData.gameUserInfo.leave_time); // if(window.globalData.gameUserInfo.reward1) // StorageMessage.setStorage("share_Array",window.globalData.gameUserInfo.reward1); // if(window.globalData.gameUserInfo.reward2) // StorageMessage.setStorage("share_100",window.globalData.gameUserInfo.reward2); // StorageMessage.setStorage("GM_INFO",window.GM_INFO); console.log("本地数据丢失,重新启动游戏"); cc.director.loadScene('LoadScene'); }, setGameInfo: function setGameInfo(pd) { var data = {// "richest_coin":window.coin+"", // "arm":window.arm, // "buy_time":window.buy_Time, // "dimond":parseInt(window.dimond), // "dimond_time":window.dimond_Time, // "level":window.GM_INFO.level, // "total_speed":window.total_Speed+"", // "reward_1":window.GM_INFO.share_Array, //用于存储6个红包的领取信息 // "reward_2":window.share_100 //用于存储100个红包的领取信息 }; // console.log("存储之前检测:",window.arm,arms); window.GameTool.httpRequest(window.globalData.richest, data, function (res) { console.log('存储userInfo成功', res); if (!pd) { window.GM_INFO.leave_time = res.data.leave_time; StorageMessage.setStorage("leave_time", window.GM_INFO.leave_time); } }, function (res) { console.log('存储userInfo失败', res); if (!pd) { window.GM_INFO.leave_time = parseInt(new Date().valueOf() / 1000); StorageMessage.setStorage("leave_time", data); } }); }, //输入秒,返回需要展示时间格式 getTimeMargin: function getTimeMargin(second) { var total = second; var hour = parseInt(total / (60 * 60)); //计算整数小时数 var afterHour = total - hour * 60 * 60; //取得算出小时数后剩余的秒数 var min = parseInt(afterHour / 60); //计算整数分 var afterMin = total - hour * 60 * 60 - min * 60; //取得算出分后剩余的秒数 return '剩余:' + hour + ':' + min + ':' + afterMin; }, getTimeMargin2: function getTimeMargin2(second) { var total = second; var hour = parseInt(total / (60 * 60)); //计算整数小时数 var afterHour = total - hour * 60 * 60; //取得算出小时数后剩余的秒数 var min = parseInt(afterHour / 60); //计算整数分 if (min < 10) min = "0" + min; var afterMin = total - hour * 60 * 60 - min * 60; //取得算出分后剩余的秒数 if (afterMin < 10) afterMin = "0" + afterMin; return '剩余:' + min + ':' + afterMin; }, setFrindBlock: function setFrindBlock(userid) { // console.log("添加时候的userid"); var data = { user_id: userid }; // if(userid != window.globalData.gameUserInfo.user_id){ // console.log("进分享的不是自己"); // { window.GameTool.httpRequest(window.globalData.entryBlock, data, function (res) { console.log('添加红包助力信息成功', res); }, function (res) { console.log('添加红包助力信息失败', res); }); // } // } // else{ // console.log("进入分享的是自己"); // } } }, _window$GameTool["getSeedRandom"] = function getSeedRandom(min, max) { //包含min 不包含max max = max || 1; min = min || 0; GM_INFO.currSeed = (GM_INFO.currSeed * 9301 + 49297) % 233280; var rnd = GM_INFO.currSeed / 233280.0; var tmp = min + rnd * (max - min); return parseInt(tmp); }, _window$GameTool.getTime = function getTime(time) { var label = ""; var number_1 = parseInt(time / 60); var number_2 = time - number_1 * 60; if (number_1 < 10) { number_1 = "0" + number_1; } if (number_2 < 10) { number_2 = "0" + number_2; } label = number_1 + ":" + number_2; return label; }, _window$GameTool.getNumber = function getNumber(number, type) { // console.log(type,"需要转换的数字为:",number); var count = 1000; var place = type - 3; if (number > Math.pow(count, 8) * Math.pow(10, place)) { if (number / Math.pow(count, 8) < 100) number = parseInt(number / Math.pow(count, 8) * 10) / 10 + "S";else number = parseInt(number / Math.pow(count, 8)) + "S"; } else if (number > Math.pow(count, 7) * Math.pow(10, place)) { if (number / Math.pow(count, 7) < 100) number = parseInt(number / Math.pow(count, 7) * 10) / 10 + "s";else number = parseInt(number / Math.pow(count, 7)) + "s"; } else if (number > Math.pow(count, 6) * Math.pow(10, place)) { if (number / Math.pow(count, 6) < 100) number = parseInt(number / Math.pow(count, 6) * 10) / 10 + "Q";else number = parseInt(number / Math.pow(count, 6)) + "Q"; } else if (number > Math.pow(count, 5) * Math.pow(10, place)) { if (number / Math.pow(count, 5) < 100) number = parseInt(number / Math.pow(count, 5) * 10) / 10 + "q";else number = parseInt(number / Math.pow(count, 5)) + "q"; } else if (number > Math.pow(count, 4) * Math.pow(10, place)) { if (number / Math.pow(count, 4) < 100) number = parseInt(number / Math.pow(count, 4) * 10) / 10 + "t";else number = parseInt(number / Math.pow(count, 4)) + "t"; } else if (number > Math.pow(count, 3) * Math.pow(10, place)) { if (number / Math.pow(count, 3) < 100) number = parseInt(number / Math.pow(count, 3) * 10) / 10 + "b";else number = parseInt(number / Math.pow(count, 3)) + "b"; } else if (number > Math.pow(count, 2) * Math.pow(10, place)) { if (number / Math.pow(count, 2) < 100) number = parseInt(number / Math.pow(count, 2) * 10) / 10 + "m";else number = parseInt(number / Math.pow(count, 2)) + "m"; } else if (number > Math.pow(count, 1) * Math.pow(10, place)) { if (number / Math.pow(count, 1) < 100) { number = parseInt(number / Math.pow(count, 1) * 10) / 10 + "k"; } else number = parseInt(number / Math.pow(count, 1)) + "k"; } else { number = parseInt(number / Math.pow(count, 0)) + ""; } // console.log("结果为"+type+"位数",number); return number; }, _window$GameTool.begainTiming = function begainTiming() { this._startTime = new Date().getTime(); }, _window$GameTool["endTiming"] = function endTiming() { var endTime = new Date().getTime(); this._endTime = endTime; this._totalTime = endTime - this._startTime; }, _window$GameTool.getScoreTime = function getScoreTime() { var endTime = new Date().getTime(); var tempTime = endTime - this._startTime; if (tempTime < 0 || tempTime > 500) { tempTime = Math.round(cc.director.getAnimationInterval() * 1000); } GM_INFO.currScore += tempTime; this._startTime = endTime; }, _window$GameTool.getFormatTime = function getFormatTime(time) { var date = new Date(); date.setTime(time); var ms = date.getMilliseconds(); // let msString = date.getMilliseconds(); var msString = parseInt(ms / 10) % 100; var secondString = date.getSeconds(); var minString = date.getMinutes(); if (msString < 10) { msString = "0" + msString; } if (secondString < 10) { secondString = "0" + secondString; } secondString = secondString + ":"; if (minString < 10) { minString = "0" + minString; } minString = minString + ":"; return minString + secondString + msString; }, _window$GameTool.pushLister = function pushLister() {}, _window$GameTool.removeAllLister = function removeAllLister() {}, _window$GameTool); cc._RF.pop();