PlayerController.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. onBoxingPostHit(data)
  70. {
  71. if(this.GameStatesScp.progress != this.GameStatesScp.progressTag.start)//2 start game
  72. {
  73. this.GameModeScp.StarGame();
  74. }
  75. else
  76. {
  77. this.Run(1);
  78. }
  79. },
  80. onEventStart(worldPoint)
  81. {
  82. this.Run(0);
  83. },
  84. Run(bPunch)
  85. {
  86. //是否游戏开始
  87. if(this.GameStatesScp.progress != this.GameStatesScp.progressTag.start) return;
  88. let time = new Date();
  89. if(this.PlayerStatesScp.lastPucnTime!=0)
  90. {
  91. let dtTime = time.getMilliseconds() - this.PlayerStatesScp.lastPucnTime.getMilliseconds();
  92. if(bPunch)
  93. {
  94. if(dtTime < 500)
  95. {
  96. this.PlayerStatesScp.dtDownSpeed = 0.1;
  97. }
  98. else if(dtTime >= 500 && dtTime < 1000)
  99. {
  100. this.PlayerStatesScp.dtDownSpeed = 0.2;
  101. }
  102. else if(dtTime >= 1000 && dtTime < 2000)
  103. {
  104. this.PlayerStatesScp.dtDownSpeed = 0.5;
  105. }
  106. }
  107. else
  108. {
  109. if(dtTime < 200)
  110. {
  111. this.PlayerStatesScp.dtDownSpeed = 0.1;
  112. }
  113. else if(dtTime >= 200 && dtTime < 300)
  114. {
  115. this.PlayerStatesScp.dtDownSpeed = 0.2;
  116. }
  117. else if(dtTime >= 300 && dtTime < 500)
  118. {
  119. this.PlayerStatesScp.dtDownSpeed = 0.5;
  120. }
  121. }
  122. }
  123. this.PlayerStatesScp.lastPucnTime = time;
  124. this.GameStatesScp.PunchTimes++;
  125. this.PuchTimesLabel.string = this.GameStatesScp.PunchTimes;
  126. if(this.PlayerStatesScp.speed+this.PlayerStatesScp.dtSpeed<=10)
  127. {
  128. this.PlayerStatesScp.speed+=this.PlayerStatesScp.dtSpeed;
  129. }
  130. //console.log('this.PlayerStatesScp.speed=',this.PlayerStatesScp.speed)
  131. this.unschedule(this.StopRun);
  132. if(bPunch)
  133. {
  134. this.scheduleOnce(this.StopRun,2);
  135. }
  136. else
  137. {
  138. this.scheduleOnce(this.StopRun,0.5);
  139. }
  140. },
  141. StopRun()
  142. {
  143. this.PlayerStatesScp.speed = 0;
  144. this.CharactorScp.Idel();
  145. },
  146. SpeedDown()
  147. {
  148. if(this.PlayerStatesScp.speed<=0) return;
  149. this.PlayerStatesScp.speed-=this.PlayerStatesScp.dtDownSpeed;
  150. },
  151. Reset()
  152. {
  153. this.PlayerNode.x = this.GameStatesScp.startPosition.x;
  154. this.PlayerNode.y = this.GameStatesScp.startPosition.y;
  155. this.PuchTimesLabel.string = '0';
  156. },
  157. update (dt)
  158. {
  159. if(this.PlayerStatesScp.speed>0 && this.PlayerStatesScp.speed<3)//run
  160. {
  161. this.CharactorScp.Run(1);
  162. }
  163. else if(this.PlayerStatesScp.speed>=3 && this.PlayerStatesScp.speed<8)
  164. {
  165. this.CharactorScp.Run(2);
  166. }
  167. else if(this.PlayerStatesScp.speed>=9)
  168. {
  169. this.CharactorScp.Run(3);
  170. }
  171. this.PlayerNode.x += this.PlayerStatesScp.speed;
  172. },
  173. });