tools.ts 3.4 KB

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