HeroControl.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. const PlayerStateStatic = require('PlayerState');
  2. const GameStatesStatic = require('GameStates');
  3. const MOVE_RIGHT = 2;
  4. cc.macro.ENABLE_TILEDMAP_CULLING = false;
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. InitialPositionY: 0,
  9. PlayerStateScript: null,
  10. jumps: 2,
  11. acceleration: 2500,
  12. jumpSpeed: 600,
  13. drag: 600,
  14. CharactorScript: null,
  15. javelinTag: false,
  16. PerfectGrade: 0,
  17. isPlayingBoos: false,
  18. CameraNode: {
  19. default: null,
  20. type: cc.Node
  21. },
  22. Cushion30AndBunker1M: {//标枪起跳
  23. default: null,
  24. type: cc.Prefab
  25. },
  26. javetemp_Hero: {//标枪起跳
  27. default: null,
  28. type: cc.Prefab
  29. },
  30. javetemp_Rivel: {//标枪起跳
  31. default: null,
  32. type: cc.Prefab
  33. },
  34. sendFrameDataTag: false,
  35. longJumpCount: 0,//跳远加的距离
  36. takeOffDistance: 0,//跳远开始的距离。
  37. isChangeSpeed: false,//是否正在改变速度
  38. timer: 0.3,//计时时间
  39. HeroIsStop: false,//hero是否停止状态
  40. isJumpOver: false,//如果是黑屏瞬移过去的,加上这个限制
  41. getGameStates:null,
  42. },
  43. onEnable: function () {
  44. cc.director.getPhysicsManager().attachDebugDrawToCamera(this);
  45. },
  46. onDisable: function () {
  47. cc.director.getPhysicsManager().detachDebugDrawFromCamera(this);
  48. },
  49. onLoad: function () {
  50. this.javeCount = 0;// parseInt(Math.random()*10+30);
  51. this.node.setLocalZOrder(1000);
  52. this.body = this.getComponent(cc.RigidBody);
  53. this.speed = cc.p(0, 0);
  54. this._moveFlags = -1;
  55. this.PlayerStateScript = this.node.getComponent("PlayerState");
  56. var Player = this.node.getChildByName('Player');
  57. this.CharactorScript = Player.getComponent("Charactor");
  58. //踉跄 被拽过去的tag 如果是 true 就不重置速度 如果是 如果是 false 就充值速度
  59. this.getGameStates = cc.find('Canvas').getComponent('GameStates');
  60. },
  61. SetCurrentSpeed: function (SpeedValue) {
  62. var GameMode = cc.find('Canvas').getComponent('GameMode');
  63. if (GameMode.isEndGame)
  64. return;
  65. if (SpeedValue > this.PlayerStateScript.MaxSpeed) return;
  66. if (SpeedValue < 0) return;
  67. this.PlayerStateScript.CurrentSpeed = SpeedValue;
  68. return SpeedValue;
  69. },
  70. SetCurrentSpeed_noRestrictions: function (SpeedValue) {
  71. this.PlayerStateScript.CurrentSpeed = SpeedValue;
  72. },
  73. SetCurrentSpeed_max: function (SpeedValue) {
  74. var seed = (SpeedValue * 420) / this.PlayerStateScript.MaxSpeed;
  75. this.PlayerStateScript.CurrentSpeed = seed + 1800;
  76. return this.PlayerStateScript.CurrentSpeed * (SpeedValue / this.PlayerStateScript.MaxSpeed);
  77. },
  78. SetCurrentSpeed_max_jumpLong: function (SpeedValue) {
  79. var seed = (SpeedValue * 200) / this.PlayerStateScript.MaxSpeed;
  80. this.PlayerStateScript.CurrentSpeed = seed + 800;
  81. return this.PlayerStateScript.CurrentSpeed * (SpeedValue / this.PlayerStateScript.MaxSpeed);
  82. },
  83. SetCurrentSpeedInitialSpeed: function () {
  84. this.PlayerStateScript.CurrentSpeed = this.PlayerStateScript.InitialSpeed;
  85. },
  86. SetSpeedToZeroAndIdle: function () {
  87. this.HeroIsStop = true;//玩家停止
  88. this.SetCurrentSpeed(0);
  89. this.idle();
  90. },
  91. SetCurrentSpeedAndAnimation: function (SpeedValue) {
  92. this.CharactorScript.setType();
  93. var CurrentSpeed = this.SetCurrentSpeed(SpeedValue);
  94. var state = PlayerStateStatic.SpeedRangeForAnimation;
  95. if (CurrentSpeed >= state.SlowSpeed[0] && CurrentSpeed <= state.SlowSpeed[1]) {
  96. this.CharactorScript.SetCurrentAnimationIdx(0);
  97. }
  98. if (CurrentSpeed >= state.MiddleSpeed[0] && CurrentSpeed <= state.MiddleSpeed[1]) {
  99. this.CharactorScript.SetCurrentAnimationIdx(1);
  100. }
  101. if (CurrentSpeed >= state.HightSpeed[0] && CurrentSpeed <= state.HightSpeed[1]) {
  102. this.CharactorScript.SetCurrentAnimationIdx(2);
  103. }
  104. },
  105. SetCurrentSpeed_set: function (SpeedValue) {
  106. this.CharactorScript.setType();
  107. if (SpeedValue > this.PlayerStateScript.MaxSpeed) return;
  108. if (SpeedValue < 0) return;
  109. this.SetCurrentSpeed(SpeedValue);
  110. var CurrentSpeed = this.PlayerStateScript.CurrentSpeed;
  111. // cc.log("设置 速度",CurrentSpeed ,cc.find('Canvas').getComponent('GameStates').bServer);
  112. var state = PlayerStateStatic.SpeedRangeForAnimation;
  113. if (CurrentSpeed >= state.SlowSpeed[0] && CurrentSpeed <= state.SlowSpeed[1]) {
  114. this.CharactorScript.SetCurrentAnimationIdx_set(0);
  115. }
  116. if (CurrentSpeed >= state.MiddleSpeed[0] && CurrentSpeed <= state.MiddleSpeed[1]) {
  117. this.CharactorScript.SetCurrentAnimationIdx_set(1);
  118. }
  119. if (CurrentSpeed >= state.HightSpeed[0] && CurrentSpeed <= state.HightSpeed[1]) {
  120. this.CharactorScript.SetCurrentAnimationIdx_set(2);
  121. }
  122. },
  123. //就是跑步 加上速度
  124. SetCurrentSpeed_noType: function (SpeedValue) {
  125. if (SpeedValue > this.PlayerStateScript.MaxSpeed) return;
  126. if (SpeedValue < 0) return;
  127. this.SetCurrentSpeed(SpeedValue);
  128. this.CharactorScript.type = 0;
  129. this.CharactorScript.SetCurrentAnimationIdx_set(0);
  130. },
  131. SpeedUp: function (SpeedUpValue) {
  132. if (cc.find('Canvas').getComponent('GameStates').bStopAllAnimationAndSchedule) return;
  133. // return;
  134. var ToSpeed = this.PlayerStateScript.CurrentSpeed + SpeedUpValue;
  135. this.SetCurrentSpeedAndAnimation(ToSpeed);
  136. var data = { FunctionName: 'SetCurrentSpeedAndAnimation', Speed: ToSpeed, "CurrentPositionX": this.node.position.x };
  137. cc.find('Canvas').getComponent('GameMode').SynchronizationFun(data);
  138. },
  139. SpeedDown: function (SpeedUpValue) {
  140. if (cc.find('Canvas').getComponent('GameStates').bStopAllAnimationAndSchedule) return;
  141. var ToSpeed = this.PlayerStateScript.CurrentSpeed - SpeedUpValue;
  142. if (ToSpeed <= this.PlayerStateScript.InitialSpeed) {
  143. ToSpeed = this.PlayerStateScript.InitialSpeed;
  144. }
  145. // return;
  146. this.SetCurrentSpeedAndAnimation(ToSpeed);
  147. // cc.log("当前速度是 踉跄",this.PlayerStateScript.CurrentSpeed);
  148. },
  149. ResetSpeed: function () {
  150. if (cc.find('Canvas').getComponent('GameStates').bStopAllAnimationAndSchedule) return;
  151. var ASpeed = this.PlayerStateScript.InitialSpeed;
  152. this.SetCurrentSpeedAndAnimation(ASpeed);
  153. },
  154. ResetSpeed_noType_lerp: function (dt) {
  155. if (this.isChangeSpeed) {
  156. var ASpeed = this.PlayerStateScript.InitialSpeed;
  157. var CSpeed = this.PlayerStateScript.CurrentSpeed;
  158. var speedFactor = (ASpeed - CSpeed) * 0.4;
  159. this.timer -= dt;//timer =0.3
  160. if (this.timer <= 0) {
  161. this.timer = 0.3;
  162. CSpeed += speedFactor;
  163. if (CSpeed <= ASpeed) {
  164. this.unschedule(this.resetSpeedCallBack);
  165. CSpeed = ASpeed;
  166. this.isChangeSpeed = false;
  167. }
  168. console.log("ResetSpeed_noType", CSpeed);
  169. this.SetCurrentSpeed_noType(CSpeed);
  170. }
  171. }
  172. },
  173. ResetSpeed_noType_lerp_Stop: function () {
  174. this.isChangeSpeed = false;
  175. },
  176. ResetSpeed_noType: function () {
  177. if (this.HeroIsStop) {
  178. // console.log('当前玩家是停止状态,不能设置ResetSpeed_noType!');
  179. return;
  180. }
  181. var ASpeed = this.PlayerStateScript.InitialSpeed;
  182. this.SetCurrentSpeed_noType(ASpeed);
  183. },
  184. ResetSpeed_set: function () {
  185. this.SetCurrentSpeed_set(this.PlayerStateScript.InitialSpeed);
  186. },
  187. //todo 根据当前 速度设置 动作
  188. SetCurrentSpeedAction: function () {
  189. // if(!bDebug) return;
  190. if (!cc.find('Canvas').getComponent('GameStates').bServer) return;
  191. this.SetCurrentSpeedAndAnimation(this.PlayerStateScript.CurrentSpeed);
  192. },
  193. HeroJump: function (callBack) {
  194. if ("camera_ai" == cc.director.getScene().name) {
  195. this.HeroJumpAnimation(callBack);
  196. return;
  197. }
  198. var Hero = cc.find("Hero");
  199. var NetworkSocket = Hero.getComponent('NetworkSocket');
  200. var data = {};
  201. if (this.node.name === 'Hero') {
  202. data = { FunctionName: 'HeroJumpHeroAnimation' };
  203. NetworkSocket.sendSyncData(JSON.stringify(data));
  204. }
  205. this.HeroJumpAnimation(callBack);
  206. },
  207. HeroJumpAnimation: function (callBack) {
  208. var Player = this.node.getChildByName('Player');
  209. Player.getComponent("Charactor").setOnHurdlingListener(callBack);
  210. this.playAudioByName("Hei");
  211. },
  212. EnableHeroMoving: function (bMoving) {
  213. if (bMoving) {
  214. this._moveFlags = MOVE_RIGHT;
  215. }
  216. else {
  217. this._moveFlags = -1;
  218. }
  219. },
  220. WriggleControl: function (dt) {
  221. if (this.WriggleTag) {
  222. // cc.log("减速",this.PlayerStateScript.CurrentSpeed);
  223. if (this.PlayerStateScript.CurrentSpeed >= this.PlayerStateScript.InitialSpeed) {
  224. this.PlayerStateScript.CurrentSpeed -= dt * 300;
  225. } else {
  226. this.WriggleTag = false;
  227. this.SetCurrentSpeed_set(this.PlayerStateScript.CurrentSpeed);
  228. }
  229. }
  230. },
  231. //todo 开启加速
  232. addSeendTempControl: function () {
  233. if (this.addSeendTempTag) {
  234. return;
  235. }
  236. if (this.PlayerStateScript.CurrentSpeed <= 400) {
  237. this.addSeendCurrentSpeed = this.PlayerStateScript.CurrentSpeed;
  238. this.PlayerStateScript.CurrentSpeed += 400;
  239. // cc.log("加速了吗",this.PlayerStateScript.CurrentSpeed);
  240. this.addSeendTempTag = true;
  241. }
  242. },
  243. addSeendTemp: function (dt) {
  244. if (this.addSeendTempTag) {
  245. // cc.log("减速",this.PlayerStateScript.CurrentSpeed);
  246. if (this.addSeendCurrentSpeed <= this.PlayerStateScript.CurrentSpeed) {
  247. this.PlayerStateScript.CurrentSpeed -= dt * 200;
  248. // cc.log("加速了吗-",dt);
  249. } else {
  250. this.addSeendTempTag = false;
  251. }
  252. }
  253. },
  254. HeroLongJump: function (name) {
  255. // console.log("HeroLongJump==", this.longJumpCount);
  256. var Hero = cc.find("Hero");
  257. var NetworkSocket = Hero.getComponent('NetworkSocket');
  258. var data = {};
  259. var longJumpData = {
  260. name: "Hero",
  261. longJumpCount: this.longJumpCount,//跳的距离
  262. takeOffDistance: this.takeOffDistance,//蓝区域跳的距离范围
  263. }
  264. // cc.log("当前玩家是 跳远发送",cc.find('Canvas').getComponent('GameStates').bServer);
  265. if (this.node.name === 'Hero') {
  266. data = { FunctionName: 'HeroLongJumpHeroAnimation', netData: longJumpData };
  267. NetworkSocket.sendSyncData(JSON.stringify(data));
  268. }
  269. this.HeroLongJumpAnimation(longJumpData);
  270. this.playAudioByName("Hei");
  271. },
  272. HeroLongJumpAnimation: function (tempData) {
  273. var Player = this.node.getChildByName('Player');
  274. var Hero = cc.find(tempData.name);
  275. this.PlayerStateScript.TouchControlEnableTouch = false;
  276. this.SetCurrentSpeed(0);
  277. // this.javelinTag = true;
  278. var jumpDistance = 3 + tempData.longJumpCount; //跳了多少米
  279. var jumpPixelSpeed = jumpDistance > 4 ? 900 : 600;
  280. var jumpDistancePixel = jumpDistance * 240 + tempData.takeOffDistance;//1m约等于 240像素,并且加上 开始的距离
  281. var jumpTimer = jumpDistancePixel / jumpPixelSpeed;//跳远的时间
  282. // var actionMoveBy = cc.moveBy(jumpTimer, cc.p(jumpDistancePixel, 0));
  283. var data = {
  284. duration: jumpTimer,
  285. position: jumpDistancePixel,
  286. y: 0
  287. };
  288. // if (!this.javelinTag) {
  289. // return;
  290. // }
  291. this.javelinmove(data, this.node, function () {
  292. }.bind(this), function () {
  293. // if (!this.javelinTag) {
  294. // return;
  295. // }
  296. this.SetCurrentSpeed(200);
  297. }.bind(this));
  298. // this.node.runAction(actionMoveBy);
  299. var jumpHeight = (jumpDistance - 3) * 10 + 100;//100是固定高度
  300. var actionBy = cc.jumpBy(jumpTimer, 0, 0, jumpHeight, 1);
  301. // console.log("跳远的距离====:", tempData.name, jumpDistance, this.takeOffDistance);
  302. // console.log("跳远的距离:",jumpDistance);
  303. this.PlayerStateScript.EndDistance = jumpDistance;//记录位置信息
  304. this.PlayerStateScript.JumpTimer = jumpTimer;
  305. // if (!this.javelinTag) {
  306. // return;
  307. // }
  308. Player.getComponent("Charactor").node.runAction(actionBy);
  309. //跳远开始
  310. Player.getComponent("Charactor").setOnLongJumpListener(jumpTimer);
  311. if (Hero.name == "Hero") {
  312. //扔标枪 计数算法 todo
  313. var UIControl = cc.find("UIControl").getComponent("UIControl");
  314. UIControl.startDistance_Hero(0, Hero, true);
  315. }
  316. // if (!this.javelinTag) {
  317. // return;
  318. // }
  319. //跳远结束
  320. Player.getComponent("Charactor").setTimeOutLongJumpListener(function () {
  321. // return;
  322. // console.log('跳远结束了,我是来自hero的setTimeOutLongJumpListener,tag是 '+this.javelinTag);
  323. // if (!this.javelinTag) {
  324. // return;
  325. // }
  326. setTimeout(function () {
  327. if (this.isJumpOver) {
  328. return;
  329. }
  330. Player.getComponent("Charactor").spine.setAnimation(0, Player.getComponent("Charactor").statusArr_longjump[6], false);
  331. Player.getComponent("Charactor").spine.timeScale = 1;
  332. // this.SetCurrentSpeed(0);
  333. setTimeout(function () {
  334. // this.SetSpeedToZeroAndIdle();
  335. this.ResetSpeed_noType();
  336. }.bind(this), 300);
  337. }.bind(this), jumpTimer + 300);
  338. setTimeout(function () {
  339. if (this.isJumpOver || this.node.name == 'Rivel') {
  340. return;
  341. }
  342. //加分
  343. // this.GameMode = cc.find('Canvas').getComponent('GameMode');
  344. // this.GameMode.addScore(cc.find('Hero'));
  345. if (!cc.find('Canvas').getComponent('GameStates').bServer) {
  346. var data = { FunctionName: 'addScore', PlayerName: 'Hero', value: jumpDistance };
  347. cc.find('Canvas').getComponent('GameMode').SynchronizationFun(data);
  348. // cc.log("--我是client,我发送给server请求加分--longJump当前项目是"+cc.find('Canvas').getComponent('GameStates').strCureentLevel);
  349. }
  350. else {
  351. cc.find('Canvas').getComponent('GameMode').addScore(cc.find('Hero'), jumpDistance, true);
  352. }
  353. CustomLog("跳远 加分");
  354. }.bind(this), 700);
  355. //
  356. this.playAudioByName("JumpDown");
  357. // this.resetPerfectGrade();//踉跄则重置perfect等级
  358. }.bind(this));
  359. },
  360. FromLongJumpToHurdle: function () {
  361. setTimeout(function () {
  362. var GameMode = cc.find('Canvas').getComponent('GameMode');
  363. var GameStates = cc.find('Canvas').getComponent('GameStates');
  364. CustomLog("跳远 黑屏 进入", cc.find("Shade").getComponent('Shade').FadeTag);
  365. //跳远结束
  366. var data = {
  367. gameLv: 1,
  368. startPositionX: 50000
  369. }
  370. // console.log('跳远结束了,准备初始化跨栏项目setStart');
  371. cc.find("Level").getComponent("LevelControl").setStart(data);
  372. GameStates.StartLinePositionX = cc.find("Level").getComponent("LevelControl").tempStartNode.x + 720;
  373. GameMode.CheckPlayersDistance();
  374. var data = { FunctionName: 'CheckPlayersDistance', StartLinePositionX: GameStates.StartLinePositionX };
  375. GameMode.SynchronizationFun(data);
  376. }.bind(this), 500);
  377. },
  378. StopPlayerAllSchedule: function () {
  379. // var GameMode = cc.find('Canvas').getComponent('GameMode');
  380. var GameStates = cc.find('Canvas').getComponent('GameStates');
  381. var Hero = cc.find('Hero');
  382. var Rivel = cc.find('Rivel');
  383. var HeroControl = Hero.getComponent('HeroControl');
  384. var RivelControl = Rivel.getComponent('HeroControl');
  385. var HeroCharactor = Hero.getChildByName('Player').getComponent('Charactor');
  386. var RivelCharactor = Rivel.getChildByName('Player').getComponent('Charactor');
  387. var HeroPlayerState = Hero.getComponent('PlayerState');
  388. var RivelPlayerState = Rivel.getComponent('PlayerState');
  389. HeroCharactor.unscheduleAllCallbacks();
  390. RivelCharactor.unscheduleAllCallbacks();
  391. HeroPlayerState.unscheduleAllCallbacks();
  392. RivelPlayerState.unscheduleAllCallbacks();
  393. Hero.stopAllActions();
  394. Rivel.stopAllActions();
  395. //cc.log("!!!!!!!!!!!!rival明明停止了stopAllActions");
  396. HeroControl.javelinTag = false;
  397. RivelControl.javelinTag = false;
  398. HeroControl.CameraNode.parent = Hero;
  399. GameStates.bStopAllAnimationAndSchedule = true;
  400. HeroControl.SetSpeedToZeroAndIdle();
  401. RivelControl.SetSpeedToZeroAndIdle();
  402. this.schedule(function () {
  403. GameStates.bStopAllAnimationAndSchedule = false;
  404. }, 1, 0);
  405. },
  406. Stagger: function () {
  407. // return;
  408. this.StaggerAnimation();
  409. // cc.log("踉跄 本地",this.node);
  410. var data = { FunctionName: 'StaggerAnimation', PositionX: this.node.convertToWorldSpaceAR(cc.Vec2.ZERO).x };
  411. cc.find('Canvas').getComponent('GameMode').SynchronizationFun(data);
  412. this.playAudioByName("Boos");
  413. // this.resetPerfectGrade();
  414. },
  415. StaggerAnimation: function () {
  416. //console.log("进来 踉跄",this.PlayerStateScript.BStagger,cc.find('Canvas').getComponent('GameStates').bStopAllAnimationAndSchedule);
  417. if (cc.find('Canvas').getComponent('GameStates').bStopAllAnimationAndSchedule) return;
  418. if (this.PlayerStateScript.BStagger) return;
  419. this.PlayerStateScript.BStagger = true;
  420. //踉跄+减速
  421. var Player = this.node.getChildByName('Player');
  422. Player.getComponent("Charactor").setOnTripListener();
  423. this.PlayerStateScript.LastSpeed = this.PlayerStateScript.CurrentSpeed;
  424. this.ResetSpeed();
  425. this.schedule(this.StaggerAnimationSchedule, 0.7, 0);
  426. },
  427. StaggerAnimationSchedule: function () {
  428. if (cc.find('Canvas').getComponent('GameStates').bStopAllAnimationAndSchedule) return;
  429. this.PlayerStateScript.BStagger = !this.PlayerStateScript.BStagger;
  430. // cc.log("this.LastSpeed is "+this.PlayerStateScript.LastSpeed+" this.CurrentSpeed is "+this.PlayerStateScript.CurrentSpeed);
  431. if (this.PlayerStateScript.CurrentSpeed != 0) {
  432. this.SpeedDown(this.PlayerStateScript.DownSpeed);
  433. }
  434. // this.CurrentSpeed = this.LastSpeed;
  435. if (this.PlayerStateScript.BStagger) {
  436. return;
  437. }
  438. var ToSpeed = this.PlayerStateScript.LastSpeed;//-this.PlayerStateScript.CurrentSpeed;
  439. this.SetCurrentSpeedAndAnimation(ToSpeed);
  440. },
  441. unSchedules: function () {
  442. this.unschedule(this.StaggerAnimationSchedule);
  443. },
  444. StaggerAnimationUnch: function () {
  445. this.unschedule(this.StaggerAnimationSchedule);
  446. },
  447. SendSyncHeroPosition: function () {
  448. var Hero = cc.find("Hero");
  449. var PositionStr = Hero.position.x.toString();
  450. var data = { FunctionName: 'Hero', PositionX: PositionStr, Speed: Hero.getComponent('PlayerState').CurrentSpeed };
  451. cc.find('Canvas').getComponent('GameMode').SynchronizationFun(data);
  452. },
  453. SendSyncRivelPosition: function () {
  454. var Rivel = cc.find("Rivel");
  455. var PositionStr = Rivel.position.x.toString();
  456. var Hero = cc.find("Hero");
  457. var data = { FunctionName: 'Rivel', PositionX: PositionStr, Speed: Hero.getComponent('PlayerState').CurrentSpeed };
  458. cc.find('Canvas').getComponent('GameMode').SynchronizationFun(data);
  459. },
  460. // called every frame, uncomment this function to activate update callback
  461. update: function (dt) {
  462. var PlayerCollisionLine = this.node.getChildByName('Player').getChildByName('PlayerCollisionLine');
  463. if (PlayerCollisionLine) {
  464. if (!PlayerCollisionLine.active) {
  465. if (this.node.position.x - this.node.getComponent('PlayerState').AtStartLinePositionX > 100) {
  466. PlayerCollisionLine.active = true;
  467. }
  468. }
  469. }
  470. var Hero = this.getGameStates.getHero;
  471. var Rivel = this.getGameStates.getRivel;
  472. var HeroControl = this.getGameStates.getHeroControl;
  473. var RivelControl = this.getGameStates.getRivelControl;
  474. var DistanceBetweenMeAndRival = Rivel.x - Hero.x;
  475. // var absDistanceBetweenMeAndRival = Math.abs(Math.floor(DistanceBetweenMeAndRival));
  476. // if (cc.find('LeftRivalHead') != null) {
  477. // cc.find('LeftRivalHead').getChildByName("RivalDistance").getComponent('RivalDistance').setRivalDistanceNum(absDistanceBetweenMeAndRival.toString());
  478. // }
  479. // if (cc.find('RightRivalHead') != null) {
  480. // cc.find('RightRivalHead').getChildByName("RivalDistance").getComponent('RivalDistance').setRivalDistanceNum(absDistanceBetweenMeAndRival.toString());
  481. // }
  482. // cc.log("这是多少",dt);
  483. //这里注释一下,屏幕宽720,一半为360,人物宽65,相加为425,也就是说对手与自己距离425像素外则需显示头像
  484. if (DistanceBetweenMeAndRival > 425) {//大于425,则头像在右边
  485. if (!cc.find('LeftRivalHead')) {
  486. cc.find('UIControl').getComponent('GameUI').displayRivalHead(false);
  487. }
  488. }
  489. if (DistanceBetweenMeAndRival < -425) {//小于-425,则头像在左边
  490. if (!cc.find('RightRivalHead')) {
  491. cc.find('UIControl').getComponent('GameUI').displayRivalHead(true);
  492. }
  493. }
  494. if (DistanceBetweenMeAndRival >= -425 && DistanceBetweenMeAndRival <= 425) {
  495. if (cc.find('RightRivalHead') || cc.find('LeftRivalHead')) {
  496. cc.find('UIControl').getComponent('GameUI').hideRivalHead();
  497. }
  498. }
  499. // this.speed = this.body.linearVelocity;
  500. // this.speed = cc.p(-50,0);
  501. // cc.log("刚体X",this.speed);
  502. // var Level = cc.find("Level");
  503. if(this.getGameStates.getLevelControl)
  504. this.getGameStates.getLevelControl.SetDistance(this.node.getPosition().x);
  505. if (this._moveFlags === MOVE_RIGHT) {
  506. if (this.node.scaleX < 0) {
  507. this.node.scaleX *= -1;
  508. }
  509. var PlayerPositionX = this.node.getPositionX();
  510. PlayerPositionX += 1 * dt * this.PlayerStateScript.CurrentSpeed;
  511. this.node.setPositionX(PlayerPositionX);
  512. }
  513. if (this.jumps > 0 && this._up) {
  514. this.speed.y = this.jumpSpeed;
  515. this.jumps--;
  516. }
  517. //Stop at start line
  518. var PlayerState = this.node.getComponent('PlayerState');
  519. var StartLineStopPositionArray = PlayerState.StartLineStopPositionArray;
  520. if (StartLineStopPositionArray.length != 0) {
  521. // var StartLineStopPositionOriginalX = PlayerState.StartLineStopPositionArray[0].x;
  522. var StartLineStopPositionX = PlayerState.StartLineStopPositionArray[0].x;
  523. if (this.node.name == 'Hero') {
  524. StartLineStopPositionX -= 30;
  525. }
  526. if ((this.node.position.x - StartLineStopPositionX) > -10) {
  527. if (this.node.name == 'Hero') {
  528. // console.log("Hero 停!!");
  529. this.node.setPositionX(StartLineStopPositionX);
  530. cc.find('Rivel').setPositionX(StartLineStopPositionX + 30);
  531. if (PlayerState.StartLineStopPositionArray[0].type == 2) {
  532. //设置 重要的 当前项目是什么
  533. cc.find('Canvas').getComponent('GameStates').CurrentProgress = PlayerState.StartLineStopPositionArray[0].type;
  534. }
  535. PlayerState.StartLineStopPositionArray.shift();
  536. }
  537. }
  538. }
  539. //Reduce speed at Finish line
  540. var FinishLineReduceSpeedPositionArray = PlayerState.FinishLineReduceSpeedPositionArray;
  541. if (FinishLineReduceSpeedPositionArray.length != 0) {
  542. //单车项目是终点,不处理减速
  543. var FinishLineStopPositionX = PlayerState.FinishLineReduceSpeedPositionArray[0];
  544. if ((this.node.position.x - FinishLineStopPositionX) > -10) {
  545. if (cc.find('Canvas').getComponent('GameStates').strCureentLevel == "RideBike") {
  546. // console.log('自行车部分')
  547. return;
  548. }
  549. if (!this.HeroIsStop) {//当前玩家是停止状态,不能设置ResetSpeed_noType
  550. this.ResetSpeed_noType();
  551. if (this.node.name == 'Hero') {
  552. var NetworkSocket = cc.find('Hero').getComponent('NetworkSocket');
  553. var data = {};
  554. data = { FunctionName: 'FinishLineReduceSpeedPositionArray' };
  555. // console.log("hero 减速!!!");
  556. NetworkSocket.sendSyncData(JSON.stringify(data));
  557. }
  558. }
  559. // console.log("Name:",this.node.name);
  560. // this.isChangeSpeed = true;//开启减速
  561. PlayerState.FinishLineReduceSpeedPositionArray.shift();
  562. }
  563. }
  564. this._up = false;
  565. this.body.linearVelocity = this.speed;
  566. this.WriggleControl(dt);
  567. this.addSeendTemp(dt);
  568. // if (!cc.find('Canvas').getComponent('GameStates').BStartGame) return;
  569. if (this.node.name != 'Rivel') return;
  570. var SyschroAtionArray = this.PlayerStateScript.SyschroAtionArray;
  571. if (SyschroAtionArray.length > 0) {
  572. // CustomLog("现在里面有设么没处理的", SyschroAtionArray);
  573. if (SyschroAtionArray[0].FunctionName === 'StaggerAnimation') {
  574. // cc.log("踉跄 通知",this.PlayerStateScript.CurrentSpeed);
  575. if (this.PlayerStateScript.CurrentSpeed != 0) {
  576. this.StaggerAnimation();
  577. }
  578. // cc.log("踉跄 网络",this.node);
  579. SyschroAtionArray.shift();
  580. }
  581. else if (SyschroAtionArray[0].FunctionName === 'SetCurrentSpeedAndAnimation') {
  582. this.SetCurrentSpeedAndAnimation(SyschroAtionArray[0].Speed);
  583. SyschroAtionArray.shift();
  584. }
  585. else if ('ResetSpeed' === SyschroAtionArray[0].FunctionName) {
  586. this.ResetSpeed_noType();
  587. SyschroAtionArray.shift();
  588. }
  589. else if ('SetSpeedToZeroAndIdle' === SyschroAtionArray[0].FunctionName) {
  590. this.SetSpeedToZeroAndIdle();
  591. SyschroAtionArray.shift();
  592. }
  593. else if (SyschroAtionArray[0].FunctionName === 'HeroLongJumpHeroAnimation') {//todo 跳远
  594. // cc.log("当前玩家是 跳远接收",cc.find('Canvas').getComponent('GameStates').bServer);
  595. // cc.log('这里执行了几次。。。。。。。。。');
  596. var tempData = SyschroAtionArray[0].netData;
  597. var longJumpData = {
  598. name: "Rivel",
  599. longJumpCount: tempData.longJumpCount,//跳的距离
  600. takeOffDistance: tempData.takeOffDistance,//蓝区域跳的距离范围
  601. }
  602. RivelControl.HeroLongJumpAnimation(longJumpData);
  603. // console.log("我现在 处理 跳远 2 ", SyschroAtionArray[0].FunctionName, tempData);
  604. // CustomLog("我现在 处理 跳远 2 ", SyschroAtionArray[0].FunctionName);
  605. SyschroAtionArray.shift();
  606. }
  607. else if (SyschroAtionArray[0].FunctionName === 'SetCurrentSpeed_max_jumpLong') {
  608. // CustomLog("跳远设置速度");
  609. //todo 标记2
  610. this.seend = this.PlayerStateScript.CurrentSpeed;
  611. this.SetCurrentSpeed_max_jumpLong(this.seend);
  612. SyschroAtionArray.shift();
  613. }
  614. else if (SyschroAtionArray[0].FunctionName === 'HeroJumpHeroAnimation') {
  615. this.HeroJumpAnimation();
  616. SyschroAtionArray.shift();
  617. }
  618. else if (SyschroAtionArray[0].FunctionName === 'PerfectHurdleBandRailing') {
  619. // CustomLog("跨栏 撞倒","PerfectHurdleBandRailing");
  620. var data = SyschroAtionArray[0];
  621. var NetworkSocket = Hero.getComponent('NetworkSocket');
  622. // CustomLog("跨栏 撞倒", data);
  623. var Obj = NetworkSocket.GetSyncObj(data.ParentName, data.ObjName, data.PositionX);
  624. SyschroAtionArray.shift();
  625. if (Obj == null) {
  626. return;
  627. }
  628. Obj.getComponent("Barrier").FallingDown();
  629. }
  630. else if (SyschroAtionArray[0].FunctionName === 'PerfectHurdleBandBarrier') {
  631. // CustomLog("跨栏 撞倒","PerfectHurdleBandBarrier");
  632. var data = SyschroAtionArray[0];
  633. var NetworkSocket = Hero.getComponent('NetworkSocket');
  634. // CustomLog("跨栏 撞倒",data);
  635. var Obj = NetworkSocket.GetSyncObj(data.ParentName, data.ObjName, data.PositionX);
  636. SyschroAtionArray.shift();
  637. if (Obj == null) {
  638. return;
  639. }
  640. Obj.getComponent("Barrier").FallingDown();
  641. }
  642. else if (SyschroAtionArray[0].FunctionName === 'ShakeBarrier') {
  643. // CustomLog("跨栏 撞倒","PerfectHurdleBandBarrier");
  644. var data = SyschroAtionArray[0];
  645. var NetworkSocket = Hero.getComponent('NetworkSocket');
  646. // CustomLog("跨栏 晃",data);
  647. var Obj = NetworkSocket.GetSyncObj(data.ParentName, data.ObjName, data.PositionX);
  648. SyschroAtionArray.shift();
  649. if (Obj == null) {
  650. return;
  651. }
  652. Obj.getComponent("Barrier").Shake();
  653. }
  654. else if (SyschroAtionArray[0].FunctionName === 'upBike') {
  655. // CustomLog("跨栏 撞倒","PerfectHurdleBandBarrier");
  656. // cc.log("上车 网络", this.node);
  657. this.SetCurrentSpeed(0);
  658. this.upBike();
  659. var Charactor = this.node.getChildByName('Player');
  660. var CharactorScript = Charactor.getComponent("Charactor");
  661. setTimeout(function () {
  662. CharactorScript.spine.timeScale = 1;
  663. cc.find('Canvas').getComponent('GameStates').CurrentProgress = GameStatesStatic.GameProgress.RideBike;
  664. this.SetCurrentSpeed(PlayerStateStatic.SpeedRangeForAnimation.SlowSpeed[1]);
  665. }.bind(this), 600);
  666. SyschroAtionArray.shift();
  667. }
  668. else if (SyschroAtionArray[0].FunctionName === 'downBikeHero') {
  669. // CustomLog("跨栏 撞倒","PerfectHurdleBandBarrier");
  670. SyschroAtionArray.shift();
  671. }
  672. else if (SyschroAtionArray[0].FunctionName === 'upJavelinHeroAnimation') {
  673. var data = SyschroAtionArray[0];
  674. var NetworkSocket = Hero.getComponent('NetworkSocket');
  675. CustomLog("捡起来标枪");
  676. var Obj = NetworkSocket.GetSyncObj(data.ParentName, data.ObjName, data.PositionX);
  677. this.upJavelin();
  678. this.SetCurrentSpeed(PlayerStateStatic.SpeedRangeForAnimation.SlowSpeed[1]);
  679. if (Obj != null) {
  680. Obj.active = false;
  681. }
  682. cc.find('Canvas').getComponent('GameStates').CurrentProgress = GameStatesStatic.GameProgress.Javelin;
  683. SyschroAtionArray.shift();
  684. }
  685. else if (SyschroAtionArray[0].FunctionName === 'Restrictions') {
  686. this.SetCurrentSpeed_noRestrictions(600);
  687. SyschroAtionArray.shift();
  688. }
  689. else if (SyschroAtionArray[0].FunctionName === 'FinishLineReduceSpeedPositionArray') {
  690. this.ResetSpeed_noType();
  691. SyschroAtionArray.shift();
  692. }
  693. else if (SyschroAtionArray[0].FunctionName === 'PlayersStartToRun') {
  694. // console.log("PlayersStartToRun!!!");
  695. Hero.getComponent('HeroControl').playAudioByName('StartGun');//项目开始时哨声
  696. //项目开始时,UI比分淡出
  697. if (cc.find('UIPKScore')) {
  698. cc.find('UIControl').getComponent('GameUI').hideMiddlePKScore();
  699. }
  700. this.PlayerStateScript.TouchControlEnableTouch = false;
  701. HeroControl.HeroIsStop = false;
  702. RivelControl.HeroIsStop = false;
  703. HeroControl.ResetSpeed_noType();
  704. RivelControl.ResetSpeed_noType();
  705. // cc.log("被拉过来的起点位置",cc.find('Canvas').getComponent('GameStates').StartLinePositionX);
  706. SyschroAtionArray.shift();
  707. }
  708. else if (SyschroAtionArray[0].FunctionName === 'StopPlayerAllSchedule') {
  709. HeroControl.StopPlayerAllSchedule();
  710. RivelControl.StopPlayerAllSchedule();
  711. SyschroAtionArray.shift();
  712. }
  713. else if (SyschroAtionArray[0].FunctionName === 'upJavelinend') {
  714. this.SetCurrentSpeed_noRestrictions(SyschroAtionArray[0].Speed);
  715. SyschroAtionArray.shift();
  716. }
  717. else if (SyschroAtionArray[0].FunctionName === 'JavelinData') {
  718. this.javelinRivel("Rivel", SyschroAtionArray[0].JavelinData, SyschroAtionArray[0].JaveCount);
  719. SyschroAtionArray.shift();
  720. }
  721. else if (SyschroAtionArray[0].FunctionName === 'resetHero') {
  722. this.resetHero();
  723. SyschroAtionArray.shift();
  724. }
  725. else if (SyschroAtionArray[0].FunctionName === 'resetHerojavelin') {
  726. this.stopAudioByName("JavelinFlying");//停止标枪飞的声音
  727. // this.resets();
  728. this.SetSpeedToZeroAndIdle();
  729. SyschroAtionArray.shift();
  730. }
  731. else if (SyschroAtionArray[0].FunctionName === 'endGame') {
  732. var HeroControl = Hero.getComponent("HeroControl");
  733. var NetworkSocket = Hero.getComponent("NetworkSocket");
  734. NetworkSocket.endGameAll(2);
  735. //console.log('退出游戏时候的数据上报 ==',NetworkSocket.isReported);
  736. if (!NetworkSocket.isReported) {
  737. if(SyschroAtionArray[0].showText){
  738. NetworkSocket.setToast(SyschroAtionArray[0].showText);
  739. }
  740. var data = {
  741. isSelf: SyschroAtionArray[0].num
  742. }
  743. NetworkSocket.quitGame(data);
  744. }
  745. SyschroAtionArray.shift();
  746. }
  747. }
  748. },
  749. upJavelin: function () {
  750. var Charactor = this.node.getChildByName('Player');
  751. var CharactorScript = Charactor.getComponent("Charactor");
  752. CharactorScript.upJavelin();
  753. this.SetCurrentSpeedAction();
  754. },
  755. upBike: function () {
  756. var Charactor = this.node.getChildByName('Player');
  757. var CharactorScript = Charactor.getComponent("Charactor");
  758. CharactorScript.upBike();
  759. this.SetCurrentSpeedAction();
  760. },
  761. upJavelin_go: function (callBack) {
  762. var Charactor = this.node.getChildByName('Player');
  763. var CharactorScript = Charactor.getComponent("Charactor");
  764. CharactorScript.upJavelin_go(callBack);
  765. this.playAudioByName("Hei");
  766. },
  767. idle: function (callBack) {
  768. var Charactor = this.node.getChildByName('Player');
  769. var CharactorScript = Charactor.getComponent("Charactor");
  770. CharactorScript.idle(callBack);
  771. },
  772. readyRun: function (callBack) {
  773. var Charactor = this.node.getChildByName('Player');
  774. var CharactorScript = Charactor.getComponent("Charactor");
  775. CharactorScript.readyRun(callBack);
  776. },
  777. addidle: function (callBack) {
  778. var Charactor = this.node.getChildByName('Player');
  779. var CharactorScript = Charactor.getComponent("Charactor");
  780. CharactorScript.addidle(callBack);
  781. },
  782. idleadd: function (callBack) {
  783. var Charactor = this.node.getChildByName('Player');
  784. var CharactorScript = Charactor.getComponent("Charactor");
  785. CharactorScript.idleadd(callBack);
  786. },
  787. playAudioByName: function (AudioName) {
  788. var AudioSource = this.getAudioSourceByName(AudioName);
  789. if (AudioSource != null) {
  790. // if (AudioName == "Boos") {//踉跄时的音效:若正在播放此音效则不能播放;
  791. // if (this.isPlayingBoos == false) {
  792. // this.isPlayingBoos = true;
  793. // AudioSource.play();
  794. // this.scheduleOnce(function () {
  795. // this.isPlayingBoos = false;
  796. // }, AudioSource.getDuration());
  797. // }
  798. // } else {
  799. AudioSource.play();
  800. // }
  801. }
  802. },
  803. stopAudioByName: function (AudioName) {
  804. // cc.log("我明明stop了啊!!!!!!"+AudioName);
  805. var AudioSource = this.getAudioSourceByName(AudioName);
  806. if (AudioSource != null) {
  807. AudioSource.stop();
  808. // cc.log("我明明stop了啊!!!!!!222222");
  809. }
  810. },
  811. getAudioSourceByName: function (AudioName) {
  812. var HeroPlayer = null;
  813. // cc.log(AudioName);
  814. if (this.node.name == "Hero") {
  815. if (cc.find('Hero').getChildByName('Player') != null) {
  816. HeroPlayer = cc.find('Hero').getChildByName('Player');
  817. if (HeroPlayer.getChildByName("MyAudio") != null) {
  818. var AudioControlScript = HeroPlayer.getChildByName('MyAudio').getComponent("AudioControl");
  819. if (AudioControlScript.getAudioSourceByName(AudioName) != null) {
  820. var AudioSource = AudioControlScript.getAudioSourceByName(AudioName);
  821. return AudioSource;
  822. }
  823. }
  824. }
  825. }
  826. },
  827. addPerfectGradeAndPlay: function () {
  828. if (this.PerfectGrade < 10) {
  829. this.PerfectGrade += 1;
  830. }
  831. this.playAudioByName("Perfect" + this.PerfectGrade);
  832. },
  833. resetPerfectGrade: function () {
  834. this.PerfectGrade = 0;
  835. },
  836. javelin: function () {
  837. //获取场景
  838. var scene = cc.director.getScene();
  839. // cc.log("父亲",scene);
  840. var charactor = scene.getChildByName("Hero").getChildByName("Player").getComponent("Charactor");
  841. // cc.log("有么",charactor);
  842. var HeroControl = scene.getChildByName("Hero").getComponent("HeroControl");
  843. // console.log("javelin");
  844. if (HeroControl != null) {
  845. var Player = scene.getChildByName("Hero").getChildByName("Player");
  846. //扔标枪
  847. HeroControl.upJavelin_go();
  848. this.seend = HeroControl.PlayerStateScript.CurrentSpeed;
  849. setTimeout(function () {
  850. // cc.log("标枪预制 1",this.javelin_prefab);
  851. var tPrefab = this.tPrefabs = cc.instantiate(this.javelin_prefab);
  852. // cc.log("标枪预制 2",tPrefab);
  853. var Level = cc.find("Level");
  854. var Hero = cc.find("Hero");
  855. //扔标枪 计数算法 todo
  856. var UIControl = cc.find("UIControl").getComponent("UIControl");
  857. UIControl.startDistance(1, Hero, true);
  858. tPrefab.parent = Hero;
  859. tPrefab.zIndex = 999;
  860. tPrefab.setPosition(100, -50);
  861. var HeroControl = scene.getChildByName("Hero").getComponent("HeroControl");
  862. var data = {
  863. duration: 3.5,
  864. position: 0,
  865. y: -70,
  866. height: 500,
  867. jumps: 1
  868. };
  869. tPrefab.getComponent("Charactor_javeLin").javelin(data, function () {
  870. var datas = {
  871. duration: 1,
  872. position: -1000,
  873. y: 0
  874. };
  875. this.javelinmoveHero(datas, Player);
  876. HeroControl.SetCurrentSpeed(this.seend + 1000);
  877. console.log("Start Javelin;")
  878. }.bind(this), function () {
  879. console.log("End Javelin;")
  880. var Level = cc.find("Level");
  881. HeroControl.SetCurrentSpeed(0);
  882. var datas = {
  883. duration: 2,
  884. position: 1000,
  885. y: 0
  886. };
  887. charactor.readyRun();
  888. //人物进场
  889. this.javelinmoveHero(datas, Player, null, function () {
  890. var p = tPrefab.getBoundingBoxToWorld();
  891. tPrefab.parent = Level;
  892. tPrefab.setPosition(p.x - 360, p.y - 640);
  893. // CustomLog("现在坐标 父类",p,p1,tPrefab.getBoundingBoxToWorld());
  894. HeroControl.SetCurrentSpeed(500);
  895. UIControl.startDistance(1, Hero, false);
  896. }.bind(this));
  897. }.bind(this));
  898. this.node.destroy();
  899. }.bind(this), 500);
  900. }
  901. },
  902. javelinmove: function (data, view, starListener, endListener) {
  903. var actionBy = cc.moveBy(data.duration, data.position, data.y);
  904. var start = cc.callFunc(function () {
  905. if (starListener != null) {
  906. starListener();
  907. }
  908. }.bind(this), this);
  909. var stop = cc.callFunc(function () {
  910. if (endListener != null) {
  911. endListener();
  912. }
  913. }.bind(this), this);
  914. var myAction = cc.sequence(start, actionBy, stop);
  915. view.runAction(myAction);
  916. },
  917. javelinRivel: function (name, javalinData, JaveCount) {
  918. // console.log("javelin rivel;");
  919. //获取场景
  920. var scene = cc.director.getScene();
  921. // cc.log("父亲",scene);
  922. var charactor = scene.getChildByName(name).getChildByName("Player").getComponent("Charactor");
  923. // cc.log("有么",charactor);
  924. var HeroControl = scene.getChildByName(name).getComponent("HeroControl");
  925. HeroControl.javelinTag = true;
  926. //console.log("javelin rivel;");
  927. if (HeroControl != null) {
  928. var Hero = scene.getChildByName(name);
  929. //扔标枪
  930. HeroControl.upJavelin_go();
  931. HeroControl.idleadd();
  932. HeroControl.SetCurrentSpeed(0);
  933. // return;
  934. setTimeout(function () {
  935. HeroControl = scene.getChildByName(name).getComponent("HeroControl");
  936. if (!HeroControl.javelinTag) {
  937. return;
  938. }
  939. var tPrefab = cc.instantiate(this.javetemp_Rivel);
  940. tPrefab.parent = cc.director.getScene();//中部PK积分显示后隐藏
  941. cc.find("Level").getComponent("LevelControl").addJaveLin(tPrefab);
  942. this.addCamera(tPrefab);
  943. tPrefab.active = true;
  944. tPrefab.isTag = true;
  945. tPrefab.zIndex = 999;
  946. tPrefab.setPosition(Hero.x, Hero.y + 100);
  947. var HeroControl = scene.getChildByName(name).getComponent("HeroControl");
  948. var x = 1440 + JaveCount * 72;
  949. var data = javalinData;
  950. tPrefab.javeTime = data.duration;
  951. if (cc.find('Canvas').getComponent('GameStates').bServer) {//如果是服务器,扔标枪后记录在自己的GameMode上面
  952. var TempGameMode = cc.find('Canvas').getComponent('GameMode');
  953. TempGameMode.isClientJaveDis = x;
  954. }
  955. var datas = {
  956. duration: data.duration,
  957. position: x,
  958. y: 0
  959. };
  960. tPrefab.getChildByName("jave").getComponent("Charactor_javeLin").javelin(data, function () {
  961. if (!HeroControl.javelinTag) {
  962. tPrefab.isTag = null;
  963. return;
  964. }
  965. HeroControl.javelinmoveHero(datas, tPrefab);
  966. }.bind(this), function () {
  967. // return;
  968. // tPrefab.isTag = null;
  969. // if (!HeroControl.javelinTag) {
  970. // tPrefab.isTag = null;
  971. // return;
  972. // }
  973. // HeroControl.javelinmoveHero_act(datas);
  974. // if (tPrefab.x - this.node.x > 3000) {
  975. // this.node.x = tPrefab.x-720;
  976. // }
  977. // charactor.readyRun();
  978. }.bind(this));
  979. }.bind(this), 150);
  980. }
  981. },
  982. //计算height,和duration
  983. javelinData: function (x) {
  984. var MiddleX = x / 2;
  985. var highest = Math.sqrt(x * x - MiddleX * MiddleX) - x;
  986. highest = highest < 0 ? -highest : highest;
  987. highest += 0.2;
  988. // console.log("标枪的x值=", x, "this.javeCount=", this.javeCount, "highest==", highest);
  989. // console.log("扔多高",highest);
  990. var tempDuration = x > 5000 ? 4 : 2;
  991. var data = {
  992. duration: tempDuration,
  993. position: 0,
  994. y: -70,
  995. height: 600,//600
  996. jumps: 1
  997. };
  998. return data;
  999. },
  1000. javelinHero: function (name) {
  1001. //获取场景
  1002. // var scene = cc.director.getScene();
  1003. // cc.log("父亲",scene);
  1004. var charactor = this.CharactorScript;//scene.getChildByName(name).getChildByName("Player").getComponent("Charactor");
  1005. // cc.log("有么",charactor);
  1006. var HeroControl = this;//scene.getChildByName(name).getComponent("HeroControl");
  1007. var Hero = this.node;//cc.find(name);
  1008. var NetworkSocket = Hero.getComponent('NetworkSocket');
  1009. // cc.log("有没有",HeroControl.javelinTag);
  1010. HeroControl.javelinTag = true;
  1011. if (HeroControl != null) {
  1012. //扔标枪
  1013. HeroControl.upJavelin_go();
  1014. var UIControl = cc.find("UIControl").getComponent("UIControl");
  1015. HeroControl.idleadd();
  1016. HeroControl.SetCurrentSpeed(0);
  1017. // HeroControl.HeroIsStop = true;//玩家停止
  1018. var x = 1440 + this.javeCount * 72;
  1019. var data = this.javelinData(x);
  1020. //同步数据给rivel
  1021. var NetData = { FunctionName: 'JavelinData', JavelinData: data, JaveCount: this.javeCount, HeroPosition: Hero.position.x };
  1022. NetworkSocket.sendSyncData(JSON.stringify(NetData));
  1023. if (cc.find('Canvas').getComponent('GameStates').bServer) {//如果是服务器,扔标枪后记录在自己的GameMode上面
  1024. var TempGameMode = cc.find('Canvas').getComponent('GameMode');
  1025. TempGameMode.isSeverJaveDis = x;
  1026. }
  1027. // return;
  1028. setTimeout(function () {
  1029. // HeroControl = scene.getChildByName(name).getComponent("HeroControl");
  1030. // cc.log("到终点",HeroControl.javelinTag);
  1031. if (!HeroControl.javelinTag) {
  1032. return;
  1033. }
  1034. var tPrefab = cc.instantiate(this.javetemp_Hero);
  1035. tPrefab.parent = cc.director.getScene();//中部PK积分显示后隐藏
  1036. cc.find("Level").getComponent("LevelControl").addJaveLin(tPrefab);
  1037. tPrefab.active = true;
  1038. tPrefab.isTag = true;
  1039. HeroControl.CameraNode.parent = tPrefab;
  1040. this.addCamera(tPrefab);
  1041. tPrefab.zIndex = 999;
  1042. tPrefab.setPosition(Hero.x, Hero.y);
  1043. // var HeroControl = scene.getChildByName(name).getComponent("HeroControl");
  1044. var datas = {
  1045. duration: data.duration,
  1046. position: x,
  1047. y: 0
  1048. };
  1049. tPrefab.getChildByName("jave").getComponent("Charactor_javeLin").javelin(data, function () {
  1050. if (!HeroControl.javelinTag) {
  1051. tPrefab.isTag = null;
  1052. return;
  1053. }
  1054. tPrefab.javeTime = data.duration;
  1055. UIControl.startDistance_Hero(1, tPrefab, true);
  1056. HeroControl.javelinmoveHero(datas, tPrefab);
  1057. // cc.log("变速 扔标枪1", HeroControl.PlayerStateScript.CurrentSpeed);
  1058. }.bind(this), function () {
  1059. this.stopAudioByName("JavelinFlying");//停止标枪飞的声音
  1060. this.playAudioByName("JavelinDown");//播放标枪落地的声音
  1061. if (!HeroControl.javelinTag) {
  1062. tPrefab.isTag = null;
  1063. return;
  1064. }
  1065. //加分
  1066. setTimeout(function () {
  1067. UIControl.setCallBack_Hero(function (m) {
  1068. }.bind(this));
  1069. // if (!cc.find('Canvas').getComponent('GameStates').bServer) {
  1070. // var data = { FunctionName: 'reqAddScore'};
  1071. // cc.find('Canvas').getComponent('GameMode').SynchronizationFun(data);
  1072. // }
  1073. // else {
  1074. // cc.find('Canvas').getComponent('GameMode').SyncJaveEndAddScore();
  1075. // }
  1076. //加分
  1077. if (!cc.find('Canvas').getComponent('GameStates').bServer) {
  1078. var data = { FunctionName: 'addScore', PlayerName: 'Hero', value: x };
  1079. cc.find('Canvas').getComponent('GameMode').SynchronizationFun(data);
  1080. }
  1081. else {
  1082. cc.find('Canvas').getComponent('GameMode').addScore(cc.find('Hero'), x, true);
  1083. }
  1084. tPrefab.isTag = null;
  1085. charactor.readyRun();
  1086. // var p1 = tPrefab.getPosition();
  1087. }.bind(this), 1000);
  1088. }.bind(this));
  1089. }.bind(this), 150);
  1090. }
  1091. },
  1092. resets: function () {//这个是加分后执行的
  1093. setTimeout(function () {
  1094. var GameMode = cc.find('Canvas').getComponent('GameMode');
  1095. var GameStates = cc.find('Canvas').getComponent('GameStates');
  1096. this.SetSpeedToZeroAndIdle();
  1097. //标枪结束
  1098. var data = {
  1099. gameLv: 2,
  1100. startPositionX: 150000
  1101. }
  1102. cc.find("Level").getComponent("LevelControl").setStart(data);
  1103. GameStates.StartLinePositionX = cc.find("Level").getComponent("LevelControl").tempStartNode.x + 720;
  1104. GameMode.CheckPlayersDistance();
  1105. var data = { FunctionName: 'CheckPlayersDistance', StartLinePositionX: GameStates.StartLinePositionX };
  1106. GameMode.SynchronizationFun(data);
  1107. }.bind(this), 1000);
  1108. },
  1109. javelinmoveRivel: function (data, view, starListener, endListener) {
  1110. var actionBy = cc.moveBy(data.duration, data.position, data.y);
  1111. var start = cc.callFunc(function () {
  1112. if (starListener != null) {
  1113. starListener();
  1114. }
  1115. }.bind(this), this);
  1116. var stop = cc.callFunc(function () {
  1117. if (endListener != null) {
  1118. endListener();
  1119. }
  1120. }.bind(this), this);
  1121. var myAction = cc.sequence(start, actionBy, stop);
  1122. view.runAction(myAction);
  1123. },
  1124. javelinmoveHero: function (data, view, starListener, endListener) {
  1125. // console.log("javelinMoveHero = ", data);
  1126. var actionBy = cc.moveBy(data.duration, data.position, data.y);
  1127. var start = cc.callFunc(function () {
  1128. if (starListener != null) {
  1129. starListener();
  1130. }
  1131. }.bind(this), this);
  1132. var stop = cc.callFunc(function () {
  1133. if (endListener != null) {
  1134. endListener();
  1135. }
  1136. }.bind(this), this);
  1137. var myAction = cc.sequence(start, actionBy, stop);
  1138. view.runAction(myAction);
  1139. },
  1140. javelinmoveHero_act: function (data) {
  1141. var speed = data.position / (data.duration + 1);
  1142. // cc.log("速度是",speed);
  1143. this.SetCurrentSpeed_noRestrictions(speed);
  1144. },
  1145. stopBikeHero: function (name) {
  1146. var charactor = cc.find(name).getChildByName("Player").getComponent("Charactor");
  1147. if (charactor != null) {
  1148. // charactor.stop();
  1149. cc.find(name).getComponent('HeroControl').SetCurrentSpeed(0);
  1150. cc.find(name).getComponent("PlayerState").enableTouch = false;
  1151. }
  1152. },
  1153. downBikeHero: function (name) {
  1154. var charactor = cc.find(name).getChildByName("Player").getComponent("Charactor");
  1155. // console.log(name, ":当前目标1");
  1156. if (charactor != null) {
  1157. charactor.downBike(function () {
  1158. //todo
  1159. }.bind(this));
  1160. charactor.idleadd();
  1161. cc.find(name).getComponent('HeroControl').SetCurrentSpeed(0);
  1162. cc.find(name).getComponent("PlayerState").enableTouch = false;
  1163. // console.log(name, ":当前目标2");
  1164. }
  1165. },
  1166. downBikeRivel: function (name) {
  1167. var charactor = cc.find(name).getChildByName("Player").getComponent("Charactor");
  1168. if (charactor != null) {
  1169. charactor.downBike(function () {
  1170. }.bind(this));
  1171. charactor.idleadd();
  1172. cc.find(name).getComponent('HeroControl').SetCurrentSpeed(0);
  1173. cc.find(name).getComponent("PlayerState").enableTouch = false;
  1174. }
  1175. },
  1176. addCamera: function (node) {
  1177. // var CameraNode = cc.find("Hero").getChildByName("CameraNode");
  1178. var Hero;
  1179. var CameraNode;
  1180. var javetemp_Hero;
  1181. if (cc.find("Hero").getChildByName("CameraNode") != null) {
  1182. Hero = cc.find("Hero").getChildByName("CameraNode").getComponent("cc.Camera")
  1183. }
  1184. if (cc.find("CameraNode") != null) {
  1185. CameraNode = cc.find("CameraNode").getComponent("cc.Camera");
  1186. }
  1187. if (cc.find("javetemp_Hero") != null) {
  1188. if (cc.find("javetemp_Hero").getChildByName("CameraNode") != null) {
  1189. javetemp_Hero = cc.find("javetemp_Hero").getChildByName("CameraNode").getComponent("cc.Camera");
  1190. }
  1191. }
  1192. if (Hero != null) {
  1193. var Camera = cc.find("Hero").getChildByName("CameraNode").getComponent("cc.Camera");
  1194. Camera.addTarget(node);
  1195. }
  1196. else if (CameraNode != null) {
  1197. var Camera = cc.find("CameraNode").getComponent("cc.Camera");
  1198. Camera.addTarget(node);
  1199. }
  1200. else if (javetemp_Hero != null) {
  1201. var Camera = cc.find("javetemp_Hero").getChildByName("CameraNode").getComponent("cc.Camera");
  1202. Camera.addTarget(node);
  1203. }
  1204. },
  1205. resetHero: function (data) {
  1206. var Hero = cc.find('Hero');
  1207. var Rivel = cc.find('Rivel');
  1208. var HeroHeroControl = Hero.getComponent("HeroControl");
  1209. var RivelHeroControl = Rivel.getComponent("HeroControl");
  1210. HeroHeroControl.SetSpeedToZeroAndIdle();
  1211. RivelHeroControl.SetSpeedToZeroAndIdle();
  1212. HeroHeroControl.javelinTag = false;
  1213. RivelHeroControl.javelinTag = false;
  1214. Hero.getChildByName('Player').stopAllActions();
  1215. Rivel.getChildByName('Player').stopAllActions();
  1216. Hero.getChildByName('Player').setPositionY(-132);
  1217. Rivel.getChildByName('Player').setPositionY(-37);
  1218. HeroHeroControl.CameraNode.parent = Hero;
  1219. RivelHeroControl.isJumpOver = true;
  1220. HeroHeroControl.isJumpOver = true;
  1221. //重置人物踉跄判定
  1222. var PlayerStateScript = Rivel.getComponent("PlayerState");
  1223. PlayerStateScript.CurrentInZoneType = PlayerStateStatic.ZoneType.None;
  1224. PlayerStateScript.BTouchedScreenInTheZone = false;
  1225. PlayerStateScript.BStagger = false;
  1226. if (data != null) {
  1227. Hero.setPositionX(data.node.x + 700);
  1228. Rivel.setPositionX(data.node.x + 730);
  1229. }
  1230. },
  1231. });