123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- cc.Class({
- extends: cc.Component,
- properties: {
- rateLabel: cc.Label,
- getScoreLabel: cc.Label,
- rocket: cc.Node,
- dotX: cc.Node,
- dotY: cc.Node,
- jumpNode: cc.Node,
- headSprite: [cc.SpriteFrame]
- },
- onLoad() {
- this.initMapData();
- this.initData();
- this.initFunc();
- },
- start() {
- // this.openSchedule(58000, true);
- },
- reset() {
- this.stageX = 0;
- this.stageY = 0;
- this.jumpID = 1;
- this.scale = 1;
- this.scale2 = 1;
- this.maxRate = 0;
- this.jumpNum = 1;
- this.jumpMap = {};
- this.graphics.clear();
- this.touches = [cc.v2(0, 0)];
- this.lineX.destroyAllChildren();
- this.lineY.destroyAllChildren();
- this.jump.destroyAllChildren();
- this.initDotX();
- this.initDotY();
- this.initMapData();
- },
- openSchedule(time = 0, bool) {
- // if (!bool) { return; }
- // 0 1 2 3 4 5 6
- let color = [null, new cc.color("#DC2A09"), new cc.color("#50FD8D"), null, new cc.color("#3A9EEC"), null, new cc.color("#FCF695")][this.baseScript.randomRocket];
- this.graphics.strokeColor = color;
- this.closeSchedule();
- if (time != 0) {
- this.reConnetMap(time);
- }
- this.dt = time / 1000;
- this.dtFunc = (dt) => {
- this.updateTime(dt);
- this.updateDotX();
- this.updateDotY();
- this.updateJumpNode();
- }
- this.schedule(this.dtFunc);
- },
- closeSchedule() {
- this.dtFunc && this.unschedule(this.dtFunc);
- },
- reConnetMap(time) {
- let dt = 0;
- while (dt < time) {
- if (time - dt < 100) {
- dt = time;
- } else {
- dt += 100;
- }
- let rate = Math.pow(Math.pow(200 / 91, 1 / 750), dt / 20) * 91;
- this.controlRocket(rate, dt, false);
- }
- },
- initData() {
- this.baseScript = this.node.parent.parent.getComponent("CRASHBase");
- this.touches = [cc.v2(0, 0)];
- this.graphics = this.node.addComponent(cc.Graphics);
- this.graphics.lineWidth = 7;
- // this.graphics.strokeColor = new cc.Color(61, 180, 202, 255);
- this.dt = 0;
- this.msgID = 0;
- this.maxWidth = 880;
- this.maxHeight = 557;
- this.widthBox = this.maxWidth / 8000;
- this.heightBox = this.maxHeight / 272;
- // this.widthBox = 1;
- // this.heightBox = 1;
- this.stageX = 0;
- this.stageY = 0;
- this.keysX = Object.keys(this.DOTConfigX);
- this.keysY = Object.keys(this.DOTConfigY);
- this.lineX = cc.find("lineX", this.node);
- this.lineY = cc.find("lineY", this.node);
- this.layer_removeX = cc.find("layer_removeX", this.node);
- this.layer_removeY = cc.find("layer_removeY", this.node);
- this.jump = cc.find("jump", this.node);
- this.jumpID = 1;
- this.scale = 1;
- this.maxRate = 0;
- this.jumpNum = 1;
- this.scale2 = 1;
- this.jumpMap = {};
- },
- initFunc() {
- this.initDotX();
- this.initDotY();
- },
- initMapData() {
- this.DOTConfigX = {
- "0_20": {
- space: 2,
- dotNow: 0,
- dotLimit: 10
- }, "20_50": {
- space: 5,
- dotNow: 0,
- dotLimit: 6
- }, "50_100": {
- space: 10,
- dotNow: 0,
- dotLimit: 5
- }, "100_300": {
- space: 50,
- dotNow: 0,
- dotLimit: 4
- }
- }
- this.DOTConfigY = {
- "1_5": {
- space: 0.5,
- dotNow: 0,
- dotLimit: 8,
- }, "5_10": {
- space: 1,
- dotNow: 0,
- dotLimit: 5,
- }, "10_20": {
- space: 2,
- dotNow: 0,
- dotLimit: 5,
- }, "20_50": {
- space: 5,
- dotNow: 0,
- dotLimit: 6,
- }, "50_100": {
- space: 10,
- dotNow: 0,
- dotLimit: 5,
- }, "100_300": {
- space: 20,
- dotNow: 0,
- dotLimit: 10,
- }
- }
- },
- addjumpNode(playerData, useAnim) {
- let name2 = playerData.name;
- if (name2.length > 6) {
- name2 = name2.slice(0, 6);
- name2 += "...";
- }
- let jumpNode = cc.instantiate(this.jumpNode);
- jumpNode.setChildSpriteFrame("sprite_head", playerData.headID ? this.headSprite[playerData.headID] : cc.vv.globalUserInfo.getHeadSpriteFrameByFaceID(""));
- jumpNode.setChildString("label_score", "+$" + playerData.score);
- jumpNode.setChildString("label_name", name2);
- if (playerData.name == this.baseScript.myselfPlayer.getNickName()) {
- jumpNode.getChildByName("label_name").color = new cc.color(255, 204, 0, 255);
- } else {
- jumpNode.getChildByName("label_name").color = new cc.color(198, 201, 255, 255);
- }
- if (this.jumpNum % 2 == 0) {
- jumpNode.getChildByName("label_name").y *= -1;
- jumpNode.getChildByName("label_score").y *= -1;
- }
- this.jumpNum += 1;
- let data = this.json[playerData.jumpID];
- this.jumpMap[playerData.name] = {
- headID: playerData.headID,
- mult: data.multiplier,
- name: playerData.name,
- width: data.time * 20 * this.widthBox,
- height: (data.multiplier - 91) * this.heightBox,
- jumpNode: jumpNode
- }
- let jumpData = this.jumpMap[playerData.name];
- jumpNode.active = true;
- jumpNode.parent = this.jump;
- if (useAnim) {
- jumpNode.scale = 0;
- cc.tween(jumpNode)
- .to(0.4, { scale: 1 }, { easing: "elasticInOut" })
- .start();
- }
- let posY = this.maxRate ? this.scale2 * (jumpData.mult - 91) : this.scale * jumpData.height;
- jumpNode.position = cc.v2(this.scale * jumpData.width, posY);
- },
- initDotX() {
- for (let i = 0; i < 4; i += 1) {
- let position = cc.v2(this.maxWidth / 4 * (i + 1), 20);
- this.createDOTX((i + 1) * 2, position);
- this.DOTConfigX["0_20"].dotNow += 1;
- }
- },
- initDotY() {
- for (let i = 0; i < 8; i += 1) {
- cc.log((i + 1) * 0.5 * 100);
- let position = cc.v2(10, this.heightBox * (i + 1) * 0.5 * 100);
- cc.log(position.y);
- this.createDOTY((1 + (i + 1) * 0.5), position);
- this.DOTConfigY["1_5"].dotNow += 1;
- }
- },
- createDOTX(time, position, useAnim = false) {
- let node = cc.instantiate(this.dotX);
- node.active = true;
- node.parent = this.lineX;
- node.position = position;
- node.timeIndex = time;
- node.stage = this.stageX;
- node.setChildString("label_time", node.timeIndex + "s");
- if (useAnim) {
- node.y = -20;
- node.opacity = 100;
- cc.tween(node)
- .to(1, { y: 20, opacity: 255 }, { easing: 'expoOut' })
- .start();
- }
- return node;
- },
- createDOTY(rate, position, useAnim = false) {
- let node = cc.instantiate(this.dotY);
- node.active = true;
- node.parent = this.lineY;
- node.position = position;
- node.rateIndex = rate;
- node.stage = this.stageY;
- node.setChildString("label_rate", rate + "x");
- if (useAnim) {
- node.x = -20;
- node.opacity = 100;
- cc.tween(node)
- .to(1, { x: 10, opacity: 255 }, { easing: 'expoOut' })
- .start();
- }
- return node;
- },
- updateDotX() {
- let key = this.keysX[this.stageX];
- let dotConfig = this.DOTConfigX[key];
- let lowerLimit = key.split("_")[0];
- let upperLimit = key.split("_")[1];
- if (this.dt >= lowerLimit && this.dt <= upperLimit) {
- while (this.dt > (dotConfig.dotNow + 1) * dotConfig.space) {
- let time = (dotConfig.dotNow + 1) * dotConfig.space;
- this.createDOTX(time, cc.v2(time / this.dt * this.maxWidth, 20), true);
- dotConfig.dotNow += 1;
- }
- } else {
- this.stageX += 1;
- this.updateDotX();
- }
- for (let i = this.lineX.children.length - 1; i >= 0; i -= 1) {
- let node = this.lineX.children[i];
- if (node.stage != this.stageX) {
- node.parent = this.layer_removeX;
- cc.tween(node)
- .to(1, { y: 0, opacity: 0 }, { easing: 'expoOut' })
- .call(() => { node.destroy(); })
- .start();
- } else if (this.dt > 8) {
- node.position = cc.v2(node.timeIndex / this.dt * this.maxWidth, 20);
- }
- }
- },
- updateDotY() {
- let key = this.keysY[this.stageY];
- let dotConfig = this.DOTConfigY[key];
- let lowerLimit = key.split("_")[0];
- let upperLimit = key.split("_")[1];
- let maxRate = this.maxRate || 272 / this.scale;
- let maxRate2 = maxRate / 100;
- if ((maxRate2 >= lowerLimit && maxRate2 <= upperLimit) || maxRate2 <= 1) {
- while (upperLimit > (dotConfig.dotNow + 1) * dotConfig.space) {
- let rate = (dotConfig.dotNow + 1) * dotConfig.space;
- this.createDOTY(rate, cc.v2(10, ((rate - 1) * 100) / maxRate), true);
- dotConfig.dotNow += 1;
- }
- } else {
- this.stageY += 1;
- this.updateDotY();
- }
- for (let i = this.lineY.children.length - 1; i >= 0; i -= 1) {
- let node = this.lineY.children[i];
- if (node.stage != this.stageY) {
- node.parent = this.layer_removeY;
- cc.tween(node)
- .to(1, { x: 30, opacity: 0 }, { easing: 'expoOut' })
- .call(() => { node.destroy(); })
- .start();
- } else if (this.dt > 8) {
- node.position = cc.v2(10, ((node.rateIndex - 1) * 100) / maxRate * this.maxHeight);
- }
- }
- },
- updateJumpNode() {
- let keys = Object.keys(this.jumpMap);
- for (let i = 0; i < keys.length; i += 1) {
- let data = this.jumpMap[keys[i]];
- let posY = this.maxRate ? this.scale2 * (data.mult - 91) : this.scale * data.height;
- data.jumpNode.position = cc.v2(this.scale * data.width, posY);
- }
- },
- updateTime(dt) {
- this.dt += dt;
- let ms1 = this.dt * 1000;
- if (ms1 >= 110460) {
- return;
- }
- let rate = Math.pow(Math.pow(200 / 91, 1 / 750), ms1 / 20) * 91;
- this.updateLabel(rate, ms1);
- this.controlRocket(rate, ms1);
- },
- updateLabel(rate) {
- this.rateLabel.string = (rate / 100).toFixed(2) + "x";
- if (this.baseScript.isBet) {
- let data = this.baseScript.CRASHScrollView.dataMap[this.baseScript.myselfPlayer.getNickName()];
- if (data && data.score && !data.mult) {
- let score = data.score;
- this.getScoreLabel.string = "+$" + (score * rate / 100).toFixed(2);
- }
- }
- },
- controlRocket(rate, ms, fillMap = true) {
- let data1 = ms * this.widthBox;
- let data2 = (rate - 91) * this.heightBox;
- let pos = cc.v2(data1, data2);
- pos.originalX = data1;
- pos.originalY = data2;
- pos.originalRate = rate;
- this.touches.push(pos);
- let touches = this.touches;
- while (ms > this.json[this.jumpID].time * 20) {
- this.jumpID += 1;
- }
- let lastPos = touches[touches.length - 1];
- if (data1 > this.maxWidth && fillMap) {
- let bool = false;
- let scale = this.maxWidth / data1;
- let scale2 = 0;
- this.scale = scale;
- if (lastPos.originalY * scale > this.maxHeight) {
- bool = true;
- scale2 = this.maxHeight / (lastPos.originalRate - 91);
- this.scale2 = scale2;
- this.maxRate = rate;
- }
- for (let i = 1; i < touches.length; i += 1) {
- touches[i].x = touches[i].originalX * scale;
- if (bool) {
- touches[i].y = (touches[i].originalRate - 91) * scale2;
- } else {
- touches[i].y = touches[i].originalY * scale;
- }
- }
- }
- if (fillMap) {
- let pos1 = touches[touches.length - 2];
- let pos2 = touches[touches.length - 1];
- this.rocket.position = cc.v2(pos2.x, pos2.y);
- this.rocket.rotation = Math.atan2((pos1.y - pos2.y), (pos2.x - pos1.x)) * (180 / Math.PI) + 90;
- const MIN_POINT_DISTANCE = 0.2;
- this.graphics.clear();
- this.graphics.moveTo(0, 0);
- let lastIndex = 0;
- for (let i = 1, l = touches.length; i < l; i++) {
- if (touches[i].sub(touches[lastIndex]).mag() < MIN_POINT_DISTANCE) {
- continue;
- }
- lastIndex = i;
- this.graphics.lineTo(touches[i].x, touches[i].y);
- }
- this.graphics.stroke();
- }
- },
- });
|