import { _decorator, Component, Node, SpriteFrame, assetManager, Texture2D, Sprite, ImageAsset, isValid } from 'cc'; import { Constant, xiaoxi_haoyou_status_type } from './constant'; import GBoardChess from './Game/ChessGame/GBoardChess'; import { GameMng } from './GameMng'; const { ccclass, property } = _decorator; @ccclass('Tools') export class Tools { public static cache: { [name: string]: SpriteFrame } = {}; public static time(m){ if(m<60){ return m+"秒" }else{ return m/60+"分" } } public static ms_time(s){ var n_s = Math.floor(s/60) if(n_s<0){ n_s = 0; } var n_s_str = n_s.toString() var n_m = s%60 if(n_m<0){ n_m = 0; } var n_m_str = n_m.toString() if(n_s<10&&n_s>=0){ n_s_str = "0"+n_s } if(n_m<10&&n_m>=0){ n_m_str = "0"+n_m } return n_s_str+":"+n_m_str } public static makeUrl(){ return "ws://"+Constant.SEVER_INFO.IP+":"+Constant.SEVER_INFO.PORT+"/ws" } public static getShareObj():string{ let roomId = 0; if(GameMng._userData.room!=null){ roomId = GameMng._userData.room.roomid; } let firendOpenId = ""; if(GameMng._userData.userid!=null&&GameMng._userData.userid!=undefined){ firendOpenId = GameMng._userData.userid+""; } let s = "firendOpenId=" +firendOpenId+"&roomId="+roomId; let s_o = {"webpageUrl":"http://xiangqi.moyue2020.com/xint.html?"+s, "title":"楚汉对决,等你来战!", "description":"末将在!待主公一声令下,必将杀他个片甲不留...", }; return JSON.stringify(s_o); } public static loadWeiXinHead(avatarurl:string,sf:Sprite){ if(Tools.cache[avatarurl]!=null){ sf.spriteFrame = Tools.cache[avatarurl] return } let obj = null; if(avatarurl.substring(avatarurl.length-3,avatarurl.length)!="png"){ obj= {ext: '.png'} } assetManager.loadRemote(avatarurl,obj, (err, imageAsset) => { if (!err && imageAsset) { let spFrame = Tools.cache[avatarurl]; if (!spFrame) { const texture = new Texture2D(); texture.image = imageAsset; spFrame = new SpriteFrame(); spFrame.texture = texture; imageAsset.addRef(); Tools.cache[avatarurl] = spFrame; // 添加映射表记录 sf.spriteFrame = spFrame } spFrame.addRef(); // 计数加1 } }); } releaseRemoteSprite(node: Node) { if (!isValid(node)) { return; } const sp = node.getComponent(Sprite) as Sprite; if (sp && sp.spriteFrame) { const spFrame = sp.spriteFrame; sp.spriteFrame.decRef(false); // 只把计数减1 sp.spriteFrame = null; if (spFrame.refCount <= 0) { let texture = spFrame.texture as Texture2D; // 如果已加入动态合图,必须取原始的Texture2D if (spFrame.packable) { texture = spFrame.original?._texture as Texture2D; } if (texture) { delete Tools.cache[texture.image!._nativeUrl]; // 删除映射表记录 texture.image?.decRef(); texture.destroy(); } spFrame.destroy(); } } } public static getAvatarByIndex(index:number):string{ if(index<=14&&index>0){ return "PubImgs_avatar_avatar"+index; } return "PubImgs_avatar_avatar1"; } public static getTongBiNumber(num:number):string{ let _z = num + "" if(num>=10000){ _z = (num / 10000)+"" if(_z.length>=3){ _z = _z.substring(0,3) }else{ _z = _z.substring(0,_z.length) } _z = _z+"万" } return _z; } public static getUserStatusNameByStatus(status:xiaoxi_haoyou_status_type):string{ let name = "" switch ( status) { case xiaoxi_haoyou_status_type.deng_dai_kai_ju: name = "等待对局"; break; case xiaoxi_haoyou_status_type.xian_guang: name = "闲逛"; break; case xiaoxi_haoyou_status_type.dui_zhan: name = "正在对战"; break; } return name; } public static getChessPos(){ let tempArray = [] let temp_Array = [] for (const key in GBoardChess.instance.pos.squares) { if(GBoardChess.instance.pos.squares[key]!=0){ tempArray.push({"sq": key,"pc":GBoardChess.instance.pos.squares[key],"x":0,"y":0}) } } for (let index = 0; index < tempArray.length; index++) { const element = tempArray[index]; var node__ = GBoardChess.instance.imgSquares[GBoardChess.instance.flipped(element.sq)]; element.x = node__.position.x element.y = node__.position.y temp_Array.push(element) } return temp_Array } }