let Config = require('config'); let Global = require('global'); let Res = require('res'); let Util = require('util'); cc.Class({ extends: cc.Component, properties: { aniFace: cc.Animation, aniWeep: cc.Animation, aniBubble: cc.Animation, aniExplode: cc.Animation, aniItem: cc.Animation, prefabScore: [cc.Prefab], prefabSpecialScore: cc.Prefab, redPack: cc.Node, cubeNd: cc.Node, }, onLoad() { }, onDestroy() { }, setPosX(x) { this.node.x = x; if (this.shader) { this.shader.x = x; } }, onTouchStart(event) { if (!this.panel.isCanTouch || this.panel.gameScene.isSeeOther) { return; } if (this.panel.crazyMode) { return; } // if (this.typeNum === 2) { // console.log('flash brick clicked'); // this.panel.removeOneRow(this); // } else { if (this.panel.onMove) { this.canMoveing = false; return; } else { this.canMoveing = true; } // this.panel.unschedule(this.panel.set_face_wait) // this.panel.scheduleOnce(this.panel.set_face_wait,Config.blank_wait_time); if (this.panel.useItemIndex !== -1) { return; } this.panel.onMove = true; this.copyObj(); this.showShader(); // } }, onTouchMove(event) { if (!this.panel.isCanTouch) { return; } if (this.panel.gameScene.isSeeOther) { return; } if (this.panel.useItemIndex !== -1) { return; } if (this.panel.crazyMode || this.isActing) { return; } if (!this.canMoveing) { return; } let touch = event.touch; let startPoint = touch.getStartLocation(); let curPoint = touch.getLocation(); this.setPosX(this.oldX + (curPoint.x - startPoint.x)); let myLeft = this.node.x - Config.singleSize * this.gridNum / 2; let myRight = this.node.x + Config.singleSize * this.gridNum / 2; let leftCookie = null; let rightCookie = null; let container = this.panel.container; let curColumns = container[container.length - this.row - 1]; if (curColumns && curColumns.length > 0) { curColumns.sort(function (a, b) { return a.column - b.column; }) } for (let i = 0; i < curColumns.length; i++) { if (curColumns[i].column === this.column) { if (i - 1 >= 0) { leftCookie = curColumns[i - 1]; } if (i + 1 < curColumns.length) { rightCookie = curColumns[i + 1]; } break; } } let leftBorder = 0; let rightBorder = this.parentWidth; if (leftCookie) { leftBorder = leftCookie.node.x + Config.singleSize * leftCookie.gridNum / 2; } if (rightCookie) { rightBorder = rightCookie.node.x - Config.singleSize * rightCookie.gridNum / 2 } //超出边界不动 if (myLeft < leftBorder) { this.setPosX(leftBorder + Config.singleSize * this.gridNum / 2); return; } else if (myRight > rightBorder) { this.setPosX(rightBorder - Config.singleSize * this.gridNum / 2); return; } if (myLeft % Config.singleSize <= Config.singleSize * Config.absort_rate) { //贴左边 this.setPosX(Math.floor(myLeft / Config.singleSize) * Config.singleSize + Config.singleSize * this.gridNum / 2); } else if (myRight % Config.singleSize >= Config.singleSize * (1 - Config.absort_rate)) { //贴右边 this.setPosX(Math.ceil(myLeft / Config.singleSize) * Config.singleSize + Config.singleSize * this.gridNum / 2); } }, onTouchEnd(event) { if (!this.panel.isCanTouch) { return; } if (this.panel.gameScene.isSeeOther) { return; } if (this.panel.crazyMode) { //this.isActing || this.crazyHit(); return; } if (this.panel.useItemIndex !== -1) { this.useItem(this.panel.useItemIndex); return; } if (!this.canMoveing) { return; } this.panel.onMove = false; let self = this; this.destroyCopy(); let oldColumn = this.column; this.setPosX(Math.round((this.node.x - Config.singleSize * this.gridNum / 2) / Config.singleSize) * Config.singleSize + Config.singleSize * this.gridNum / 2); this.updateColumn(); this.hideShader(); if (this.column !== oldColumn) { this.panel.isCanTouch = false; this.panel.step(function () { if (self && self.panel) { if (!Global.getHelpStatus(6)) { self.panel.showCakeHelp(); } self.panel.operationCallback(); if (!self.node.active) { self.node.destroy(); } self.panel.checkLevelUp(); } }); } }, onTouchCancel(event) { this.onTouchEnd(event); }, updateColumn() { this.column = Math.floor((this.node.x - Config.singleSize * this.gridNum / 2) / Config.singleSize); this.oldX = this.column * Config.singleSize + Config.singleSize * this.gridNum / 2; }, // start () { // }, initUI() { this.typeNum = 0;//是否是巧克力 this.hasRedPack = false; // 是否含有红包 this.gridNum = 1;//占格子数 this.colorType = 1;//颜色1-4 this.spr = this.getComponent(cc.Sprite); this.column = 0;//从左往右0开始的格子位置 this.row = 0;//从下往上数的行数 this.oldX = 0; this.parentWidth = this.node.parent.width; this.panel = this.node.parent.parent.getComponent('gamePanel'); this.copy = null; this.shader = null; this.aniFace.node.active = false; this.aniItem.node.active = false; this.aniWeep.node.active = false; this.aniBubble.node.active = false; this.aniExplode.node.active = false; }, /** * gridNum: 方块占的格自数量 * column: 方块所在的列 * typeNum: 特殊方块标记位: * 0: 普通方块 * 1: 巧克力方块 * 2: 删除一行的方块 * 100+: 食材方块 * hasRedPack: 是否含有红包 * hasCube: 是否有宝箱 * eventData: 一些额外的参数 * y: 方块所在行数 * color: 方块的颜色 * * */ initData({gridNum, column, eventData, hasRedPack, typeNum, hasCube}) { this.typeNum = typeNum || 0; this.hasRedPack = hasRedPack || false; this.hasCube = hasCube || false; this.gridNum = (this.typeNum >= 1) ? 1 : gridNum; this.redPack.active = this.hasRedPack; this.cubeNd.active = this.hasCube; if (eventData && eventData.color !== undefined) { this.colorType = eventData.color || 1; } else { this.colorType = Util.getRandom(Config.color_num + 1, 1); if (isNaN(this.colorType)) this.colorType = 1; } this.column = column; this.score = 1; let url = ''; if (this.typeNum === 1) { // 巧克力方块 url = '' + this.gridNum + '_bomb'; this.spr.spriteFrame = Res.getSpriteFrame(url, true); Res.initSpecialBubbleAniWithGridNum(this.aniBubble, this.gridNum); Res.initSpecialBubbleAniWithGridNum(this.aniExplode, this.gridNum); Res.initSpecialFaceAniWithGridNum(this.aniFace, this.gridNum); Res.initSpecialFaceAniWithGridNum(this.aniWeep, this.gridNum); } else { if (this.typeNum === 2) { // 消除一行的道具 url = `1_flash`; } else if (this.typeNum >= 100) { for (let cookie of Config.cookies) { if (cookie.id === this.typeNum) { url = cookie.icon; break; } } }else { // 普通方块 url = `${this.gridNum}_${this.colorType}` } url = url || `${this.gridNum}_${this.colorType}`; this.spr.spriteFrame = Res.getSpriteFrame(url, true); Res.initBubbleAniWithGridNum(this.aniBubble, this.gridNum); Res.initBubbleAniWithGridNum(this.aniExplode, this.gridNum); Res.initFaceAniWithGridNum(this.aniFace, this.gridNum); Res.initFaceAniWithGridNum(this.aniWeep, this.gridNum); } Res.initExtraAni(this.aniBubble); Res.initExtraAni(this.aniExplode); Res.initItemAni(this.aniItem); this.node.setContentSize(Config.singleSize * this.gridNum, Config.singleSize); this.aniItem.node.setContentSize(Config.singleSize * 0.6, Config.singleSize * 0.6); this.aniFace.node.setContentSize(Config.singleSize * this.gridNum * 0.6, Config.singleSize * 0.6); this.aniWeep.node.setContentSize(Config.singleSize * this.gridNum * 0.6, Config.singleSize * 0.6); this.aniBubble.node.setContentSize(Config.singleSize * this.gridNum, Config.singleSize); this.aniExplode.node.setContentSize(Config.singleSize * this.gridNum, Config.singleSize); this.node.x = this.column * Config.singleSize + Config.singleSize * this.gridNum / 2; this.oldX = this.node.x; if (eventData && eventData.y !== undefined) { this.row = eventData.y; this.node.y = Config.singleSize * this.row + Config.singleSize / 2; } else { this.node.y = Config.singleSize / 2; } this.aniFace.on('stop', this.onStop, this); this.aniWeep.on('stop', this.onWeepStop, this); this.aniBubble.on('stop', this.onBubbleStop, this); this.aniExplode.on('stop', this.onExplodeStop, this); this.aniItem.on('stop', this.onItemAniStop, this); this.isMyPanel = this.panel.isMyPanel; this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); }, playAct(act) { let state = this.aniWeep.getAnimationState('blank'); if (state && state.isPlaying) { //nothing } else { if (act === 'blank') { this.aniWeep.node.active = true; this.aniWeep.play(act); } else { this.aniFace.node.active = true; this.aniFace.play(act); } } }, playBubble(act) { if (act === 'explode') { this.aniExplode.node.active = true; this.aniExplode.play(act); } else { this.aniBubble.node.active = true; this.aniBubble.play(act); } }, playItemAni(act) { this.aniItem.node.active = true; this.aniItem.play(act); }, // 播放动画结束,并消除该方块 onWeepStop(eventType, state) { if (!this.aniWeep || (this.aniWeep && !this.aniWeep.isValid)) { return; } this.aniWeep.node.active = false; this.shake(); }, onItemAniStop(eventType, state) { // this.aniItem.node.active = false; }, onStop(eventType, state) { if (!this.aniFace || (this.aniFace && !this.aniFace.isValid)) { return; } this.aniFace.node.active = false; }, onExplodeStop(eventType, state) { if (!this.aniExplode || (this.aniExplode && !this.aniExplode.isValid)) { return; } if (state.name === 'explode') { this.aniExplode.node.active = false; if (this.typeNum === 1 || this.chocolate) { this.node.active = false; this.node.parent = null; } } }, onBubbleStop(eventType, state) { if (!this.aniBubble || (this.aniBubble && !this.aniBubble.isValid)) { return; } this.playBubble('explode'); }, //下落N格 descend() { let oldRow = this.row; for (let i = 0; i < this.panel.container.length; i++) { let rows = this.panel.container[i]; for (let j = 0; j < rows.length; j++) { if (rows[j] === this) { this.row = this.panel.container.length - i - 1; this.node.runAction(cc.moveBy(Config.descend_time, cc.v2(0, -Config.singleSize * (oldRow - this.row))).easing(cc.easeCircleActionIn())); return; } } } }, //上升一格 upper() { this.row++; this.node.runAction(cc.moveBy(Config.rise_time, cc.v2(0, Config.singleSize)).easing(cc.easeCircleActionOut())); }, //抖动 shake() { let self = this; let moveTime = Config.remove_time / (3 * 3); let offsetX = Config.singleSize / 4; let x = this.node.x; let y = this.node.y; let act = cc.repeat(cc.sequence([cc.moveTo(moveTime, cc.v2(x - offsetX, y)), cc.moveTo(moveTime, cc.v2(x + offsetX, y)), cc.moveTo(moveTime, cc.v2(x, y))]), 3); if (this.typeNum === 1 || this.chocolate) { this.node.runAction(act); } else { this.node.runAction(cc.sequence(act, cc.callFunc(function () { self.node.active = false; self.node.parent = null; }))); } }, addScore() { let self = this; let nodeScore = null; let lblScore = null; if (this.typeNum === 1) { nodeScore = cc.instantiate(this.prefabSpecialScore); lblScore = nodeScore.getComponent(cc.Label); } else { //黄色字体的font错的,所以暂时去掉 let fontColor = 0; fontColor = Util.getRandom(2, 1); nodeScore = cc.instantiate(this.prefabScore[fontColor]); lblScore = nodeScore.getComponent(cc.Label); } this.panel.content.addChild(nodeScore); nodeScore.x = this.node.x; nodeScore.y = this.node.y; lblScore.string = '' + this.score; lblScore.node.runAction(cc.sequence(cc.moveBy(Config.square_score_time, cc.v2(0, Config.singleSize)), cc.callFunc(function () { nodeScore.destroy(); // 20190710 播放完后销毁当前node,已解决偶尔出现的砖块无法消除的bug和可能引起内存溢出的问题 // if (self.node) { // self.node.active = false; // } }))); if (this.isMyPanel) { this.panel.gameScene.addScore(this.score); } else { this.panel.addScore(this.score); } }, //copy一份放原地 copyObj() { let brick = cc.instantiate(this.node); this.panel.content.addChild(brick); this.copy = brick.getComponent('gameCookie'); this.copy.initUI(); brick.opacity = 255 * Config.copy_alpha; brick.targetOff(this.copy); }, destroyCopy() { if (this.copy) { this.copy.node.destroy(); this.copy = null; } }, showShader() { this.shader = this.panel.nodeShader; this.shader.active = true; this.shader.x = this.node.x; this.shader.y = this.node.y; this.shader.width = this.node.width; this.shader.height = this.panel.node.height * 2; }, hideShader() { if (this.shader) { this.shader.active = false; this.shader = null; } }, useItem(index, isEvent) { if (index === 0) { this.useItemEat(isEvent); } else if (index === 1) { this.useItemHammer(isEvent); } else if (index === 2) { this.useItemSauce(isEvent); } }, eatItem() { let self = this; let score_rate = this.panel.get_score_rate(1); let score; let remaining_count = 1; let check_finish = function () { if (--remaining_count === 0) { self.panel.settle(); self.panel.isCanEvent = true; self.panel.useItemIndex = -1; self.panel.checkLevelUp(); } }; if (this.typeNum === 1) {//巧克力方块 let curIndex = 0; let curRow = this.panel.container.length - this.row - 1; for (let i = 0; i < this.panel.container[curRow].length; i++) { if (this.panel.container[curRow][i] === this) { curIndex = i; break; } } this.panel.container[curRow].splice(curIndex, 1); let exploded_objs = []; let is_exploded = []; for (let i = 0; i < this.panel.container.length; ++i) { is_exploded.push({}); } score = this.panel.count_exploded([], exploded_objs, is_exploded, this, curRow); this.score = this.gridNum * score_rate * 2; this.panel.exploded_eliminate(exploded_objs, () => { self.panel.score_process(exploded_objs, score, () => { check_finish(); }); }); } else {//材料 let objRemove = []; for (let i = 0; i < this.panel.container.length; i++) { let subRemain = []; let bricks = this.panel.container[i]; for (let j = bricks.length - 1; j >= 0; j--) { if ((bricks[j].typeNum === this.typeNum && bricks[j].typeNum > 1 && this.typeNum > 1) || (bricks[j].colorType === this.colorType && bricks[j].typeNum === 0 && this.typeNum === 0) ) { bricks[j].score = bricks[j].gridNum * score_rate objRemove.push(this.panel.container[i][j]); bricks.splice(j, 1); } } } this.score = this.gridNum * score_rate; score = this.score; this.panel.normal_eliminate(objRemove, [], score, check_finish); } } , //道具吃方块 useItemEat(isEvent) { // if (!Global.getHelpStatus(6)) { // Global.updateHelpStatus(6); // cc.EffectMgr.hideFingerEffect(); // } this.panel.gameScene.nodeUserItem.active = false; if (!isEvent) { cc.jc.gamelog.logUseItem(1, 1, '', ''); if (this.panel.gameScene.levelItems[0] > 0) { this.panel.gameScene.levelItems[0] = 0; } else { Global.item[0]--; Global.saveItem(); } this.panel.gameScene.updateItemBtnState(); if (this.isMyPanel) { cc.SoundMgr.playSound(cc.SoundMgr['item_eat']); } } this.panel.isCanTouch = false; if (this.typeNum === 1) { this.playItemAni('eat'); } else if (this.typeNum > 1) { for (let i = 0; i < this.panel.container.length; i++) { for (let j = 0; j < this.panel.container[i].length; j++) { if (this.panel.container[i][j].typeNum === this.typeNum) { this.panel.container[i][j].playItemAni('eat'); } } } } else { for (let i = 0; i < this.panel.container.length; i++) { for (let j = 0; j < this.panel.container[i].length; j++) { if (this.panel.container[i][j].colorType === this.colorType && this.panel.container[i][j].typeNum === 0) { this.panel.container[i][j].playItemAni('eat'); } } } } let self = this; this.scheduleOnce(function () { self.eatItem(); }, 1); }, //道具巧克力酱 useItemSauce(isEvent) { if (this.typeNum === 1) { return; } this.panel.gameScene.nodeUserItem.active = false; if (!isEvent) { cc.jc.gamelog.logUseItem(3, 1, '', ''); Global.item[2]--; Global.saveItem(); this.panel.gameScene.updateItemBtnState(); if (this.isMyPanel) { cc.SoundMgr.playSound(cc.SoundMgr['item_chocolate']); } } else { this.panel.isCanEvent = true; } this.panel.useItemIndex = -1; this.typeNum = 1; let url = '' + this.gridNum + '_bomb'; this.spr.spriteFrame = Res.getSpriteFrame(url, true); Res.initSpecialBubbleAniWithGridNum(this.aniBubble, this.gridNum); Res.initSpecialBubbleAniWithGridNum(this.aniExplode, this.gridNum); let clips = this.aniFace.getClips(); for (let i = 0; i < clips.length; i++) { this.aniFace.removeClip(clips[i], true); } clips = this.aniWeep.getClips(); for (let i = 0; i < clips.length; i++) { this.aniWeep.removeClip(clips[i], true); } Res.initSpecialFaceAniWithGridNum(this.aniFace, this.gridNum); Res.initSpecialFaceAniWithGridNum(this.aniWeep, this.gridNum); }, //道具敲方块 useItemHammer(isEvent) { if (this.gridNum === 1) { Util.showTips(this.panel.gameScene.node, cc.i18n.t('tips.no_beat')); return; } // if (!Global.getHelpStatus(5)) { // Global.updateHelpStatus(5); // cc.EffectMgr.hideFingerEffect(); // } this.panel.gameScene.nodeUserItem.active = false; if (!isEvent) { cc.jc.gamelog.logUseItem(2, 1, '', ''); if (this.panel.gameScene.levelItems[1] > 0) { this.panel.gameScene.levelItems[1] = 0; } else { Global.item[1]--; Global.saveItem(); } this.panel.gameScene.updateItemBtnState(); if (this.isMyPanel) { cc.SoundMgr.playSound(cc.SoundMgr['item_hammer']); } } this.panel.breakAllBrick(); }, // 疯狂模式敲方块 crazyHit() { this.isActing = true; this.playItemAni('hammer'); this.scheduleOnce(this.removeSelf, 1); }, removeSelf() { let self = this; cc.jc.gameVibrateLong(); let score_rate = this.panel.get_score_rate(1); if (this.typeNum === 1) { let curIndex = 0; let curRow = this.panel.container.length - this.row - 1; for (let i = 0; i < this.panel.container[curRow].length; i++) { if (this.panel.container[curRow][i] === this) { curIndex = i; break; } } this.panel.container[curRow].splice(curIndex, 1); let exploded_objs = []; let is_exploded = []; for (let i = 0; i < this.panel.container.length; ++i) { is_exploded.push({}); } let score = this.panel.count_exploded([], exploded_objs, is_exploded, this, curRow); this.score = this.gridNum * score_rate * 2; for (let obj of exploded_objs) { obj.isActing = true; } this.panel.exploded_eliminate(exploded_objs, () => { self.panel.score_process(exploded_objs, this.score, () => { if (!self.panel.crazyMode) { self.panel.settle(function(){ self.panel.checkLevelUp(); }); } }); }); } else { let curIndex = 0; let curRow = this.panel.container.length - this.row - 1; for (let i = 0; i < this.panel.container[curRow].length; i++) { if (this.panel.container[curRow][i] === this) { curIndex = i; break; } } this.panel.container[curRow].splice(curIndex, 1); this.score = this.gridNum * score_rate * 2; this.panel.normal_eliminate([this], [], this.score, function(){ if (!self.panel.crazyMode) { self.panel.settle(function(){ self.panel.checkLevelUp(); }); } }); } }, EventMoveTo(toX) { let self = this; this.column = toX; this.node.x = this.column * Config.singleSize + Config.singleSize * this.gridNum / 2; this.panel.step(function () { if (self && self.panel) { if (self && self.panel) { self.panel.isCanEvent = true; self.panel.operationCallback(); if (!self.node.active) { self.node.destroy(); } } } }); }, EventUseItem(index) { this.useItem(index, true); } // update (dt) {}, });