tools.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { _decorator, assetManager, Component, ImageAsset, instantiate, Node, Prefab, resources, SpriteFrame, Texture2D } from 'cc';
  2. import { config } from './config';
  3. import { main } from './main';
  4. import { car_item_data, edit_game_config_data, model_item_data, userData } from './data';
  5. import { game } from './game/game';
  6. import { Util } from './util';
  7. import { http } from './http';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('tools')
  10. export class tools {
  11. public static levels:string[] = []
  12. public static tpl_list:model_item_data[] = []
  13. public static game_config:edit_game_config_data = null
  14. public static all_car_list:car_item_data[] =[]
  15. public static init(n:Node){
  16. tools.parent = n
  17. }
  18. public static playGame(call_back){
  19. resources.load(config.PREFAB.game,(err,prefab:Prefab)=>{
  20. let node = instantiate(prefab)
  21. node.parent = tools.parent
  22. node.getComponent(game).show(call_back)
  23. })
  24. }
  25. public static parent:Node = null;
  26. public static getBoxRandom(type:number){
  27. for (let index = 0; index < tools.game_config.box_list.length; index++) {
  28. const element = tools.game_config.box_list[index];
  29. if(element.type==type){
  30. return element
  31. }
  32. }
  33. }
  34. public static isBox(type:number){
  35. if(type<=5||type>=9){
  36. return true
  37. }
  38. return false
  39. }
  40. public static randomCoin(){
  41. return Util.getRandomInt(tools.game_config.scores.min,tools.game_config.scores.max)
  42. }
  43. public static loadRemoteImg(url:string,call,tag:number=-1){
  44. if(url==undefined) { return null }
  45. if(url.length<=0){ return null }
  46. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  47. if (!err && imageAsset2) {
  48. const texture = new Texture2D();
  49. texture.image = imageAsset2;
  50. let spFrame2 = new SpriteFrame();
  51. spFrame2.texture = texture;
  52. spFrame2.addRef()
  53. if(call) {
  54. call({"url":url,"sf":spFrame2,"tag":tag})
  55. }
  56. }
  57. });
  58. }
  59. // stype 1:注册 2:重新设置
  60. public static requestUserSetRegion(region_id:number,stype:number,cb) {
  61. let opt = {'region_id':region_id, 'stype':stype}
  62. http.post(config.API.user_set_region, opt, (err,d)=>{
  63. let nd = JSON.parse(d)
  64. if(nd.code === config.status.SUCCESS){
  65. if(cb!=null){
  66. cb(nd.content)
  67. }
  68. }
  69. })
  70. }
  71. }