PlayerController.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. let aWebView = require("WebView");
  2. let aLib = require("Library");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. GameMode: {
  7. default: null,
  8. type: cc.Node,
  9. serializable: true,
  10. },
  11. GameStates: {
  12. default: null,
  13. type: cc.Node,
  14. serializable: true,
  15. },
  16. PlayerNode: {
  17. default: null,
  18. type: cc.Node,
  19. serializable: true,
  20. },
  21. CharactorNode: {
  22. default: null,
  23. type: cc.Node,
  24. serializable: true,
  25. },
  26. PlayerStatesNode: {
  27. default: null,
  28. type: cc.Node,
  29. serializable: true,
  30. },
  31. PunchTimes: {
  32. default: null,
  33. type: cc.Node,
  34. serializable: true,
  35. },
  36. PuchTimesLabel: {
  37. default: null,
  38. type: cc.Label,
  39. serializable: true,
  40. },
  41. },
  42. start () {
  43. //初始化
  44. this.GameModeScp = this.GameMode.getComponent('GameMode');
  45. this.GameStatesScp = this.GameStates.getComponent('GameStates');
  46. this.CharactorScp = this.CharactorNode.getComponent('Charactor');
  47. this.PlayerStatesScp = this.PlayerStatesNode.getComponent('PlayerStates');
  48. this.PunchTimesAnim = this.PunchTimes.getComponent(cc.Animation);
  49. //触摸事件
  50. this.TouchEventScp = this.PlayerNode.getComponent('TouchEvent');
  51. this.TouchEventScp.registerListener(this.node);
  52. this.node.on('touchstart',this.onEventStart,this);
  53. //记录开始位置
  54. this.GameStatesScp.startPosition.x = this.PlayerNode.x;
  55. this.GameStatesScp.startPosition.y = this.PlayerNode.y;
  56. this.PlayerStatesScp.state = this.PlayerStatesScp.stateTag.idle;//idle
  57. //减慢玩家速度
  58. this.schedule(function(){
  59. this.SpeedDown();
  60. }.bind(this),0.1,cc.macro.REPEAT_FOREVER);
  61. //是否在pC
  62. if(!aLib.isMobile()) return;
  63. let self = this;
  64. aWebView.init(self.node, ()=>{
  65. aWebView.onBindHitBoxingPost();
  66. self.node.on('onBoxingPostHit',self.onBoxingPostHit,self);
  67. });
  68. },
  69. //页面退出回调
  70. onQuit(data)
  71. {
  72. console.log('onQuit=',data);
  73. },
  74. //弹出框回调
  75. onQuitModal(res)
  76. {
  77. if (res.data.confirm) {
  78. console.log("退出");
  79. }else if(res.data.cancel){
  80. console.log("取消退出");
  81. }
  82. },
  83. onBoxingPostHit(data)
  84. {
  85. if(this.GameStatesScp.progress == this.GameStatesScp.progressTag.default)
  86. {
  87. this.GameModeScp.StarGame();
  88. }
  89. if(this.GameStatesScp.progress == this.GameStatesScp.progressTag.start)
  90. {
  91. this.Run(1);
  92. }
  93. },
  94. onEventStart(worldPoint)
  95. {
  96. this.Run(0);
  97. },
  98. Run(bPunch)
  99. {
  100. //是否游戏开始
  101. if(this.GameStatesScp.progress != this.GameStatesScp.progressTag.start) return;
  102. let time = new Date();
  103. if(this.PlayerStatesScp.lastPucnTime!=0)
  104. {
  105. let dtTime = time.getMilliseconds() - this.PlayerStatesScp.lastPucnTime.getMilliseconds();
  106. if(bPunch)
  107. {
  108. if(dtTime < 500)
  109. {
  110. this.PlayerStatesScp.dtDownSpeed = 0.1;
  111. }
  112. else if(dtTime >= 500 && dtTime < 1000)
  113. {
  114. this.PlayerStatesScp.dtDownSpeed = 0.2;
  115. }
  116. else if(dtTime >= 1000 && dtTime < 2000)
  117. {
  118. this.PlayerStatesScp.dtDownSpeed = 0.5;
  119. }
  120. }
  121. else
  122. {
  123. if(dtTime < 200)
  124. {
  125. this.PlayerStatesScp.dtDownSpeed = 0.1;
  126. }
  127. else if(dtTime >= 200 && dtTime < 300)
  128. {
  129. this.PlayerStatesScp.dtDownSpeed = 0.2;
  130. }
  131. else if(dtTime >= 300 && dtTime < 500)
  132. {
  133. this.PlayerStatesScp.dtDownSpeed = 0.5;
  134. }
  135. }
  136. }
  137. this.PlayerStatesScp.lastPucnTime = time;
  138. this.GameStatesScp.PunchTimes++;
  139. this.PuchTimesLabel.string = this.GameStatesScp.PunchTimes;
  140. if(this.PlayerStatesScp.speed+this.PlayerStatesScp.dtSpeed<=10)
  141. {
  142. this.PlayerStatesScp.speed+=this.PlayerStatesScp.dtSpeed;
  143. }
  144. //console.log('this.PlayerStatesScp.speed=',this.PlayerStatesScp.speed)
  145. this.unschedule(this.StopRun);
  146. if(bPunch)
  147. {
  148. this.scheduleOnce(this.StopRun,2);
  149. }
  150. else
  151. {
  152. this.scheduleOnce(this.StopRun,0.5);
  153. }
  154. },
  155. StopRun()
  156. {
  157. this.PlayerStatesScp.speed = 0;
  158. this.CharactorScp.Idel();
  159. },
  160. SpeedDown()
  161. {
  162. if(this.PlayerStatesScp.speed<=0) return;
  163. this.PlayerStatesScp.speed-=this.PlayerStatesScp.dtDownSpeed;
  164. },
  165. Reset()
  166. {
  167. this.PlayerNode.x = this.GameStatesScp.startPosition.x;
  168. this.PlayerNode.y = this.GameStatesScp.startPosition.y;
  169. this.PuchTimesLabel.string = '0';
  170. },
  171. update (dt)
  172. {
  173. if(this.PlayerStatesScp.speed>0 && this.PlayerStatesScp.speed<3)//run
  174. {
  175. this.CharactorScp.Run(1);
  176. }
  177. else if(this.PlayerStatesScp.speed>=3 && this.PlayerStatesScp.speed<8)
  178. {
  179. this.CharactorScp.Run(2);
  180. }
  181. else if(this.PlayerStatesScp.speed>=9)
  182. {
  183. this.CharactorScp.Run(3);
  184. }
  185. this.PlayerNode.x += this.PlayerStatesScp.speed;
  186. },
  187. });