BasePlayerController.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var gameConfig = require("../GameConfig.js");
  2. var playerConfig = require("../PlayerConfig.js");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. gameMode: {
  7. default: null,
  8. type: cc.Node,
  9. serializable: true,
  10. },
  11. charactor: {
  12. default: null,
  13. type: cc.Node,
  14. serializable: true,
  15. },
  16. playerStates: {
  17. default: null,
  18. type: cc.Node,
  19. serializable: true,
  20. },
  21. },
  22. onLoad() {
  23. this.init();
  24. },
  25. init() {
  26. //gameModeScript
  27. this.gmSt = this.gameMode.getComponent('GameMode');
  28. //gStatesSt
  29. this.gStatesSt = this.gmSt.gStatesSt;
  30. //ctorSt
  31. this.ctorSt = this.charactor.getComponent('BaseCharactor');
  32. //pStatesSt
  33. this.pStatesSt = this.playerStates.getComponent('BasePlayerStates');
  34. },
  35. start() { },//override
  36. resetZindex() {
  37. this.node.zIndex = this.ctorSt.zOrder;
  38. },
  39. run() {
  40. // this.ctorSt.run('Run1');
  41. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  42. this.ctorSt.run('idle');
  43. // this.pStatesSt.currentState = this.pStatesSt.playerAnimState.idle;
  44. },
  45. update(dt) {
  46. if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return;
  47. // console.log(this.pStatesSt.currentSpeed);
  48. if (this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run) {
  49. //跑
  50. this.node.x += this.pStatesSt.currentSpeed;
  51. //如果不加速跑,自动逐渐减速
  52. // if (this.pStatesSt.currentSpeed > playerConfig.playerSpeedGrade.idle) {
  53. // if (this.pStatesSt.currentSpeed - playerConfig.speedReducing > playerConfig.playerSpeedGrade.idle) {
  54. // this.pStatesSt.currentSpeed -= playerConfig.speedReducing;
  55. // } else {
  56. // this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.idle;
  57. // }
  58. // }
  59. if (this.pStatesSt.currentSpeed <= playerConfig.playerSpeedGrade.idle) {
  60. this.ctorSt.run('idle');
  61. this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.idle;
  62. } else {
  63. if (this.pStatesSt.currentSpeed - playerConfig.speedReducing > playerConfig.playerSpeedGrade.idle) {
  64. if (this.pStatesSt.currentSpeed < playerConfig.playerSpeedGrade.medium) {
  65. if (this.pStatesSt.currentSpeedGrade != playerConfig.playerSpeedGrade.slow) {
  66. this.ctorSt.run('Run1');
  67. this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.slow;
  68. }
  69. }
  70. else if (this.pStatesSt.currentSpeed > playerConfig.playerSpeedGrade.medium &&
  71. this.pStatesSt.currentSpeed < playerConfig.playerSpeedGrade.fast) {
  72. if (this.pStatesSt.currentSpeedGrade != playerConfig.playerSpeedGrade.medium) {
  73. this.ctorSt.run('Run2');
  74. this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.medium;
  75. }
  76. }
  77. this.pStatesSt.currentSpeed -= playerConfig.speedReducing;
  78. } else {
  79. this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.idle;
  80. }
  81. }
  82. // passed front of Handrail
  83. if (this.pStatesSt.passedHurdrailNum <= this.pStatesSt.handrailArr.length - 1) {
  84. if (this.node.x >= this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX + gameConfig.handrailLength + 20) {
  85. this.passedHandrail();
  86. this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].bFallDown = 1;
  87. this.pStatesSt.passedHurdrailNum++;
  88. }
  89. }
  90. }
  91. //Enter end area
  92. if (this.node.x > this.gmSt.terminal.x - 320 &&
  93. this.pStatesSt.currentProState < this.pStatesSt.pProStates.intoEnd) {
  94. this.pStatesSt.currentProState = this.pStatesSt.pProStates.intoEnd;
  95. this.setEndSpeed();
  96. }
  97. //Prepare to endLine
  98. if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.intoEnd) {
  99. if (this.pStatesSt.currentSpeed <= 0) return;
  100. this.pStatesSt.currentSpeed -= this.pStatesSt.intoTheEndSpeed;
  101. }
  102. //Enter endline
  103. if (this.node.x > this.gmSt.terminal.x - this.pStatesSt.endLineStopPoint && this.gmSt.terminal.active) {
  104. this.enterEndline();
  105. }
  106. },
  107. setEndSpeed() {},//overwrite
  108. passedHandrail() {},//overwrite
  109. enterEndline() {
  110. this.pStatesSt.currentSpeed = 0;
  111. this.ctorSt.setAnim('idle', false);
  112. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.default;
  113. this.pStatesSt.currentProState = this.pStatesSt.pProStates.end;
  114. this.gStatesSt.arrEndPArr.push(this.pStatesSt);
  115. },
  116. speedUp() {
  117. this.pStatesSt.runTimes++;
  118. if (this.pStatesSt.currentSpeed <= playerConfig.playerSpeedGrade.fast &&
  119. this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run) {
  120. this.pStatesSt.currentSpeed += playerConfig.touchToAddSpeed;
  121. let Self = this;
  122. let stopRunAnimation = function ()
  123. {
  124. Self.playerStates.getComponent('BasePlayerStates').currentSpeed = 0;
  125. console.log('11111111')
  126. }
  127. clearTimeout(this.aTimer);
  128. this.aTimer = setTimeout(stopRunAnimation,500);
  129. // console.log('00000')
  130. // this.unschedule(this.stopRunAnimation);
  131. // this.scheduleOnce(this.stopRunAnimation,1);
  132. }
  133. },
  134. jump() {}//overwrite
  135. });