2020-08-07 09:55:56 +08:00

210 lines
5.9 KiB
JavaScript

let Promise = require('ZPromise');
var util = {
timeSeed:0,
netTimeSeed:[],
setTimeSeed:function(time)
{
this.timeSeed = time;
},
rnd:function( seed ){
seed = ( seed * 9301 + 49297 ) % 233280; //神奇数字
this.timeSeed = seed;
return seed / ( 233280.0 );
},
rand01(){
return this.getRandom(10000)/10000
},
/**
* 获取_min至_max之间的随机整数, 不包括_max
* */
getRandom:function(_max,_min)
{
_min = _min || 0;
return Math.floor(this.rnd( this.timeSeed ) * (_max-_min))+_min;
},
resetNetTimeSeed:function()
{
this.netTimeSeed = [];
},
setNetTimeSeed:function(time)
{
for(let i = 0; i < 4; i++)
{
this.netTimeSeed.push(time+i);
}
},
netRnd:function(seed,netIndex)
{
seed = ( seed * 9301 + 49297 ) % 233280; //神奇数字
if(this.netTimeSeed.length!=4){
if(CC_DEBUG)
console.log(`setNetTimeSeed${this.netTimeSeed.length} netRand netIndex=${netIndex}`);
return 0;
}
this.netTimeSeed[netIndex] = seed;
return seed / ( 233280.0 );
},
fixNumber(_v)
{
let str = _v.toFixed(3);
return parseFloat(str);
},
netRand01(netIndex)
{
return this.fixNumber(this.getNetRandom(netIndex,10000)/10000);
},
getNetRandom(netIndex,_max,_min)
{
_min = _min || 0;
return Math.floor(this.netRnd( this.netTimeSeed[netIndex],netIndex ) * (_max-_min))+_min;
},
getCurTime()
{
return (new Date).getTime();
},
showTips(parent,str,delayTime)
{
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
wx.showToast({
title: str,
icon: 'none',
});
} else {
if(!parent)
{
return;
}
if(!str || str == "")
{
return;
}
delayTime = 0 || delayTime;
let node = new cc.Node();
parent.addChild(node);
node.zIndex = 150;
node.color = new cc.Color(0,0,0);
let lbl = node.addComponent(cc.Label);
lbl.isSystemFontUsed = true;
lbl.fontFamily = "Arial";
lbl.fontSize = 40;
lbl.lineHeight = 50;
lbl.string = str;
node.x = 0;
node.y = 0;
let act = cc.spawn(cc.moveBy(3 ,cc.v2(0,200)),cc.fadeOut(3));
if(delayTime > 0)
{
node.opacity = 1;
node.runAction(cc.sequence(cc.delayTime(delayTime),cc.fadeIn(0.1),act));
}
else
{
node.runAction(act);
}
}
},
playSwapAnim(nd, angle, persec){
if(nd){
if(!nd.orginrotation){
nd.orginrotation = nd.rotation;
}
var rt = nd.orginrotation;
nd.rotation = rt;
nd.active = true;
var seq = cc.repeatForever(cc.sequence(cc.rotateTo(persec, angle),
cc.rotateTo(persec, 0), cc.rotateTo(persec, -angle), cc.rotateTo(persec, 0)));
nd.runAction(seq);
}
},
stopSwapAnim(nd){
if(nd){
nd.stopAllActions();
if(typeof(nd.orginrotation) == 'number'){
nd.rotation = nd.orginrotation;
}else{
nd.rotation = 0;
}
}
},
isIphoneX: function () {
var size = cc.view.getFrameSize();
var isIphoneX = (size.width / size.height == 812 / 375) ||
(size.height / size.width == 812 / 375) ||
(size.height / size.width == 2688 / 1242) ||
(size.width / size.height == 2688 / 1242);
// var isIphoneX = (size.width == 2436 && size.height == 1125) ||
// (size.width == 1125 && size.height == 2436) ||
// (size.width == 1624 && size.height == 750) ||
// (size.width == 750 && size.height == 1624) ||
// (size.width == 812 && size.height == 375) ||
// (size.width == 375 && size.height == 812);
return isIphoneX;
},
// 获取当前时间到0点的秒数
getSeconds: function () {
return Math.floor((new Date().getTime()/1000 + 8*60*60) % (24*60*60));
},
getRandomArrayElements: function (arr, count) {
let shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(min);
},
/**
* 显示插屏广告
* @param {object} target
* */
checkInsertAd(target) {
console.log(`check insert ad with mode: ${target.mode}`);
let adIds = cc.gg.basecfg.wxInsertAdId ? cc.gg.basecfg.wxInsertAdId.split('|') : [];
let insertCfg;
if (cc.gg.basecfg.insert_ad_cfg) {
try {
let allCfg = JSON.parse(cc.gg.basecfg.insert_ad_cfg)
insertCfg = allCfg.find(element => {
return element.id === parseInt(target.mode);
});
} catch (err) {
}
}
if (!insertCfg) {
return false;
}
if (insertCfg.enabled && adIds.length > 0 && !cc.jc._offinsert) {
let sec = insertCfg.autoShowSeconds || 0;
target.scheduleOnce(function () {
let n = Math.floor(Math.random() * adIds.length);
cc.jc.ADInsert_Show(adIds[n], '', null, null);
}, sec);
}
},
// 延迟几秒
delay(time, obj) {
return new Promise((resolve, reject) => {
try {
setTimeout(function(){
resolve(obj);
}, time * 1000);
} catch (err) {
reject(err);
}
})
},
};
module.exports = util;