BasePlayerController.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. var gameConfig = require("../GameConfig.js");
  2. var BasePlayerConfig = require("./BasePlayerConfig");
  3. var playerConfig = new BasePlayerConfig();
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. gameMode: {
  8. default: null,
  9. type: cc.Node,
  10. serializable: true,
  11. },
  12. charactor: {
  13. default: null,
  14. type: cc.Node,
  15. serializable: true,
  16. },
  17. playerStates: {
  18. default: null,
  19. type: cc.Node,
  20. serializable: true,
  21. },
  22. lastTime:0,
  23. deltaTime:0
  24. },
  25. onLoad() {
  26. this.init();
  27. },
  28. init() {
  29. //gameModeScript
  30. this.gmSt = this.gameMode.getComponent('GameMode');
  31. //gStatesSt
  32. this.gStatesSt = this.gmSt.gStatesSt;
  33. //ctorSt
  34. this.ctorSt = this.charactor.getComponent('BaseCharactor');
  35. //pStatesSt
  36. this.pStatesSt = this.playerStates.getComponent('BasePlayerStates');
  37. this.runDuration = 1000;
  38. this.playerConfig = playerConfig;
  39. },
  40. start() { },//override
  41. resetZindex() {
  42. this.node.zIndex = this.ctorSt.zOrder;
  43. },
  44. update(dt) {
  45. //计算deltatime cocos 的dt坏了
  46. let ms = new Date().getMilliseconds();
  47. if(ms>this.lastTime)
  48. {
  49. this.deltaTime = ms-this.lastTime;
  50. }
  51. else{
  52. this.deltaTime = 1000-this.lastTime+ms;
  53. }
  54. this.lastTime = ms;
  55. if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return;
  56. //跑 run
  57. if (this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run) {
  58. if(this.pStatesSt.restMovePos!=0)
  59. {
  60. if(this.pStatesSt.targetMovePos-this.node.x<0)
  61. {
  62. this.node.x = this.pStatesSt.targetMovePos;
  63. this.pStatesSt.restMovePos=0;
  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 - this.playerConfig.speedReducing > this.playerConfig.playerSpeedGrade.idle) {
  80. if (this.pStatesSt.currentSpeed < this.playerConfig.playerSpeedGrade.medium) {
  81. if (this.pStatesSt.currentSpeedGrade != this.playerConfig.playerSpeedGrade.slow) {
  82. this.ctorSt.run('Run1');
  83. this.pStatesSt.currentSpeedGrade = this.playerConfig.playerSpeedGrade.slow;
  84. }
  85. }
  86. else if (this.pStatesSt.currentSpeed > this.playerConfig.playerSpeedGrade.medium &&
  87. this.pStatesSt.currentSpeed < this.playerConfig.playerSpeedGrade.fast) {
  88. if (this.pStatesSt.currentSpeedGrade != this.playerConfig.playerSpeedGrade.medium) {
  89. this.ctorSt.run('Run2');
  90. this.pStatesSt.currentSpeedGrade = this.playerConfig.playerSpeedGrade.medium;
  91. }
  92. }
  93. this.pStatesSt.currentSpeed -= this.playerConfig.speedReducing;
  94. } else {
  95. this.pStatesSt.currentSpeed = this.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. }
  107. //Enter end area
  108. if (this.node.x > this.gmSt.terminal.x - 320 &&
  109. this.pStatesSt.currentProState < this.pStatesSt.pProStates.intoEnd) {
  110. this.pStatesSt.currentProState = this.pStatesSt.pProStates.intoEnd;
  111. this.setEndSpeed();
  112. }
  113. //Prepare to endLine
  114. if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.intoEnd) {
  115. if (this.pStatesSt.currentSpeed <= 0) return;
  116. this.pStatesSt.currentSpeed -= this.pStatesSt.intoTheEndSpeed;
  117. }
  118. //Enter endline
  119. if (this.node.x > this.gmSt.terminal.x - this.pStatesSt.endLineStopPoint && this.gmSt.terminal.active) {
  120. this.enterEndline();
  121. }
  122. },
  123. setEndSpeed() {},//overwrite
  124. passedHandrail() {},//overwrite
  125. enterEndline() {
  126. this.pStatesSt.currentSpeed = 0;
  127. this.ctorSt.setAnim('idle', false);
  128. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.default;
  129. this.pStatesSt.currentProState = this.pStatesSt.pProStates.end;
  130. this.gStatesSt.arrEndPArr.push(this.pStatesSt);
  131. },
  132. speedUp() {
  133. if(this.pStatesSt.currentState != this.pStatesSt.playerAnimState.run) return;
  134. if(this.pStatesSt.currentSpeed > this.playerConfig.playerSpeedGrade.fast) return;
  135. if(this.pStatesSt.restMovePos != 0) return;
  136. this.pStatesSt.runTimes++;
  137. this.pStatesSt.currentSpeed += this.playerConfig.acceleration;
  138. this.runDuration = 1000-this.pStatesSt.currentSpeed;
  139. this.pStatesSt.targetMovePos = this.node.x+this.playerConfig.perRunDistance;
  140. // console.log('targetMovePos=',this.pStatesSt.targetMovePos)
  141. this.pStatesSt.restMovePos = this.playerConfig.perRunDistance;
  142. this.ctorSt.run('Run1');
  143. },
  144. // speedUp() {
  145. // this.pStatesSt.runTimes++;
  146. // if(this.pStatesSt.currentState != this.pStatesSt.playerAnimState.run) return;
  147. // this.pStatesSt.futureSpeed += playerConfig.acceleration;
  148. // if(this.pStatesSt.futureSpeed > playerConfig.playerSpeedGrade.fast)
  149. // {
  150. // this.pStatesSt.futureSpeed = playerConfig.playerSpeedGrade.fast;
  151. // }
  152. // if(this.pStatesSt.actionArr.length == 0)
  153. // {
  154. // this.pStatesSt.actionArr.push(this.pStatesSt.currentSpeed);
  155. // this.run();
  156. // }
  157. // else if(this.pStatesSt.actionArr.length == 1)
  158. // {
  159. // this.pStatesSt.actionArr.push(this.pStatesSt.currentSpeed);
  160. // }
  161. // },
  162. // run() {
  163. // if (this.pStatesSt.currentSpeed <= playerConfig.playerSpeedGrade.fast &&
  164. // this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run &&
  165. // this.pStatesSt.restMovePos == 0)
  166. // {
  167. // // console.log('lastSpeed =',this.pStatesSt.lastSpeed)
  168. // // console.log('futureSpeed =',this.pStatesSt.futureSpeed)
  169. // if(this.pStatesSt.lastSpeed <this.pStatesSt.futureSpeed)
  170. // {
  171. // this.pStatesSt.currentSpeed = this.pStatesSt.lastSpeed + playerConfig.acceleration;
  172. // this.pStatesSt.lastSpeed = this.pStatesSt.currentSpeed;
  173. // }
  174. // // console.log('currentSpeed =',this.pStatesSt.currentSpeed)
  175. // this.runDuration = 1000-this.pStatesSt.currentSpeed;
  176. // this.pStatesSt.targetMovePos = this.node.x+playerConfig.perRunDistance;
  177. // this.pStatesSt.restMovePos = playerConfig.perRunDistance;
  178. // this.ctorSt.run('Run1');
  179. // }
  180. // },
  181. stop(){
  182. this.node.x = this.pStatesSt.targetMovePos;
  183. this.pStatesSt.restMovePos=0;
  184. this.resetRunState();
  185. },
  186. resetRunState(){
  187. this.pStatesSt.actionArr.length = 0;
  188. this.ctorSt.run('idle');
  189. this.pStatesSt.currentSpeedGrade = this.playerConfig.playerSpeedGrade.idle;
  190. // this.pStatesSt.lastSpeed = playerConfig.playerSpeedGrade.idle;
  191. this.pStatesSt.currentSpeed = this.playerConfig.playerSpeedGrade.idle;
  192. // this.pStatesSt.futureSpeed = playerConfig.playerSpeedGrade.idle;
  193. this.runDuration = 1000;
  194. },
  195. speedUp_client(){},
  196. jump() {}//overwrite
  197. });