tools.ts 3.5 KB

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