TestAnimation.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. //新动作
  29. var AI_Worker_Animation_Name = [
  30. "empty_walk_front",
  31. "empty_walk_back",
  32. "hatchet_walk_front",
  33. "hatchet_walk_back",
  34. "hatchet_cut_front",
  35. "pickaxe_walk_back",
  36. "pickaxe_walk_front",
  37. "poke_front",
  38. "empty_idle_front",
  39. "empty_idle_back",
  40. ]
  41. //骨架
  42. var AI_Worker_Armature_Name = [
  43. "WithoutTricycl",
  44. "WithTricycle",
  45. ]
  46. var AI_Worker_Animation_Name_Sanlun = [
  47. "bag_front",
  48. "bag_back",
  49. "box_front",
  50. "box_back",
  51. ]
  52. cc.Class({
  53. extends: cc.Component,
  54. properties: {
  55. //人物类型
  56. AI_Worker_Type: {
  57. default: reGameStates.AI_Worker_Type.Rube,
  58. type: cc.Enum(reGameStates.AI_Worker_Type),
  59. serializable: true,
  60. },
  61. AI_Animation_Type: {
  62. default: reGameStates.AI_Animation_Type.None,
  63. type: cc.Enum(reGameStates.AI_Animation_Type),
  64. serializable: true,
  65. },
  66. RightName: {
  67. default: '',
  68. visible: false,
  69. serializable: false,
  70. },
  71. DownName: {
  72. default: '',
  73. visible: false,
  74. serializable: false,
  75. },
  76. LeftName: {
  77. default: '',
  78. visible: false,
  79. serializable: false,
  80. },
  81. UpName: {
  82. default: '',
  83. visible: false,
  84. serializable: false,
  85. },
  86. IdleName: {
  87. default: '',
  88. visible: false,
  89. serializable: false,
  90. },
  91. workerLeftName: {
  92. default: '',
  93. visible: false,
  94. serializable: false,
  95. },
  96. workerRightName: {
  97. default: '',
  98. visible: false,
  99. serializable: false,
  100. },
  101. //是否循环
  102. isLoop: false,
  103. aiTypeName: {
  104. default: '',
  105. visible: false,
  106. serializable: false,
  107. },
  108. count: 0,
  109. armatureCount: 0,
  110. currentAnimationName: null,
  111. },
  112. onLoad: function () {
  113. //获取 ArmatureDisplay
  114. this._armatureDisPlay = this.node.getComponent(dragonBones.ArmatureDisplay)
  115. console.log('this._armatureDisPlay:', this._armatureDisPlay);
  116. // if (this._armatureDisPlay) {
  117. // // cc.log('_animationType11111==',AI_Type[this.AI_Worker_Type]);
  118. // this.aiTypeName = AI_Type[this.AI_Worker_Type];
  119. // //获取 Armatrue
  120. // this._armatureDisPlay.armatureName = this.aiTypeName;
  121. // this.onSwitchWorkerState(this.AI_Animation_Type);
  122. // this._armature = this._armatureDisPlay.armature();
  123. // } else {
  124. // cc.warn('this._armatureDisPlay 为空。');
  125. // }
  126. },
  127. onSwitchFromButton() {
  128. if (this.count < this.currentAnimationName.length)
  129. this.count++;
  130. else
  131. this.count = 0;
  132. // this._armatureDisPlay.armatureName = AI_Worker_Animation_Name[this.count];
  133. this._armatureDisPlay.playAnimation(this.currentAnimationName[this.count], 0);
  134. },
  135. //切换骨架
  136. onSwitchArmature() {
  137. if (this.armatureCount < AI_Worker_Armature_Name.length)
  138. this.armatureCount++;
  139. else
  140. this.armatureCount = 0;
  141. //设置当前骨架名称
  142. this._armatureDisPlay.armatureName = AI_Worker_Armature_Name[this.armatureCount];
  143. if (this.armatureCount == 0)
  144. this.currentAnimationName = AI_Worker_Animation_Name;
  145. else
  146. this.currentAnimationName = AI_Worker_Animation_Name_Sanlun;
  147. //重置动画下标
  148. this.count = 0;
  149. //调用一下动画
  150. this.onSwitchFromButton();
  151. },
  152. onSwitchFromDirection() {
  153. this.node.scaleX *= -1;
  154. },
  155. // 切换当前是什么工人
  156. onSwitchWorker(_workerType) {
  157. this.AI_Worker_Type = _workerType;
  158. if (this._armatureDisPlay) {
  159. // cc.log('_animationType',AI_Type[this.AI_Worker_Type]);
  160. this.aiTypeName = AI_Type[this.AI_Worker_Type];
  161. //获取 Armatrue
  162. this._armatureDisPlay.armatureName = this.aiTypeName;
  163. this.onSwitchWorkerState(this.AI_Animation_Type);
  164. this._armature = this._armatureDisPlay.armature();
  165. } else {
  166. cc.warn('this._armatureDisPlay 为空。');
  167. }
  168. },
  169. //切换工作状态
  170. onSwitchWorkerState(_animationType) {
  171. // cc.log('onSwitchWorkerState ==',_animationType);
  172. // Idle: -1,//站立
  173. // Carry: -1,//运输
  174. // CargoCarryEmpty:-1,//空的货物运输
  175. // CargoCarry: -1,//货物运输,有车
  176. // CargoCarryRaw: -1,//载货原料
  177. let _AddName = '';
  178. switch (_animationType) {
  179. case reGameStates.AI_Animation_Type.Idle:
  180. //设置站立
  181. _AddName = AI_Worker_EndStatus[0];
  182. break;
  183. case reGameStates.AI_Animation_Type.Carry:
  184. //动态设置名字
  185. _AddName = AI_Worker_EndStatus[1];
  186. break;
  187. case reGameStates.AI_Animation_Type.CargoCarryEmpty:
  188. _AddName = AI_Worker_EndStatus[2];
  189. break;
  190. case reGameStates.AI_Animation_Type.CargoCarry:
  191. _AddName = AI_Worker_EndStatus[3];
  192. break;
  193. case reGameStates.AI_Animation_Type.CargoCarryRaw:
  194. _AddName = AI_Worker_EndStatus[4];
  195. break;
  196. }
  197. //动态设置名字
  198. this.RightName = this.aiTypeName + AI_Animation_Name[0] + _AddName;
  199. this.LeftName = this.aiTypeName + AI_Animation_Name[1] + _AddName;
  200. this.DownName = this.aiTypeName + AI_Animation_Name[2] + _AddName;
  201. this.UpName = this.aiTypeName + AI_Animation_Name[3] + _AddName;
  202. this.workerLeftName = this.aiTypeName + AI_Animation_Name[4];
  203. this.workerRightName = this.aiTypeName + AI_Animation_Name[5];
  204. },
  205. switchAnimation: function (direction) {
  206. //动画执行方式二
  207. // cc.log('switchAnimation', direction);
  208. let loopValue = this.isLoop ? 0 : 1;
  209. switch (direction) {
  210. case reGameStates.moveType.moveUp:
  211. if (this.UpName)
  212. this._armatureDisPlay.playAnimation(this.UpName, loopValue);
  213. break;
  214. case reGameStates.moveType.moveRight:
  215. if (this.RightName)
  216. this._armatureDisPlay.playAnimation(this.RightName, loopValue);
  217. break;
  218. case reGameStates.moveType.moveDown:
  219. if (this.DownName)
  220. this._armatureDisPlay.playAnimation(this.DownName, loopValue);
  221. break;
  222. case reGameStates.moveType.moveLeft:
  223. if (this.LeftName)
  224. this._armatureDisPlay.playAnimation(this.LeftName, loopValue);
  225. break;
  226. }
  227. },
  228. //工作动画
  229. playWorkerAnimation(isLeft) {
  230. if (isLeft) {
  231. this._armatureDisPlay.playAnimation(this.workerLeftName, 0);
  232. // cc.log(11111);
  233. } else {
  234. this._armatureDisPlay.playAnimation(this.workerRightName, 0);
  235. // cc.log(22222);
  236. }
  237. },
  238. })