DragonManager.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { SingleClass } from "./SingleClass";
  2. const { ccclass, property } = cc._decorator;
  3. export enum AnimType {
  4. Dog = 1,
  5. Daoju = 2,
  6. }
  7. @ccclass
  8. export default class DragonManager extends SingleClass {
  9. public armatureDisplay: dragonBones.ArmatureDisplay = null;//动画
  10. public armatureDisplayList: any = [];//已加载动画组
  11. public armature: dragonBones.Armature;
  12. public factory: dragonBones.CCFactory;
  13. public currentAnimName: string = '';
  14. /**
  15. * 播放龙骨动画
  16. * @param animationName 动画名称
  17. * @param completeCallback 播放回调
  18. * @param playTimes 播放次数
  19. */
  20. public PlayDragonAnimation(animationName: string, completeCallback: any = null, playTimes: number = 1) {
  21. // this.armatureDisplay.armatureName = armatureName;
  22. this.removeListen();
  23. this.currentAnimName = animationName;
  24. this.armatureDisplay.playAnimation(animationName, playTimes);
  25. this.armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, completeCallback.bind(this, this.armatureDisplay.node));
  26. }
  27. /**
  28. * 生成骨骼动画
  29. * @param node 当前的节点
  30. * @param path 龙骨地址
  31. * @param armatureName Armature名称
  32. * @param newAnimation Animation名称
  33. * @param completeCallback 动画播放完毕的回调
  34. * @param playTimes 播放次数 -1是根据龙骨文件 0五险循环 >0是播放次数
  35. */
  36. public dragonBoneAnim(node: cc.Node, path: string, armatureName: string, newAnimation: string, completeCallback, playTimes: number = 1) {
  37. let armatureDisplay = null;
  38. this.armatureDisplay = node.getComponent(dragonBones.ArmatureDisplay);
  39. armatureDisplay = this.armatureDisplay;
  40. cc.loader.loadResDir(path, (err, assets) => {
  41. assets.forEach(asset => {
  42. if (asset instanceof dragonBones.DragonBonesAsset) {
  43. armatureDisplay.dragonAsset = asset;
  44. }
  45. if (asset instanceof dragonBones.DragonBonesAtlasAsset) {
  46. armatureDisplay.dragonAtlasAsset = asset;
  47. }
  48. });
  49. armatureDisplay.armatureName = armatureName;
  50. armatureDisplay.playAnimation(newAnimation, playTimes);
  51. armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, completeCallback);
  52. });
  53. }
  54. /**
  55. * 更换龙骨皮肤
  56. */
  57. public ChangeDragonSkin(skinName: string) {
  58. try {
  59. let skinNames = ["shou1", "shou", "pigu_"];
  60. let skinIndexArr = {
  61. "default": [0, 0, 0],
  62. "skin_one": [1, 1, 3],
  63. "skin_two": [0, 0, 1],
  64. "skin_three": [2, 2, 2],
  65. };
  66. let currentIndexArr = skinIndexArr[skinName];
  67. for (var i = 0; i < skinNames.length - 1; i++) {
  68. var bones = this.armatureDisplay.armature().getSlot(skinNames[i]);
  69. bones.displayIndex = currentIndexArr[i] - 0;
  70. // console.log('bones****', bones, ' ', currentIndexArr[i]);
  71. }
  72. let slot = this.armatureDisplay.armature().getSlot("Armature");
  73. slot.displayList[0].getSlot("pigu_").displayIndex = skinIndexArr[skinName][2];
  74. slot.displayList[0].getSlot("yan").displayIndex = (skinName == "skin_one") ? -1 : 0;
  75. // console.log('slot.armature.name:', slot);
  76. } catch (err) {
  77. }
  78. }
  79. /**删除动画完毕的监听 */
  80. public removeListen() {
  81. if (this.armatureDisplay)
  82. this.armatureDisplay.removeEventListener(dragonBones.EventObject.COMPLETE);
  83. }
  84. // /**
  85. // *初始化骨骼动画资源
  86. // */
  87. // public initArmatureFile(skeletonData, textureData, texturePng) {
  88. // let factory = this.getFactory;
  89. // //this.armatureDisplay.getArmatureKey
  90. // //factory.parseTextureAtlasData(cc.loader.loadRes(textureData), cc.loader.loadRes(texturePng));
  91. // }
  92. // /**获取骨骼工厂 */
  93. // public get getFactory(): dragonBones.CCFactory {
  94. // return dragonBones.CCFactory.getInstance();
  95. // }
  96. /**获取动画对象 */
  97. // public buildArmature(armatureName): dragonBones.Armature {
  98. // this.armature = this.getFactory.buildArmature(armatureName);
  99. // return this.armature;
  100. // }
  101. // /**
  102. // * 播放动作
  103. // * @param action
  104. // * @param playNum 播放次数
  105. // */
  106. // public play(action: string, playNum: number = 1) {
  107. // if (this.dog_armatureDisplay)
  108. // this.dog_armatureDisplay.playAnimation(action, playNum);
  109. // }
  110. // /**
  111. // * 替换动画
  112. // */
  113. // public replaceAnim(armatureName: string) {
  114. // }
  115. // /**
  116. // * 生成骨骼动画
  117. // * @param node 当前的节点
  118. // * @param path 龙骨地址
  119. // * @param armatureName Armature名称
  120. // * @param newAnimation Animation名称
  121. // * @param completeCallback 动画播放完毕的回调
  122. // * @param playTimes 播放次数 -1是根据龙骨文件 0五险循环 >0是播放次数
  123. // */
  124. // public dragonBoneAnim(node: cc.Node, path: string, armatureName: string, newAnimation: string, completeCallback, playTimes: number = 1) {
  125. // let armatureDisplay = null;
  126. // // if (!this.dog_armatureDisplay) {
  127. // this.dog_armatureDisplay = node.getComponent(dragonBones.ArmatureDisplay);
  128. // armatureDisplay = this.dog_armatureDisplay;
  129. // // }
  130. // cc.loader.loadResDir(path, (err, assets) => {
  131. // assets.forEach(asset => {
  132. // if (asset instanceof dragonBones.DragonBonesAsset) {
  133. // armatureDisplay.dragonAsset = asset;
  134. // }
  135. // if (asset instanceof dragonBones.DragonBonesAtlasAsset) {
  136. // armatureDisplay.dragonAtlasAsset = asset;
  137. // }
  138. // });
  139. // armatureDisplay.armatureName = armatureName;
  140. // armatureDisplay.playAnimation(newAnimation, playTimes);
  141. // armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, completeCallback);
  142. // });
  143. // }
  144. // /**
  145. // * 获取当前的动画
  146. // */
  147. // public getCurrentAnim(node: cc.Node): dragonBones.ArmatureDisplay {
  148. // let armatureDisplay = node.getComponent(dragonBones.ArmatureDisplay);
  149. // armatureDisplay.removeEventListener(dragonBones.EventObject.COMPLETE);
  150. // return armatureDisplay;
  151. // }
  152. }