AI_worker_Animation.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var reGameStates = require('GameStates');
  2. // Rube:-1,//农夫
  3. // Lumberjack:-1,//伐木工
  4. // Miner:-1,//矿工
  5. // CarrayGay:-1,//搬运工
  6. //AI类型名称
  7. var AI_Type = ['Rube',
  8. 'Lumberjack',
  9. 'Miner',
  10. 'CarryGay',
  11. ]
  12. //动画名字
  13. var AI_Animation_Name = ["BankRight",
  14. "FrontRight",
  15. "FrontLeft",
  16. "BankLeft",
  17. "WorkLeft",
  18. "WorkRight",
  19. ]
  20. //切换状态时候名字带的后缀
  21. var AI_Worker_EndStatus = [
  22. '_Idle',
  23. '_Carry',
  24. '_CargoCarry_Empty',
  25. '_CargoCarry',
  26. '_CargoCarry_Raw',
  27. ]
  28. cc.Class({
  29. extends: cc.Component,
  30. properties: {
  31. //人物类型
  32. AI_Worker_Type: {
  33. default: reGameStates.AI_Worker_Type.Rube,
  34. type: cc.Enum(reGameStates.AI_Worker_Type),
  35. serializable: true,
  36. },
  37. AI_Animation_Type: {
  38. default: reGameStates.AI_Animation_Type.None,
  39. type: cc.Enum(reGameStates.AI_Animation_Type),
  40. serializable: true,
  41. },
  42. RightName: {
  43. default: '',
  44. visible: false,
  45. serializable: false,
  46. },
  47. DownName: {
  48. default: '',
  49. visible: false,
  50. serializable: false,
  51. },
  52. LeftName: {
  53. default: '',
  54. visible: false,
  55. serializable: false,
  56. },
  57. UpName: {
  58. default: '',
  59. visible: false,
  60. serializable: false,
  61. },
  62. IdleName: {
  63. default: '',
  64. visible: false,
  65. serializable: false,
  66. },
  67. workerLeftName: {
  68. default: '',
  69. visible: false,
  70. serializable: false,
  71. },
  72. workerRightName: {
  73. default: '',
  74. visible: false,
  75. serializable: false,
  76. },
  77. //是否循环
  78. isLoop: false,
  79. aiTypeName: {
  80. default: '',
  81. visible: false,
  82. serializable: false,
  83. },
  84. },
  85. onLoad: function () {
  86. //获取 ArmatureDisplay
  87. this._armatureDisPlay = this.node.getChildByName('dragonBones').getComponent(dragonBones.ArmatureDisplay)
  88. if (this._armatureDisPlay) {
  89. // cc.log('_animationType11111==',AI_Type[this.AI_Worker_Type]);
  90. this.aiTypeName = AI_Type[this.AI_Worker_Type];
  91. //获取 Armatrue
  92. this._armatureDisPlay.armatureName = this.aiTypeName;
  93. this.onSwitchWorkerState(this.AI_Animation_Type);
  94. this._armature = this._armatureDisPlay.armature();
  95. //cc.log('this.AI_Worker_Type', this.AI_Worker_Type, this._armatureDisPlay, this._armature.animation.animationNames);
  96. //添加动画监听
  97. // this._armatureDisPlay.addEventListener(dragonBones.EventObject.FADE_IN_COMPLETE, this.animationEventHandler, this)
  98. // this._armatureDisPlay.addEventListener(dragonBones.EventObject.FADE_OUT_COMPLETE, this.animationEventHandler, this)
  99. } else {
  100. cc.warn('this._armatureDisPlay 为空。');
  101. }
  102. },
  103. // 切换当前是什么工人
  104. onSwitchWorker(_workerType) {
  105. this.AI_Worker_Type = _workerType;
  106. if (this._armatureDisPlay) {
  107. // cc.log('_animationType',AI_Type[this.AI_Worker_Type]);
  108. this.aiTypeName = AI_Type[this.AI_Worker_Type];
  109. //获取 Armatrue
  110. this._armatureDisPlay.armatureName = this.aiTypeName;
  111. this.onSwitchWorkerState(this.AI_Animation_Type);
  112. this._armature = this._armatureDisPlay.armature();
  113. } else {
  114. cc.warn('this._armatureDisPlay 为空。');
  115. }
  116. },
  117. //切换工作状态
  118. onSwitchWorkerState(_animationType) {
  119. // cc.log('onSwitchWorkerState ==',_animationType);
  120. // Idle: -1,//站立
  121. // Carry: -1,//运输
  122. // CargoCarryEmpty:-1,//空的货物运输
  123. // CargoCarry: -1,//货物运输,有车
  124. // CargoCarryRaw: -1,//载货原料
  125. let _AddName = '';
  126. switch (_animationType) {
  127. case reGameStates.AI_Animation_Type.Idle:
  128. //设置站立
  129. _AddName = AI_Worker_EndStatus[0];
  130. break;
  131. case reGameStates.AI_Animation_Type.Carry:
  132. //动态设置名字
  133. _AddName = AI_Worker_EndStatus[1];
  134. break;
  135. case reGameStates.AI_Animation_Type.CargoCarryEmpty:
  136. _AddName = AI_Worker_EndStatus[2];
  137. break;
  138. case reGameStates.AI_Animation_Type.CargoCarry:
  139. _AddName = AI_Worker_EndStatus[3];
  140. break;
  141. case reGameStates.AI_Animation_Type.CargoCarryRaw:
  142. _AddName = AI_Worker_EndStatus[4];
  143. break;
  144. }
  145. //动态设置名字
  146. this.RightName = this.aiTypeName + AI_Animation_Name[0] + _AddName;
  147. this.LeftName = this.aiTypeName + AI_Animation_Name[1] + _AddName;
  148. this.DownName = this.aiTypeName + AI_Animation_Name[2] + _AddName;
  149. this.UpName = this.aiTypeName + AI_Animation_Name[3] + _AddName;
  150. this.workerLeftName = this.aiTypeName + AI_Animation_Name[4];
  151. this.workerRightName = this.aiTypeName + AI_Animation_Name[5];
  152. // cc.log('_animationType',this.RightName);
  153. },
  154. // attack: function () {
  155. // //动画执行方式一
  156. // this._armature.animation.fadeIn('attack1', -1, -1, 0, 'hit');
  157. // },
  158. switchAnimation: function (direction) {
  159. //动画执行方式二
  160. // cc.log('switchAnimation', direction);
  161. let loopValue = this.isLoop ? 0 : 1;
  162. switch (direction) {
  163. case reGameStates.moveType.moveUp:
  164. if (this.UpName)
  165. this._armatureDisPlay.playAnimation(this.UpName, loopValue);
  166. break;
  167. case reGameStates.moveType.moveRight:
  168. if (this.RightName)
  169. this._armatureDisPlay.playAnimation(this.RightName, loopValue);
  170. break;
  171. case reGameStates.moveType.moveDown:
  172. if (this.DownName)
  173. this._armatureDisPlay.playAnimation(this.DownName, loopValue);
  174. break;
  175. case reGameStates.moveType.moveLeft:
  176. if (this.LeftName)
  177. this._armatureDisPlay.playAnimation(this.LeftName, loopValue);
  178. break;
  179. }
  180. },
  181. //工作动画
  182. playWorkerAnimation(isLeft) {
  183. if (isLeft) {
  184. this._armatureDisPlay.playAnimation(this.workerLeftName, 0);
  185. // cc.log(11111);
  186. } else {
  187. this._armatureDisPlay.playAnimation(this.workerRightName, 0);
  188. // cc.log(22222);
  189. }
  190. },
  191. animationEventHandler: function animationEventHandler(event) {
  192. if (event.type == dragonBones.EventObject.FADE_IN_COMPLETE) {
  193. cc.log(event.detail.animationName + ' fade in complete');
  194. } else if (event.type == dragonBones.EventObject.FADE_OUT_COMPLETE) {
  195. cc.log(event.detail.animationName + ' fade out complete');
  196. }
  197. },
  198. //从资源中创建
  199. onClick_add: function () {
  200. var self = this;
  201. cc.loader.loadResAll('Dragon', function (err, assets) {
  202. if (err) {
  203. return;
  204. }
  205. if (assets.length <= 0) {
  206. return;
  207. }
  208. var newHero = new cc.Node();
  209. self.node.addChild(newHero);
  210. newHero.setPosition(cc.p(0, 0));
  211. newHero.setScale(0.5, 0.5);
  212. var dragonDisplay = newHero.addComponent(dragonBones.ArmatureDisplay);
  213. for (var i in assets) {
  214. if (assets[i] instanceof dragonBones.DragonBonesAsset) {
  215. dragonDisplay.dragonAsset = assets[i];
  216. }
  217. if (assets[i] instanceof dragonBones.DragonBonesAtlasAsset) {
  218. dragonDisplay.dragonAtlasAsset = assets[i];
  219. }
  220. }
  221. dragonDisplay.armatureName = 'Dragon';
  222. dragonDisplay.playAnimation('stand');
  223. })
  224. }
  225. })