strike.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. // foo: {
  14. // // ATTRIBUTES:
  15. // default: null, // The default value will be used only when the component attaching
  16. // // to a node for the first time
  17. // type: cc.SpriteFrame, // optional, default is typeof default
  18. // serializable: true, // optional, default is true
  19. // },
  20. // bar: {
  21. // get () {
  22. // return this._bar;
  23. // },
  24. // set (value) {
  25. // this._bar = value;
  26. // }
  27. // },
  28. },
  29. // LIFE-CYCLE CALLBACKS:
  30. // onLoad () {},
  31. start() {
  32. Global.strike = this;
  33. this._initView();
  34. this.scoreCoefficient = 1;
  35. utils.isMobile(function () {
  36. Manager.strike = this;
  37. this.scoreCoefficient = Manager.scoreCoefficient;
  38. }.bind(this));
  39. // if(Manager.scoreCoefficient){
  40. // this.scoreCoefficient = Manager.scoreCoefficient;
  41. // cc.log("lalallala");
  42. // }else{
  43. // this.scoreCoefficient = 4.5;
  44. // cc.log("lilililili");
  45. // }
  46. },
  47. _initView: function () {
  48. this.strikex = UtilsPrefabs.getNode("strikex", this.node);
  49. this.strikey = UtilsPrefabs.getNode("strikey", this.node);
  50. this.strikex4 = UtilsPrefabs.getNode("strikex4", this.node);
  51. this.strikey5 = UtilsPrefabs.getNode("strikey5", this.node);
  52. this.rest = UtilsPrefabs.getNode("rest", this.node);
  53. let Canvas = cc.find("Canvas");
  54. let score = UtilsPrefabs.getNode("score", Canvas);
  55. let scoreNum = this.scoreNum = UtilsPrefabs.getNode("scoreNum", score);
  56. let gamelayout = UtilsPrefabs.getNode("gamelayout", Canvas);
  57. this.gamelayoutManager = gamelayout.getComponent('gamelayoutManager');
  58. this.rest.on('click', function () {
  59. this._init();
  60. if (this.onRestClick != null) {
  61. this.onRestClick();
  62. }
  63. }.bind(this), this);
  64. this._init();
  65. },
  66. onRestClick: function () {
  67. },
  68. setOnRestClick: function (onRestClick) {
  69. this.onRestClick = onRestClick;
  70. },
  71. _init: function () {
  72. this.hits = 0;
  73. this.hitsMax = 3;
  74. this.hitFristTime = null;
  75. this.hitTime = null;
  76. this.power = 0;
  77. this.scoreMax = 1199999;
  78. UserInfo.PlayerGameData.powerMax = this.scoreMax;
  79. UserInfo.PlayerGameData.powerMaxKm = 5913520000;
  80. UserInfo.PlayerGameData.coefficient = UserInfo.PlayerGameData.powerMaxKm / UserInfo.PlayerGameData.powerMax;
  81. //分数=力量*100*间隔时间所得分*准确度所得分
  82. this.hits1 = [
  83. {
  84. //间隔
  85. time: 0,
  86. //间隔时间对应所得分数
  87. score: 100,
  88. //打偏 和 打正 的 分数
  89. type: [1, 2],
  90. }
  91. ];
  92. this.hits2 = [
  93. {
  94. time: 800,
  95. score: 6,
  96. type: [15, 18],
  97. },
  98. {
  99. time: 1200,
  100. score: 4,
  101. type: [9, 12],
  102. },
  103. {
  104. time: 1500,
  105. score: 2,
  106. type: [2, 6],
  107. },
  108. ];
  109. this.hits3 = [
  110. {
  111. time: 800,
  112. score: 6,
  113. type: [15, 18],
  114. },
  115. {
  116. time: 1200,
  117. score: 4,
  118. type: [9, 12],
  119. },
  120. {
  121. time: 1500,
  122. score: 2,
  123. type: [2, 6],
  124. },
  125. ];
  126. this.xishu = [
  127. 0.1,
  128. 0.4,
  129. 0.9
  130. ];
  131. utils.isMobile(function () {
  132. this.strikex.active = false;
  133. this.strikey.active = false;
  134. this.strikex4.active = false;
  135. this.strikey5.active = false;
  136. }.bind(this));
  137. this.scoreNum.getComponent(cc.Label).string = '0';
  138. // this.strikex.off('click',this.onclickcallbackx);
  139. // this.strikey.off('click',this.onclickcallbacky);
  140. // this.strikex4.off('click',this.onclickcallbackx);
  141. // this.strikey5.off('click',this.onclickcallbacky);
  142. this.strikex.on('click', this.onclickcallbackx, this);
  143. this.strikey.on('click', this.onclickcallbacky, this);
  144. this.strikex4.on('click', this.onclickcallbackx4, this);
  145. this.strikey5.on('click', this.onclickcallbacky5, this);
  146. this.setPowerCallBack(function (hits) {
  147. if (this.onPowerCallBackforMain != null) {
  148. this.p = 0;
  149. if (parseFloat(this.power / this.scoreMax) < 0.4) {
  150. this.p = 0;
  151. } else if (parseFloat(this.power / this.scoreMax) >= 0.4 && parseFloat(this.power / this.scoreMax) < 0.8) {
  152. this.p = 1;
  153. } else if (parseFloat(this.power / this.scoreMax) >= 0.8) {
  154. this.p = 2;
  155. }
  156. this.ChangeCallBack(this.scoreNum, this.power);
  157. this.onPowerCallBackforMain(parseFloat(this.power / this.scoreMax), this.p);
  158. switch (hits) {
  159. case 1 :
  160. // console.log("游戏结束 1",this.power,this.hits);
  161. break;
  162. case 2 :
  163. // console.log("游戏结束 2",this.power,this.hits);
  164. break;
  165. case 3 :
  166. // console.log("游戏结束 3 真结束了",this.power,this.hits);
  167. //总分数
  168. UserInfo.PlayerGameData.power = this.power;
  169. // console.log("游戏结束 3 所有数据",UserInfo);
  170. if (this.ongameoverCallBack != null) {
  171. this.ongameoverCallBack();
  172. }
  173. break;
  174. }
  175. // console.log("流浪地球*** 总分数power 是"+this.power+
  176. // ",次数hits是"+this.hits+
  177. // ",UserInfo.PlayerGameData.power is "+UserInfo.PlayerGameData.power);
  178. }
  179. }.bind(this));
  180. },
  181. onPowerCallBackforMain: function () {
  182. },
  183. setOnPowerCallBackforMain: function (onPowerCallBackforMain) {
  184. this.onPowerCallBackforMain = onPowerCallBackforMain;
  185. },
  186. ongameoverCallBack: function () {
  187. },
  188. setOnGameoverCallBack: function (ongameoverCallBack) {
  189. this.ongameoverCallBack = ongameoverCallBack;
  190. },
  191. //4000
  192. onclickcallbackx4: function (button) {
  193. var datastrike = {
  194. der: 3,
  195. pianyi: 100,
  196. power: 30,
  197. ballSpeed: 4,
  198. kitTime : 800,
  199. ATime : 280
  200. }
  201. this.onclickcallback(datastrike);
  202. },
  203. //5000
  204. onclickcallbacky5: function (button) {
  205. var datastrike = {
  206. der: 3,
  207. pianyi: 100,
  208. power: 50,
  209. ballSpeed: 15,
  210. kitTime : 800,
  211. ATime : 280
  212. }
  213. this.onclickcallback(datastrike);
  214. },
  215. //7500
  216. onclickcallbackx: function (button) {
  217. var datastrike = {
  218. der: 3,
  219. pianyi: 100,
  220. power: 40,
  221. ballSpeed: 10,
  222. kitTime : 800,
  223. ATime : 280
  224. }
  225. this.onclickcallback(datastrike);
  226. },
  227. //8000
  228. onclickcallbacky: function (button) {
  229. var datastrike = {
  230. der: 3,
  231. pianyi: 100,
  232. power: 35,
  233. ballSpeed: 8,
  234. kitTime : 800,
  235. ATime : 280
  236. }
  237. this.onclickcallback(datastrike);
  238. },
  239. onclickcallback: function (datastrike) {
  240. // console.log("当前是",num);
  241. /**
  242. *
  243. * @param der 方向 1234
  244. * @param pianyi 1 打中 0打偏
  245. * @param pow 力量值
  246. * @param ballSpeed 球速
  247. */
  248. //这一段是因为:后来传过来的pianyi是相对于der的,但是我们游戏需要的是相对于正前方是否打正或打偏,所以做出以下变动
  249. // if(datastrike.der == 3 && datastrike.pianyi == 1){
  250. // console.log("流浪地球,打正了");
  251. // }else{
  252. // datastrike.pianyi = 0;
  253. // console.log("流浪地球,打偏了");
  254. // }
  255. let time;
  256. if (this.hitFristTime == null) {
  257. this.hitFristTime = this.getTime();
  258. } else {
  259. this.hitTime = this.getTime();
  260. time = this.hitTime - this.hitFristTime;
  261. // console.log("现在打击了和上次 相隔",time,"当前打击次数",this.hits);
  262. this.hitFristTime = this.hitTime;
  263. }
  264. this.hits++;
  265. if (this.hits == 1) {
  266. this.currTime = 200;
  267. this.getInterval(this.hits1, null, datastrike);
  268. } else if (this.hits == 2) {
  269. this.currTime = 200;
  270. this.getInterval(this.hits2, time, datastrike);
  271. } else if (this.hits == 3) {
  272. this.currTime = 200;
  273. this.getInterval(this.hits3, time, datastrike);
  274. }
  275. },
  276. getInterval: function (arr, time, datastrike) {
  277. //1
  278. if (arr.length == 1) {
  279. //打偏或打正的所得分
  280. // let type = arr[0].type[datastrike.pianyi];
  281. let type = datastrike.pianyi;
  282. //两拳时间间隔所得分
  283. let score = arr[0].score;
  284. // let time = datastrike.time1;
  285. let temppower = Math.abs(datastrike.power-10)*10;
  286. let power = temppower *app.punchHitVars[this.hits-1] * type * score ;
  287. console.log("流浪地球 力量 运算1" +
  288. ",力量 " + temppower +
  289. ",偏移 " + type +
  290. ",系数"+app.punchHitVars[this.hits-1]+
  291. ",时间分 " + score +
  292. ",拳速 " + datastrike.ballSpeed +
  293. ",此次分数 " + power +
  294. ",次数hits是" + this.hits);
  295. // console.log("power" + power);
  296. this.setPower(power,datastrike);
  297. UserInfo.PlayerGameData.everypowerdata = [];
  298. let data = {
  299. //每次的力量
  300. everypower: datastrike.power,
  301. //相隔 时间
  302. time: 0,
  303. //是否打中 打偏
  304. type: datastrike.pianyi,
  305. datastrike: datastrike
  306. }
  307. UserInfo.PlayerGameData.everypowerdata.push(data);
  308. return;
  309. }
  310. //2 3
  311. // if (time != null) {
  312. // for (var i = 0; i < arr.length; i++) {
  313. // var a = arr[i];
  314. // // 小于
  315. // if (time < a.time) {
  316. // //打偏或打正的所得分
  317. // // let type = arr[i].type[datastrike.pianyi];
  318. // let type = datastrike.pianyi;
  319. // //两拳时间间隔所得分
  320. // // let score = arr[i].score;
  321. // let score = 6;
  322. //
  323. //
  324. // let power = datastrike.power * this.scoreCoefficient * type * score * datastrike.ballSpeed;
  325. // console.log("流浪地球 力量 运算2" +
  326. // ",datastrike.power is " + datastrike.power +
  327. // ",type is " + type +
  328. // ",score is " + score +
  329. // ",此次power is " + power +
  330. // ",次数hits是" + this.hits);
  331. // this.setPower(power,datastrike);
  332. //
  333. // let data = {
  334. // //每次的力量
  335. // everypower: datastrike.power,
  336. // //相隔 时间
  337. // time: time,
  338. // //是否打中 打偏
  339. // type: datastrike.pianyi,
  340. //
  341. // datastrike: datastrike
  342. // }
  343. // UserInfo.PlayerGameData.everypowerdata.push(data);
  344. // break;
  345. // }
  346. //
  347. // //大于等于
  348. // else {
  349. // if (i == arr.length - 1) {
  350. // //打偏或打正的所得分
  351. // // let type = arr[i].type[datastrike.pianyi];
  352. // let type = datastrike.pianyi;
  353. // //两拳时间间隔所得分
  354. // // let score = arr[i].score;
  355. // let score = 6;
  356. // let power = datastrike.power * this.scoreCoefficient * type * score * datastrike.ballSpeed;
  357. //
  358. //
  359. // console.log("流浪地球 力量 运算3" +
  360. // ",datastrike.power is " + datastrike.power +
  361. // ",type is " + type +
  362. // ",score is " + score +
  363. // ",此次power is " + power +
  364. // ",次数hits是" + this.hits);
  365. // this.setPower(power,datastrike);
  366. // let data = {
  367. // //每次的力量
  368. // everypower: datastrike.power,
  369. // //相隔 时间
  370. // time: time,
  371. // //是否打中 打偏
  372. // type: datastrike.pianyi,
  373. //
  374. // datastrike: datastrike
  375. // }
  376. // UserInfo.PlayerGameData.everypowerdata.push(data);
  377. // }
  378. //
  379. // }
  380. // }
  381. // }
  382. if (this.hits ==2 ||this.hits ==3 ) {
  383. //打偏或打正的所得分
  384. // let type = arr[i].type[datastrike.pianyi];
  385. let type = datastrike.pianyi;
  386. //两拳时间间隔所得分
  387. // let score = arr[i].score;
  388. let score = this.getTimeScore(datastrike.kitTime,datastrike.ATime);
  389. console.log("现在时间分是",score);
  390. let temppower = Math.abs(datastrike.power-10)*10;
  391. let power = temppower * app.punchHitVars[this.hits-1] * type * score ;
  392. // console.log("流浪地球 力量 运算2" +
  393. // ",datastrike.power is " + datastrike.power +
  394. // ",type is " + type +
  395. // ",score is " + score +
  396. // ",此次power is " + power +
  397. // ",次数hits是" + this.hits);
  398. console.log("流浪地球 力量 运算" +this.hits+
  399. ",力量 " + temppower +
  400. ",偏移 " + type +
  401. ",系数"+app.punchHitVars[this.hits-1]+
  402. ",时间分 " + score +
  403. ",拳速 " + datastrike.ballSpeed +
  404. ",此次分数 " + power +
  405. ",次数hits是" + this.hits);
  406. this.setPower(power,datastrike);
  407. let data = {
  408. //每次的力量
  409. everypower: datastrike.power,
  410. //相隔 时间
  411. time: time,
  412. //是否打中 打偏
  413. type: datastrike.pianyi,
  414. datastrike: datastrike
  415. }
  416. UserInfo.PlayerGameData.everypowerdata.push(data);
  417. }
  418. },
  419. getTimeScore : function(kitTime,ATime){
  420. let score = 2;
  421. let tagTime = 1000;
  422. let min = score;
  423. let max = 30;
  424. let AETime = app.AETime;
  425. let x = kitTime-ATime;
  426. console.log("kitTime",kitTime);
  427. console.log("ATime",ATime);
  428. console.log("kitTime-ATime = x",x);
  429. //小于最低
  430. if (x<AETime) {
  431. score = 5;
  432. console.log("x<AETime 时间分",score);
  433. }else if (x >= AETime && x <= tagTime) {
  434. let tempTime= x-AETime
  435. let tempScore = parseInt(max-Math.round(tempTime*(max-1)/(tagTime-AETime) ));
  436. console.log("x >= AETime && x <= tagTime tempScore",tempScore);
  437. // console.log("时间 精密计算","AETime",Math.round(x / tagTime * 10000) / max);
  438. if (tempScore > score) {
  439. score = tempScore;
  440. console.log("tempScore > score ",score);
  441. }else{
  442. score = 5;
  443. console.log("tempScore <= score ",score);
  444. }
  445. }else if (x > tagTime) {
  446. score = 2;
  447. console.log("x > tagTime score",score);
  448. }
  449. console.log("时间分返回 score",score);
  450. return score;
  451. },
  452. setPower: function (n,data) {
  453. this.gamelayoutManager.thisOneScore = n;
  454. this.gamelayoutManager.showData = data;
  455. this.power += n;
  456. UserInfo.PlayerGameData.power = this.power;
  457. console.log("流浪地球 总分数power 是" + this.power +
  458. ",次数hits是" + this.hits +
  459. ",UserInfo.PlayerGameData.power is " + UserInfo.PlayerGameData.power);
  460. if (this.onPowerCallBack != null) {
  461. this.onPowerCallBack(this.hits);
  462. }
  463. },
  464. onPowerCallBack: function () {
  465. },
  466. setPowerCallBack: function (onPowerCallBack) {
  467. this.onPowerCallBack = onPowerCallBack;
  468. },
  469. ChangeCallBack: function (NumNode, CurrentMoney) {
  470. let LastMoney = this.getNum(NumNode.getComponent(cc.Label).string);
  471. let Diff = CurrentMoney - LastMoney;
  472. let InitMoneyNum = LastMoney;
  473. let AnimTimes = 0;
  474. if (this.p == 2) {
  475. this.currTime = 630;
  476. }
  477. let Times = this.currTime;
  478. console.log("现在时间是", Times);
  479. this.unschedule(this.callback);
  480. this.callback = function () {
  481. if (Diff > 0) {
  482. InitMoneyNum += parseInt((CurrentMoney - LastMoney) / Times);
  483. } else {
  484. InitMoneyNum -= parseInt((CurrentMoney - LastMoney) / Times);
  485. }
  486. let result = this.FormatMoney(InitMoneyNum);
  487. if (AnimTimes == Times - 1) {
  488. this.scoreNum.getComponent(cc.Label).string = this.FormatMoney(CurrentMoney);
  489. this.unschedule(this.callback);
  490. return;
  491. }
  492. this.scoreNum.getComponent(cc.Label).string = result;
  493. RootNode.soundNode.getComponent("soundManage").playScore();
  494. AnimTimes++;
  495. }
  496. this.schedule(this.callback, 0.01, Times);
  497. },
  498. timeCallback: function () {
  499. },
  500. FormatMoney(s, n) {
  501. n = n > 0 && n <= 20 ? n : 2;
  502. s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
  503. var l = s.split(".")[0].split("").reverse(),
  504. r = s.split(".")[1];
  505. let t = "";
  506. for (let i = 0; i < l.length; i++) {
  507. t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
  508. }
  509. return t.split("").reverse().join(""); //+ "." + r;
  510. },
  511. getNum: function (num) {
  512. return parseInt(num.split(",").join(''));
  513. },
  514. getTime: function () {
  515. return new Date().getTime();
  516. }
  517. // update (dt) {},
  518. });