import SKDataUtil from "../../ts/gear_2.3.4/util/SKDataUtil"; import SKUIUtil from "../../ts/gear_2.3.4/util/SKUIUtil"; import GameModel from "../../ts/core/GameModel"; //人物模型相關 export default class ShapeUtil { static shared: ShapeUtil = new ShapeUtil(); static debug: boolean = false; constructor() { } static playAni(node: cc.Node, roleId: number, dir: number, action: string, self: any) { if (!node) { return; } let url = this.getRoleURL(action, dir, roleId); let roleNode = node.getChildByName("role"); if (roleNode) { this.playAnishape(roleNode, url, 15, action, roleId, dir); } url = this.getRolersURL(action, dir, roleId); let rolersNode = node.getChildByName("role").getChildByName("maskframe"); if (rolersNode) { this.playAnishape(rolersNode, url, 15, action, roleId, dir); } url = '/shap/'+ roleId + "/" + action + "_" + dir + "_addon"; let addonNode = node.getChildByName("addon"); if (addonNode&&roleId==4007||roleId==4037||roleId==2033||roleId==1131||roleId==1132) { addonNode.active = true; this.playAnishape(addonNode, url, 15, action, roleId, dir); }else{ addonNode.active = false; } url = this.getRoleyzURL(action, dir, roleId); let roleyzNode = node.getChildByName("shadow"); if (roleyzNode) { this.playAnishape(roleyzNode, url, 15, action, roleId, dir); } self.scheduleOnce(() => { roleyzNode.scaleX = 450 / roleyzNode.width; roleyzNode.scaleY = 450 / roleyzNode.height; rolersNode.anchorY = roleNode.anchorY; rolersNode.anchorX = roleNode.anchorX; roleyzNode.anchorY = roleNode.anchorY; roleyzNode.anchorX = roleNode.anchorX; }, 0); } static playshadowAni(node: cc.Node, roleId: number, dir: number, action: string, self: any) { if (!node) { return; } let url = this.getRoleyzURL(action, dir, roleId); if (node) { this.playAnishape(node, url, 15, action, roleId, dir); self.scheduleOnce(() => { node.scaleX = 450 / node.width; node.scaleY = 450 / node.height; }, 0); } } static getRoleURL(action:string,dir:number,resid:number):string { return '/shap/'+ resid + "/" + action + "_" + dir; } static getRolersURL(action:string,dir:number,resid:number):string { return '/shape_mask/'+ resid + "/" + action + "_" + dir; } static getRoleyzURL(action:string,dir:number,resid:number):string { return '/ccbtex/shadow/' + resid + "_" + action + "_" + dir + "_sw"; } static playAnishape(node: cc.Node, url: string, speed: number, action:string, roleId: number, dir: number) { if (!node) { cc.log("必須有結點才能播放動畫", url); return; } if (SKDataUtil.isEmptyString(url)) { let sprite = node.getComponent(cc.Sprite); if (sprite) { sprite.spriteFrame = null; } let ani = node.getComponent(cc.Animation); if (ani) { ani.stop(); } return; } let atlas = cc.loader.getRes(url, cc.SpriteAtlas); if (atlas) { this.playOfAtlas(node, atlas, url, speed, action, roleId, dir); if (this.debug) { cc.log("從加載圖集緩存中播放動畫", url); } return; } cc.loader.loadRes(url, cc.SpriteAtlas, (error, result: cc.SpriteAtlas) => { if (error) { cc.warn("加載動畫資源錯誤", url, error.message); return; } this.playOfAtlas(node, result, url, speed, action, roleId, dir); }); } // 通過紋理圖集播放動畫 static playOfAtlas(node: cc.Node, atlas: cc.SpriteAtlas, url: string, speed: number, action:string, roleId: number, dir: number) { if (!SKUIUtil.isValid(node)) { return; } let frames = atlas.getSpriteFrames(); let clip = cc.AnimationClip.createWithSpriteFrames(frames, speed); clip.name = action; if (action == 'stand' || action == 'run') { clip.wrapMode = cc.WrapMode.Loop; } else { clip.wrapMode = cc.WrapMode.Normal; } let sprite = node.getComponent(cc.Sprite); if (!sprite) { sprite = node.addComponent(cc.Sprite); } sprite.spriteFrame = frames[0]; //sprite.sizeMode = cc.Sprite.SizeMode.RAW; sprite.trim = false; let ani = node.getComponent(cc.Animation); if (!ani) { ani = node.addComponent(cc.Animation); } let index = ani.getClips().indexOf(clip); if (index == -1) { ani.addClip(clip); } ani.play(clip.name); if (this.debug) { cc.log("播放動畫:", url); } } }