tools.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { _decorator, assetManager, Component, ImageAsset, instantiate, Label, Node, Prefab, resources, SpriteFrame, sys, Texture2D } from 'cc';
  2. import { config } from './config';
  3. import { car_item_data, edit_game_config_data, model_item_data, rankData, sysConfig, user_red_dot_data, userData } from './data';
  4. import { game } from './game/game';
  5. import { Util } from './util';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('tools')
  8. export class tools {
  9. public static platform = config.Platform.BROWSER
  10. public static levels:string[] = []
  11. public static tpl_list:model_item_data[] = []
  12. public static game_config:edit_game_config_data = null
  13. public static sys_config:sysConfig = null
  14. public static mine_rank_data:rankData = null
  15. public static all_car_list:car_item_data[] =[]
  16. public static all_car_map:Map<number,car_item_data> = new Map
  17. public static all_car_page_list = []
  18. public static init(n:Node){
  19. tools.parent = n
  20. }
  21. public static playGame(call_back){
  22. resources.load(config.PREFAB.game,(err,prefab:Prefab)=>{
  23. let node = instantiate(prefab)
  24. node.parent = tools.parent
  25. node.getComponent(game).show(call_back)
  26. })
  27. }
  28. public static parent:Node = null;
  29. public static getBoxRandom(type:number){
  30. for (let index = 0; index < tools.game_config.box_list.length; index++) {
  31. const element = tools.game_config.box_list[index];
  32. if(element.type==type){
  33. return element
  34. }
  35. }
  36. }
  37. public static isBox(type:number){
  38. if(type<=5||type>=9){
  39. return true
  40. }
  41. return false
  42. }
  43. public static randomCoin(){
  44. return Util.getRandomInt(tools.game_config.scores.min,tools.game_config.scores.max)
  45. }
  46. public static loadRemoteImg(url:string,call,tag:number=-1){
  47. if(url==undefined) { return null }
  48. if(url.length<=0){ return null }
  49. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  50. if (!err && imageAsset2) {
  51. const texture = new Texture2D();
  52. texture.image = imageAsset2;
  53. let spFrame2 = new SpriteFrame();
  54. spFrame2.texture = texture;
  55. spFrame2.addRef()
  56. if(call) {
  57. call({"url":url,"sf":spFrame2,"tag":tag})
  58. }
  59. }
  60. });
  61. }
  62. // 去除左边和右边空格
  63. public static trimLeftAndRightblank(str: string):string {
  64. const reg = /^\s+|\s+$/g;
  65. return str.replace(reg, '')
  66. }
  67. public static labelCutString(node:Node, str:string, length:number, is_text_center:boolean = true) {
  68. let lab_component = node.getComponent(Label)
  69. if(lab_component==null) { return }
  70. if(str.length>length) {
  71. lab_component.string = str.substring(0,length) + '...'
  72. lab_component.horizontalAlign = Label.HorizontalAlign.LEFT
  73. } else {
  74. lab_component.string = str
  75. if(is_text_center) {
  76. lab_component.horizontalAlign = Label.HorizontalAlign.CENTER
  77. }
  78. }
  79. }
  80. public static substringRankRegionName(node:Node,string:string) {
  81. if(string==undefined||string==null) { return "" }
  82. let lab_component = node.getComponent(Label)
  83. if(lab_component==null) { return }
  84. if(string.length>4) {
  85. string = string.substring(0,4)
  86. }
  87. lab_component.fontSize = 27
  88. if(string.length==2) {
  89. lab_component.lineHeight = 55
  90. } else if(string.length==3) {
  91. lab_component.lineHeight = 35
  92. } else {
  93. lab_component.lineHeight = 27
  94. }
  95. lab_component.string = string
  96. }
  97. }