Tools.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { _decorator, Component, Node, SpriteFrame, assetManager, Texture2D, Sprite, ImageAsset, isValid } from 'cc';
  2. import { Constant, xiaoxi_haoyou_status_type } from './constant';
  3. import GBoardChess from './Game/ChessGame/GBoardChess';
  4. import { GameMng } from './GameMng';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('Tools')
  7. export class Tools {
  8. public static cache: { [name: string]: SpriteFrame } = {};
  9. public static time(m){
  10. if(m<60){
  11. return m+"秒"
  12. }else{
  13. return m/60+"分"
  14. }
  15. }
  16. public static ms_time(s){
  17. var n_s = Math.floor(s/60)
  18. if(n_s<0){
  19. n_s = 0;
  20. }
  21. var n_s_str = n_s.toString()
  22. var n_m = s%60
  23. if(n_m<0){
  24. n_m = 0;
  25. }
  26. var n_m_str = n_m.toString()
  27. if(n_s<10&&n_s>=0){
  28. n_s_str = "0"+n_s
  29. }
  30. if(n_m<10&&n_m>=0){
  31. n_m_str = "0"+n_m
  32. }
  33. return n_s_str+":"+n_m_str
  34. }
  35. public static makeUrl(){
  36. return "ws://"+Constant.SEVER_INFO.IP+":"+Constant.SEVER_INFO.PORT+"/ws"
  37. }
  38. public static getShareObj():string{
  39. let roomId = 0;
  40. if(GameMng._userData.room!=null){
  41. roomId = GameMng._userData.room.roomid;
  42. }
  43. let firendOpenId = "";
  44. if(GameMng._userData.userid!=null&&GameMng._userData.userid!=undefined){
  45. firendOpenId = GameMng._userData.userid+"";
  46. }
  47. let s = "firendOpenId=" +firendOpenId+"&roomId="+roomId;
  48. let s_o = {"webpageUrl":"http://xiangqi.moyue2020.com/xint.html?"+s,
  49. "title":"楚汉对决,等你来战!",
  50. "description":"末将在!待主公一声令下,必将杀他个片甲不留...",
  51. };
  52. return JSON.stringify(s_o);
  53. }
  54. public static loadWeiXinHead(avatarurl:string,sf:Sprite){
  55. if(Tools.cache[avatarurl]!=null){
  56. sf.spriteFrame = Tools.cache[avatarurl]
  57. return
  58. }
  59. let obj = null;
  60. if(avatarurl.substring(avatarurl.length-3,avatarurl.length)!="png"){
  61. obj= {ext: '.png'}
  62. }
  63. assetManager.loadRemote<ImageAsset>(avatarurl,obj, (err, imageAsset) => {
  64. if (!err && imageAsset) {
  65. let spFrame = Tools.cache[avatarurl];
  66. if (!spFrame) {
  67. const texture = new Texture2D();
  68. texture.image = imageAsset;
  69. spFrame = new SpriteFrame();
  70. spFrame.texture = texture;
  71. imageAsset.addRef();
  72. Tools.cache[avatarurl] = spFrame; // 添加映射表记录
  73. sf.spriteFrame = spFrame
  74. }
  75. spFrame.addRef(); // 计数加1
  76. }
  77. });
  78. }
  79. releaseRemoteSprite(node: Node) {
  80. if (!isValid(node)) {
  81. return;
  82. }
  83. const sp = node.getComponent(Sprite) as Sprite;
  84. if (sp && sp.spriteFrame) {
  85. const spFrame = sp.spriteFrame;
  86. sp.spriteFrame.decRef(false); // 只把计数减1
  87. sp.spriteFrame = null;
  88. if (spFrame.refCount <= 0) {
  89. let texture = spFrame.texture as Texture2D;
  90. // 如果已加入动态合图,必须取原始的Texture2D
  91. if (spFrame.packable) {
  92. texture = spFrame.original?._texture as Texture2D;
  93. }
  94. if (texture) {
  95. delete Tools.cache[texture.image!._nativeUrl]; // 删除映射表记录
  96. texture.image?.decRef();
  97. texture.destroy();
  98. }
  99. spFrame.destroy();
  100. }
  101. }
  102. }
  103. public static getAvatarByIndex(index:number):string{
  104. if(index<=14&&index>0){
  105. return "PubImgs_avatar_avatar"+index;
  106. }
  107. return "PubImgs_avatar_avatar1";
  108. }
  109. public static getTongBiNumber(num:number):string{
  110. let _z = num + ""
  111. if(num>=10000){
  112. _z = (num / 10000)+""
  113. if(_z.length>=3){
  114. _z = _z.substring(0,3)
  115. }else{
  116. _z = _z.substring(0,_z.length)
  117. }
  118. _z = _z+"万"
  119. }
  120. return _z;
  121. }
  122. public static getUserStatusNameByStatus(status:xiaoxi_haoyou_status_type):string{
  123. let name = ""
  124. switch ( status) {
  125. case xiaoxi_haoyou_status_type.deng_dai_kai_ju:
  126. name = "等待对局";
  127. break;
  128. case xiaoxi_haoyou_status_type.xian_guang:
  129. name = "闲逛";
  130. break;
  131. case xiaoxi_haoyou_status_type.dui_zhan:
  132. name = "正在对战";
  133. break;
  134. }
  135. return name;
  136. }
  137. public static getChessPos(){
  138. let tempArray = []
  139. let temp_Array = []
  140. for (const key in GBoardChess.instance.pos.squares) {
  141. if(GBoardChess.instance.pos.squares[key]!=0){
  142. tempArray.push({"sq": key,"pc":GBoardChess.instance.pos.squares[key],"x":0,"y":0})
  143. }
  144. }
  145. for (let index = 0; index < tempArray.length; index++) {
  146. const element = tempArray[index];
  147. var node__ = GBoardChess.instance.imgSquares[GBoardChess.instance.flipped(element.sq)];
  148. element.x = node__.position.x
  149. element.y = node__.position.y
  150. temp_Array.push(element)
  151. }
  152. return temp_Array
  153. }
  154. }