HeroControl.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. var GameStates = require('GameStates');
  2. var Constants = require('Constants');
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. isPlayingBoos: false,
  7. PerfectGrade: 0,
  8. MyAudio: {
  9. default: null,
  10. type: cc.Node,
  11. },
  12. FemaleDragonBone: cc.Node,
  13. PlayerAnimControl: null,
  14. PlayerStatesScript: null,
  15. isBack: false,
  16. isMoveToOrigin: false,//回到原点
  17. backwardsDistance: 100,
  18. JavelinPrefab: {
  19. default: null,
  20. type: cc.Prefab,
  21. },
  22. isJavelinFlying: false,
  23. JavelinTempPrefab: null,
  24. //起跳点距离
  25. takeOffDistance: 0,
  26. },
  27. start() {
  28. this.PlayerAnimControl = this.FemaleDragonBone.getComponent('PlayerAnimControl');
  29. this.PlayerStatesScript = this.getComponent('PlayerStates');
  30. // cc.log('this.PlayerStatesScript is ', this.PlayerStatesScript.name);
  31. // this.PlayerAnimControl.setEndListener(function (name) {
  32. // cc.log('333',name);
  33. // }.bind(this));
  34. },
  35. onLoad: function () {
  36. },
  37. update(dt) {
  38. var enterDeceleration = GlobalData.game.isEnterGameFinishZone;
  39. if (enterDeceleration) {
  40. GlobalData.gameMode.DecelerationGameSpeed_lerp(dt);
  41. }
  42. var alwaysAddGameSpeed = GlobalData.game.playerCurrentZone == 1 && GlobalData.game.isItAlwaysTouch;
  43. if (alwaysAddGameSpeed) {//加速带加速
  44. GlobalData.gameMode.AddGameSpeed(Constants.longAccelerationAddSpeedNum * dt);
  45. }
  46. //玩家模型向后移动
  47. if (this.isBack) {
  48. this.nodeBackwards(dt, this.FemaleDragonBone);
  49. }
  50. if (this.isMoveToOrigin) {
  51. this.PlayerMoveToOrigin(dt);
  52. }
  53. if (this.isJavelinFlying) {
  54. if (this.JavelinTempPrefab != null) {
  55. this.nodeBackwards(dt, this.JavelinTempPrefab);
  56. }
  57. }
  58. },
  59. //***************声音部分***start*/
  60. addPerfectGradeAndPlay: function () {
  61. if (this.PerfectGrade < 10) {
  62. this.PerfectGrade += 1;
  63. }
  64. this.playAudioByName("Perfect" + this.PerfectGrade);
  65. },
  66. resetPerfectGrade: function () {
  67. this.PerfectGrade = 0;
  68. },
  69. playAudioByName(AudioName) {
  70. // console.log(AudioName);
  71. var AudioSource = this.getAudioSourceByName(AudioName);
  72. if (AudioSource != null) {
  73. if (AudioName == "Boos") {//踉跄时的音效:若正在播放此音效则不能播放;
  74. if (this.isPlayingBoos == false) {
  75. this.isPlayingBoos = true;
  76. AudioSource.play();
  77. this.scheduleOnce(function () {
  78. this.isPlayingBoos = false;
  79. }, AudioSource.getDuration());
  80. }
  81. } else {
  82. AudioSource.play();
  83. }
  84. }
  85. },
  86. stopAudioByName(AudioName) {
  87. var AudioSource = this.getAudioSourceByName(AudioName);
  88. if (AudioSource != null) {
  89. AudioSource.stop();
  90. }
  91. },
  92. getAudioSourceByName(AudioName) {
  93. if (this.MyAudio != null) {
  94. var AudioControlScript = this.MyAudio.getComponent("AudioControl");
  95. if (AudioControlScript.getAudioSourceByName(AudioName) != null) {
  96. var AudioSource = AudioControlScript.getAudioSourceByName(AudioName);
  97. return AudioSource;
  98. }
  99. }
  100. },
  101. //***************声音部分***end*/
  102. // ***********碰撞***start/
  103. onCollisionEnter: function (other) {
  104. // console.log('on collision enter', other.node.name);
  105. if (other.node.name == 'LongABand') {
  106. GlobalData.game.isMustTouch = false;//只要进入长加带,就重置一下这个变量
  107. GlobalData.game.playerCurrentZone = GameStates.EnterZoneName.LongAccelerationZone;
  108. } else if (other.node.name == 'Javelin') {
  109. other.node.active = false;
  110. //拿起标枪 todo...
  111. this.PlayerStatesScript.strPlayerStates = 'HoldJavelinRun';
  112. GlobalData.game.PlayerRunState = GlobalData.GameManager.PlayerRunState.HoldJavelinRun;
  113. // console.log('GlobalData.game.PlayerRunState',GlobalData.game.PlayerRunState);
  114. this.PlayerAnimControl.SpeedChange();
  115. } else if (other.node.name == 'ThrowingPoint') {
  116. //到达扔标枪的地点 todo...
  117. // cc.log(this.PlayerStatesScript.strPlayerStates, '++++', this.PlayerAnimControl.PlayerStatesScript.strPlayerStates);
  118. GlobalData.game.isCanTouch = false;
  119. this.PlayerAnimControl.ThrowJavelinOut();
  120. GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Stop;
  121. setTimeout(function () {//完成动作后扔标枪
  122. GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Run;
  123. this.PlayerStatesScript.strPlayerStates = 'NormalRun';
  124. GlobalData.game.PlayerRunState = GlobalData.GameManager.PlayerRunState.NormalRun;
  125. //玩家向后移动
  126. this.isBack = true;
  127. this.javelinHero();
  128. }.bind(this), 300)//需要标枪动作时间
  129. } else if (other.node.name == 'Bike') {
  130. //碰撞到自行车 todo...
  131. // other.node.active = false;
  132. GlobalData.game.isCanTouch = false;
  133. GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Stop;
  134. //处理骑车动画
  135. this.PlayerStatesScript.strPlayerStates = 'RideElephantRun';
  136. GlobalData.game.PlayerRunState = GlobalData.GameManager.PlayerRunState.RideElephantRun;
  137. this.PlayerAnimControl.JumpOnElephant();
  138. var otherNode = other.node;
  139. cc.find('Canvas').getChildByName('PlayerShadow').active = false;
  140. this.PlayerAnimControl.setEndListener(function () {
  141. otherNode.active = false;
  142. cc.find('Canvas').getChildByName('PlayerShadow').active = true;
  143. cc.find('Canvas').getChildByName('PlayerShadow').scale = 3;
  144. })
  145. setTimeout(function () {
  146. GlobalData.game.isCanTouch = true;
  147. GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Run;
  148. }, 1000);
  149. } else if (other.node.name == 'GroundCollision') {
  150. GlobalData.game.playerCurrentZone = GameStates.EnterZoneName.GroundZone;
  151. GlobalData.game.isMustTouch = false;
  152. }
  153. },
  154. onCollisionExit: function (other, self) {
  155. if (other.node.name == 'LongABand') {
  156. GlobalData.game.playerCurrentZone = GameStates.EnterZoneName.null
  157. // console.log('on collision enter', other.node.name, GlobalData.game.playerCurrentZone);
  158. if (!GlobalData.game.isMustTouch) {//如果一次都没点击过
  159. GlobalData.gameMode.GameOver();
  160. return;
  161. }
  162. if (GlobalData.game.isItAlwaysTouch) {
  163. //如果还在按钮按着,就摔倒。todo....
  164. GlobalData.gameMode.GameOver();
  165. GlobalData.gameMode.StopTouchLongAcceleration();
  166. } else {
  167. GlobalData.gameMode.StopTouchLongAcceleration();
  168. }
  169. } else if (other.node.name == 'GroundCollision') {
  170. GlobalData.game.playerCurrentZone = GameStates.EnterZoneName.null;
  171. } else if (other.node.name == 'AccelerationBandNormal' || other.node.name == 'PerfectHurdleBand'||other.node.name =='EndNormalLongJumpBand') {
  172. if (!GlobalData.game.isMustTouch) {
  173. // console.log('进入普通加速带没点击。GameOver');
  174. //跨栏没跨过播放跨栏断绳子的动画
  175. if(other.node.parent.name == 'CushionAndRailing'){
  176. var animCtrl = other.node.parent.getChildByName('Railing').getComponent(cc.Animation);
  177. animCtrl.play("RailingBreak");
  178. }
  179. GlobalData.gameMode.GameOver();
  180. }
  181. }else if (other.node.name == 'EndNormalLongJumpBand') {
  182. if (!GlobalData.game.isMustTouch) {
  183. // console.log('进入普通加速带没点击。GameOver');
  184. GlobalData.gameMode.GameOver();
  185. }
  186. }
  187. },
  188. // ***********碰撞***end/
  189. //跳远
  190. HeroLongJump: function () {
  191. var longJumpData = {
  192. name: "Hero",
  193. longJumpCount: GlobalData.game.longJumpCount,//跳的距离
  194. takeOffDistance: this.takeOffDistance,//蓝区域跳的距离范围
  195. }
  196. this.HeroLongJumpAnimation(longJumpData);
  197. this.playAudioByName("Hei");
  198. },
  199. HeroLongJumpAnimation: function (tempData) {
  200. //跳远 计数算法 todo
  201. // var UIControl = cc.find("UIControl").getComponent("UIControl");
  202. var oldSpeed = GlobalData.game.GameSpeed;
  203. var jumpDistance = 3 + tempData.longJumpCount; //跳了多少米
  204. var jumpPixelSpeed = jumpDistance > 4 ? 900 : 600;
  205. var jumpDistancePixel = jumpDistance * 240 + tempData.takeOffDistance;//1m约等于 240像素,并且加上 开始的距离
  206. var jumpTimer = jumpDistancePixel / jumpPixelSpeed;//跳远的时间
  207. GlobalData.game.GameSpeed = jumpPixelSpeed;//跳远时候用跳远的速度
  208. // var actionMoveBy = cc.moveBy(jumpTimer, cc.p(jumpDistancePixel, 0));
  209. // this.node.runAction(actionMoveBy);
  210. var jumpHeight = (jumpDistance - 3) * 10 + 100;//100是固定高度
  211. var actionBy = cc.jumpBy(jumpTimer, 0, 0, jumpHeight, 1);
  212. // console.log("跳远的距离====:", jumpDistance, jumpTimer, tempData.takeOffDistance);
  213. this.node.runAction(actionBy);
  214. this.PlayerAnimControl.LongJumpUp(jumpTimer);
  215. this.PlayerAnimControl.setEndListener(function (name) {
  216. if(name == 'FemaleJump'){
  217. this.PlayerAnimControl.LongJumpDown(jumpTimer);
  218. }
  219. }.bind(this));
  220. //显示UI
  221. GlobalData.game.getGameUIState().onStartShowUIDistance(jumpDistance, jumpTimer);
  222. //加分
  223. GlobalData.game.GameScore +=jumpDistance*300;
  224. setTimeout(function () {
  225. //跳远完成,停1s
  226. GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Stop;
  227. GlobalData.game.GameSpeed = oldSpeed;
  228. setTimeout(function () {
  229. GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Run;
  230. this.PlayerAnimControl.SpeedChange();
  231. }.bind(this), 1000);
  232. }.bind(this), jumpTimer * 1000);
  233. },
  234. //标枪
  235. //模型向后移动
  236. nodeBackwards(dt, node) {
  237. // console.log('===',node.name);
  238. if (node.name == 'FemaleDragonBone') {
  239. node.x += -GlobalData.game.GameSpeed * dt;
  240. if (node.x <= -this.backwardsDistance) {
  241. this.isBack = false;
  242. }
  243. } else if (node.name == 'JavelinFlying') {
  244. if (GlobalData.game.PlayerState == GlobalData.GameManager.PlayerState.Stop) {
  245. return;
  246. }
  247. node.x += -GlobalData.game.GameSpeed * dt;
  248. if (node.x <= -this.backwardsDistance) {
  249. // this.JavelinTempPrefab = null;
  250. this.isJavelinFlying = false;
  251. node.destroy();
  252. // console.log('66',this.JavelinTempPrefab.name);
  253. }
  254. }
  255. },
  256. //模型回到原点
  257. PlayerMoveToOrigin(dt) {
  258. //Constants.JaveEndSpeed 定义速度
  259. this.FemaleDragonBone.x += Constants.JaveEndSpeed * dt;
  260. if (this.FemaleDragonBone.x >= 0) {
  261. this.FemaleDragonBone.x = 0;
  262. this.isMoveToOrigin = false;
  263. //回到原点后 todo
  264. this.isJavelinFlying = true;
  265. GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Run;
  266. GlobalData.game.GameSpeed = Constants.JaveEndSpeed;
  267. GlobalData.game.isCanTouch = true;
  268. }
  269. },
  270. //计算height,和duration
  271. javelinData: function (x) {
  272. var MiddleX = x / 2;
  273. var highest = Math.sqrt(x * x - MiddleX * MiddleX) - x;
  274. highest = highest < 0 ? -highest : highest;
  275. highest += 120;
  276. var temp = x / 720;
  277. // console.log("标枪的x值=", temp, "this.javeCount=", GlobalData.game.javeCount, "highest==", highest);
  278. var tempDuration = x > 5000 ? 4 : 2;
  279. var data = {
  280. duration: tempDuration,
  281. position: 0,
  282. y: 0,
  283. height: highest,//600
  284. jumps: 1
  285. };
  286. return data;
  287. },
  288. javelinHero: function () {
  289. //扔标枪 动作 todo...
  290. var x = Constants.JaveBaseDistance + GlobalData.game.javeCount * 72;
  291. var tPrefab = cc.instantiate(this.JavelinPrefab);
  292. tPrefab.parent = this.node;
  293. tPrefab.active = true;
  294. tPrefab.setPosition(30, 50);
  295. var oldSpeed = GlobalData.game.GameSpeed;
  296. GlobalData.game.GameSpeed = 1500;
  297. var flyTimer = x / GlobalData.game.GameSpeed;//跳远的时间
  298. //vt+(1/2)at^2 v= gt g=9.8
  299. var V_up = Math.tan(45) * GlobalData.game.GameSpeed;
  300. var height = V_up * flyTimer * 0.5 + 0.5 * -320 * (flyTimer * 0.5 * flyTimer * 0.5);
  301. var actionBy = cc.jumpTo(flyTimer, 0, 0, height / 4, 1);
  302. tPrefab.runAction(actionBy);
  303. var actionrotateBy = cc.rotateBy(flyTimer, 120);
  304. tPrefab.runAction(actionrotateBy);
  305. this.playAudioByName("JavelinFlying");
  306. //显示UI
  307. var javeShowDistance = (x)/72;
  308. GlobalData.game.getGameUIState().onStartShowUIDistance(javeShowDistance, flyTimer);
  309. //加分
  310. GlobalData.game.GameScore +=javeShowDistance*300;
  311. setTimeout(function () {
  312. this.stopAudioByName("JavelinFlying");//停止标枪飞的声音
  313. this.playAudioByName("JavelinDown");//播放标枪落地的声音
  314. this.PlayerAnimControl.SpeedChange();
  315. this.JavelinTempPrefab = tPrefab;
  316. //停下来,等模型回到原点继续跑
  317. GlobalData.game.PlayerState = GlobalData.GameManager.PlayerState.Stop;
  318. this.isMoveToOrigin = true;
  319. GlobalData.game.GameSpeed = oldSpeed;
  320. }.bind(this), flyTimer * 1000);
  321. },
  322. })