PlayerAnimControl.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. const constants = require('Constants');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. ArmatureDisPlay:null,
  6. AnimationsNameArr:[],
  7. AnimPlayScript:null,
  8. PlayerControlScript:null,
  9. PlayerStatesScript:null,
  10. DragonBonesAsset:null,
  11. LastSpeedLevel:0,
  12. IsSlowingDownSpeed:false,
  13. },
  14. start () {
  15. this.Init();
  16. },
  17. Init:function(){
  18. this.AnimPlayScript = this.node.getComponent('AnimPlay');
  19. this.ArmatureDisPlay = this.getComponent(dragonBones.ArmatureDisplay);
  20. this.DragonBonesAsset = this.ArmatureDisPlay.dragonAsset;
  21. this.AnimPlayScript.ArmatureDisPlay = this.ArmatureDisPlay;
  22. this.SetArmatureName('Female');
  23. this.AnimAddEventListener();
  24. this.PlayerStatesScript = GlobalData.game.getHeroControl().getComponent('PlayerStates');
  25. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);//测试数据
  26. },
  27. testLog:function(){
  28. var a = JSON.parse(this.DragonBonesAsset.dragonBonesJson);
  29. cc.log(a.frameRate+'*****');
  30. },
  31. GetSpeedLevel:function(SpeedMin){
  32. if(SpeedMin){
  33. var a = constants.configGameSpeedMax/6;
  34. var b = Math.floor(SpeedMin/a);
  35. return b;
  36. }else{
  37. var a = constants.configGameSpeedMax/6;
  38. var b = Math.floor(GlobalData.game.GameSpeed/a);
  39. return b;
  40. }
  41. },
  42. /**
  43. * ArmatureName = 'Female' or 'Jav' or 'Mammoth'
  44. * **/
  45. SetArmatureName:function(ArmatureName){
  46. this.ArmatureDisPlay.armatureName = ArmatureName;
  47. },
  48. onKeyDown: function (event) {//测试数据
  49. switch (event.keyCode) {
  50. case cc.KEY.a:
  51. cc.log('Press a ');
  52. this.AnimPlayScript.PlayIdleAnim();
  53. break;
  54. case cc.KEY.s:
  55. cc.log('Press s');
  56. this.AnimPlayScript.PlayFemaleRunAnim(1);
  57. break;
  58. case cc.KEY.d:
  59. cc.log('Press d');
  60. this.AnimPlayScript.PlayLongJumpDownAnim();
  61. this.AddAnimFadeInListener();
  62. break;
  63. }
  64. },
  65. // update(dt){
  66. //
  67. // },
  68. /**
  69. * 以下动画骨骼为Female
  70. * **/
  71. /*
  72. * this.PlayerStatesScript.PlayerStates = 'NormalRun'or'HoldJavelinRun'or'RideElephantRun'or'Over',
  73. * */
  74. //加速
  75. SpeedChange:function(){
  76. if(this.PlayerStatesScript.strPlayerStates == 'NormalRun'){
  77. this.AnimPlayScript.PlayFemaleRunAnim(this.GetSpeedLevel(),0);
  78. }else if(this.PlayerStatesScript.strPlayerStates == 'HoldJavelinRun'){
  79. this.AnimPlayScript.PlayJavelinRunAnim(this.GetSpeedLevel(),0);
  80. }else if(this.PlayerStatesScript.strPlayerStates == 'RideElephantRun'){
  81. this.AnimPlayScript.PlayRideElephantRunAnim(this.GetSpeedLevel(),0);
  82. }else{
  83. cc.log(this.PlayerStatesScript.strPlayerStates);
  84. }
  85. this.ArmatureDisPlay.timeScale = 1;
  86. },
  87. //减速
  88. SlowingDownSpeed:function(SpeedLevel,PlayTimes){
  89. if(this.PlayerStatesScript.strPlayerStates == 'NormalRun'){
  90. this.AnimPlayScript.PlayFemaleRunAnim(SpeedLevel,PlayTimes);
  91. }else if(this.PlayerStatesScript.strPlayerStates == 'HoldJavelinRun'){
  92. this.AnimPlayScript.PlayJavelinRunAnim(SpeedLevel,PlayTimes);
  93. }else if(this.PlayerStatesScript.strPlayerStates == 'RideElephantRun'){
  94. this.AnimPlayScript.PlayRideElephantRunAnim(SpeedLevel,PlayTimes);
  95. }else{
  96. cc.log(this.PlayerStatesScript.strPlayerStates);
  97. }
  98. this.ArmatureDisPlay.timeScale = 1;
  99. this.setEndListener(function (name) {
  100. if(this.LastSpeedLevel>1){
  101. this.LastSpeedLevel-=1;
  102. this.SlowingDownSpeed(this.LastSpeedLevel,1);
  103. }else if(this.LastSpeedLevel == 1){
  104. this.SlowingDownSpeed(1,0);
  105. }
  106. }.bind(this));
  107. },
  108. //减速前的判断.目前是项目结束的地方调用。由于是每帧调用,所以需要加个判断,不能让动画不断重播
  109. JudgeBeforeSpeedChange:function(){
  110. if(this.IsSlowingDownSpeed == false){
  111. this.IsSlowingDownSpeed = true;
  112. this.LastSpeedLevel = this.GetSpeedLevel();
  113. this.SlowingDownSpeed(this.LastSpeedLevel,1);
  114. }
  115. },
  116. //摔倒
  117. Stagger:function(){
  118. if(this.PlayerStatesScript.strPlayerStates == 'NormalRun'){
  119. this.AnimPlayScript.PlayFemaleFallAnim();
  120. }else if(this.PlayerStatesScript.strPlayerStates == 'HoldJavelinRun'){
  121. this.AnimPlayScript.PlayJavelinFallAnim();
  122. }else if(this.PlayerStatesScript.strPlayerStates == 'RideElephantRun'){
  123. this.AnimPlayScript.PlayElephantFallAnim();
  124. }
  125. },
  126. //空闲
  127. Idle:function(){
  128. this.AnimPlayScript.PlayIdleAnim();
  129. },
  130. //跨栏
  131. JumpToHurdle:function(){
  132. this.AnimPlayScript.PlayHurdleJumpAnim();
  133. },
  134. //扔标枪
  135. ThrowJavelinOut:function(){
  136. this.AnimPlayScript.PlayJavelinOutAnim();
  137. },
  138. //跳远起跳
  139. LongJumpUp:function(jumpTimer){
  140. this.AnimPlayScript.PlayLongJumpUpAnim();
  141. this.ArmatureDisPlay.timeScale = 1/jumpTimer;
  142. },
  143. //跳远落地
  144. LongJumpDown:function(jumpTimer){
  145. this.AnimPlayScript.PlayLongJumpDownAnim();
  146. this.ArmatureDisPlay.timeScale = 1/jumpTimer;
  147. },
  148. //跳上大象
  149. JumpOnElephant:function(){
  150. this.AnimPlayScript.PlayUpElephantAnim();
  151. },
  152. //跳下大象
  153. JumpOutElephant:function(){
  154. this.AnimPlayScript.PlayElephantOutAnim();
  155. },
  156. /**
  157. * 以下动画骨骼为Jav
  158. * **/
  159. //标枪飞行及落地
  160. JavelinFlyToDown:function(){
  161. this.AnimPlayScript.PlayJavelinFlyAnim();
  162. },
  163. /**
  164. * 以下动画骨骼为Mammoth
  165. * **/
  166. //大象跑步动画
  167. ElephantRun:function(){
  168. this.AnimPlayScript.PlayElephantRunAnim();
  169. },
  170. //=============================================
  171. AnimAddEventListener:function(){
  172. this.ArmatureDisPlay.addEventListener(dragonBones.EventObject.START, this._animationEventHandler, this);
  173. this.ArmatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this._animationEventHandler, this)
  174. },
  175. _animationEventHandler: function(event) {
  176. if (event.type === dragonBones.EventObject.START) {
  177. if (event.detail.animationState.name === "JavFly") {
  178. // this._armature.animation.fadeIn("jump_2", -1, -1, 0, NORMAL_ANIMATION_GROUP);
  179. } else if (event.detail.animationState.name === "jump_4") {
  180. this._updateAnimation();
  181. }
  182. }
  183. else if (event.type === dragonBones.EventObject.COMPLETE) {
  184. // cc.log('animationName is '+event.detail.animationState.name);
  185. if(GlobalData.game.getHeroControl().PlayerStatesScript.strPlayerStates == 'Over'){
  186. return;
  187. }else{
  188. // cc.log('strPlayerStates is '+GlobalData.game.getHeroControl().PlayerStatesScript.strPlayerStates);
  189. }
  190. if (this.endListener!=null){
  191. this.endListener(event.detail.animationState.name);
  192. }
  193. if (event.detail.animationState.name === "JavFly") {
  194. this.AnimPlayScript.PlayJavelinDownAnim();//标枪在空中飞行完成后,播放落地动画
  195. }else if(event.detail.animationState.name === "FemaleSpan"){
  196. this.AnimPlayScript.PlayFemaleRunAnim(this.GetSpeedLevel());//跨栏落地后,继续跑
  197. }else if(event.detail.animationState.name === "FemaleJavOut"){
  198. this.AnimPlayScript.PlayIdleAnim();//扔出标枪后,闲置
  199. }else if(event.detail.animationState.name === "FAnimalUp"){
  200. this.AnimPlayScript.PlayRideElephantRunAnim(this.GetSpeedLevel(),0);//跳上大象后,开跑
  201. }else if(event.detail.animationState.name === "FAnimalOut"){
  202. this.AnimPlayScript.PlayFemaleRunAnim(this.GetSpeedLevel(),0);//跳下大象后,单人跑
  203. }
  204. }
  205. },
  206. _updateAnimation:function () {
  207. },
  208. setEndListener: function (endListener) {
  209. this.endListener = endListener;
  210. },
  211. endListener: function () {
  212. var a = JSON.parse(this.DragonBonesAsset.dragonBonesJson);
  213. // cc.log(a.frameRate+'*****');
  214. // cc.log(a.armature[0].animation+'+++++');
  215. },
  216. /**
  217. * 以下是animation动画
  218. */
  219. //无敌状态闪烁
  220. PlayPlayerFlashingAnim:function () {
  221. var animCtrl = this.getComponent(cc.Animation);
  222. animCtrl.play("PlayerFlashing");
  223. },
  224. StopPlayerFlashingAnim:function () {
  225. var animCtrl = this.getComponent(cc.Animation);
  226. animCtrl.stop("PlayerFlashing");
  227. this.node.opacity = 255;
  228. },
  229. });