import { sys,tween,Node,Vec3, UITransform } from "cc"; export default class GameUtil { private static moveLocPos:Vec3=new Vec3(); public static moveToPos(moveNode:Node, worldPos:Vec3,callFun:Function,time:number=0.2,_scale:number=1){ this.moveLocPos=moveNode.parent.getComponent(UITransform).convertToNodeSpaceAR(worldPos); tween(moveNode) .to(time, { position: this.moveLocPos,scale:new Vec3(_scale,_scale,_scale)}) .call(()=>{ if(callFun)callFun(); }) .start() } //删除指定元素 public static arrDeleteObj(arr,deleteobj){ let index=arr.indexOf(deleteobj); if(index!=-1) arr.splice(index,1); } /** * 获取随机数 * @param {Number} min 最小值 * @param {Number} max 最大值 * @returns */ public static Random(min: number, max: number) { return Math.random() * (max - min) + min; } /** * 随机函数 * @param des minValue到maxValue+1可以取值为[minValue,maxValue] */ public static RandomRangeInt(minValue: number, maxValue: number): number { let r = maxValue - minValue; let rand = Math.random(); return (minValue + Math.floor(rand * r)); } public static SystemTime(): number { return sys.now(); } private static _sms: number = 0; public static SystemMillionSecond(): number { if (GameUtil._sms == 0) { GameUtil.UpdateSystemTime(); } return GameUtil._sms; } public static UpdateSystemTime(): void { GameUtil._sms = new Date().getTime(); } public static SystemTimeHHMMSS(): string { var s = this.SystemMillionSecond(); var ss: string = s.toString(); ss = ss.substr(ss.length - 4); return GameUtil.FormatTime_HHMMSS(s) + " " + ss; } public static SystemDay(): number { var t = this.GetDay(GameUtil.SystemMillionSecond()); return t; } public static GetDay(time: number) { var t = time / 1000; t = t / 60; t = t / 60; t = t + 8; t = t / 24; t = Math.floor(t); return t; } public static FormatTime_HHMMSS(millionSecond: number): string { let timeGap = millionSecond / 1000; let hour = (Array(2).join('0') + Math.floor(timeGap / 3600)).slice(-2); let min = (Array(2).join('0') + Math.floor((timeGap % 3600) / 60)).slice(-2); let sec = (Array(2).join('0') + Math.floor(timeGap) % 60).slice(-2); if (hour == "00") { return min + ":" + sec; } else { return hour + ":" + min + ":" + sec; } } public static FormatTime_DDHHMMSS(millionSecond: number): string { var dval = millionSecond / 1000; var ss = dval % 60; dval = Math.floor(dval / 60); var mm = dval % 60; dval = Math.floor(dval / 60); var hh = dval % 24; dval = Math.floor(dval / 24); var dd = dval; var ret = ""; if (dd > 0) { ret = dd + "天"; if (hh > 0) { ret += hh + "小时"; } } else { if (hh > 0) { ret += hh + "小时"; } if (mm > 0) { ret += mm + "分"; } // else if (ss > 0) { // ret += "零" // } if (ss > 0) { ret += Math.floor(ss) + "秒"; } } return ret; } public static timestampToTime() { var n_data = new Date(); var timestamp = Date.parse(n_data.toString()); timestamp = timestamp / 1000; var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000 //var Y = date.getFullYear() + '-'; var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-'; var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' '; var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':'; var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()); //var s = date.getSeconds() < 10 ? ':0'+date.getSeconds():date.getSeconds(); //return Y+M+D+h+m+s; return M+D+h+m; } public static curTime() { var n_data = new Date(); var timestamp = Date.parse(n_data.toString()); timestamp = timestamp / 1000; var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = date.getFullYear() + '-'; var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-'; var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' '; var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':'; var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes())+ ':'; var s = date.getSeconds() < 10 ? ':0'+date.getSeconds():date.getSeconds(); return Y+M+D+h+m+s; } }