Formula.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // import GameMath from './GameMath';
  2. // import { gameMethod } from '../cfg/gameMethod';
  3. // import { gameCfg } from '../cfg/GameCfg';
  4. // import Config from '../Config';
  5. // import { Hdcid } from '../cfg/XyS';
  6. import { gameMethod } from '../common/gameMethod';
  7. import GameMath from './GameMath';
  8. import { I18n } from './I18nUtil';
  9. // import { WorkType } from '../cfg/XyWork';
  10. // 本地化,格式化文字
  11. export function localized(key: string, ...args): string {
  12. return stringFormat(key, args)
  13. // if (key == null || key.length == 0) {
  14. // return stringFormat(key, args)
  15. // }
  16. // let _lang = Config.lang
  17. // let data = GameCfg.lang.getItem(key)
  18. // if (data == null) {
  19. // return stringFormat(key, args)
  20. // }
  21. // let result = data[_lang]
  22. // if (result == null) {
  23. // return stringFormat(key, args)
  24. // }
  25. // return stringFormat(result, args)
  26. }
  27. // // 获取client配置表
  28. // export function getWord(str: string, ...args): string {
  29. // if (gameMethod.isEmpty(gameCfg.langClient.getItem(str))) {
  30. // return str
  31. // }
  32. // let result = gameCfg.langClient.getItem(str)[Config.lang]
  33. // return stringFormat(result, args)
  34. // }
  35. // // 获取 clientText.csv配置表文字
  36. // export function getText(str: string, ...args) {
  37. // let result = gameCfg.langClientText.getItem(str)[Config.lang]
  38. // return stringFormat(result, args)
  39. // }
  40. export function stringFormat(str: string, list: any[]): string {
  41. if (str == null) { return "" }
  42. let result = str
  43. list.forEach((element, index) => {
  44. result = result.replace(`%{${index}}`, element)
  45. })
  46. return result
  47. }
  48. // /**
  49. // * 显示倒计时
  50. // * @param time 倒计时结束时间戳
  51. // * @param act 活动key
  52. // * @param hdcid 活动Hdcid
  53. // * @param type 倒计时展示类型
  54. // * @returns
  55. // */
  56. // export function showCdTime(time: number, act: WorkType, hdcid: string, type: string = '1') {
  57. // let cdTime: number = 0
  58. // cdTime = time - GameDataCenter.timeModel.sevTime
  59. // if (cdTime >= 0) {
  60. // return this.showTime(cdTime, type)
  61. // } else {
  62. // // 防止延迟会一直重复发送请求
  63. // if (GameDataCenter.timeModel.getIsKeyAdok(act)) {
  64. // GameDataCenter.timeModel.sendAdokKey(time, act, hdcid)
  65. // }
  66. // }
  67. // }
  68. /**
  69. * 秒级别(10位数)
  70. * @param time 时间戳
  71. * @param type 类型
  72. * 1: 大于一天,显示x天x时;小于一天,显示00:00:00(天数不足,补0)
  73. * 2: 1天36分(只显示最前面的两位)
  74. * 3: 小于一小时,显示xx:xx(分秒)
  75. * 4: 只显示最高单位 大于1天 显示1天 大于1小时 显示1时
  76. * @returns
  77. */
  78. export function showTime(time: number, type: string = "1"): string {
  79. if (time.toString().length == 13) {
  80. time = Math.floor(time / 1000)
  81. }
  82. if (time <= 0) {
  83. return type == "3" ? "00:00" : "00:00:00"
  84. }
  85. let tTime = ''
  86. let day: number = 0
  87. let hour: number = 0
  88. let minute: number = 0
  89. let second: number = 0
  90. day = Math.floor(time / (24 * 60 * 60))
  91. hour = Math.floor((time % (24 * 60 * 60)) / (60 * 60))
  92. minute = Math.floor((time % (60 * 60)) / 60)
  93. second = Math.floor(time % 60)
  94. if (type == "1") {
  95. if (day > 0) {
  96. tTime = I18n.getI18nText('common_timeformat_dh', day, hour)
  97. } else if (hour > 0) {
  98. tTime = `${GameMath.addZero(day * 24 + hour)}:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  99. // tTime = `${GameMath.addZero(day * 24 + hour)}时${GameMath.addZero(minute)}分`
  100. } else if (minute > 0) {
  101. tTime = `00:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  102. // tTime = `${GameMath.addZero(minute)}分${GameMath.addZero(second)}秒`
  103. } else {
  104. tTime = `00:00:${GameMath.addZero(second)}`
  105. // tTime = `${GameMath.addZero(second)}秒`
  106. }
  107. } else if (type == "2") {
  108. if (day > 0) {
  109. tTime = I18n.getI18nText('common_timeformat_dh', day, hour)
  110. } else if (hour > 0) {
  111. tTime = `${GameMath.addZero(day * 24 + hour)}时${GameMath.addZero(minute)}分`
  112. } else if (minute > 0) {
  113. tTime = `${GameMath.addZero(minute)}分${GameMath.addZero(second)}秒`
  114. } else {
  115. tTime = `${GameMath.addZero(second)}秒`
  116. }
  117. } else if (type == '3') {
  118. if (day > 0) {
  119. tTime = I18n.getI18nText('common_timeformat_dh', day, hour)
  120. } else if (hour > 0) {
  121. tTime = `${GameMath.addZero(day * 24 + hour)}:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  122. // tTime = `${GameMath.addZero(day * 24 + hour)}时${GameMath.addZero(minute)}分`
  123. } else if (minute > 0) {
  124. tTime = `${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  125. // tTime = `${GameMath.addZero(minute)}分${GameMath.addZero(second)}秒`
  126. } else {
  127. tTime = `00:${GameMath.addZero(second)}`
  128. // tTime = `${GameMath.addZero(second)}秒`
  129. }
  130. } else if (type == '4') {
  131. if (day > 1) {
  132. tTime = I18n.getI18nText('common_timeformat_d', day)
  133. } else if (day > 0 || hour > 0) {
  134. tTime = I18n.getI18nText('common_timeformat_h', day * 24 + hour)
  135. } else if (minute > 0) {
  136. tTime = I18n.getI18nText('common_timeformat_m', minute)
  137. } else {
  138. tTime = I18n.getI18nText('common_timeformat_s', second)
  139. }
  140. } else if (type == '5') {
  141. if (day > 0) {
  142. tTime = `${GameMath.addZero(day * 24 + hour)}:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  143. } else if (hour > 0) {
  144. tTime = `${GameMath.addZero(day * 24 + hour)}:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  145. } else if (minute > 0) {
  146. tTime = `00:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  147. } else {
  148. tTime = `00:00:${GameMath.addZero(second)}`
  149. }
  150. } else if (type == '6') {
  151. if (day > 0) {
  152. tTime = I18n.getI18nText('common_timeformat_dhms', day, hour, minute, second);// x天x小时x分x秒
  153. } else if (hour > 0) {
  154. tTime = I18n.getI18nText('common_timeformat_hms', hour, minute, second);// x小时x分x秒
  155. } else if (minute > 0) {
  156. tTime = I18n.getI18nText('common_timeformat_ms', minute, second);// x分x秒
  157. } else {
  158. tTime = I18n.getI18nText('common_timeformat_s', second);// x秒
  159. }
  160. } else if (type == '7') {
  161. if (day > 0) {
  162. tTime = I18n.getI18nText('common_timeformat_dhms_2', day, GameMath.addZero(hour), GameMath.addZero(minute), GameMath.addZero(second))
  163. } else if (hour > 0) {
  164. tTime = `${GameMath.addZero(hour)}:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  165. } else if (minute > 0) {
  166. tTime = `00:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  167. } else {
  168. tTime = `00:00:${GameMath.addZero(second)}`
  169. }
  170. } else if (type == '8') { // 显示两个单位
  171. if (day > 0) {
  172. tTime = I18n.getI18nText('common_timeformat_dh', day, hour)
  173. } else if (hour > 0) {
  174. tTime = `${GameMath.addZero(day * 24 + hour)}:${GameMath.addZero(minute)}`
  175. } else if (minute > 0) {
  176. tTime = `${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  177. } else {
  178. tTime = `00:${GameMath.addZero(second)}`
  179. }
  180. }
  181. return tTime
  182. }
  183. export function showTimeYMD(shijianchuo: number, type: number = 0): string {
  184. if (shijianchuo.toString().length == 10) {
  185. shijianchuo = shijianchuo * 1000
  186. }
  187. var time = new Date(shijianchuo);
  188. var y = time.getFullYear()
  189. var m = time.getMonth() + 1
  190. var d = time.getDate()
  191. var h = time.getHours()
  192. var mm = time.getMinutes()
  193. var s = time.getSeconds()
  194. if (type == 0) {
  195. return `${y}-${GameMath.addZero(m)}-${GameMath.addZero(d)} ${GameMath.addZero(h)}:${GameMath.addZero(mm)}:${GameMath.addZero(s)}`
  196. } else if (type == 1) {
  197. return `${GameMath.addZero(y)}-${GameMath.addZero(m)}-${GameMath.addZero(d)}`
  198. } else if (type == 2) {
  199. return `${GameMath.addZero(y)}.${GameMath.addZero(m)}.${GameMath.addZero(d)}`
  200. } else if (type == 3) {
  201. return `${GameMath.addZero(m)}-${GameMath.addZero(d)} ${GameMath.addZero(h)}:${GameMath.addZero(mm)}`
  202. } else if (type == 4) {
  203. return `${GameMath.addZero(m)}/${GameMath.addZero(d)}`
  204. } else if (type == 5) {
  205. return `${GameMath.addZero(y)}/${GameMath.addZero(m)}/${GameMath.addZero(d)}`
  206. } else if (type == 6) {//设置界面时间显示
  207. return `${GameMath.addZero(y)}-${GameMath.addZero(m)}-${GameMath.addZero(d)} ${GameMath.addZero(h)}:${GameMath.addZero(mm)}:${GameMath.addZero(s)}`
  208. } else if (type == 7) {
  209. return `${GameMath.addZero(h)}:${GameMath.addZero(mm)}`
  210. } else if (type == 8) {
  211. return I18n.getI18nText('common_timeformat_ymd', y, m, d)
  212. } else if (type == 9) {
  213. return I18n.getI18nText('common_timeformat_md', m, d)
  214. } else if (type == 10) {
  215. return `${y}/${m}/${d}`
  216. } else if (type == 11) { // 00:00:00格式
  217. return `${GameMath.addZero(h)}:${GameMath.addZero(mm)}:${GameMath.addZero(s)}`
  218. } else if (type == 12) {
  219. return `${GameMath.addZero(y)}/${GameMath.addZero(m)}/${GameMath.addZero(d)} ${GameMath.addZero(h)}:${GameMath.addZero(mm)}:${GameMath.addZero(s)}`
  220. } else if (type == 13) {
  221. return `${GameMath.addZero(m)}/${GameMath.addZero(d)} ${GameMath.addZero(h)}:${GameMath.addZero(mm)}:${GameMath.addZero(s)}`
  222. }
  223. }
  224. export function showTimeWord(time: number, type: string = "1"): string {
  225. let tTime = ''
  226. let month: number = 0
  227. let day: number = 0
  228. let hour: number = 0
  229. let minute: number = 0
  230. let second: number = 0
  231. month = Math.floor(time / (30 * 24 * 60 * 60))
  232. day = Math.floor(time / (24 * 60 * 60))
  233. hour = Math.floor((time % (24 * 60 * 60)) / (60 * 60))
  234. minute = Math.floor((time % (60 * 60)) / 60)
  235. second = Math.floor(time % 60)
  236. if (type == "1") {
  237. if (month > 0) {
  238. tTime = I18n.getI18nText('common_timeword_4', month)
  239. } else if (day > 0) {
  240. tTime = I18n.getI18nText('common_timeword_3', day)
  241. } else if (hour > 0) {
  242. tTime = I18n.getI18nText('common_timeword_2', hour)
  243. } else if (minute > 0) {
  244. tTime = I18n.getI18nText('common_timeword_1', minute)
  245. } else {
  246. tTime = I18n.getI18nText('common_timeword_5')
  247. }
  248. } else if (type == "2") {
  249. if (month > 0) {
  250. tTime = I18n.getI18nText('common_timeformat_mdhms_2', month, day, GameMath.addZero(hour), GameMath.addZero(minute), GameMath.addZero(second))
  251. } else if (day > 0) {
  252. tTime = I18n.getI18nText('common_timeformat_dhms_2', day, GameMath.addZero(hour), GameMath.addZero(minute), GameMath.addZero(second))
  253. } else if (hour > 0) {
  254. tTime = `${hour}:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  255. } else if (minute > 0) {
  256. tTime = `00:${GameMath.addZero(minute)}:${GameMath.addZero(second)}`
  257. } else if (second > 0) {
  258. tTime = `00:00:${GameMath.addZero(second)}`
  259. } else {
  260. tTime = `00:00:00`
  261. }
  262. } else {
  263. tTime = I18n.getI18nText('common_timeformat_unknowtype')
  264. }
  265. return tTime
  266. }
  267. export function IsSameDay(timestamp1: number, timestamp2: number) {
  268. const date1 = new Date(timestamp1 * 1000); // 转化为毫秒时间戳
  269. const date2 = new Date(timestamp2 * 1000);
  270. return (
  271. date1.getFullYear() === date2.getFullYear() &&
  272. date1.getMonth() === date2.getMonth() &&
  273. date1.getDate() === date2.getDate()
  274. );
  275. }
  276. // 阿拉伯数字转中文
  277. // export function chineseByNumber(number: number): string {
  278. // const chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
  279. // const chineseUnits = ['', '十', '百', '千', '万'];
  280. // if (number <= 10) {
  281. // return chineseNumbers[number];
  282. // } else {
  283. // const numberString = number.toString();
  284. // let result = ' ';
  285. // for (let i = 0; i < numberString.length; i++) {
  286. // const digit = parseInt(numberString[i]);
  287. // if (digit !== 0) {
  288. // if (numberString.length === 2 && i === 0 && digit === 1) {
  289. // result += chineseUnits[numberString.length - i - 1];
  290. // } else {
  291. // result += chineseNumbers[digit] + chineseUnits[numberString.length - i - 1];
  292. // }
  293. // } else {
  294. // if (i === 0 || numberString[i - 1] !== '0') {
  295. // result += chineseNumbers[digit];
  296. // }
  297. // }
  298. // }
  299. // return result;
  300. // }
  301. // }
  302. export class FormulaCom {
  303. // return random int [min,max]
  304. static random(min: number, max: number) {
  305. return Math.floor(Math.random() * (max - min + 1) + min)
  306. }
  307. static uuid(len1, len2) {
  308. let timestamp = (new Date()).valueOf();
  309. return FormulaCom.random(Math.pow(10, len1 - 1) - 1, Math.pow(10, len1)).toString() + timestamp % (Math.pow(10, len2))
  310. }
  311. static time: number = 0
  312. static costTime(type: number) {
  313. if (type == 1) {
  314. this.time = Date.now()
  315. } else if (type == 2) {
  316. console.log(`cost ${Date.now() - this.time}ms`)
  317. }
  318. }
  319. //随机抽取元素不重复
  320. static getRandomArrayElements<T>(arr: Array<T>, count: number): Array<T> {
  321. count = Math.min(count, arr.length)
  322. var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
  323. while (i-- > min) {
  324. index = Math.floor((i + 1) * Math.random());
  325. temp = shuffled[index];
  326. shuffled[index] = shuffled[i];
  327. shuffled[i] = temp;
  328. }
  329. return shuffled.slice(min);
  330. }
  331. // /**
  332. // * 一个时间点 距离今天相差几天 以1开始算 今天算1天 今天之后的算0天
  333. // * @param str
  334. // */
  335. // static passDay(str: number): number {
  336. // let Dates = new Date(GameDataCenter.timeModel.sevTime * 1000).setHours(0, 0, 0, 0);//获取今天0点0分0秒0毫秒。
  337. // let checkTime = new Date(str * 1000).setHours(0, 0, 0, 0);//获取当天0点0分0秒0毫秒。
  338. // if (Dates < checkTime) {
  339. // return 0
  340. // }
  341. // return Math.abs(Math.floor((Dates - checkTime) / 86400000)) + 1;
  342. // }
  343. // a>b则返回>0 a=b则返回0 a<b则返回<0
  344. static compareVersion(versionA: string, versionB: string): number {
  345. var vA = versionA.split('.')
  346. var vB = versionB.split('.')
  347. for (var i = 0; i < vA.length; ++i) {
  348. var a = parseInt(vA[i])
  349. var b = 0 //parseInt(vB[i] || 0)
  350. if (vB[i]) {
  351. b = parseInt(vB[i])
  352. }
  353. if (a === b) {
  354. continue
  355. }
  356. else {
  357. return a - b
  358. }
  359. }
  360. if (vB.length > vA.length) {
  361. return -1
  362. }
  363. else {
  364. return 0
  365. }
  366. }
  367. static getStrLength(str: string): number {
  368. let a = 0;
  369. for (var i = 0; i < str.length; i++) {
  370. if (str.charCodeAt(i) > 255)
  371. a += 2;//按照预期计数增加2
  372. else
  373. a++;
  374. }
  375. return a;
  376. }
  377. // 限制(默认16个字符) 中文2 英文1
  378. static getLimitStr(str: string, limit: number = 16): string {
  379. let a = 0;
  380. for (var i = 0; i < str.length; i++) {
  381. if (str.charCodeAt(i) > 255)
  382. a += 2;//按照预期计数增加2
  383. else
  384. a++;
  385. if (a > limit) {
  386. return str.slice(0, i)
  387. }
  388. }
  389. return str
  390. }
  391. static isOverLimit(str: string, limit: number = 16): boolean {
  392. let a = 0;
  393. for (var i = 0; i < str.length; i++) {
  394. if (str.charCodeAt(i) > 255)
  395. a += 2;//按照预期计数增加2
  396. else
  397. a++;
  398. if (a > limit) {
  399. return true
  400. }
  401. }
  402. return false
  403. }
  404. // 多个对象在界面上的居中展示,需要box的锚点在中心 example: 3个道具 就是 -50 0 50
  405. // index以0为初始
  406. static getPosInBox(index: number, total: number, bgWidth: number, itemWidth: number): number {
  407. let space = total == 1 ? 0 : (((bgWidth - itemWidth * total) / (total - 1) + itemWidth))
  408. let posx = 0
  409. posx = -((total - 1) * (space / 2)) + index * (space)
  410. return posx
  411. }
  412. // static isMinGan(str: string): boolean {
  413. // str = str.replace("\\u00A0", "") //去空
  414. // if (GameCfg.words.minGan.pool != null && str.length > 0) {
  415. // for (let i in GameCfg.words.minGan.pool) {
  416. // let _name = GameCfg.words.minGan.pool[i].name
  417. // if (str.toLowerCase().indexOf(_name.toLowerCase()) != -1) {
  418. // return true
  419. // }
  420. // }
  421. // }
  422. // return false
  423. // }
  424. // 提取包含 <%t>格式的字段
  425. static getExecStrs(str): string[] {
  426. // var reg = /\<\%(.+?)\>/g
  427. var reg = /\<(.+?)\>/g
  428. var list = []
  429. var result = null
  430. do {
  431. result = reg.exec(str)
  432. result && list.push(result[1])
  433. } while (result)
  434. return list
  435. }
  436. // // 将文字中的<%t12648673123> 转化为时间
  437. // // 返回 [解析后的字符串,时间戳,时间戳...]
  438. // static textAnalysis(text: string): string[] {
  439. // // list列表 ["t12323451514","t145132154"]
  440. // let list = FormulaCom.getExecStrs(text)
  441. // let timeList = []
  442. // let result = []
  443. // let resultText = text
  444. // list.forEach(element => {
  445. // if (element[0] == "t") {
  446. // // 去除英文,保留数字
  447. // timeList.push(element.replace(/[^\d]/g, ''))
  448. // }
  449. // });
  450. // timeList.forEach(time => {
  451. // let tag = "<%t" + time + ">"
  452. // // resultText = resultText.replace(tag, new Date(time).toTimeString())
  453. // resultText = resultText.replace(tag, showTimeYMD(time))
  454. // })
  455. // result.push(resultText)
  456. // timeList.forEach(time => {
  457. // result.push(time)
  458. // });
  459. // return result
  460. // }
  461. /** 让动画在指定区间内循环 */
  462. static loopAtFrame(te: sp.spine.TrackEntry, sFrame: number, eFrame: number) {
  463. if (te.animationStart === 0) {
  464. te.animationStart = sFrame / 30; // 30是Spine动画的帧率
  465. te.animationEnd = eFrame / 30;
  466. }
  467. }
  468. /**
  469. * 停止在指定帧。1是开始,-1是最后
  470. * @param frame 帧数
  471. */
  472. static stopAtFrame(spineComp: sp.Skeleton, frame: number) {
  473. const te = spineComp.getCurrent(0) as sp.spine.TrackEntry;
  474. // 算出帧对应的时间
  475. let time;
  476. if (frame === -1) {
  477. time = te.animation.duration; // 最后一帧
  478. } else if (frame > 1) {
  479. time = (frame - 1) / 30; // 根据帧率算出对应时间,spine帧率是30
  480. } else {
  481. time = 0; // 首帧
  482. }
  483. // 对time作限制
  484. if (time < 0) time = 0;
  485. if (time >= te.animation.duration) time = te.animation.duration - 0.01; // 太精确的话,动画会停在首帧,所以要减一点
  486. te.timeScale = 0; // 让动画停止
  487. te.trackTime = time;
  488. }
  489. /**
  490. * 把服务器时间戳 改为本地时间戳
  491. * @param time 需要转换的时间戳
  492. * @param utcMin 服务器时区(按分钟计)
  493. // -300 西5区美国 -5*60 GMT-5
  494. // 0
  495. // 480 东8区北京 8*60
  496. */
  497. static getUtcTime(time: number, utcHour: number): number {
  498. let utcMin = utcHour * 60
  499. //本地时间类
  500. let localtime = new Date();
  501. //本地时区针对标准时区的偏移
  502. let pym = localtime.getTimezoneOffset()
  503. //时间转换为指定时区时间
  504. time += (pym + utcMin) * 60
  505. return time
  506. }
  507. static getRandomTag(): string {
  508. let outString: string = '';
  509. let inOptions: string = 'abcdefghijklmnopqrstuvwxyz0123456789';
  510. for (let i = 0; i < 16; i++) {
  511. outString += inOptions.charAt(Math.floor(Math.random() * inOptions.length));
  512. }
  513. return outString;
  514. }
  515. static getDistance(x1: number, y1: number, x2: number, y2: number) {
  516. var a = x2 - x1
  517. var b = y2 - y1
  518. return Math.sqrt(a * a + b * b)
  519. }
  520. /**
  521. * 根据锚点重新计算中心点世界坐标坐标
  522. * @param pos 锚点的世界坐标
  523. * @param node 节点
  524. * @returns
  525. */
  526. static fixAnchor(pos: cc.Vec2, node: cc.Node): cc.Vec2 {
  527. let x = pos.x - node.anchorX * node.width + node.width / 2
  528. let y = pos.y - node.anchorY * node.height + node.height / 2
  529. return new cc.Vec2(x, y)
  530. }
  531. /**
  532. * 获得某个节点中心点的世界坐标
  533. * @param node
  534. * @returns
  535. */
  536. static getWorldCenterPos(node: cc.Node): cc.Vec2 {
  537. return this.fixAnchor(this.getWorldPos(node), node)
  538. }
  539. static getWorldPos(node: cc.Node): cc.Vec2 {
  540. if (gameMethod.isEmpty(node)) return cc.v2(0, 0);
  541. return node.convertToWorldSpaceAR(cc.Vec2.ZERO)
  542. }
  543. static setPosByWorldPos(node: cc.Node, pos: cc.Vec2) {
  544. if (gameMethod.isEmpty(node)) return;
  545. let _pos = node.parent.convertToNodeSpaceAR(pos)
  546. node.x = _pos.x
  547. node.y = _pos.y
  548. }
  549. //获取node位于target的坐标相对坐标
  550. static getPositionInView(node: cc.Node, target: cc.Node): { x: number, y: number } {
  551. let worldPos = node.parent.convertToWorldSpaceAR(node.position);
  552. let viewPos = target.convertToNodeSpaceAR(worldPos);
  553. return viewPos;
  554. }
  555. // 获取玩家最后一次登录时间
  556. static getUserState(lastadok: number, sevTime: number) {
  557. let userState: string = ''
  558. let time = Math.floor((sevTime - lastadok) / 60)
  559. if (time < 6) {
  560. userState = I18n.getI18nText('common_userLoginState_1')
  561. } else if (time < 60) {
  562. userState = I18n.getI18nText('common_timeword_1', time)
  563. } else if (time < 24 * 60) {
  564. userState = I18n.getI18nText('common_timeword_2', Math.floor(time / 60))
  565. } else {
  566. userState = I18n.getI18nText('common_timeword_3', Math.floor((time / 60) / 24))
  567. }
  568. return userState
  569. }
  570. // 已知圆心,半径,角度,求坐标
  571. static getPosbyAngle(centerX: number, centerY: number, range: number, angle: number): { x: number, y: number } {
  572. let radians = this.getRadiansByAngle(angle)
  573. let dx = Math.cos(radians) * range
  574. let dy = Math.sin(radians) * range
  575. return { x: centerX + dx, y: centerY + dy }
  576. }
  577. // 已知坐标求角度
  578. static getAngleByPos(x1: number, y1: number, x2: number, y2: number): number {
  579. if (x1 == x2 && y1 == y2) {
  580. return 0
  581. }
  582. let degree = Math.atan2(y2 - y1, x2 - x1)
  583. return degree * 180 / Math.PI
  584. }
  585. //求两个点的角度
  586. static getAngle(startPos: cc.Vec2, endPos: cc.Vec2): number {
  587. //计算出朝向
  588. var dx = endPos.x - startPos.x;
  589. var dy = endPos.y - startPos.y;
  590. var dir = cc.v2(dx, dy);
  591. //根据朝向计算出夹角弧度
  592. var angle = dir.signAngle(cc.v2(1, 0));
  593. //将弧度转换为欧拉角
  594. var degree = angle / Math.PI * 180;
  595. return -degree
  596. }
  597. // 根据角度获取弧度
  598. static getRadiansByAngle(angle: number) {
  599. return angle / 180 * Math.PI
  600. }
  601. // 根据弧度获取角度
  602. static getAngleByRadians(radians: number) {
  603. return radians * 180 / Math.PI
  604. }
  605. static obj2Array<T>(object: { [s: string]: T }): T[] {
  606. if (typeof object != "object") {
  607. return []
  608. }
  609. return Object.values(object)
  610. // let array = []
  611. // for (const key in object) {
  612. // array.push(object[key])
  613. // }
  614. // return array
  615. }
  616. static strDLength(str: string): number {
  617. let rStr = str.replace(/[^\x00-\xff]/g, '')//双字节字符:(包括汉字在内,可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1))
  618. return str.length - rStr.length
  619. }
  620. // 首字母大写
  621. static firstUpCase(str: string) {
  622. return str[0].toUpperCase() + str.substring(1)
  623. }
  624. // 判断字符长度
  625. static getStrCharacterLength(str): number {
  626. let patternChinese = new RegExp("[\u4E00-\u9FA5]+"); // 中文
  627. let leng = 0
  628. for (let index = 0; index < str.length; index++) {
  629. if (patternChinese.test(str[index])) {
  630. leng += 2
  631. } else {
  632. leng += 1
  633. }
  634. }
  635. return leng
  636. }
  637. //根据索引获取当前行列数
  638. // index 当前索引值(从0开始) elementsPerRow 一行有几个
  639. static getRowAndColumnFromIndex(index: number, columnCount: number) {
  640. if (columnCount < 1) {
  641. throw new Error('Column count must be a positive integer.');
  642. }
  643. const rowIndex = Math.floor(index / columnCount);
  644. const columnIndex = index % columnCount;
  645. return { rowIndex, columnIndex };
  646. }
  647. // 对数组按数量分组
  648. static groupTwoByTwo<T>(input: T[], count: number): T[][] {
  649. const result: T[][] = [];
  650. for (let i = 0; i < input.length; i += count) {
  651. if (gameMethod.isEmpty(input[i]) || gameMethod.isEmpty(input[i + 1])) {
  652. console.error("该数组分配不足,检查数组长度")
  653. }
  654. result.push([input[i], input[i + 1]]);
  655. }
  656. return result;
  657. }
  658. }
  659. // 阿拉伯数字转中文
  660. export function chineseByNumber(number: number): string {
  661. const chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
  662. const chineseUnits = ['', '十', '百', '千', '万'];
  663. if (number <= 10) {
  664. return chineseNumbers[number];
  665. } else {
  666. const numberString = number.toString();
  667. let result = ' ';
  668. for (let i = 0; i < numberString.length; i++) {
  669. const digit = parseInt(numberString[i]);
  670. if (digit !== 0) {
  671. if (numberString.length === 2 && i === 0 && digit === 1) {
  672. result += chineseUnits[numberString.length - i - 1];
  673. } else {
  674. result += chineseNumbers[digit] + chineseUnits[numberString.length - i - 1];
  675. }
  676. } else {
  677. if (i === 0 || numberString[i - 1] !== '0') {
  678. result += chineseNumbers[digit];
  679. }
  680. }
  681. }
  682. return result;
  683. }
  684. }