CRASHMap.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. rateLabel: cc.Label,
  5. getScoreLabel: cc.Label,
  6. rocket: cc.Node,
  7. dotX: cc.Node,
  8. dotY: cc.Node,
  9. jumpNode: cc.Node,
  10. headSprite: [cc.SpriteFrame]
  11. },
  12. onLoad() {
  13. this.initMapData();
  14. this.initData();
  15. this.initFunc();
  16. },
  17. start() {
  18. // this.openSchedule(58000, true);
  19. },
  20. reset() {
  21. this.stageX = 0;
  22. this.stageY = 0;
  23. this.jumpID = 1;
  24. this.scale = 1;
  25. this.scale2 = 1;
  26. this.maxRate = 0;
  27. this.jumpNum = 1;
  28. this.jumpMap = {};
  29. this.graphics.clear();
  30. this.touches = [cc.v2(0, 0)];
  31. this.lineX.destroyAllChildren();
  32. this.lineY.destroyAllChildren();
  33. this.jump.destroyAllChildren();
  34. this.initDotX();
  35. this.initDotY();
  36. this.initMapData();
  37. },
  38. openSchedule(time = 0, bool) {
  39. // if (!bool) { return; }
  40. // 0 1 2 3 4 5 6
  41. let color = [null, new cc.color("#DC2A09"), new cc.color("#50FD8D"), null, new cc.color("#3A9EEC"), null, new cc.color("#FCF695")][this.baseScript.randomRocket];
  42. this.graphics.strokeColor = color;
  43. this.closeSchedule();
  44. if (time != 0) {
  45. this.reConnetMap(time);
  46. }
  47. this.dt = time / 1000;
  48. this.dtFunc = (dt) => {
  49. this.updateTime(dt);
  50. this.updateDotX();
  51. this.updateDotY();
  52. this.updateJumpNode();
  53. }
  54. this.schedule(this.dtFunc);
  55. },
  56. closeSchedule() {
  57. this.dtFunc && this.unschedule(this.dtFunc);
  58. },
  59. reConnetMap(time) {
  60. let dt = 0;
  61. while (dt < time) {
  62. if (time - dt < 100) {
  63. dt = time;
  64. } else {
  65. dt += 100;
  66. }
  67. let rate = Math.pow(Math.pow(200 / 91, 1 / 750), dt / 20) * 91;
  68. this.controlRocket(rate, dt, false);
  69. }
  70. },
  71. initData() {
  72. this.baseScript = this.node.parent.parent.getComponent("CRASHBase");
  73. this.touches = [cc.v2(0, 0)];
  74. this.graphics = this.node.addComponent(cc.Graphics);
  75. this.graphics.lineWidth = 7;
  76. // this.graphics.strokeColor = new cc.Color(61, 180, 202, 255);
  77. this.dt = 0;
  78. this.msgID = 0;
  79. this.maxWidth = 880;
  80. this.maxHeight = 557;
  81. this.widthBox = this.maxWidth / 8000;
  82. this.heightBox = this.maxHeight / 272;
  83. // this.widthBox = 1;
  84. // this.heightBox = 1;
  85. this.stageX = 0;
  86. this.stageY = 0;
  87. this.keysX = Object.keys(this.DOTConfigX);
  88. this.keysY = Object.keys(this.DOTConfigY);
  89. this.lineX = cc.find("lineX", this.node);
  90. this.lineY = cc.find("lineY", this.node);
  91. this.layer_removeX = cc.find("layer_removeX", this.node);
  92. this.layer_removeY = cc.find("layer_removeY", this.node);
  93. this.jump = cc.find("jump", this.node);
  94. this.jumpID = 1;
  95. this.scale = 1;
  96. this.maxRate = 0;
  97. this.jumpNum = 1;
  98. this.scale2 = 1;
  99. this.jumpMap = {};
  100. },
  101. initFunc() {
  102. this.initDotX();
  103. this.initDotY();
  104. },
  105. initMapData() {
  106. this.DOTConfigX = {
  107. "0_20": {
  108. space: 2,
  109. dotNow: 0,
  110. dotLimit: 10
  111. }, "20_50": {
  112. space: 5,
  113. dotNow: 0,
  114. dotLimit: 6
  115. }, "50_100": {
  116. space: 10,
  117. dotNow: 0,
  118. dotLimit: 5
  119. }, "100_300": {
  120. space: 50,
  121. dotNow: 0,
  122. dotLimit: 4
  123. }
  124. }
  125. this.DOTConfigY = {
  126. "1_5": {
  127. space: 0.5,
  128. dotNow: 0,
  129. dotLimit: 8,
  130. }, "5_10": {
  131. space: 1,
  132. dotNow: 0,
  133. dotLimit: 5,
  134. }, "10_20": {
  135. space: 2,
  136. dotNow: 0,
  137. dotLimit: 5,
  138. }, "20_50": {
  139. space: 5,
  140. dotNow: 0,
  141. dotLimit: 6,
  142. }, "50_100": {
  143. space: 10,
  144. dotNow: 0,
  145. dotLimit: 5,
  146. }, "100_300": {
  147. space: 20,
  148. dotNow: 0,
  149. dotLimit: 10,
  150. }
  151. }
  152. },
  153. addjumpNode(playerData, useAnim) {
  154. let name2 = playerData.name;
  155. if (name2.length > 6) {
  156. name2 = name2.slice(0, 6);
  157. name2 += "...";
  158. }
  159. let jumpNode = cc.instantiate(this.jumpNode);
  160. jumpNode.setChildSpriteFrame("sprite_head", playerData.headID ? this.headSprite[playerData.headID] : cc.vv.globalUserInfo.getHeadSpriteFrameByFaceID(""));
  161. jumpNode.setChildString("label_score", "+$" + playerData.score);
  162. jumpNode.setChildString("label_name", name2);
  163. if (playerData.name == this.baseScript.myselfPlayer.getNickName()) {
  164. jumpNode.getChildByName("label_name").color = new cc.color(255, 204, 0, 255);
  165. } else {
  166. jumpNode.getChildByName("label_name").color = new cc.color(198, 201, 255, 255);
  167. }
  168. if (this.jumpNum % 2 == 0) {
  169. jumpNode.getChildByName("label_name").y *= -1;
  170. jumpNode.getChildByName("label_score").y *= -1;
  171. }
  172. this.jumpNum += 1;
  173. let data = this.json[playerData.jumpID];
  174. this.jumpMap[playerData.name] = {
  175. headID: playerData.headID,
  176. mult: data.multiplier,
  177. name: playerData.name,
  178. width: data.time * 20 * this.widthBox,
  179. height: (data.multiplier - 91) * this.heightBox,
  180. jumpNode: jumpNode
  181. }
  182. let jumpData = this.jumpMap[playerData.name];
  183. jumpNode.active = true;
  184. jumpNode.parent = this.jump;
  185. if (useAnim) {
  186. jumpNode.scale = 0;
  187. cc.tween(jumpNode)
  188. .to(0.4, { scale: 1 }, { easing: "elasticInOut" })
  189. .start();
  190. }
  191. let posY = this.maxRate ? this.scale2 * (jumpData.mult - 91) : this.scale * jumpData.height;
  192. jumpNode.position = cc.v2(this.scale * jumpData.width, posY);
  193. },
  194. initDotX() {
  195. for (let i = 0; i < 4; i += 1) {
  196. let position = cc.v2(this.maxWidth / 4 * (i + 1), 20);
  197. this.createDOTX((i + 1) * 2, position);
  198. this.DOTConfigX["0_20"].dotNow += 1;
  199. }
  200. },
  201. initDotY() {
  202. for (let i = 0; i < 8; i += 1) {
  203. cc.log((i + 1) * 0.5 * 100);
  204. let position = cc.v2(10, this.heightBox * (i + 1) * 0.5 * 100);
  205. cc.log(position.y);
  206. this.createDOTY((1 + (i + 1) * 0.5), position);
  207. this.DOTConfigY["1_5"].dotNow += 1;
  208. }
  209. },
  210. createDOTX(time, position, useAnim = false) {
  211. let node = cc.instantiate(this.dotX);
  212. node.active = true;
  213. node.parent = this.lineX;
  214. node.position = position;
  215. node.timeIndex = time;
  216. node.stage = this.stageX;
  217. node.setChildString("label_time", node.timeIndex + "s");
  218. if (useAnim) {
  219. node.y = -20;
  220. node.opacity = 100;
  221. cc.tween(node)
  222. .to(1, { y: 20, opacity: 255 }, { easing: 'expoOut' })
  223. .start();
  224. }
  225. return node;
  226. },
  227. createDOTY(rate, position, useAnim = false) {
  228. let node = cc.instantiate(this.dotY);
  229. node.active = true;
  230. node.parent = this.lineY;
  231. node.position = position;
  232. node.rateIndex = rate;
  233. node.stage = this.stageY;
  234. node.setChildString("label_rate", rate + "x");
  235. if (useAnim) {
  236. node.x = -20;
  237. node.opacity = 100;
  238. cc.tween(node)
  239. .to(1, { x: 10, opacity: 255 }, { easing: 'expoOut' })
  240. .start();
  241. }
  242. return node;
  243. },
  244. updateDotX() {
  245. let key = this.keysX[this.stageX];
  246. let dotConfig = this.DOTConfigX[key];
  247. let lowerLimit = key.split("_")[0];
  248. let upperLimit = key.split("_")[1];
  249. if (this.dt >= lowerLimit && this.dt <= upperLimit) {
  250. while (this.dt > (dotConfig.dotNow + 1) * dotConfig.space) {
  251. let time = (dotConfig.dotNow + 1) * dotConfig.space;
  252. this.createDOTX(time, cc.v2(time / this.dt * this.maxWidth, 20), true);
  253. dotConfig.dotNow += 1;
  254. }
  255. } else {
  256. this.stageX += 1;
  257. this.updateDotX();
  258. }
  259. for (let i = this.lineX.children.length - 1; i >= 0; i -= 1) {
  260. let node = this.lineX.children[i];
  261. if (node.stage != this.stageX) {
  262. node.parent = this.layer_removeX;
  263. cc.tween(node)
  264. .to(1, { y: 0, opacity: 0 }, { easing: 'expoOut' })
  265. .call(() => { node.destroy(); })
  266. .start();
  267. } else if (this.dt > 8) {
  268. node.position = cc.v2(node.timeIndex / this.dt * this.maxWidth, 20);
  269. }
  270. }
  271. },
  272. updateDotY() {
  273. let key = this.keysY[this.stageY];
  274. let dotConfig = this.DOTConfigY[key];
  275. let lowerLimit = key.split("_")[0];
  276. let upperLimit = key.split("_")[1];
  277. let maxRate = this.maxRate || 272 / this.scale;
  278. let maxRate2 = maxRate / 100;
  279. if ((maxRate2 >= lowerLimit && maxRate2 <= upperLimit) || maxRate2 <= 1) {
  280. while (upperLimit > (dotConfig.dotNow + 1) * dotConfig.space) {
  281. let rate = (dotConfig.dotNow + 1) * dotConfig.space;
  282. this.createDOTY(rate, cc.v2(10, ((rate - 1) * 100) / maxRate), true);
  283. dotConfig.dotNow += 1;
  284. }
  285. } else {
  286. this.stageY += 1;
  287. this.updateDotY();
  288. }
  289. for (let i = this.lineY.children.length - 1; i >= 0; i -= 1) {
  290. let node = this.lineY.children[i];
  291. if (node.stage != this.stageY) {
  292. node.parent = this.layer_removeY;
  293. cc.tween(node)
  294. .to(1, { x: 30, opacity: 0 }, { easing: 'expoOut' })
  295. .call(() => { node.destroy(); })
  296. .start();
  297. } else if (this.dt > 8) {
  298. node.position = cc.v2(10, ((node.rateIndex - 1) * 100) / maxRate * this.maxHeight);
  299. }
  300. }
  301. },
  302. updateJumpNode() {
  303. let keys = Object.keys(this.jumpMap);
  304. for (let i = 0; i < keys.length; i += 1) {
  305. let data = this.jumpMap[keys[i]];
  306. let posY = this.maxRate ? this.scale2 * (data.mult - 91) : this.scale * data.height;
  307. data.jumpNode.position = cc.v2(this.scale * data.width, posY);
  308. }
  309. },
  310. updateTime(dt) {
  311. this.dt += dt;
  312. let ms1 = this.dt * 1000;
  313. if (ms1 >= 110460) {
  314. return;
  315. }
  316. let rate = Math.pow(Math.pow(200 / 91, 1 / 750), ms1 / 20) * 91;
  317. this.updateLabel(rate, ms1);
  318. this.controlRocket(rate, ms1);
  319. },
  320. updateLabel(rate) {
  321. this.rateLabel.string = (rate / 100).toFixed(2) + "x";
  322. if (this.baseScript.isBet) {
  323. let data = this.baseScript.CRASHScrollView.dataMap[this.baseScript.myselfPlayer.getNickName()];
  324. if (data && data.score && !data.mult) {
  325. let score = data.score;
  326. this.getScoreLabel.string = "+$" + (score * rate / 100).toFixed(2);
  327. }
  328. }
  329. },
  330. controlRocket(rate, ms, fillMap = true) {
  331. let data1 = ms * this.widthBox;
  332. let data2 = (rate - 91) * this.heightBox;
  333. let pos = cc.v2(data1, data2);
  334. pos.originalX = data1;
  335. pos.originalY = data2;
  336. pos.originalRate = rate;
  337. this.touches.push(pos);
  338. let touches = this.touches;
  339. while (ms > this.json[this.jumpID].time * 20) {
  340. this.jumpID += 1;
  341. }
  342. let lastPos = touches[touches.length - 1];
  343. if (data1 > this.maxWidth && fillMap) {
  344. let bool = false;
  345. let scale = this.maxWidth / data1;
  346. let scale2 = 0;
  347. this.scale = scale;
  348. if (lastPos.originalY * scale > this.maxHeight) {
  349. bool = true;
  350. scale2 = this.maxHeight / (lastPos.originalRate - 91);
  351. this.scale2 = scale2;
  352. this.maxRate = rate;
  353. }
  354. for (let i = 1; i < touches.length; i += 1) {
  355. touches[i].x = touches[i].originalX * scale;
  356. if (bool) {
  357. touches[i].y = (touches[i].originalRate - 91) * scale2;
  358. } else {
  359. touches[i].y = touches[i].originalY * scale;
  360. }
  361. }
  362. }
  363. if (fillMap) {
  364. let pos1 = touches[touches.length - 2];
  365. let pos2 = touches[touches.length - 1];
  366. this.rocket.position = cc.v2(pos2.x, pos2.y);
  367. this.rocket.rotation = Math.atan2((pos1.y - pos2.y), (pos2.x - pos1.x)) * (180 / Math.PI) + 90;
  368. const MIN_POINT_DISTANCE = 0.2;
  369. this.graphics.clear();
  370. this.graphics.moveTo(0, 0);
  371. let lastIndex = 0;
  372. for (let i = 1, l = touches.length; i < l; i++) {
  373. if (touches[i].sub(touches[lastIndex]).mag() < MIN_POINT_DISTANCE) {
  374. continue;
  375. }
  376. lastIndex = i;
  377. this.graphics.lineTo(touches[i].x, touches[i].y);
  378. }
  379. this.graphics.stroke();
  380. }
  381. },
  382. });