GameUtil.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { sys,tween,Node,Vec3, UITransform } from "cc";
  2. export default class GameUtil {
  3. private static moveLocPos:Vec3=new Vec3();
  4. public static moveToPos(moveNode:Node, worldPos:Vec3,callFun:Function,time:number=0.2,_scale:number=1){
  5. this.moveLocPos=moveNode.parent.getComponent(UITransform).convertToNodeSpaceAR(worldPos);
  6. tween(moveNode)
  7. .to(time, { position: this.moveLocPos,scale:new Vec3(_scale,_scale,_scale)})
  8. .call(()=>{
  9. if(callFun)callFun();
  10. })
  11. .start()
  12. }
  13. //删除指定元素
  14. public static arrDeleteObj(arr,deleteobj){
  15. let index=arr.indexOf(deleteobj);
  16. if(index!=-1)
  17. arr.splice(index,1);
  18. }
  19. /**
  20. * 获取随机数
  21. * @param {Number} min 最小值
  22. * @param {Number} max 最大值
  23. * @returns
  24. */
  25. public static Random(min: number, max: number) {
  26. return Math.random() * (max - min) + min;
  27. }
  28. /**
  29. * 随机函数
  30. * @param des minValue到maxValue+1可以取值为[minValue,maxValue]
  31. */
  32. public static RandomRangeInt(minValue: number, maxValue: number): number {
  33. let r = maxValue - minValue;
  34. let rand = Math.random();
  35. return (minValue + Math.floor(rand * r));
  36. }
  37. public static SystemTime(): number {
  38. return sys.now();
  39. }
  40. private static _sms: number = 0;
  41. public static SystemMillionSecond(): number {
  42. if (GameUtil._sms == 0) {
  43. GameUtil.UpdateSystemTime();
  44. }
  45. return GameUtil._sms;
  46. }
  47. public static UpdateSystemTime(): void {
  48. GameUtil._sms = new Date().getTime();
  49. }
  50. public static SystemTimeHHMMSS(): string {
  51. var s = this.SystemMillionSecond();
  52. var ss: string = s.toString();
  53. ss = ss.substr(ss.length - 4);
  54. return GameUtil.FormatTime_HHMMSS(s) + " " + ss;
  55. }
  56. public static SystemDay(): number {
  57. var t = this.GetDay(GameUtil.SystemMillionSecond());
  58. return t;
  59. }
  60. public static GetDay(time: number) {
  61. var t = time / 1000;
  62. t = t / 60;
  63. t = t / 60;
  64. t = t + 8;
  65. t = t / 24;
  66. t = Math.floor(t);
  67. return t;
  68. }
  69. public static FormatTime_HHMMSS(millionSecond: number): string {
  70. let timeGap = millionSecond / 1000;
  71. let hour = (Array(2).join('0') + Math.floor(timeGap / 3600)).slice(-2);
  72. let min = (Array(2).join('0') + Math.floor((timeGap % 3600) / 60)).slice(-2);
  73. let sec = (Array(2).join('0') + Math.floor(timeGap) % 60).slice(-2);
  74. if (hour == "00") {
  75. return min + ":" + sec;
  76. } else {
  77. return hour + ":" + min + ":" + sec;
  78. }
  79. }
  80. public static FormatTime_DDHHMMSS(millionSecond: number): string {
  81. var dval = millionSecond / 1000;
  82. var ss = dval % 60;
  83. dval = Math.floor(dval / 60);
  84. var mm = dval % 60;
  85. dval = Math.floor(dval / 60);
  86. var hh = dval % 24;
  87. dval = Math.floor(dval / 24);
  88. var dd = dval;
  89. var ret = "";
  90. if (dd > 0) {
  91. ret = dd + "天";
  92. if (hh > 0) {
  93. ret += hh + "小时";
  94. }
  95. } else {
  96. if (hh > 0) {
  97. ret += hh + "小时";
  98. }
  99. if (mm > 0) {
  100. ret += mm + "分";
  101. }
  102. // else if (ss > 0) {
  103. // ret += "零"
  104. // }
  105. if (ss > 0) {
  106. ret += Math.floor(ss) + "秒";
  107. }
  108. }
  109. return ret;
  110. }
  111. public static timestampToTime() {
  112. var n_data = new Date();
  113. var timestamp = Date.parse(n_data.toString());
  114. timestamp = timestamp / 1000;
  115. var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  116. //var Y = date.getFullYear() + '-';
  117. var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
  118. var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
  119. var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
  120. var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes());
  121. //var s = date.getSeconds() < 10 ? ':0'+date.getSeconds():date.getSeconds();
  122. //return Y+M+D+h+m+s;
  123. return M+D+h+m;
  124. }
  125. public static curTime() {
  126. var n_data = new Date();
  127. var timestamp = Date.parse(n_data.toString());
  128. timestamp = timestamp / 1000;
  129. var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  130. var Y = date.getFullYear() + '-';
  131. var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
  132. var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
  133. var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
  134. var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes())+ ':';
  135. var s = date.getSeconds() < 10 ? ':0'+date.getSeconds():date.getSeconds();
  136. return Y+M+D+h+m+s;
  137. }
  138. }