BaseCharactor.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var gameConfig = require("../GameConfig.js");
  2. var playerConfig = require("../PlayerConfig.js");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. zOrder: {
  7. // ATTRIBUTES:
  8. default: 0,
  9. },
  10. playerStates: {
  11. default: null,
  12. type: cc.Node,
  13. serializable: true,
  14. },
  15. },
  16. onLoad () {
  17. this.init();
  18. this.bJump = false;
  19. this.handrailEndX = 0;
  20. this.jumpSpeedX = 0;
  21. this.jumpDuration = 1;
  22. },
  23. init()
  24. {
  25. this.playerController = this.node.parent;
  26. //pConSt
  27. this.pConSt = this.playerController.getComponent('BasePlayerController');
  28. //gStatesSt
  29. this.gStatesSt = this.pConSt.gStatesSt;
  30. //pStatesSt
  31. this.pStatesSt = this.playerStates.getComponent('BasePlayerStates');
  32. // this.spine = this.node.getComponent(sp.Skeleton);
  33. this.armatureDisplay = this.node.getComponent(dragonBones.ArmatureDisplay);
  34. this.armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, this.animationEventHandler, this);
  35. // this.testWarlock.getComponent(dragonBones.ArmatureDisplay);
  36. this.armature = this.armatureDisplay.armature();
  37. },
  38. run(animName,callback) {
  39. this.setAnim(animName,true,callback);
  40. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  41. },
  42. /**
  43. * @param hurdlingTag 0不在跨栏区域,1在跨栏区域
  44. */
  45. jump(animName,callback,hurdlingTag) {
  46. if(hurdlingTag == 0){
  47. if(callback){
  48. callback();
  49. }
  50. return;
  51. }
  52. //because action shcedule timer is not excellent so need to use update time to make animation for networking
  53. let hurdrailStartPX = this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX;
  54. this.handrailEndX = hurdrailStartPX+gameConfig.handrailLength;
  55. // this.jumpSpeedX = (this.handrailEndX+this.pStatesSt.handrailExtlen - this.playerController.x)*0.001/this.jumpDuration;
  56. // this.bJump = true;
  57. // jumpBy
  58. // cc.jumpBy(time,cc.v2(x,y),height,count)
  59. // cc.jumpBy(时间,停止跳跃时的坐标:原坐标 + (x,y)的值,跳跃高度,跳跃次数)
  60. let tw = cc.tween(this.playerController);
  61. // if(hurdlingTag ==0){
  62. // tw.then(cc.jumpTo(0.5, cc.v2(this.pConSt.node.x+200, this.playerController.y), 0, 1));
  63. // }else{
  64. // tw.then(cc.jumpTo(0.5, cc.v2(this.handrailEndX+200, this.playerController.y), 0, 1));
  65. // }
  66. if(hurdlingTag == 1){
  67. tw.then(cc.jumpTo(0.5, cc.v2(this.handrailEndX+200, this.playerController.y), 0, 1));
  68. }
  69. tw.call(function() {
  70. this.run('Run1',true);
  71. if(!callback) return;
  72. callback();
  73. }.bind(this));
  74. tw.start();
  75. this.setAnim(animName,false,function () {}.bind(this));
  76. this.playJumpEffect();
  77. },
  78. playJumpEffect(){},
  79. stagger(callback)
  80. {
  81. this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.slow;
  82. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  83. // this.setAnim('hurdling_2',false,function () {
  84. this.setAnim('Stagger',false,function () {
  85. this.run('Run1',true);
  86. if(!callback) return;
  87. callback();
  88. }.bind(this));
  89. },
  90. setAnim(animName,bLoop,callback)
  91. {
  92. // let track = this.spine.setAnimation(0, animName, bLoop);
  93. if(bLoop)
  94. {
  95. bLoop = 0;
  96. }
  97. else{
  98. bLoop = 1;
  99. }
  100. this.armatureDisplay.playAnimation(animName, bLoop);
  101. this.callback = callback;
  102. // console.log('bLoop=',bLoop)
  103. // if (track) {
  104. // 注册动画的结束回调
  105. // this.spine.setCompleteListener((trackEntry, loopCount) => {
  106. // let name = trackEntry.animation ? trackEntry.animation.name : '';
  107. // if (name === animName && callback) {
  108. // callback();
  109. // }
  110. // });
  111. // }
  112. },
  113. animationEventHandler(event)
  114. {
  115. if (event.type === dragonBones.EventObject.COMPLETE) {
  116. if (event.animationState.name === "Run1") {
  117. // console.log('Run1 动作播放完毕!!!')
  118. //TODO:
  119. this.callback();
  120. }
  121. else if (event.animationState.name === "Stagger") {
  122. // console.log('Stagger 动作播放完毕!!!')
  123. this.callback();
  124. }
  125. }
  126. },
  127. update (dt) {
  128. if(this.bJump)
  129. {
  130. //count time to 0 second
  131. this.jumpDuration-=dt;
  132. //跑
  133. this.playerController.x += this.jumpSpeedX*dt/0.001;
  134. if(this.jumpDuration<=0)
  135. {
  136. this.bJump = false;
  137. this.handrailEndX = 0;
  138. this.jumpSpeedX = 0;
  139. this.jumpDuration = 1;
  140. }
  141. }
  142. }
  143. });