BasePlayerController.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. var gameConfig = require("../GameConfig.js");
  2. var playerConfig = require("../Player/PlayerConfig");
  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. lastTime:0,
  22. deltaTime:0
  23. },
  24. onLoad() {
  25. this.init();
  26. },
  27. init() {
  28. //gameModeScript
  29. this.gmSt = this.gameMode.getComponent('GameMode');
  30. //gStatesSt
  31. this.gStatesSt = this.gmSt.gStatesSt;
  32. //ctorSt
  33. this.ctorSt = this.charactor.getComponent('BaseCharactor');
  34. //pStatesSt
  35. this.pStatesSt = this.playerStates.getComponent('BasePlayerStates');
  36. this.runDuration = 1000;
  37. },
  38. start() { },//override
  39. resetZindex() {
  40. this.node.zIndex = this.ctorSt.zOrder;
  41. },
  42. update(dt) {
  43. //计算deltatime cocos 的dt坏了
  44. let ms = new Date().getMilliseconds();
  45. if(ms>this.lastTime)
  46. {
  47. this.deltaTime = ms-this.lastTime;
  48. }
  49. else{
  50. this.deltaTime = 1000-this.lastTime+ms;
  51. }
  52. this.lastTime = ms;
  53. if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return;
  54. //跑 run
  55. if (this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run && this.pStatesSt.restMovePos!=0) {
  56. if(this.pStatesSt.targetMovePos-this.node.x<0)
  57. {
  58. this.node.x = this.pStatesSt.targetMovePos;
  59. this.pStatesSt.restMovePos=0;
  60. // if((this.pStatesSt.futureSpeed - playerConfig.acceleration/2)>0)
  61. // {
  62. // this.pStatesSt.futureSpeed -= playerConfig.acceleration/2;
  63. // }
  64. this.pStatesSt.actionArr.shift();
  65. if(this.pStatesSt.actionArr.length==0)
  66. {
  67. this.resetRunState();
  68. }
  69. else{
  70. this.run();
  71. }
  72. }
  73. if(this.pStatesSt.restMovePos>0)
  74. {
  75. // console.log('x =',this.pStatesSt.restMovePos * this.deltaTime / this.runDuration)
  76. this.node.x += this.pStatesSt.restMovePos * this.deltaTime / this.runDuration;
  77. this.runDuration -= this.deltaTime;
  78. //不同速度下不同跑步動畫切換
  79. if (this.pStatesSt.currentSpeed - playerConfig.speedReducing > playerConfig.playerSpeedGrade.idle) {
  80. if (this.pStatesSt.currentSpeed < playerConfig.playerSpeedGrade.medium) {
  81. if (this.pStatesSt.currentSpeedGrade != playerConfig.playerSpeedGrade.slow) {
  82. this.ctorSt.run('Run1');
  83. this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.slow;
  84. }
  85. }
  86. else if (this.pStatesSt.currentSpeed > playerConfig.playerSpeedGrade.medium &&
  87. this.pStatesSt.currentSpeed < playerConfig.playerSpeedGrade.fast) {
  88. if (this.pStatesSt.currentSpeedGrade != playerConfig.playerSpeedGrade.medium) {
  89. this.ctorSt.run('Run2');
  90. this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.medium;
  91. }
  92. }
  93. this.pStatesSt.currentSpeed -= playerConfig.speedReducing;
  94. } else {
  95. this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.idle;
  96. }
  97. if (this.pStatesSt.passedHurdrailNum <= this.pStatesSt.handrailArr.length - 1) {
  98. if (this.node.x >= this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX + gameConfig.handrailLength + 20) {
  99. this.passedHandrail();
  100. this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].bFallDown = 1;
  101. this.pStatesSt.passedHurdrailNum++;
  102. }
  103. }
  104. }
  105. }
  106. //Enter end area
  107. if (this.node.x > this.gmSt.terminal.x - 320 &&
  108. this.pStatesSt.currentProState < this.pStatesSt.pProStates.intoEnd) {
  109. this.pStatesSt.currentProState = this.pStatesSt.pProStates.intoEnd;
  110. this.setEndSpeed();
  111. }
  112. //Prepare to endLine
  113. if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.intoEnd) {
  114. if (this.pStatesSt.currentSpeed <= 0) return;
  115. this.pStatesSt.currentSpeed -= this.pStatesSt.intoTheEndSpeed;
  116. }
  117. //Enter endline
  118. if (this.node.x > this.gmSt.terminal.x - this.pStatesSt.endLineStopPoint && this.gmSt.terminal.active) {
  119. this.enterEndline();
  120. }
  121. },
  122. setEndSpeed() {},//overwrite
  123. passedHandrail() {},//overwrite
  124. enterEndline() {
  125. this.pStatesSt.currentSpeed = 0;
  126. this.ctorSt.setAnim('idle', false);
  127. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.default;
  128. this.pStatesSt.currentProState = this.pStatesSt.pProStates.end;
  129. this.gStatesSt.arrEndPArr.push(this.pStatesSt);
  130. },
  131. speedUp() {
  132. this.pStatesSt.runTimes++;
  133. if(this.pStatesSt.currentState != this.pStatesSt.playerAnimState.run) return;
  134. this.pStatesSt.futureSpeed += playerConfig.acceleration;
  135. if(this.pStatesSt.futureSpeed > playerConfig.playerSpeedGrade.fast)
  136. {
  137. this.pStatesSt.futureSpeed = playerConfig.playerSpeedGrade.fast;
  138. }
  139. if(this.pStatesSt.actionArr.length == 0)
  140. {
  141. this.pStatesSt.actionArr.push(this.pStatesSt.currentSpeed);
  142. this.run();
  143. }
  144. else if(this.pStatesSt.actionArr.length == 1)
  145. {
  146. this.pStatesSt.actionArr.push(this.pStatesSt.currentSpeed);
  147. }
  148. },
  149. run() {
  150. if (this.pStatesSt.currentSpeed <= playerConfig.playerSpeedGrade.fast &&
  151. this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run &&
  152. this.pStatesSt.restMovePos == 0)
  153. {
  154. // console.log('lastSpeed =',this.pStatesSt.lastSpeed)
  155. // console.log('futureSpeed =',this.pStatesSt.futureSpeed)
  156. if(this.pStatesSt.lastSpeed <this.pStatesSt.futureSpeed)
  157. {
  158. this.pStatesSt.currentSpeed = this.pStatesSt.lastSpeed + playerConfig.acceleration;
  159. this.pStatesSt.lastSpeed = this.pStatesSt.currentSpeed;
  160. }
  161. // console.log('currentSpeed =',this.pStatesSt.currentSpeed)
  162. this.runDuration = 1000-this.pStatesSt.currentSpeed;
  163. this.pStatesSt.targetMovePos = this.node.x+playerConfig.perRunDistance;
  164. this.pStatesSt.restMovePos = playerConfig.perRunDistance;
  165. this.ctorSt.run('Run1');
  166. }
  167. },
  168. stop(){
  169. this.node.x = this.pStatesSt.targetMovePos;
  170. this.pStatesSt.restMovePos=0;
  171. this.resetRunState();
  172. },
  173. resetRunState(){
  174. this.pStatesSt.actionArr.length = 0;
  175. this.ctorSt.run('idle');
  176. this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.idle;
  177. this.pStatesSt.lastSpeed = playerConfig.playerSpeedGrade.idle;
  178. this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.idle;
  179. this.pStatesSt.futureSpeed = playerConfig.playerSpeedGrade.idle;
  180. this.runDuration = 1000;
  181. },
  182. speedUp_client(){},
  183. jump() {}//overwrite
  184. });