Charactor.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. const GameStatesStatic = require('GameStates');
  2. const PlayerState = require('PlayerState');
  3. const GameProgress = cc.Enum({
  4. default: 0,
  5. Race: 1,
  6. LongJump: 2,
  7. Hurdle: 3,
  8. RideBike: 4,
  9. Javelin: 5,
  10. });
  11. cc.Class({
  12. extends: cc.Component,
  13. properties: {
  14. readyRunTag: 0,
  15. //当前进行的项目
  16. type: 0,
  17. Canvas: {
  18. default: null,
  19. type: cc.Node
  20. },
  21. Hero: null,
  22. HeroControlScript: null,
  23. CurrentProgress: {
  24. default: GameProgress.Race,
  25. type: cc.Enum(GameProgress)
  26. },
  27. },
  28. // onLoad: function () {
  29. // this.Hero = this.node.parent;
  30. // this.HeroControlScript = this.Hero.getComponent('HeroControl');
  31. // },
  32. setSkins: function (skinName) {
  33. //随机皮肤
  34. this.spine.setSkin(skinName);
  35. },
  36. onLoad: function () {
  37. this.Hero = this.node.parent;
  38. this.HeroControlScript = this.Hero.getComponent('HeroControl');
  39. var spine = this.spine = this.getComponent('sp.Skeleton');
  40. // cc.log("现在 播放 骨骼动画对象", spine);
  41. this._hasStop = true;
  42. this.setType();
  43. this.initData();
  44. this.idle();
  45. // .setStartListener()用来设置开始播放动画的事件监听。
  46. spine.setStartListener(function (trackEntry) {
  47. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  48. // cc.log("[track %s][animation %s] start.", trackEntry.trackIndex, animationName);
  49. // cc.log("现在 播放 的动画名字是", animationName);
  50. if (animationName == this.arr[this.type][this.walkLv]) {
  51. if (this.readyRunTag == 0) {
  52. }
  53. this.readyRunTag++;
  54. // this._hasStop = false;
  55. }
  56. this.setStatus(animationName);
  57. }.bind(this));
  58. // .setInterruptListener()用来设置动画被打断的事件监听。
  59. spine.setInterruptListener(function (trackEntry) {
  60. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  61. if (this.interruptListener != null) {
  62. this.interruptListener(animationName);
  63. }
  64. }.bind(this));
  65. // .setEndListener()用来设置动画播放完后的事件监听。
  66. spine.setEndListener(function (trackEntry) {
  67. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  68. // cc.log("现在 停止 的动画名字是", animationName);
  69. if (animationName == this.statusArr[6] || animationName == this.statusArr[5]) {
  70. }
  71. if (this.endListener != null) {
  72. this.endListener(animationName);
  73. }
  74. }.bind(this));
  75. // .setDisposeListener()用来设置动画将被销毁的事件监听。
  76. spine.setDisposeListener(function (trackEntry) {
  77. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  78. }.bind(this));
  79. spine.setCompleteListener(function (trackEntry, loopCount) {
  80. }.bind(this));
  81. // .setEventListener()用来设置动画播放过程中帧事件的监听。
  82. spine.setEventListener(function (trackEntry, event) {
  83. }.bind(this));
  84. },
  85. initData: function () {
  86. //初始化 动作列表
  87. this.statusArr = [
  88. //0
  89. "none",
  90. //1
  91. "idle",
  92. //2
  93. "idle-run",
  94. //3
  95. "Run1",
  96. //4
  97. "Run2",
  98. //5
  99. "Run3",
  100. //6
  101. "Run4",
  102. //7
  103. "Run5",
  104. //8
  105. "hurdling_1",
  106. //9
  107. "hurdling_2",
  108. //10
  109. "longjump-01",
  110. //11
  111. "longjump-02",
  112. "bybike_1",
  113. "bybike_2",
  114. "bybike_3",
  115. "bybike_4",
  116. "bybike_5",
  117. "bybike_5bike",
  118. "bybike_5boy",
  119. "bybike_redy",
  120. "bybike_redy_bike",
  121. "bybike_redy_boy",
  122. "Biaoqiang01",
  123. "Biaoqiang02",
  124. "Biaoqiang03",
  125. "javelin_1",
  126. "javelin_2",
  127. "javelin_2a",
  128. "javelin_3",
  129. "javelin_3a",
  130. "javelin_4",
  131. "javelin_5",
  132. "javelin_redy"
  133. ];
  134. //todo 准备跑步动作
  135. this.statusArr_redyrun = [
  136. //0
  137. "idle",
  138. //1
  139. "idle-run",
  140. ];
  141. this.arr = [];
  142. //todo 跑步动作
  143. this.statusArr_run = [
  144. //0
  145. "Run1",
  146. //1
  147. "Run2",
  148. //2
  149. "Run3",
  150. //3
  151. "Run4",
  152. //4
  153. "Run5",
  154. ];
  155. this.arr.push(this.statusArr_run);
  156. //todo 跳远
  157. var a = [];
  158. this.statusArr_longjump = a.concat(this.statusArr_run);
  159. //5
  160. this.statusArr_longjump.push("longjump-Test");
  161. //6
  162. this.statusArr_longjump.push("longjump-02");
  163. this.arr.push(this.statusArr_longjump);
  164. //todo 跨栏
  165. a = [];
  166. this.statusArr_hurdling = a.concat(this.statusArr_run);
  167. //5
  168. this.statusArr_hurdling.push("hurdling_1");
  169. //6
  170. this.statusArr_hurdling.push("hurdling_2");
  171. this.arr.push(this.statusArr_hurdling);
  172. //todo 蹬三轮
  173. this.statusArr_bike = [
  174. //0
  175. "bybike_1",
  176. //1
  177. "bybike_2",
  178. //2
  179. "bybike_6",
  180. //3
  181. "bybike_4",
  182. //4
  183. "bybike_5",
  184. //5
  185. "bybike_5bike",
  186. //6
  187. "bybike_5boy",
  188. //7
  189. "bybike_redy",
  190. //8
  191. "bybike_redy_bike",
  192. //9
  193. "bybike_redy",
  194. //10
  195. "bybike_3",
  196. ];
  197. this.arr.push(this.statusArr_bike);
  198. //todo 标枪项目
  199. this.statusArr_javelin = [
  200. //0
  201. "javelin_1",
  202. //1
  203. "javelin_2",
  204. //2
  205. // "javelin_2a",
  206. //2
  207. "javelin_3",
  208. //3
  209. // "javelin_3a",
  210. //3
  211. "javelin_4",
  212. //4
  213. "javelin_5",
  214. //5
  215. "javelin_redy",
  216. //6
  217. "javelin_5",
  218. ];
  219. this.arr.push(this.statusArr_javelin);
  220. //todo 标枪
  221. this.statusArr_biaoqiang = [
  222. "Biaoqiang01",
  223. "Biaoqiang02",
  224. "Biaoqiang03",
  225. ];
  226. //踉跄数组
  227. this.staggerArr = [];
  228. this.staggerArr.push(this.statusArr_run[3]);
  229. this.staggerArr.push(this.statusArr_javelin[3]);
  230. this.staggerArr.push(this.statusArr_bike[3]);
  231. //初始化动作状态
  232. this.setStatus(this.statusArr[1]);
  233. this.setMyTag(null);
  234. },
  235. setMyTag: function (obj) {
  236. this.myTag = obj;
  237. },
  238. idle: function () {
  239. this.spine.timeScale = 1.0;
  240. this.spine.setAnimation(0, this.statusArr_redyrun[0], true);
  241. },
  242. idleadd: function () {
  243. this.spine.addAnimation(0, this.statusArr_redyrun[0], true);
  244. },
  245. addidle: function () {
  246. this.spine.setAnimation(0, this.statusArr_redyrun[0], false);
  247. },
  248. test: function () {
  249. this.spine.setAnimation(0, this.statusArr_run[2], true);
  250. },
  251. walk: function () {
  252. // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
  253. this.spine.setAnimation(0, this.statusArr[3], true);
  254. },
  255. walkadd: function () {
  256. // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
  257. this.spine.addAnimation(0, this.statusArr[3], true);
  258. },
  259. readyRun: function () {
  260. // this.spine.setAnimation(0, this.statusArr_redyrun[0], false);
  261. // this.spine.addAnimation(0, this.statusArr_redyrun[1], false);
  262. this.spine.setAnimation(0, this.statusArr_run[0], true);
  263. //奔跑等级
  264. this.walkLv = 0;
  265. },
  266. readyRun_add: function () {
  267. // this.spine.setAnimation(0, this.statusArr_redyrun[0], false);
  268. // this.spine.addAnimation(0, this.statusArr_redyrun[1], false);
  269. this.spine.addAnimation(0, this.statusArr_run[0], true);
  270. //奔跑等级
  271. this.walkLv = 0;
  272. },
  273. SetCurrentAnimationIdx: function (idx) {
  274. this.spine.addAnimation(0, this.arr[this.type][idx], true);
  275. },
  276. SetCurrentAnimationIdx_set: function (idx) {
  277. this.spine.setAnimation(0, this.arr[this.type][idx], true);
  278. },
  279. //todo 设置对应动作
  280. setCurrentSpeed: function () {
  281. this.walkLv = this.getSpeedLv();
  282. // cc.log("当前速度","档位",this.walkLv);
  283. this.spine.addAnimation(0, this.arr[this.type][this.walkLv], true);
  284. this.spine.timeScale = 1;
  285. },
  286. stagger: function () {
  287. this.spine.setAnimation(0, this.arr[this.type][3], false);
  288. },
  289. longjump: function (jumpTimer) {
  290. // this.spine.timeScale = 1.5;
  291. this.spine.setAnimation(0, this.statusArr_longjump[5], false);
  292. this.spine.timeScale = 1/jumpTimer;
  293. // console.log("timeScale ==", jumpTimer, this.spine.timeScale);
  294. var OutTimer = jumpTimer* 1000 + 300;
  295. // console.log("jumpTimer ==", jumpTimer, OutTimer);
  296. setTimeout(function () {
  297. if (this.timeOutLongJumpListener != null) {
  298. this.timeOutLongJumpListener();
  299. }
  300. }.bind(this), OutTimer);
  301. },
  302. setTimeOutLongJumpListener: function (timeOutLongJumpListener) {
  303. this.timeOutLongJumpListener = timeOutLongJumpListener;
  304. },
  305. longjump_stagger: function () {
  306. this.spine.setAnimation(0, this.statusArr_longjump[1], false);
  307. this.setCurrentSpeed();
  308. },
  309. hurdling: function (callBack) {
  310. this.spine.setAnimation(0, this.statusArr_hurdling[5], false);
  311. this.spine.addAnimation(0, this.statusArr_hurdling[this.walkLv], true);
  312. this.spine.timeScale = 1.5;
  313. if (callBack != null) {
  314. this.spine.setInterruptListener(function (trackEntry) {
  315. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  316. if ("hurdling_1" == animationName) {
  317. callBack(animationName);
  318. this.spine.timeScale = 1;
  319. }
  320. }.bind(this));
  321. }
  322. this.setCurrentSpeed();
  323. },
  324. hurdling_stagger: function () {
  325. this.spine.setAnimation(0, this.statusArr_hurdling[1], false);
  326. this.setCurrentSpeed();
  327. },
  328. //单纯的停止
  329. stop: function () {
  330. // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
  331. this.spine.clearTrack(0);
  332. this.setStatus(this.statusArr_redyrun[0]);
  333. },
  334. //调用一次 暂停 再调用 恢复
  335. paused: function () {
  336. // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
  337. if (this.spine.paused) {
  338. this.spine.paused = false;
  339. } else {
  340. this.spine.paused = true;
  341. }
  342. },
  343. /**
  344. * 设置当前的运动状态
  345. * @param status
  346. */
  347. setStatus: function (status) {
  348. this.myStatus = status;
  349. },
  350. //按屏幕了
  351. setOnTouchListener: function () {
  352. this.setType();
  353. // cc.log("当前游戏 按屏幕了", this.Canvas.getComponent("GameStates").CurrentProgress);
  354. if (this.spine.animation == this.statusArr_redyrun[0]) {
  355. switch (this.type) {
  356. case 0:
  357. this.readyRun();
  358. break;
  359. case 1:
  360. this.upBike();
  361. break;
  362. }
  363. return;
  364. }
  365. },
  366. //todo 跨栏 跳跃
  367. setOnHurdlingListener: function (callBack) {
  368. this.setType();
  369. this.hurdling(callBack);
  370. },
  371. //todo 跨栏 踉跄
  372. setOnHurdlingStaggerListener: function () {
  373. this.setType();
  374. this.hurdling_stagger();
  375. },
  376. //todo 跳远跳跃
  377. setOnLongJumpListener: function (jumpTimer) {
  378. this.setType();
  379. this.longjump(jumpTimer);
  380. },
  381. //todo 跳远 踉跄
  382. setOnLongJumpStaggerListener: function () {
  383. this.setType();
  384. this.stagger();
  385. },
  386. //摔倒了
  387. setOnTripListener: function () {
  388. this.setType();
  389. this.stagger();
  390. },
  391. toggleTimeScale: function () {
  392. // .timeScale当前骨骼中所有动画的时间缩放率。
  393. if (this.spine.timeScale === 1.0) {
  394. this.spine.timeScale = 0.3;
  395. } else {
  396. this.spine.timeScale = 1.0;
  397. }
  398. },
  399. setEndListener: function (endListener) {
  400. this.endListener = endListener
  401. },
  402. endListener: function () {
  403. },
  404. setInterruptListener: function (interruptListener) {
  405. this.interruptListener = interruptListener
  406. },
  407. interruptListener: function () {
  408. },
  409. //todo 快上车 没时间解释了
  410. upBike: function () {
  411. this.setType();
  412. this.spine.setAnimation(0, this.statusArr_bike[9], false);
  413. this.spine.addAnimation(0, this.statusArr_bike[0], true);
  414. this.walkLv = 0;
  415. },
  416. //todo 快下车 没时间解释了
  417. downBike: function (callBack) {
  418. this.setType();
  419. this.spine.setAnimation(0, this.statusArr_bike[4], false);
  420. if (callBack != null) {
  421. this.spine.setEndListener(function (trackEntry) {
  422. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  423. if (animationName == this.statusArr_bike[0]) {
  424. callBack(animationName);
  425. }
  426. }.bind(this));
  427. }
  428. this.walkLv = 0;
  429. },
  430. //todo 快拿标枪 没时间解释了
  431. upJavelin: function () {
  432. this.setType();
  433. this.spine.setAnimation(0, this.statusArr_javelin[1], true);
  434. },
  435. //todo 快扔标枪 没时间解释了
  436. upJavelin_go: function (callBack) {
  437. this.setType();
  438. this.spine.setAnimation(0, this.statusArr_javelin[6], false);
  439. this.spine.timeScale = 1.5;
  440. setTimeout(function () {
  441. this.spine.timeScale = 1;
  442. }.bind(this), 600);
  443. // this.walkLv = 0;
  444. if (callBack != null) {
  445. this.spine.setEndListener(function (trackEntry) {
  446. var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  447. callBack(animationName);
  448. }.bind(this));
  449. }
  450. },
  451. //todo 快扔标枪 没时间解释了
  452. javelin: function () {
  453. this.setType();
  454. this.spine.setAnimation(0, this.statusArr_biaoqiang[0], false);
  455. this.spine.addAnimation(0, this.statusArr_biaoqiang[1], false);
  456. this.spine.addAnimation(0, this.statusArr_biaoqiang[2], false);
  457. },
  458. setType: function () {
  459. var temp = this.Canvas.getComponent("GameStates").CurrentProgress;
  460. // cc.log("现在那标枪",temp);
  461. switch (temp) {
  462. case GameStatesStatic.GameProgress.Race:
  463. this.type = 0;
  464. // cc.log("当前的typssssse","s1");
  465. break;
  466. case GameStatesStatic.GameProgress.LongJump:
  467. this.type = 1;
  468. // cc.log("当前的typssssse","s2");
  469. break;
  470. case GameStatesStatic.GameProgress.Hurdle:
  471. this.type = 2;
  472. // cc.log("当前的typssssse","s3");
  473. break;
  474. case GameStatesStatic.GameProgress.RideBike:
  475. this.type = 3;
  476. // cc.log("当前的typssssse","s4");
  477. break;
  478. case GameStatesStatic.GameProgress.Javelin:
  479. this.type = 4;
  480. // cc.log("当前的typssssse","s5");
  481. break;
  482. }
  483. },
  484. getSpeedLv: function () {
  485. var TouchLayout = cc.find("TouchLayout");
  486. var lv = TouchLayout.getComponent("NodeTouch").Hero.getComponent("HeroControl").PlayerStateScript.CurrentSpeed;
  487. var state = PlayerState.SpeedRangeForAnimation;
  488. if (lv >= state.SlowSpeed[0] && lv <= state.SlowSpeed[1]) {
  489. return 0;
  490. }
  491. if (lv >= state.MiddleSpeed[0] && lv <= state.MiddleSpeed[1]) {
  492. return 1;
  493. }
  494. if (lv >= state.HightSpeed[0] && lv <= state.HightSpeed[1]) {
  495. return 2;
  496. }
  497. return 0;
  498. }
  499. });