BaseCharactor.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. },
  34. run(animName,callback) {
  35. this.setAnim(animName,true,callback);
  36. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  37. },
  38. /**
  39. * @param hurdlingTag 0不在跨栏区域,1在跨栏区域
  40. */
  41. jump(animName,callback,hurdlingTag) {
  42. //because action shcedule timer is not excellent so need to use update time to make animation for networking
  43. let hurdrailStartPX = this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX;
  44. this.handrailEndX = hurdrailStartPX+gameConfig.handrailLength;
  45. // this.jumpSpeedX = (this.handrailEndX+this.pStatesSt.handrailExtlen - this.playerController.x)*0.001/this.jumpDuration;
  46. // this.bJump = true;
  47. // jumpBy
  48. // cc.jumpBy(time,cc.v2(x,y),height,count)
  49. // cc.jumpBy(时间,停止跳跃时的坐标:原坐标 + (x,y)的值,跳跃高度,跳跃次数)
  50. let tw = cc.tween(this.playerController);
  51. if(hurdlingTag ==0){
  52. tw.then(cc.jumpTo(0.5, cc.v2(this.pConSt.node.x+200, this.playerController.y), 0, 1));
  53. }else{
  54. tw.then(cc.jumpTo(0.5, cc.v2(this.handrailEndX+200, this.playerController.y), 0, 1));
  55. }
  56. tw.call(function() {
  57. this.run('Run1');
  58. if(!callback) return;
  59. callback();
  60. }.bind(this));
  61. tw.start();
  62. this.setAnim(animName,false,function () {}.bind(this));
  63. this.playJumpEffect();
  64. },
  65. playJumpEffect(){},
  66. stagger(callback)
  67. {
  68. this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.slow;
  69. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  70. this.setAnim('hurdling_2',false,function () {
  71. this.run('Run1');
  72. if(!callback) return;
  73. callback();
  74. }.bind(this));
  75. },
  76. setAnim(animName,bLoop,callback)
  77. {
  78. let track = this.spine.setAnimation(0, animName, bLoop);
  79. if (track) {
  80. // 注册动画的结束回调
  81. this.spine.setCompleteListener((trackEntry, loopCount) => {
  82. let name = trackEntry.animation ? trackEntry.animation.name : '';
  83. if (name === animName && callback) {
  84. callback();
  85. }
  86. });
  87. }
  88. },
  89. update (dt) {
  90. if(this.bJump)
  91. {
  92. //count time to 0 second
  93. this.jumpDuration-=dt;
  94. //跑
  95. this.playerController.x += this.jumpSpeedX*dt/0.001;
  96. if(this.jumpDuration<=0)
  97. {
  98. this.bJump = false;
  99. this.handrailEndX = 0;
  100. this.jumpSpeedX = 0;
  101. this.jumpDuration = 1;
  102. }
  103. }
  104. }
  105. });