AI_worker_Animation.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. var reGameStates = require('GameStates');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. //人物类型
  6. AI_Worker_Type: {
  7. default: reGameStates.AI_Worker_Type.Rube,
  8. type: cc.Enum(reGameStates.AI_Worker_Type),
  9. serializable: true,
  10. },
  11. AI_Animation_Type: {
  12. default: reGameStates.AI_Animation_Type.None,
  13. type: cc.Enum(reGameStates.AI_Animation_Type),
  14. serializable: true,
  15. },
  16. FrontName: {
  17. default: '',
  18. visible: false,
  19. serializable: false,
  20. },
  21. BackName: {
  22. default: '',
  23. visible: false,
  24. serializable: false,
  25. },
  26. workerFrontName: {
  27. default: '',
  28. visible: false,
  29. serializable: false,
  30. },
  31. //是否循环
  32. isLoop: false,
  33. aiTypeName: {
  34. default: '',
  35. visible: false,
  36. serializable: false,
  37. },
  38. dragonBones: {
  39. default: null,
  40. type: cc.Node,
  41. serializable: false,
  42. }
  43. },
  44. onLoad: function () {
  45. this.dragonBones = this.node.getChildByName('dragonBones');
  46. //获取 ArmatureDisplay
  47. this._armatureDisPlay = this.dragonBones.getComponent(dragonBones.ArmatureDisplay)
  48. if (this._armatureDisPlay) {
  49. this.onSwitchWorkerState(this.AI_Animation_Type);
  50. // this._armature = this._armatureDisPlay.armature();
  51. } else {
  52. cc.warn('this._armatureDisPlay 为空。');
  53. }
  54. },
  55. //切换骨架
  56. onSwitchArmature() {
  57. switch (this.AI_Worker_Type) {
  58. case reGameStates.AI_Worker_Type.CarryGay:
  59. //运输工人时候骨架名称
  60. this._armatureDisPlay.armatureName = "WithTricycle";
  61. break;
  62. case reGameStates.AI_Worker_Type.Rube:
  63. //其他三种工作状态骨架的名称
  64. this._armatureDisPlay.armatureName = "WithoutTricycl";
  65. this.workerFrontName = 'poke_front';
  66. //动态设置名字
  67. this.FrontName = 'empty_walk_front';
  68. this.BackName = 'empty_walk_back';
  69. break;
  70. case reGameStates.AI_Worker_Type.Lumberjack:
  71. //其他三种工作状态骨架的名称
  72. this._armatureDisPlay.armatureName = "WithoutTricycl";
  73. this.workerFrontName = 'hatchet_cut_front';
  74. //动态设置名字
  75. this.FrontName = 'hatchet_walk_front';
  76. this.BackName = 'hatchet_walk_back';
  77. break;
  78. case reGameStates.AI_Worker_Type.Miner:
  79. //其他三种工作状态骨架的名称
  80. this._armatureDisPlay.armatureName = "WithoutTricycl";
  81. //矿工没有工作状态的
  82. this.workerFrontName = '';
  83. //动态设置名字
  84. this.FrontName = 'pickaxe_walk_front';
  85. this.BackName = 'pickaxe_walk_back';
  86. break;
  87. case reGameStates.AI_Worker_Type.None:
  88. //切换
  89. this._armatureDisPlay.armatureName = "WithoutTricycl";
  90. //没有工作状态的
  91. this.workerFrontName = '';
  92. break;
  93. }
  94. },
  95. // 切换动作流程:1.先切换骨架;2.再设置状态
  96. // 切换当前是什么工人
  97. onSwitchWorker(_workerType) {
  98. this.AI_Worker_Type = _workerType;
  99. switch (this.AI_Worker_Type) {
  100. case reGameStates.AI_Worker_Type.CarryGay:
  101. //运输工人时候骨架名称
  102. this._armatureDisPlay.armatureName = "WithTricycle";
  103. break;
  104. case reGameStates.AI_Worker_Type.Rube:
  105. case reGameStates.AI_Worker_Type.Lumberjack:
  106. case reGameStates.AI_Worker_Type.Miner:
  107. case reGameStates.AI_Worker_Type.Nothing:
  108. this._armatureDisPlay.armatureName = "WithoutTricycl";
  109. break;
  110. }
  111. },
  112. //切换工作状态
  113. onSwitchWorkerState(_animationType) {
  114. this.AI_Animation_Type = _animationType;
  115. // None: -1,//没有状态
  116. // Walk: -1,//目前Run
  117. // Idle: -1,//站立
  118. // Hatchet: -1,//砍树
  119. // Pickaxe: -1,//挖矿
  120. // Poke: -1,//摘果实
  121. // CargoCarryBox: -1,//货物运输,有车
  122. // CargoCarryRaw: -1,//载货原料
  123. switch (_animationType) {
  124. case reGameStates.AI_Animation_Type.None:
  125. case reGameStates.AI_Animation_Type.Idle:
  126. //动态设置名字
  127. this.FrontName = 'empty_idle_front';
  128. this.BackName = 'empty_idle_back';
  129. break;
  130. case reGameStates.AI_Animation_Type.Walk:
  131. //动态设置名字
  132. this.FrontName = 'empty_walk_front';
  133. this.BackName = 'empty_walk_back';
  134. break;
  135. case reGameStates.AI_Animation_Type.Hatchet:
  136. this.FrontName = 'hatchet_walk_front';
  137. this.BackName = 'hatchet_walk_back';
  138. this.workerFrontName = 'hatchet_cut_front';
  139. break;
  140. case reGameStates.AI_Animation_Type.Pickaxe:
  141. this.FrontName = 'pickaxe_walk_front';
  142. this.BackName = 'pickaxe_walk_back';
  143. break;
  144. case reGameStates.AI_Animation_Type.Poke:
  145. this.workerFrontName = 'poke_front';
  146. break;
  147. case reGameStates.AI_Animation_Type.CargoCarryBox:
  148. this.FrontName = 'box_front';
  149. this.BackName = 'box_back';
  150. break;
  151. case reGameStates.AI_Animation_Type.CargoCarryRaw:
  152. this.FrontName = 'bag_front';
  153. this.BackName = 'bag_back';
  154. break;
  155. }
  156. },
  157. //播放对应的动画
  158. switchAnimation: function (direction) {
  159. let loopValue = this.isLoop ? 0 : 1;
  160. switch (direction) {
  161. case reGameStates.moveType.moveUp:
  162. this.dragonBones.scaleX = 1;
  163. if (this.BackName)
  164. this._armatureDisPlay.playAnimation(this.BackName, loopValue);
  165. break;
  166. case reGameStates.moveType.moveRight:
  167. this.dragonBones.scaleX = -1;
  168. if (this.BackName)
  169. this._armatureDisPlay.playAnimation(this.BackName, loopValue);
  170. break;
  171. case reGameStates.moveType.moveDown:
  172. this.dragonBones.scaleX = 1;
  173. if (this.FrontName)
  174. this._armatureDisPlay.playAnimation(this.FrontName, loopValue);
  175. break;
  176. case reGameStates.moveType.moveLeft:
  177. this.dragonBones.scaleX = -1;
  178. if (this.FrontName)
  179. this._armatureDisPlay.playAnimation(this.FrontName, loopValue);
  180. break;
  181. }
  182. },
  183. //工作动画
  184. playWorkerAnimation(isLeft) {
  185. if (isLeft) {
  186. this.dragonBones.scaleX = 1;
  187. } else {
  188. this.dragonBones.scaleX = -1;
  189. }
  190. this._armatureDisPlay.playAnimation(this.workerFrontName, 0);
  191. },
  192. })