觉醒时刻
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
juexingshike/assets/Platform/th/Common5.ts

1655 lines
56 KiB

1 week ago
import AppConfig from "../../FrameWork/Config/AppConfig";
import BundleMgr from "../../FrameWork/Mgr/BundleMgr";
import GEMgr from "../../FrameWork/Mgr/GEMgr";
import VibrateMgr from "../../FrameWork/Mgr/VibrateMgr";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
const { ccclass, property } = cc._decorator;
@ccclass
export default class Common5 {
public static shiJianChuo:number = 0;
public static curShowTufaEvent: number = 0;
public static isTouching: boolean = false;
public static isFirstInLaji = true;//玩家是否第一次进入
public static isNewUser: boolean = false;
public static isNoEnergyMode = 0; //畅玩
public static isVideoAdStopTime: boolean = false;//
public static isMute: boolean = false;//是否静音
public static isNoMusic: boolean = false;//是否静音
public static isNoEffect: boolean = false;//是否静音
public static isNoZD: boolean = false;//是否关闭震动
public static audioId: number = 0;//音乐ID
//游戏相关
public static ISSHOWGM: boolean = true;//
public static juQingStocksConfig = []//保存剧情配置文件
public static allJuQingJson = {}//保存所有剧情对话json
public static bagConfigs = null
public static isNoPower: boolean = false;//体力不够
public static isBackLevel = 0;
public static videoGetPowerNum = 3; //普通体力视频获取点数
public static powerRecoverTime = 300; //体力恢复时间(s)
public static fisrtPower = 1; //玩家第一次进入游戏的体力值
public static maxPower = 3; //体力值上限
public static videoFirstGetPowerNum = 2; //无限体力第一次视频获取点数
public static videoSecondGetPowerNum = 3; //无限体力第二次视频获取点数
public static videoWxtlPowerNum = 3; //无限体力获取视频数
static allStocksConfig = [];
public static allJuQingChatConfig = {}//保存所有剧情聊天配置文件
public static chatPersonConfig = null//保存所有聊天人物
public static isOpenChatPeople = '';
static lixianTime: number = 0;
// onLoad () {}
public static GameProbabillityConfig = null//
public static guwanConfigs = null//
public static ChanYeLevelUpIndex = 1//
public static FengWeiLevelUpIndex= 1
public static ChanYeLevel= 0//
public static FengWeiLevel= 0
// public static NewGuideStep = 0//
public static earnMoneySecond = 240;
public static DiTanIndex = 0;
public static curGameName = '主界面';
public static isCeBianLanEnter:boolean = false//是否从侧边栏进入游戏
public static huangjinHaianConfigs = null//
public static huangjinHeDengChange = {countNum:0, dayNum:0, changeGailv:0}
public static onHideState = {
gold:0, //金币
strength:0, //体力
sendCardTimes:0, //发牌次数
unlockLevel:0, //已经解锁关卡/槽位
removeTimes:0, //消除次数
default1:0, //默认字段1
default2:0, //默认字段2
default3:0 //默认字段3
}
public static gameConfig = null;
public static selectGameNum: number = -1;//选择的是第几关
public static selectGameInfo = null;//选择的关卡信息
public static curWordGameType = 0; //记录当前在哪个分类
public static showAllGameUnLock = false;
public static wordGameType = 0;
public static showShouzhi = true
public static saveTime = 0;
public static addGameArr =
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31,32,33,34,35,36,37,38,39,40,41,42,43,
44,45,46,47,48,49,50,51,
52,53,54,55,56,57,58,59,60,61,
];
public static caiPuConfigs = null//
public static AllMineralsConfig = null//所有矿产配置文件
public static AllMountingConfig = null//所有山峰配置文件
public static meizhuangAddMoney = 0
public static commonTextTipNode = null
public static blackGuoduNode = null
/**材质存储列表 */
public static imgMap: Map<string, cc.SpriteFrame>;
public static audioMap: Map<string, cc.AudioClip>;
public static spineMap: Map<string, sp.SkeletonData>;
start() {
}
getHuangjinghaianGetReward(){
if(Common5.huangjinHeDengChange.dayNum > 0){
//0-1
return Common5.huangjinHeDengChange.changeGailv
}else{
if(Common5.huangjinHeDengChange.countNum>0){
return Common5.huangjinHeDengChange.countNum
}else{
return 0
}
}
}
public static setNodePos(pointNode:cc.Node,x,y){
pointNode.setPosition(x,y);
}
public static LoadRemoteBundle(name,func){
BundleMgr.loadBundleByName_custom(name, handleFM_custom((err, bundle) => {
if (err) {
//console.error(err);
} else {
func();
}
}, this));
}
public static loadJuQingChatConfig(){
Common5.chatPersonConfig = null
}
/**
* json配置
*/
public static loadConfigLocal_custom(AppSwitchcallback?) {
console.log("加载本地json===")
// CaiChaConfigManage.loadAllJson();
Common5.loadAllJson()
if(AppSwitchcallback){
AppSwitchcallback()
}
Common5.imgMap = new Map();
Common5.audioMap = new Map()
Common5.spineMap = new Map()
}
public static loadAllJson(){
let ChatPersonCallFunc = (callFunc)=>{
cc.loader.loadRes('Json/JuQingChatConfig/ChatPerson.json', function (err, gameconfig) {
if (err) {
//console.log(err);
return;
}
Common5.chatPersonConfig = gameconfig.json;
for(var i=0;i<Common5.chatPersonConfig.length;i++){
let jsonName = Common5.chatPersonConfig[i].jsonName
if(!Common5.allJuQingChatConfig[jsonName]){
// console.log("加载人物剧情聊天json==",jsonName)
cc.loader.loadRes('Json/JuQingChatConfig/'+jsonName+'.json', function (err, gameconfig) {
if (err) {
//console.log(err);
return;
}
Common5.allJuQingChatConfig[jsonName] = gameconfig.json;
});
}
}
callFunc()
});
}
let allStockConfigCallFunc = (callFunc)=>{
cc.loader.loadRes('Json/stockConfig/allStockConfig.json', function (err, gameconfig) {
if (err) {
return;
}
Common5.allStocksConfig = gameconfig.json;
// console.log("游戏配置加载成功", Common5.allStocksConfig);
callFunc()
});
}
// let WZGameConfigCallFunc = (callFunc)=>{
// cc.loader.loadRes('Json/WenZiGameConfig/WZGameConfig.json', function (err, gameconfig) {
// if (err) {
// console.log(err);
// return;
// }
// Common5.gameConfig = gameconfig.json;
// // console.log("游戏配置加载成功", Common5.gameConfig);
// callFunc()
// });
// }
// let GameProbabillityConfigCallFunc = (callFunc)=>{
// cc.loader.loadRes('Json/probability/GameProbabillityConfig.json', function (err, gameconfig) {
// if (err) {
// return;
// }
// Common5.GameProbabillityConfig = gameconfig.json;
// //console.log("游戏配置加载成功", Common5.juQingStocksConfig);
// callFunc()
// });
// }
// let guwanConfigsCallFunc = (callFunc)=>{
// cc.loader.loadRes('Json/guwanConfig/guwanConfigs.json', function (err, gameconfig) {
// if (err) {
// return;
// }
// Common5.guwanConfigs = gameconfig.json;
// //console.log("游戏配置加载成功", Common5.juQingStocksConfig);
// callFunc()
// });
// }
let caipuCallFunc = (callFunc)=>{
cc.loader.loadRes('Json/gameConfig/caipu.json', function (err, gameconfig) {
if (err) {
return;
}
Common5.caiPuConfigs = gameconfig.json;
//console.log("游戏配置加载成功", Common5.juQingStocksConfig);
callFunc()
});
}
// let AllMineralsConfigCallFunc = (callFunc)=>{
// cc.loader.loadRes('Json/kaiKuangConfig/AllMineralsConfig.json', function (err, gameconfig) {
// if (err) {
// //console.log(err);
// return;
// }
// Common5.AllMineralsConfig = gameconfig.json;
// //console.log("游戏配置加载成功", Common5.gameConfig);
// callFunc()
// });
// }
// let AllMountingConfigCallFunc = (callFunc)=>{
// cc.loader.loadRes('Json/kaiKuangConfig/AllMountingConfig.json', function (err, gameconfig) {
// if (err) {
// //console.log(err);
// return;
// }
// Common5.AllMountingConfig = gameconfig.json;
// //console.log("游戏配置加载成功", Common5.gameConfig);
// callFunc()
// });
// }
//这里用递归的方式加载json,处理同时加载多个json情况
let loadJsonArr = [
ChatPersonCallFunc,allStockConfigCallFunc,caipuCallFunc
]
let loadIndex = 0
let loadCallFunc = (index) => {
if (index >= loadJsonArr.length) {
return
}
let time = new Date().getTime()
console.log('shaoyang_loading页加载当前json',index,time- Common5.shiJianChuo)
console.log('loading页loadAllJson_',index)
let callFunc = ()=>{
console.log('loading页单个json加载完毕,加载下一个')
loadCallFunc(++loadIndex)
}
loadJsonArr[index](callFunc)
}
loadCallFunc(loadIndex)
}
/**
*
* @param picSp sprite
* @param url
*/
public static addUrlSprite_custom(picSp: cc.Sprite, url: string) {
cc.assetManager.loadRemote(url, (err, texture) => {
if (err) {
//console.log("加载图片失败")
return;
}
//@ts-ignore
var sprite0 = new cc.SpriteFrame(texture);
picSp.spriteFrame = sprite0;
// //console.log("this.maxBallShowSprite",this.maxBallShowSprite);
});
}
public static addResourseSprite_custom(picSp: cc.Sprite, url: string) {
cc.resources.load(url, cc.Texture2D, function (err, texture: cc.Texture2D) {
if (err) {
//console.log("加载图片失败")
return;
}
var sprite0 = new cc.SpriteFrame(texture);
picSp.spriteFrame = sprite0;
});
}
/**
*
*
* @param btnNode
* @param callback
* @param protectedTime
* @param scaleTime
* @param soundName
* @param clickSprite
*/
public static btnRegister_custom(btnNode: cc.Node, callback: Function, protectedTime: number = 0.1, scaleTime: number = 0.2, soundName: string = "ui", clickSprite: cc.SpriteFrame = null) {
//console.log("注册按钮事件", btnNode.name);
let btnSprite = null;//btnNode.getComponent(cc.Sprite).spriteFrame;
if (btnNode.getComponent(cc.Sprite) != null) {
btnSprite = btnNode.getComponent(cc.Sprite).spriteFrame;
}
//开始触摸
soundName = soundName
let tmpScale = btnNode.scale;
btnNode.on(cc.Node.EventType.TOUCH_START, () => {
// SoundMgr.playSound_custom(soundName);
// Common5.getSoundFromBundle_custom("subRes_Resources", soundName, false);
Common5.playEffect(soundName);
VibrateMgr.vibrateShort_custom();
if (scaleTime > 0) {
cc.tween(btnNode).to(scaleTime, { scale: tmpScale * 0.8 }).start();
}
if (clickSprite != null) {
//console.log("clickSprite", clickSprite);
btnNode.getComponent(cc.Sprite).spriteFrame = clickSprite;
}
}, this);
//结束触摸,响应时间
btnNode.on(cc.Node.EventType.TOUCH_END, () => {
VibrateMgr.vibrateShort_custom();
if (scaleTime > 0) {
cc.tween(btnNode).to(scaleTime, { scale: tmpScale }).start();
}
if (btnSprite != null) {
btnSprite = btnNode.getComponent(cc.Sprite).spriteFrame;
btnNode.getComponent(cc.Sprite).spriteFrame = btnSprite;
}
if (Common5.isTouching === false) {
Common5.isTouching = true;
callback(btnNode);
setTimeout(() => {
Common5.isTouching = false;
}, protectedTime * 1000);
}
}, this);
//退出按钮
btnNode.on(cc.Node.EventType.TOUCH_CANCEL, () => {
if (btnSprite != null) {
btnNode.getComponent(cc.Sprite).spriteFrame = btnSprite;
}
if (scaleTime > 0) {
cc.tween(btnNode).to(scaleTime, { scale: tmpScale }).start();
}
}, this);
}
/**
*
* @param node
*/
public static getPosInWorld_custom(node: cc.Node) {
let pos = node.getPosition();
var newVec3 = node.parent.convertToWorldSpaceAR(pos);
let returnPos = cc.v2(newVec3.x - cc.winSize.width / 2, newVec3.y - cc.winSize.height / 2)
return returnPos;
}
/**
*
* @param node0
* @param node1
* @returns
*/
public static makeNodeDistance_custom(node0: cc.Node, node1: cc.Node) {
let pos0 = Common5.getPosInWorld_custom(node0);
let pos1 = Common5.getPosInWorld_custom(node1);
return Common5.makeDistance(pos0, pos1);
}
/**
*
*
* @param pos0
* @param pos1
*/
public static makeDistance(pos0: cc.Vec2, pos1: cc.Vec2) {
// let dx = Math.abs(pos0.x - pos1.x);
// let dy = Math.abs(pos0.y - pos1.y);
var dis = Math.abs(pos0.sub(pos1).mag())//Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
return dis;
}
/**
* ,pos0,centerPos,pos1形成的角度
* @param pos0
* @param pos1
* @param centerPos
*/
public static makeAngle(pos0: cc.Vec2, pos1: cc.Vec2, centerPos: cc.Vec2) {
let vec0 = cc.v2(pos0.x - centerPos.x, pos0.y - centerPos.y);
let vec1 = cc.v2(pos1.x - centerPos.x, pos1.y - centerPos.y);
//弧度
var angle = vec1.signAngle(vec0);
//将弧度转换为欧拉角
var degree = angle / Math.PI * 180;
return -degree;
}
/**
* 
* @param dirVec 
* @returns 
*/
public static vectorsToDegress(dirVec: cc.Vec2) {
let comVec: cc.Vec2 = cc.v2(0, 1);    // 水平向上的对比向量
let radian = dirVec.signAngle(comVec);    // 求方向向量与对比向量间的弧度
let degree = cc.misc.radiansToDegrees(radian);    // 将弧度转换为角度
return -degree;
}
//从固定数组中随机抽取一个数
public static getMathInLimitArr_custom(limitArr: number[]) {
if (limitArr.length < 0) {
return 0;
}
let num = Math.floor(Math.random() * limitArr.length);
return limitArr[num];
}
//从固定数组中随机抽取一个数组,返回为被选中数组和剩下的数组
public static getMathArrInLimitArr_custom(limitArr: number[], getCount: number) {
let returnArr = [];
for (var i = 0; i < getCount; i++) {
if (limitArr.length <= 0) {
} else {
//console.log("limitArr.length", limitArr.length);
let num = Math.floor(Math.random() * limitArr.length);
returnArr.push(limitArr[num]);
limitArr.splice(num, 1);
}
}
let obj = {
"returnArr": returnArr,
"limitArr": limitArr
}
return obj;
}
/**
* min到max随机取count个数,minmax, min>=max || count<0 || (max-min+1)<count
* @param min
* @param max
* @param count
*/
public static getRandomNumArr_custom(min: number, max: number, count: number) {
//console.log("min", min, "max", max, "count", count);
if (min >= max || count < 0 || (max - min + 1) < count) {
//console.log("输入参数有误")
return [];
}
//原数组
var arr = [];
for (var i = min; i <= max; i++) {
arr.push(i)
}
//输出数组
var out = [];
//输出个数-count
while (out.length < count) {
var temp = (Math.random() * arr.length) >> 0;
for (let i = 0; i < out.length; i++){
if (temp == out[i]){
continue;
}
}
out.push(arr.splice(temp, 1)[0]);
}
return out;
}
/**
* min到max取随机数min>=max的情况
* @param min
* @param max
*/
public static getRandomNum(min:number,max:number){
let minNumber = Math.min(min,max);
let maxNumber = Math.max(min,max);
return Math.floor(Math.random()*(maxNumber - minNumber)) + minNumber;
}
// //
public static showTips_custom(text: string) {
let canvas = cc.find("Canvas");
let node = new cc.Node("tips");
let label = node.addComponent(cc.Label);
label.string = text;
label.enableBold = true;
label.fontSize = 40;
let outline = node.addComponent(cc.LabelOutline);
outline.width = 5;
outline.color = new cc.Color(0, 0, 0, 255);
canvas.addChild(node);
node.x = 0;
node.y = 0;
cc.tween(node).delay(2.5).to(1, { y: 555, opacity: 0 }).call(() => {
node.removeFromParent();
}).start();
}
public static showTips_customTime(text: string,delay:number,) {
let canvas = cc.find("Canvas");
let node = new cc.Node("tips");
let label = node.addComponent(cc.Label);
label.string = text;
label.enableBold = true;
label.fontSize = 40;
label.lineHeight = 40;
label.overflow = cc.Label.Overflow.SHRINK
label.node.height = 85
label.node.width = 600
label.horizontalAlign = cc.Label.HorizontalAlign.CENTER
let outline = node.addComponent(cc.LabelOutline);
outline.width = 3;
outline.color = new cc.Color(0, 0, 0, 255);
canvas.addChild(node);
node.x = 0;
node.y = 150;
cc.tween(node).delay(delay).to(1, { y: 555, opacity: 0 }).call(() => {
node.removeFromParent();
}).start();
}
public static showTips_customTimeAndPos(text: string,delay:number, node_:cc.Node, deltaY) {
let canvas = cc.find("Canvas");
let node = new cc.Node("tips");
let label = node.addComponent(cc.Label);
label.string = text;
label.enableBold = true;
label.fontSize = 40;
label.lineHeight = 40;
label.overflow = cc.Label.Overflow.SHRINK
label.node.height = 85
label.node.width = 600
label.horizontalAlign = cc.Label.HorizontalAlign.CENTER
let outline = node.addComponent(cc.LabelOutline);
outline.width = 3;
outline.color = new cc.Color(0, 0, 0, 255);
canvas.addChild(node);
let pos = Common5.getRelativePosition(node_, canvas);
node.x = pos.x;
node.y = pos.y;
let y = pos.y + deltaY;
cc.tween(node).delay(delay).to(1, { y: y, opacity: 0 }).call(() => {
node.removeFromParent();
}).start();
}
/**
* resourse文件夹下目录
* @param url
* @param parent
* @param callback
*/
public static addPrefab(url, parent, callback = null) {
let parentNode = parent
cc.loader.loadRes(url, (err, prefab) => {
if (err) {
//console.log('加载预制体错误>:', err)
return;
}
var prefabNode = cc.instantiate(prefab);
prefabNode.parent = parentNode;
if (callback) {
callback(prefabNode);
}
});
}
public static getSoundFromBundle_custom(bundlename = "allRes", url: string, isBgm: boolean = false) {
if (Common5.isMute == true) {
return;
}
let audioclip: cc.AudioClip = null;
let subres = cc.assetManager.getBundle(bundlename);
// url = "Sound/Wedding/" + url
if (subres) {
subres.load(url, cc.AudioClip, (err, AudioClip: cc.AudioClip) => {
if (err) {
//console.log('加载音效错误>:', err)
return null;
}
// //console.log("加载成功:",AudioClip);
audioclip = AudioClip;
if (isBgm == true) {
this.audioId = cc.audioEngine.playMusic(audioclip, true);
} else {
this.audioId = cc.audioEngine.play(audioclip, false, 1);
}
})
}
// return audioclip;
}
public static ReportDY(event,msg){
if (AppPlatform.is_TT_GAME_custom()) {
window["tt"].reportAnalytics(event, {
type: msg,
});
ZYSDK.ZYSDK.reportUserAction(msg);
GEMgr.GEReportEvent(msg);
}
if (AppPlatform.is_WECHAT_GAME_custom()) {
event = event.toLowerCase();
window["wx"].reportEvent(event, {
"type": msg,
});
ZYSDK.ZYSDK.reportUserAction(msg);
GEMgr.GEReportEvent(msg);
}
}
public static ReportDYTime(event,level, times){
let eventStr = level + '-time-' + Math.round(times / 60)
if (AppPlatform.is_TT_GAME_custom()) {
window["tt"].reportAnalytics(event, {
"type": eventStr,
});
ZYSDK.ZYSDK.reportUserAction(eventStr);
//GEMgr.GEReportEvent(eventStr);
}
if (AppPlatform.is_WECHAT_GAME_custom()) {
event = event.toLowerCase();
window["wx"].reportEvent(event, {
"type": eventStr,
});
ZYSDK.ZYSDK.reportUserAction(eventStr);
}
}
public static stopMusic(){
cc.audioEngine.stopAll();
}
public static playEffect(music){
if (this.isNoEffect){
return;
}
this.getSoundFromBundle_custom("CommonRes", music, false);
}
public static playEffectCustom(prefab,music){
// cc.audioEngine.stopAllEffects();
if (this.isNoEffect){
return;
}
this.getSoundFromBundle_custom(prefab, music, false);
}
public static playMusic(music){
if (this.isNoMusic){
return;
}
this.getSoundFromBundle_custom("CommonRes", music, true);
}
public static getPlayMusicAudioId(){
return this.audioId
}
public static getSpriteFrameFromBundle(bundlename = "subResAddMode", url: string, targetSprite: cc.Sprite = null, callback = null) {
let subres = cc.assetManager.getBundle(bundlename);
if (subres) {
subres.load(url, cc.SpriteFrame, (err, sprite: cc.SpriteFrame) => {
if (err) {
console.log(err)
return null;
}
if (targetSprite != null) {
targetSprite.spriteFrame = sprite;
}
if (callback) {
callback(sprite);
}
})
}
}
public static getSpriteFrameFromBundlePro(bundlename = "subResAddMode", url: string) {
let subres = cc.assetManager.getBundle(bundlename);
if (subres) {
console.log('预加载图片+++',url)
subres.preload(url)
}
}
/**
*
* @param bundlename
* @param url
* @param parentNode
* @param callback
*/
public static getPrefabFromBundlePrefabLayer(bundlename = "subRes_Resources", url: string, parentNode: cc.Node = null, callback = null) {
let subres = cc.assetManager.getBundle(bundlename);
// let startDaDianTime = new Date().getTime();
console.log('addChild000')
if (subres) {
subres.load(url, cc.Prefab, (err, prefab: cc.Prefab) => {
if (err) {
console.log(err,'error')
return null;
}
//console.log('addChild111')
var prefabNode = cc.instantiate(prefab);
if (parentNode != null) {
//console.log('addChild2222')
parentNode.addChild(prefabNode)
} else {
//console.log('addChild3333')
let prefabLayer = cc.director.getScene().getChildByName("Canvas").getChildByName('prefabLayer')
prefabLayer.addChild(prefabNode);
//console.log('addChild4444')
}
if (callback) {
callback(prefabNode);
}
})
}
}
public static getPrefabFromBundlePopLayer(bundlename = "subRes_Resources", url: string, parentNode: cc.Node = null, callback = null) {
let subres = cc.assetManager.getBundle(bundlename);
if (subres) {
subres.load(url, cc.Prefab, (err, prefab: cc.Prefab) => {
if (err) {
return null;
}
var prefabNode = cc.instantiate(prefab);
if (parentNode != null) {
parentNode.addChild(prefabNode)
} else {
let popLayer = cc.director.getScene().getChildByName("Canvas").getChildByName('popLayer')
popLayer.addChild(prefabNode);;
//cc.director.getScene().getChildByName("Canvas").addChild(prefabNode);;
}
if (callback) {
callback(prefabNode);
}
})
}
}
/**
*
* @param bundlename
* @param url
* @param parentNode
* @param callback
*/
public static getSpineFromBundle(bundlename = "subRes_Resources", url: string, parentNode: cc.Node = null, callback = null) {
let subres = cc.assetManager.getBundle(bundlename);
if (subres) {
subres.load(url, sp.SkeletonData, function(err, spine:sp.SkeletonData) {
if (err) {
cc.log(err.message || err);
return;
}
let node = new cc.Node('spineNode');
const ske = node.addComponent(sp.Skeleton);
ske.skeletonData = spine;
// ske.setAnimation(0, 'heishan', true);
// parentNode.addChild(node);
var prefabNode = cc.instantiate(node);
// parentNode.addChild(node);
// prefabNode.parent = parentNode;
if (callback) {
callback(prefabNode);
}
});
}
}
//随机排序数组
public static shuffle(arr) {
var len = arr.length;
let setran = []
for (var i = len - 1; i >= 0; i--) {
var rand = Math.floor(Math.random() * (arr.length - 0.01));
//console.log("rand===", rand)
setran.push(arr[rand]);
arr.splice(rand, 1);
}
return setran;
}
public static shuffle_noremove(arr) {
var len = arr.length;
let setran = []
for (var i = len - 1; i >= 0; i--) {
var rand = Math.floor(Math.random() * (arr.length - 0.01));
//console.log("rand===", rand)
setran.push(arr[rand]);
}
return setran;
}
//对数组进行分组
public static groupingArr(targetArr: number[], groupCount: number) {
let returnArr = [];
for (var i = 0; i < groupCount; i++) {
if (targetArr.length > 0) {
let mathIndex = Math.floor(Math.random() * targetArr.length);
returnArr[i] = [targetArr[mathIndex]];
targetArr.splice(mathIndex, 1);
}
}
if (targetArr.length > 0) {
for (var i = 0; i < targetArr.length; i++) {
let mathIndex = Math.floor(Math.random() * returnArr.length);
if (returnArr[mathIndex].length == 1) {
returnArr[mathIndex].push(targetArr[i]);
}
}
}
//console.log("---returnArr+++", returnArr)
return returnArr;
}
public static parabolaFunc(parabolaNode: cc.Node, startPos: cc.Vec2, endPos: cc.Vec2, height: number, time: number, func: Function = null) {
//抛物线运动,从起点到终点
let controlPos = cc.v2((startPos.x + endPos.x) / 2, ((startPos.y + endPos.y) / 2 + height));
cc.tween(parabolaNode).bezierTo(time, startPos, controlPos, endPos).call(() => {
if (func != null) {
func();
}
}).start();
}
/**
*
* @param node
* @param container
* @param pos ()
*/
public static getRelativePosition(node: cc.Node, container: cc.Node, pos?: cc.Vec2): cc.Vec2 {
let vpos = pos || cc.Vec2.ZERO;
const worldPos = node.convertToWorldSpaceAR(vpos);
return container.convertToNodeSpaceAR(worldPos);
}
// update (dt) {}
/**
*
* @param prefab
* @param parent null Canvas
*/
public static createPrefab_custom(prefab: cc.Prefab, parent?: cc.Node,) {
let node = cc.instantiate(prefab);
if (parent) {
// parent.addChild(node);
node.parent = parent
} else {
cc.find('Canvas').addChild(node, 0);
}
return node;
}
public static getJsonFromBundle(bundlename = "subRes_resources", url: string, callback) {
let subres = cc.assetManager.getBundle(bundlename);
if (subres) {
subres.load(url, cc.JsonAsset, (err, essest: cc.JsonAsset)=>{
if (err) {
console.log('加载json错误>:', err);
return null;
}
//console.log('加载json>:', bundlename, essest);
callback(essest)
})
}
}
//深拷贝
public static deepClone = <T>(origin: T, target?: Record<string, any> | T ): T => {
// 判断是否为数组
let isArr = (origin: any): boolean => {
let str = '[object Array]'
return Object.prototype.toString.call(origin) == str ? true : false
}
let tar:any = target || []
for (const key in origin) {
if (Object.prototype.hasOwnProperty.call(origin, key)) {
if (typeof origin[key] === 'object' && origin[key] !== null) {
tar[key] = isArr(origin[key]) ? [] : {}
Common5.deepClone(origin[key], tar[key])
} else {
tar[key] = origin[key]
}
}
}
return tar as T
}
/**
* !#zh
* @param rectTargetNode
* @param curNode
*/
public static checkContainsNode(rectTargetNode:cc.Node, curNode:cc.Node){
let pworld = rectTargetNode.getBoundingBoxToWorld()
let pos1 = curNode.convertToWorldSpaceAR(cc.Vec2.ZERO)
if(pworld.contains(pos1)){
return true;
}else{
return false;
}
}
/**
* !#zh
* @param curRectNode
* @param targetRectNode
*/
public static checkIntersectsBox(curRectNode:cc.Node, targetRectNode:cc.Node):boolean{
if(!curRectNode ){
console.log('curRectNode null')
return
}
if(!targetRectNode ){
console.log('targetRectNode null')
return
}
let pworld = curRectNode.getBoundingBoxToWorld()
let box1 = targetRectNode.getBoundingBoxToWorld()
if(pworld.intersects(box1)){
return true;
}else{
return false;
}
}
/**
*
* @param curNode
* @param targetNode
*/
public static setNodeToTargetPos(curNode,targetNode){
//目标节点的本地坐标
if(cc.isValid(curNode)){
}else{
return cc.v2(0,0)
}
if(cc.isValid(targetNode) && cc.isValid(targetNode.parent)){
}else{
return cc.v2(0,0)
}
let targetLocalPos = targetNode.getPosition()
//目标节点的世界坐标
let targetWorldPos = targetNode.parent.convertToWorldSpaceAR(targetLocalPos)
//目标节点相对于当前节点父节点的本地坐标
let localPos = curNode.parent.convertToNodeSpaceAR(targetWorldPos)
curNode.setPosition(localPos)
}
public static getNodeToTargetPos(curNode,targetNode){
//目标节点的本地坐标
let targetLocalPos = targetNode.getPosition()
//目标节点的世界坐标
let targetWorldPos = targetNode.parent.convertToWorldSpaceAR(targetLocalPos)
//目标节点相对于当前节点父节点的本地坐标
let localPos = curNode.parent.convertToNodeSpaceAR(targetWorldPos)
return localPos
}
/**
* //检测多边形物体是否超出多边形区域(节点必须携带PolygonCollider属性) 注意offest距离要变为0和0
* @param targetNode
* @param targetArea
* @returns
*/
public static checkIsOutOfArea(targetNode,targetArea){
let targetCollider = targetNode.getComponent(cc.PolygonCollider)
let areaCollider = targetArea.getComponent(cc.PolygonCollider)
//目标节点的本地坐标
let targetLocalPos = targetNode.getPosition()
//目标节点的世界坐标
let targetWorldPos = targetNode.parent.convertToWorldSpaceAR(targetLocalPos)
//目标节点相对于当前节点的本地坐标
let localPos = targetArea.convertToNodeSpaceAR(targetWorldPos)
for(var i=0;i<targetCollider.points.length;i++){
let newPoint = cc.v2(localPos.x+targetCollider.points[i].x,localPos.y+targetCollider.points[i].y)
if(!cc.Intersection.pointInPolygon(newPoint,areaCollider.points)){
console.log("这个点不在区域内",i)
return false
}
}
console.log("目标物全部在区域内")
return true
}
public static getNumberChangeHanzi(value:number, type ='1' ,fixedLength =1) {
const newValue = ['', '', '']
let fr = 1000
let num = 3
let text1 = ''
let text2 = ''
let fm = 1
if(value==null || isNaN(value)){
return !type?newValue:''
}
if(value<0){
value = Math.abs(value);
text2 = '-'
}
while (value / fr >= 1) {
fr *= 10
num += 1
// console.log('数字', value / fr, 'num:', num)
}
if (num <= 4) { // 千
value = Math.floor(value)
newValue[0] = value.toString()
newValue[1] = ''
} else if (num <= 8) { // 万
// text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万'
text1 = '万'
// tslint:disable-next-line:no-shadowed-variable
fm = text1 === '万' ? 10000 : 10000000
if (value % fm === 0) {
newValue[0] = parseInt((value / fm).toString()) + ''
} else {
// newValue[0] = parseFloat((value / fm).toString()).toFixed(fixedLength) + ''
let power = Math.pow(10,fixedLength)
newValue[0] = (Math.floor((value / fm)* power)/power) + ''
}
newValue[1] = text1
} else { // 亿 if (num <= 16)
// text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
text1 = '亿'
text1 = (num - 8) / 4 > 1 ? '兆' : text1
text1 = (num - 8) / 8 > 1 ? '京' : text1
text1 = (num - 8) / 12 > 1 ? '垓' : text1
text1 = (num - 8) / 16 > 1 ? '秭' : text1
text1 = (num - 8) / 20 > 1 ? '穰' : text1
text1 = (num - 8) / 24 > 1 ? '沟' : text1
text1 = (num - 8) / 28 > 1 ? '涧' : text1
text1 = (num - 8) / 32 > 1 ? '正' : text1
text1 = (num - 8) / 36 > 1 ? '载' : text1
text1 = (num - 8) / 40 > 1 ? '极' : text1
text1 = (num - 8) / 44 > 1 ? '恒河沙' : text1
text1 = (num - 8) / 48 > 1 ? '阿僧祗' : text1
text1 = (num - 8) / 52 > 1 ? '那由他' : text1
text1 = (num - 8) / 56 > 1 ? '不可思议' : text1
text1 = (num - 8) / 60 > 1 ? '无量' : text1
text1 = (num - 8) / 64 > 1 ? '大数' : text1
text1 = (num - 8) / 68 > 1 ? '全仕祥' : text1//76
text1 = (num - 8) / 72 > 1 ? '亦' : text1
text1 = (num - 8) / 76 > 1 ? '王' : text1
text1 = (num - 8) / 80 > 1 ? '肤' : text1
text1 = (num - 8) / 84 > 1 ? '张' : text1
text1 = (num - 8) / 88 > 1 ? '战' : text1
text1 = (num - 8) / 92 > 1 ? '古尔戈' : text1
text1 = (num - 8) / 96 > 1 ? '古戈尔哥' : text1
text1 = (num - 8) / 100 > 1 ? '星' : text1
text1 = (num - 8) / 104 > 1 ? '巨' : text1
// tslint:disable-next-line:no-shadowed-variable
fm = 1
if (text1 === '亿') {
fm = 100000000
} else if (text1 === '兆') {
fm = Math.pow(10,12)
} else if (text1 === '京') {
fm = Math.pow(10,16)
} else if (text1 === '垓') {
fm = Math.pow(10,20)
} else if (text1 === '秭') {
fm = Math.pow(10,24)
} else if (text1 === '穰') {
fm = Math.pow(10,28)
} else if (text1 === '沟') {
fm = Math.pow(10,32)
} else if (text1 === '涧') {
fm = Math.pow(10,36)
} else if (text1 === '正') {
fm = Math.pow(10,40)
} else if (text1 === '载') {
fm = Math.pow(10,44)
} else if (text1 === '极') {
fm = Math.pow(10,48)
} else if (text1 === '恒河沙') {
fm = Math.pow(10,52)
} else if (text1 === '阿僧祗') {
fm = Math.pow(10,56)
} else if (text1 === '那由他') {
fm = Math.pow(10,60)
}else if (text1 === '不可思议') {
fm = Math.pow(10,64)
}else if (text1 === '无量') {
fm = Math.pow(10,68)
}else if (text1 === '大数') {
fm = Math.pow(10,72)
}else if (text1 === '全仕祥') {
fm = Math.pow(10,76)
}else if (text1 === '亦') {
fm = Math.pow(10,80)
}else if (text1 === '王') {
fm = Math.pow(10,84)
}else if (text1 === '肤') {
fm = Math.pow(10,88)
}else if (text1 === '张') {
fm = Math.pow(10,92)
}else if (text1 === '战') {
fm = Math.pow(10,96)
}else if (text1 === '古尔戈') {
fm = Math.pow(10,100)
}else if (text1 === '古戈尔哥') {
fm = Math.pow(10,104)
}else if (text1 === '星') {
fm = Math.pow(10,108)
}else if (text1 === '巨') {
fm = Math.pow(10,112)
}
else{
fm = Math.pow(10,112)
}
if (value % fm === 0) {
newValue[0] = parseInt((value / fm).toString()) + ''
} else {
// newValue[0] = parseFloat((value / fm).toString()).toFixed(fixedLength) + ''
let power = Math.pow(10,fixedLength)
newValue[0] = (Math.floor((value / fm)* power)/power) + ''
}
newValue[1] = text1
}
if (value < 1000) {
newValue[0] = value + ''
newValue[1] = ''
}
newValue[0] = text2?text2 + newValue[0] : newValue[0]
if(text1 === '巨'){
let num = Number(newValue[0])
newValue[0] = num.toExponential(0)+''
}
return !type?newValue:(newValue[0] + newValue[1])
}
// let array_ = [20,20,20,20,20,20,70,70,70,70,70,70,70,70,10,0]
// let backi = this.getResult(array_)
//随机概率
public static getProbabilityResultIndex(arr){
var leng = 0;
for(var i = 0; i<arr.length; i++){
leng += arr[i] //获取总数
}
for(var i = 0; i < arr.length; i++){
var random = Math.random()*leng; //获取 0-总数 之间的一个随随机整数
if(random < arr[i]){
return i //如果在当前的概率范围内,得到的就是当前概率
}
else {
leng -= arr[i] //否则减去当前的概率范围,进入下一轮循环
}
}
}
//从min到max中随机取出n个值
public static getNumberFromArray(min,max,count){
//原数组
var arr = [];
for (var i = min; i < max; i++) {
arr.push(i)
}
//输出数组
let temp = []
let length = arr.length
while(count >0){
var index = Math.floor(Math.random()*length)
temp.push(arr[index])
arr.splice(index,1)
length--
count--
}
// console.log("temp==",temp)
return temp
}
//获取值在数组中的索引
public static getIndexInArray(arr,val) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == val) {
return i;
}
}
return -1;
};
//删除数组的某个值
public static removeValueInArray(arr,val) {
var index = this.getIndexInArray(arr,val);
if (index > -1) {
arr.splice(index, 1);
}
return arr
};
/**
*
* @param targetArr
* @returns
*/
public static getTargetArrIndexId(targetArr){
let randomIndex = Math.floor(Math.random()*100)
// console.log("randomIndex==",randomIndex)
let sum = 0
let selectId = 0
for(var j=0;j<targetArr.length;j++){
sum += targetArr[j]
if(randomIndex < sum){
selectId = j
break
}
}
// console.log("targetArr==selectId=",targetArr,selectId)
return selectId
}
public static copyex(src:string, first:string, end:string){
let len1 = first.length;
//let len2 = end.length;
let result = src.substring(src.indexOf(first)+len1, src.indexOf(end) + 0)
return result;
}
/**
*
* @param timeStampA Anumber类型
* @param timeStampB Bnumber类型
* @returns true/false
*/
public static isSameDay(timeStampA: number, timeStampB: number): boolean {
let dateA = new Date(timeStampA);
let dateB = new Date(timeStampB);
return (dateA.setHours(0, 0, 0, 0) == dateB.setHours(0, 0, 0, 0));
}
public static getCurSecond(){
return Date.parse(new Date().toString())/1000;
}
//格式化时间获取:分秒 00:00
public static formatTime3_custom(time: number): string {
let str: string = "";
let m: number = time / 60;
m = parseInt(m + "");
let s: number = time - m * 60;
s = parseInt(s + "");
if (m > 9) {
str += m + ":";
}
else {
str += "0" + m + ":";
}
if (s > 9) {
str += s;
}
else {
str += "0" + s;
}
return str;
}
//格式化时间获取:时分秒 00:00:00
public static formatTime_custom(time: number): string {
let str: string = "";
let h: number = time / 3600;
h = parseInt(h + "");
let m: number = (time - h * 3600) / 60;
m = parseInt(m + "");
let s: number = time - h * 3600 - m * 60;
s = parseInt(s + "");
if (h > 9) {
str += h + ":";
}else{
str += "0" + h + ":";
}
if (m > 9) {
str += m + ":";
}
else {
str += "0" + m + ":";
}
if (s > 9) {
str += s + "";
}
else {
str += "0" + s;
}
return str;
}
//格式化时间获取:时分 00:00
public static formatTimeHM_custom(time: number): string {
let str: string = "";
let h: number = time / 3600;
h = parseInt(h + "");
let m: number = (time - h * 3600) / 60;
m = parseInt(m + "");
// let s: number = time - h * 3600 - m * 60;
// s = parseInt(s + "");
if (h > 9) {
str += h + ":";
}else{
str += "0" + h + ":";
}
if (m > 9) {
str += m
}
else {
str += "0" + m
}
return str;
}
public static getArrDifference(arr1, arr2){
let arr3 = []
for(let value of arr1){
arr3.push(value)
}
return arr3.concat(arr2).filter(
(value, i, arr)=>{
return arr.indexOf(value) === arr.lastIndexOf(value);
}
)
}
public static playMusicCustom(bundle, music) {
if (this.isNoMusic) {
return;
}
this.getSoundFromBundle_custom(bundle, music, true);
}
public static showTips_customTimeNew(text: string, delay: number, pos) {
let canvas = cc.find("Canvas");
let node = new cc.Node("tips");
let label = node.addComponent(cc.Label);
label.string = text;
label.enableBold = false;
label.fontSize = 40;
label.lineHeight = 50;
let outline = node.addComponent(cc.LabelOutline);
outline.width = 5;
outline.color = cc.Color.RED;//new cc.Color(0, 0, 0, 255);
canvas.addChild(node);
let y = pos.y + 100;
node.x = pos.x;
node.y = pos.y;
cc.tween(node).delay(delay).to(1, { y: y, opacity: 0 }).call(() => {
node.removeFromParent();
}).start();
}
public static formatNumber(num) {
let str = ""
if (num >= 100000000) {
str = num / 100000000 + "亿"
} else if (num >= 10000) {
str = num / 10000 + "万"
}
return str
}
/**
*
* @param bundlename
* @param url
* @param parentNode
* @param callback
*/
public static getPrefabFromBundle(bundlename = "subRes_resources", url: string, parentNode: cc.Node = null, callback = null) {
let subres = cc.assetManager.getBundle(bundlename);
if (subres) {
subres.load(url, cc.Prefab, (err, prefab: cc.Prefab) => {
if (err) {
console.log('加载预制体错误>:', err);
return null;
}
console.log("加载成功:", prefab.name);
var prefabNode = cc.instantiate(prefab);
if (parentNode != null) {
parentNode.addChild(prefabNode)
} else {
cc.director.getScene().getChildByName("Canvas").addChild(prefabNode);;
}
// prefabNode.parent = parentNode;
if (callback) {
callback(prefabNode);
}
})
}
}
//判断两个数组是否相等(可判断1维或2维数组)(未完善,请慎用)
public static checkEqualOfTwoArr(arr1,arr2){
if(arr1.length != arr2.length){
return false
}
for(let i=0;i<arr1.length;i++){
if((arr1[i] instanceof Array) && (arr2[i] instanceof Array)){
if(arr1[i].length != arr2[i].length){
return false
}
// for(let j=0;j<arr1[i].length;j++){
// if(!this.isObjectEqual(arr1[i][j],arr2[i][j])){
// return false
// }
// }
}else if((arr1[i] instanceof Number) && (arr2[i] instanceof Number)){
if(arr1[i] != arr2[i]){
return false
}
}else{
return false
}
}
return true
}
//从远程url设置sprite资源
public static setRemoteSpriteFrame(sprite: cc.Sprite, url: string) {
let path: string = AppConfig.ResServer_custom + url
console.log('url图片路径:'+path)
if (this.imgMap.has(path)) {
sprite.spriteFrame = this.imgMap.get(path);
return;
}
console.log("还没有先下载");
cc.assetManager.loadRemote(path + ".png" + '?time=1.0.15', { maxRetryCount: 0 }, (err, texture: cc.Texture2D) => {
if (err) return;
this.imgMap.set(path, new cc.SpriteFrame(texture));
sprite.spriteFrame = this.imgMap.get(path);
});
}
//从远程url设置音乐
public static playRemoteAudioMusic(url: string,isLoop:boolean = true){
if (Common5.isNoMusic){
return;
}else if (Common5.isMute == true) {
return;
}
let path: string = AppConfig.ResServer_custom + url
console.log('url音乐路径:'+path)
if (this.audioMap.has(path)) {
this.audioId = cc.audioEngine.playMusic(this.audioMap.get(path), isLoop);
return;
}
console.log("还没有先下载");
cc.assetManager.loadRemote(path+'.mp3' + '?time=1.0.15', (err, audioClip:cc.AudioClip) => {
if (audioClip) {
this.audioMap.set(path, audioClip);
this.audioId = cc.audioEngine.playMusic(audioClip, isLoop);
}
});
}
//从远程url设置音效
public static playRemoteAudioEffect(url: string,isLoop:boolean = false){
// cc.audioEngine.stopAllEffects();
if (Common5.isNoEffect){
return;
}else if (Common5.isMute == true) {
return;
}
let path: string = AppConfig.ResServer_custom + url
console.log('url音效路径:'+path)
if (this.audioMap.has(path)) {
this.audioId = cc.audioEngine.play(this.audioMap.get(path), isLoop, 1);
return;
}
console.log("还没有先下载");
cc.assetManager.loadRemote(path+'.mp3' + '?time=1.0.15', (err, audioClip:cc.AudioClip) => {
if (audioClip) {
this.audioMap.set(path, audioClip);
this.audioId = cc.audioEngine.play(audioClip, isLoop, 1);
}
});
}
//从远程预加载音效/音乐
public static preloadRemoteAudio(url: string){
if(!url){
return
}
let path: string = AppConfig.ResServer_custom + url
console.log('url音效路径:'+path)
if (this.audioMap.has(path)) {
return;
}
cc.assetManager.loadRemote(path+'.mp3' + '?time=1.0.15', (err, audioClip:cc.AudioClip) => {
});
}
//从远程url设置spine
public static playRemoteSpine(_node: cc.Node, gamePath:string,spineName: string,aniName:string,isloop:boolean=true,skinName?:string) {
let playSpine = (n: sp.Skeleton | cc.Node, aniName: string, isloop: boolean = true,skinName?:string) =>{
let _sp: sp.Skeleton;
if (n instanceof cc.Node) {
_sp = n.getComponent(sp.Skeleton);
} else {
_sp = n;
}
_sp.loop = isloop;
_sp.animation = aniName;
if(skinName){
_sp.setSkin(skinName);
}
};
let path: string = AppConfig.ResServer_custom + gamePath +'/'+spineName
console.log('url动效路径:'+path)
if (this.spineMap.has(path)) {
_node.getComponent(sp.Skeleton).skeletonData = this.spineMap.get(path);
playSpine(_node, aniName, isloop,skinName)
return;
}
console.log("还没有先下载");
cc.loader.load({ url: path +".atlas", type: 'text' }, (err, spineAtlas)=> {
cc.loader.load({ url: path + ".json", type: 'json' },(err, spineJson)=> {
var asset = new sp.SkeletonData();
asset.skeletonJson = spineJson;
asset.atlasText = spineAtlas;
asset.textures = [];
cc.loader.load({ url: path + ".png", type: 'png' }, (err, texture)=> {
asset.textures.push(texture);
asset["textureNames"] = [spineName + ".png"];
this.spineMap.set( path,asset)
if(_node){
_node.getComponent(sp.Skeleton).skeletonData = asset
playSpine(_node,aniName,isloop,skinName)
}
});
});
});
}
//手动释放bundle及引用资源
public static releaseBundleAssets(bundleName){
let bundle = cc.assetManager.getBundle(bundleName)
if(!bundle){
return
}
//释放bundle内的所有资源
bundle.releaseAll()
//移除bundle
cc.assetManager.removeBundle(bundle)
}
/**
*
* @param nodePool
* @param prefab
*/
public static getPoolNode(nodePool: cc.NodePool, prefab): cc.Node {
let fish: cc.Node = null;
if (nodePool.size() > 0) {
fish = nodePool.get();
} else {
fish = cc.instantiate(prefab);
}
return fish;
}
/**
*
* @param node
* @param nodePool
*/
public static putPoolNode(node: cc.Node, nodePool: cc.NodePool): void {
nodePool.put(node);
}
}