import GameModel from "../Model/GameModel"; import Toast from "../Utils/Toast"; import SdkUtil from "../Utils/SdkUtil" import MusicImgChange from "../Utils/MusicImgChange" import AudioUtils from "../Utils/AudioUtils"; cc.Class({ extends: cc.Component, properties: { grid:{ default:null, type:cc.Node }, audioButton:{ default: null, type: cc.Node }, audioSource:{ type:cc.AudioSource }, skillTip:{ default:null, type:cc.Node }, //点击时的音效 click:{ default:null, type: cc.AudioClip }, isVideoAd:false, // //只消除一个的道具数量 // eliminateOneNum:10, // // //暂停步数的道具数量 // stepPauseNum:10, // // //剩余刷新次数 // refreshNum:10, // // //消除整行的道具数量 // crushRowAllNum:8, //静音按钮是否按下:false表示未按下 musicBtnState: false, }, isCrushOne: false, //消除一个格子 isStepPause: false, //计步器 isCrushRow: false, //消除整行 isCrushCol: false, //消除整列 isCrushOneType: false, //消除一种类型 // LIFE-CYCLE CALLBACKS: onLoad () { let audioButton = this.node.parent.getChildByName("AudioButton") audioButton.on("click",this.callback,this) SdkUtil.Instance.motivational_Video_Load() this.gameModel = new GameModel(); this.gameModel.init(4); this.gridScript = this.grid.getComponent("GridView"); this.gridScript.setController(this); this.gridScript.initWithCellModels(this.gameModel.getCells()); this.audioSource = cc.find("Canvas/GameScene")._components[1].audio; }, callback(){ let state = this.audioSource._state; state === 1 ? this.audioSource.pause() : this.audioSource.play(); Toast(state === 1 ? "关闭背景音乐🎵" : "打开背景音乐🎵"); }, //音乐开关按钮 musicBtn(){ let sprite = cc.find("Canvas/MusicBtn") // console.log(sprite) // sprite.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("MusicImgChange").imgOff this.musicBtnState = !this.musicBtnState; if (this.musicBtnState){ // console.log("音乐关") this.audioSource.pause() sprite.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("MusicImgChange").imgOff }else { // console.log("音乐开") this.audioSource.play() sprite.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("MusicImgChange").imgOn } }, selectCell(pos){ // console.log("ppos:",pos) return this.gameModel.selectCell(pos); }, cleanCmd(){ this.gameModel.cleanCmd(); }, start () { //初始化加载的动物种类的数量 // this.initNum = 4; //只消除一个的道具数量 this.eliminateOneNum = 10; //暂停步数的道具数量 this.stepPauseNum = 10; //剩余刷新次数 this.refreshNum = 10; //消除整行的道具数量 this.crushRowAllNum = 10; //消除整列的道具数量 this.crushColAllNum = 10; //消除一种类型的道具数据 this.crushOneTypeNum = 10; }, update (dt) { //获取只消除一个的道具数量 let eliminateNum = cc.find("Canvas/BottomUtils/Utils/eliminateOne/eliminateOneNum").getComponent(cc.Label); eliminateNum.string = this.eliminateOneNum; //获取暂停步数的道具数量 let stepPauseNum = cc.find("Canvas/BottomUtils/Utils/stepPause/stepPauseNum").getComponent(cc.Label); stepPauseNum.string = this.stepPauseNum; //获取刷新次数 let refreshNum = cc.find("Canvas/BottomUtils/Utils/refresh/refreshNum").getComponent(cc.Label); refreshNum.string = this.refreshNum; //获取消除整行的道具数量 let crushOneRow = cc.find("Canvas/BottomUtils/Utils/crushRow/crushRowNum").getComponent(cc.Label); crushOneRow.string = this.crushRowAllNum; //获取消除整列的道具数量 let crushOneCol = cc.find("Canvas/BottomUtils/Utils/crushCol/crushColNum").getComponent(cc.Label); crushOneCol.string = this.crushColAllNum; //获取消除一种类型的道具数量 let crushOneType = cc.find("Canvas/BottomUtils/Utils/bombOne/crushOneTypeNum").getComponent(cc.Label); crushOneType.string = this.crushOneTypeNum; }, //道具消除一个块 processCrushOne(pos){ return this.gameModel.processCrushOne(pos); }, //道具消除一行 processCrushRow(pos){ return this.gameModel.processCrushRow(pos); }, //道具消除一列 processCrushCol(pos){ return this.gameModel.processCrushCol(pos); }, //道具消除一种类型 processCrushOneType(pos){ return this.gameModel.processCrushOneType(pos); }, onSkill(e,type){ if(this.isVideoAd){ SdkUtil.Instance.motivational_Video_Show(()=>{ this[type]() }, ()=>{ console.log('激励广告加载失败') }) }else{ this[type]() } }, //道具消除一个 onEliminateOne(){ cc.audioEngine.play(this.click, false, 1); if (this.gridScript.isInPlayAni) { return } if (this.eliminateOneNum > 0){ this.eliminateOneNum--; this.isCrushOne = true; const msg = this.skillTip.getChildByName("msg"); msg.getComponent(cc.Label).string = "选择一个需要消除的块" this.showSkillTip(true); }else { Toast("道具已用完") } }, //步数暂停 onStepPause(){ cc.audioEngine.play(this.click, false, 1); if (this.gridScript.isInPlayAni) { return }; if (this.stepPauseNum > 0){ this.stepPauseNum--; this.isStepPause = true; const msg = this.skillTip.getChildByName("msg"); msg.getComponent(cc.Label).string = "步数暂停中"; this.showSkillTip(true); }else { Toast("道具已用完") } }, //刷新 onRefresh(){ cc.audioEngine.play(this.click, false, 1); if (this.gridScript.isInPlayAni) { return }; if (this.refreshNum > 0){ this.refreshNum--; this.grid.children.forEach(item => { if (item.name != "bg"){ item.destroy(); } }) this.gameModel = new GameModel(); this.gameModel.init(4); this.gridScript = this.grid.getComponent("GridView"); this.gridScript.setController(this); this.gridScript.initWithCellModels(this.gameModel.getCells()); // console.log(this.refreshNum) }else { Toast("刷新次数已用完") } }, showSkillTip(isShow){ // console.log("isShow:",isShow) this.skillTip.active = isShow; }, //道具消除整行 onSkillRowAll(){ // console.log("点击了消除整行按钮") cc.audioEngine.play(this.click, false, 1); if (this.gridScript.isInPlayAni) { return } if (this.crushRowAllNum > 0){ this.crushRowAllNum--; this.isCrushRow = true; const msg = this.skillTip.getChildByName("msg"); msg.getComponent(cc.Label).string = "选择需要删除的行"; this.showSkillTip(true); }else { Toast("道具已用完") } }, //道具消除整列 onSkillColAll(){ // console.log("点击了消除整列") cc.audioEngine.play(this.click, false, 1); //增加点击音效 if (this.gridScript.isInPlayAni) { return } if (this.crushColAllNum > 0){ this.crushColAllNum--; this.isCrushCol = true; const msg = this.skillTip.getChildByName("msg"); msg.getComponent(cc.Label).string = "选择需要删除的列"; this.showSkillTip(true); }else { Toast("道具已用完") } }, //道具消除一种类型 onSkillOneType(){ // console.log("点击了消除一种类型") // let nowAllCells = this.gameModel.getCells(); // console.log("当前所有格子:",nowAllCells) cc.audioEngine.play(this.click, false, 1); if (this.gridScript.isInPlayAni) { return } if (this.crushOneTypeNum > 0){ this.crushOneTypeNum--; this.isCrushOneType = true; const msg = this.skillTip.getChildByName("msg"); msg.getComponent(cc.Label).string = "选择需要删除的类型"; this.showSkillTip(true); }else { Toast("道具已用完") } } }); /** * 组件脚本: * AudioUtils.js、CellView.js、EffectLayer.js、GameController.js、GridView.js、LoginController.js * */