Sun_moves/assets/Script/tool/GameTool.js
2024-05-28 11:38:16 +08:00

588 lines
23 KiB
JavaScript

var StorageMessage = require("Storage").StorageMessage;
window.GameTool = {
_startTime: 0,
_endTime: 0,
_totalTime: 0,
getSeedRandom: function (min, max) {//包含min 不包含max
max = max || 1;
min = min || 0;
GM_INFO.currSeed = (GM_INFO.currSeed * 9301 + 49297) % 233280;
let rnd = GM_INFO.currSeed / 233280.0;
let tmp = min + rnd * (max - min);
return parseInt(tmp);
},
getSetScreenResolutionFlag: function () {
let size = cc.winSize;
let width = size.width;
let height = size.height;
if ((height / width) > (16.2 / 9)) return false;
return true;
},
//判断全面屏适配
setFit: function (canvas) {
let 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(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 () {
let index = Math.floor(Math.random() * 4) + 1;
switch (index) {
case 1: {
return "根据小球的位置,合理晃动手机,确保小球不掉出木板。"
}
case 2: {
return "根据小球位置控制晃动幅度,过大或过小都不是个好选择。"
}
case 3: {
return "不要被天上掉落的障碍吓到,有时候它反而会帮你保持平衡。"
}
case 4: {
return "尝试着顶开掉落的障碍,是个很酷的玩法。"
}
}
},
beginTiming: function () {
this._startTime = new Date().getTime();
},
endTiming: function () {
this._endTime = new Date().getTime();
// this._totalTime = endTime - this._startTime;
},
//如果是判断时常的游戏 调用此方法
updateScoreTime: function () {
let endTime = new Date().getTime();
let 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 () {
this._endTime = new Date().getTime();
this._totalTime = parseInt((this._endTime - this._startTime) / 1000);
GM_INFO.remainingTime = GM_INFO.totalTime - this._totalTime;
},
httpRequest(url, data, success, fail) {
console.log("进入网络请求:");
wx.getNetworkType({
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(res) {
console.log('httpRequest:::::success',res);
success(res);
},
fail(res) {
console.log('httpRequest:::::fail', res,"失败url为",url);
fail(res)
}
})
}
else{
fail(res)
}
},
fail(res){
fail(res)
}
})
// console.log('httpRequest:::::', url, data, success, fail)
},
httpRequest_Notoken(url, data, success, fail) {
// console.log('httpRequest:::::', url, data, success, fail)
wx.getNetworkType({
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(res) {
console.log('httpRequest:::::success',res);
success(res);
},
fail(res) {
console.log('httpRequest:::::fail', res,"失败url为",url);
fail(res)
}
})
}
else{
fail(res)
}
},
fail(res){
fail(res)
}
})
},
getGameInfo: function(node){
var jg = false;
var nodes = node;
let 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(){
// 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(pd){
let 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:(second) => {
let total = second;
let hour = parseInt(total / (60 * 60));//计算整数小时数
let afterHour = total - hour * 60 * 60;//取得算出小时数后剩余的秒数
let min = parseInt(afterHour / 60);//计算整数分
let afterMin = total - hour * 60 * 60 - min * 60;//取得算出分后剩余的秒数
return '剩余:' + hour + ':' + min + ':' + afterMin
},
getTimeMargin2:(second) => {
let total = second;
let hour = parseInt(total / (60 * 60));//计算整数小时数
let afterHour = total - hour * 60 * 60;//取得算出小时数后剩余的秒数
let min = parseInt(afterHour / 60);//计算整数分
if(min < 10) min = "0"+min;
let afterMin = total - hour * 60 * 60 - min * 60;//取得算出分后剩余的秒数
if(afterMin < 10) afterMin = "0" + afterMin;
return '剩余:' + min + ':' + afterMin
},
setFrindBlock: function(userid){
// console.log("添加时候的userid");
let 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("进入分享的是自己");
// }
},
// setFrindBlockMax: function(userid){
// // console.log("添加时候的userid");
// let data = {
// user_id:userid
// }
// // if(userid != window.globalData.gameUserInfo.user_id){
// // console.log("进分享的不是自己");
// // {
// window.GameTool.httpRequest(window.globalData.entryBlockMax, data, function (res) {
// console.log('添加最后的好友信息成功', res)
// }, function (res) {
// console.log('添加最后的好友信息信息失败', res)
// })
// // }
// // }
// // else{
// // console.log("进入分享的是自己");
// // }
// },
getSeedRandom: function (min, max) {//包含min 不包含max
max = max || 1;
min = min || 0;
GM_INFO.currSeed = (GM_INFO.currSeed * 9301 + 49297) % 233280;
let rnd = GM_INFO.currSeed / 233280.0;
let tmp = min + rnd * (max - min);
return parseInt(tmp);
},
//将时间转换为59:23
getTime: function(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;
},
//number 为传进来的数值,type为最多显示几位数
getNumber: function(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;
},
begainTiming: function () {
this._startTime = new Date().getTime();
},
endTiming: function () {
let endTime = new Date().getTime();
this._endTime = endTime;
this._totalTime = endTime - this._startTime;
},
getScoreTime: function () {
let endTime = new Date().getTime();
let tempTime = endTime - this._startTime;
if (tempTime < 0 || tempTime > 500) {
tempTime = Math.round(cc.director.getAnimationInterval() * 1000);
}
GM_INFO.currScore += tempTime;
this._startTime = endTime;
},
getFormatTime: function (time) {
let date = new Date();
date.setTime(time);
let ms = date.getMilliseconds();
// let msString = date.getMilliseconds();
let msString = parseInt(ms / 10) % 100;
let secondString = date.getSeconds();
let 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;
},
pushLister:function () {
},
removeAllLister:function () {
},
};