BaseCharactor.js 3.4 KB

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