BaseCharactor.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var gameConfig = require("../GameConfig.js");
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. zOrder: {
  6. // ATTRIBUTES:
  7. default: 0,
  8. },
  9. playerStates: {
  10. default: null,
  11. type: cc.Node,
  12. serializable: true,
  13. },
  14. },
  15. onLoad() {
  16. this.init();
  17. this.bJump = false;
  18. this.handrailEndX = 0;
  19. this.callback = null;
  20. },
  21. init() {
  22. this.playerController = this.node.parent;
  23. //pConSt
  24. this.pConSt = this.playerController.getComponent('BasePlayerController');
  25. //gStatesSt
  26. this.gStatesSt = this.pConSt.gStatesSt;
  27. //pStatesSt
  28. this.pStatesSt = this.playerStates.getComponent('BasePlayerStates');
  29. this.spine = this.node.getComponent(sp.Skeleton);
  30. },
  31. run(animName, callback) {
  32. this.setAnim(animName, true, callback);
  33. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  34. },
  35. /**
  36. * @param hurdlingTag 0不在跨栏区域,1在跨栏区域
  37. */
  38. jump(animName, callback, hurdlingTag) {
  39. let hurdrailStartPX = this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX;
  40. // this.handrailEndX = hurdrailStartPX + gameConfig.handrailLength;
  41. this.handrailEndX = this.countingJumpDistance(hurdrailStartPX,gameConfig);
  42. this.callback = callback;
  43. this.pStatesSt.targetMovePos = this.handrailEndX + 200;
  44. this.pStatesSt.restMovePos = this.pStatesSt.targetMovePos - this.playerController.x;
  45. this.setAnim(animName, false, function () { }.bind(this));
  46. this.playJumpEffect();
  47. },
  48. countingJumpDistance(){},
  49. playJumpEffect() { },
  50. stagger(callback) {
  51. this.pConSt.stop();
  52. // this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.slow;
  53. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.stagger;
  54. this.setAnim('hurdling_2', false, function () {
  55. // this.run('Run1');
  56. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  57. this.run('idle');
  58. if (!callback) return;
  59. callback();
  60. }.bind(this));
  61. },
  62. setAnim(animName, bLoop, callback) {
  63. let track = this.spine.setAnimation(0, animName, bLoop);
  64. if (track) {
  65. // 注册动画的结束回调
  66. this.spine.setCompleteListener((trackEntry, loopCount) => {
  67. let name = trackEntry.animation ? trackEntry.animation.name : '';
  68. if (name === animName && callback) {
  69. callback();
  70. }
  71. });
  72. }
  73. },
  74. update(dt) {
  75. if ( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump) {
  76. if (this.pStatesSt.targetMovePos - this.playerController.x < 0) {
  77. this.run('idle');
  78. this.playerController.x = this.pStatesSt.targetMovePos;
  79. this.pStatesSt.restMovePos=0;
  80. this.pStatesSt.jumpDuration = 1;
  81. if (!this.callback ) return;
  82. this.callback ();
  83. }
  84. if (this.pStatesSt.restMovePos > 0) {
  85. this.playerController.x += this.pStatesSt.restMovePos * dt / this.pStatesSt.jumpDuration;
  86. this.pStatesSt.jumpDuration -= dt;
  87. }
  88. }
  89. }
  90. });