// Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html import { SaveDataManager } from "../../component/SaveDataManager"; import { SoundManager } from "../../component/SoundManager"; import { SoundName, WindowName } from "../../Global"; import { BattleEvent, UserSkill } from "../../kernel/battle/BattleConst"; import { BattleManager } from "../../kernel/battle/BattleManager"; import { MapUtil } from "../../kernel/battle/map/MapUtil"; import { UserSkillShurikenData } from "../../kernel/battle/userSkill/UserSkillShurikenData"; import { UIBase } from "../UIBase"; import { UIManager } from "../UIManager"; import UIBattleUserSkillBtn from "./UIBattleUserSkillBtn"; import { UICommonNoticeEnableParam } from "./UICommonNoticeEnableParam"; const { ccclass, property } = cc._decorator; @ccclass export default class UIBattle extends UIBase { private _cancelNode: cc.Node; private _skillNode: cc.Node; private _roundingNode: cc.Node; private _starList: cc.Node[] = []; private _pauseNode: cc.Node; private _noticeParam: UICommonNoticeEnableParam; // private _collecting: boolean = false; //#region LIFE-CYCLE CALLBACKS: onLoad() { this._useCloseEffect = false; this._usePopUpEffect = false; this._cancelNode = this.node.getChildByName('bottom').getChildByName('nodeCancel'); this._skillNode = this.node.getChildByName('bottom').getChildByName('skillNode'); this._roundingNode = this.node.getChildByName('bottom').getChildByName('roundingNode'); this._roundingNode.active = false; this._pauseNode = this.node.getChildByName('pauseNode'); this._pauseNode.active = false; for (let i = 1; i <= 3; ++i) { this._starList.push(this.node.getChildByName('top').getChildByName('prgStar').getChildByName('star' + i.toString())); } this._noticeParam = new UICommonNoticeEnableParam(); this._noticeParam.desc = '您想重新开始游戏吗?'; this._noticeParam.callBackThisObj = this; this._noticeParam.doneCallBack = this.onNoticeRestartDoneClick; BattleManager.ins.eventNode.once(BattleEvent.BATTLE_START, this.onBattleStart, this); BattleManager.ins.eventNode.on(BattleEvent.BLOCK_COMBO, this.onBlockCombo, this); BattleManager.ins.eventNode.on(BattleEvent.ROUND_END, this.onRoundEnd, this); BattleManager.ins.eventNode.on(BattleEvent.ROUND_START, this.onRoundStart, this); BattleManager.ins.eventNode.on(BattleEvent.SKILL_START, this.onSkillStart, this); } start() { this.node.getComponent(cc.Widget).updateAlignment(); //必须先根据舞台调整尺寸 let dis = this.node.height / 2 + this.node.getChildByName('bottom').y; let nodeRounding = this.node.getChildByName('bottom').getChildByName('roundingNode'); nodeRounding.y -= (nodeRounding.height + dis); nodeRounding.active = true; } protected onEnable(): void { // this.schedule(this.onLazyUpdate, 1 / 20); } protected onDisable(): void { // this.unschedule(this.onLazyUpdate); } protected onDestroy(): void { // cc.Tween.stopAllByTarget(this._tobj); this.unschedule(this.onLazyUpdate); // BattleManager.ins.eventNode.off(BattleEvent.BATTLE_START, this.onBattleStart, this); // BattleManager.ins.eventNode.off(BattleEvent.BLOCK_COMBO, this.onBlockCombo, this); // BattleManager.ins.eventNode.off(BattleEvent.ROUND_START, this.onRoundStart, this); // BattleManager.ins.eventNode.off(BattleEvent.ROUND_END, this.onRoundEnd, this); BattleManager.ins.eventNode.targetOff(this); } //#endregion //#region public method setCancelNodeVisible(value: boolean) { this._cancelNode.active = value; } setCancelNodeHit(value: boolean) { this._cancelNode.opacity = (value ? 255 : 153); } //#endregion //#region private method private onLazyUpdate() { let battle = BattleManager.ins.curBattle if (battle) { let rate = (battle.doubleShoot ? 2 : 1); this.node.getChildByName('bottom').getChildByName('txtMarblesNum').getComponent(cc.Label).string = "弹球:" + (battle.marblesNum * rate).toString(); if (battle.mapData) { this.node.getChildByName('top').getChildByName('nodeScore').getComponentInChildren(cc.Label).string = battle.mapData.curScore.toString(); this.node.getChildByName('top').getChildByName('prgStar').getComponent(cc.ProgressBar).progress = battle.mapData.curScore / MapUtil.getThreeStarScore(battle.mapData); let starRate = MapUtil.getStarRate(battle.mapData); for (let i = 0; i < 3; ++i) { let get = (i + 1 <= starRate); let enableNode = this._starList[i].getChildByName('enable'); if (enableNode.active == false && get) { enableNode.active = get; let spine = enableNode.getComponent(sp.Skeleton); spine.setAnimation(0, 'animation2', false); spine.setCompleteListener(() => { spine.setAnimation(0, 'animation3', true); }); SoundManager.ins.playEffect(SoundName.GET_STAR); } else { enableNode.active = get; } } } let nodeSpeedUp = this.node.getChildByName('nodeSpeedUp'); if (battle.roundRunning && battle.speedUp) { if (nodeSpeedUp.active == false) { nodeSpeedUp.active = true; nodeSpeedUp.getComponentInChildren(cc.Animation).play(); } } else { nodeSpeedUp.getComponentInChildren(cc.Animation).stop(); nodeSpeedUp.active = false; } let now = Date.now(); let goldenAimLeftTime = SaveDataManager.ins.runtimeData.goldenAimEndTime - Date.now(); let txtGoldenAim: cc.Node = cc.find('top/btnGoldenAim/txtTime', this.node); if (goldenAimLeftTime > 0) { txtGoldenAim.active = true; let timeStr = slib.StringUtil.formatTimeBySecs(goldenAimLeftTime / 1000); timeStr = timeStr.slice(3, timeStr.length); txtGoldenAim.getComponent(cc.Label).string = timeStr; } else { txtGoldenAim.active = false; } } } private tweenTarget(node: cc.Node, up: boolean) { let dis = this.node.height / 2 - Math.abs(this.node.getChildByName('bottom').y); // if (node == this.node.getChildByName('bottom').getChildByName('roundingNode') && !up) { // //回收栏下降时 添加额外的回调 // cc.tween(node).to(0.3, { position: cc.v3(node.x, up ? 0 : -dis - 200, node.z) }, { easing: 'backOut' }).call(() => { this._collecting = false; }) // .start(); // } // else { cc.tween(node).to(0.3, { position: cc.v3(node.x, up ? 0 : -dis - 200, node.z) }, { easing: 'backOut' }) .start(); // } } //#endregion //#region events private onBattleStart() { let spineNode = this.node.getChildByName('spineGameStart'); spineNode.active = true; let spine = spineNode.getComponent(sp.Skeleton); spine.setCompleteListener(() => { spineNode.active = false; BattleManager.ins.shootTouchEnable = true }); spine.setAnimation(0, 'animation', false); for (let i = 0; i < 4; ++i) { this._skillNode.getChildByName('skill' + (i + 1).toString()).getComponent(UIBattleUserSkillBtn).setId(i + 1); // this._skillNode.getChildByName('skill' + (i + 1).toString()).getComponent(UIBattleUserSkillBtn).setLock(false); } this.schedule(this.onLazyUpdate, 1 / 20); } // private _tobj = { progress: 0 }; private onBlockCombo(combo: number) { let nodeCombo = this.node.getChildByName('nodeCombo'); let txtNum = nodeCombo.getChildByName('txtNum').getComponent(cc.Label); if (nodeCombo.active == false) { nodeCombo.active = true; } txtNum.string = combo.toString(); nodeCombo.getComponent(cc.Animation).play(); //连击描述 let nodeComboTip = this.node.getChildByName('nodeComboTip'); let frame = nodeComboTip.getChildByName('UI_fx_bksy').getChildByName('UI_fx_bksy02').getComponent(cc.Sprite); let showTip = true; if (combo == 5) { //很棒 cc.resources.load('ui/battle/UI_yxz_hb', cc.SpriteFrame, (error, asset) => { frame.spriteFrame = asset as cc.SpriteFrame; }); } else if (combo == 10) { //了不起 cc.resources.load('ui/battle/UI_yxz_lbq', cc.SpriteFrame, (error, asset) => { frame.spriteFrame = asset as cc.SpriteFrame; }); } else if (combo == 15) { //真了不起 cc.resources.load('ui/battle/UI_yxz_zlbq', cc.SpriteFrame, (error, asset) => { frame.spriteFrame = asset as cc.SpriteFrame; }); } else if (combo == 20) { //完美 cc.resources.load('ui/battle/UI_yxz_wm', cc.SpriteFrame, (error, asset) => { frame.spriteFrame = asset as cc.SpriteFrame; }); } else if (combo == 25) { //太疯狂了 cc.resources.load('ui/battle/UI_yxz_tfkl', cc.SpriteFrame, (error, asset) => { frame.spriteFrame = asset as cc.SpriteFrame; }); } else if (combo == 30) { //不可思议 cc.resources.load('ui/battle/UI_yxz_bksy', cc.SpriteFrame, (error, asset) => { frame.spriteFrame = asset as cc.SpriteFrame; }); } else { showTip = false; } if (showTip) { // cc.Tween.stopAllByTarget(this._tobj); nodeComboTip.active = true; nodeComboTip.getChildByName('UI_fx_bksy').getComponent(cc.Animation).once('finished', () => { nodeComboTip.active = false; }, this); nodeComboTip.getChildByName('UI_fx_bksy').getComponent(cc.Animation).play('UI_fx_bksy'); // this._tobj.progress = 0.618; // nodeComboTip.setScale(0.618, 0.618); // cc.tween(this._tobj).to(0.2, { progress: 1 }, { // onUpdate: (target: { progress: number }) => { // nodeComboTip.setScale(target.progress, target.progress); // }, // }).delay(1).to(0.2, { progress: 0 }, { // onUpdate: (target: { progress: number }) => { // nodeComboTip.setScale(target.progress, target.progress); // }, // }).call(() => { nodeComboTip.active = false; }).start(); } } private onRoundStart() { let nodeSkill = this.node.getChildByName('bottom').getChildByName('skillNode'); let nodeRounding = this.node.getChildByName('bottom').getChildByName('roundingNode'); this.tweenTarget(nodeSkill, false); this.tweenTarget(nodeRounding, true); } private onRoundEnd() { let nodeCombo = this.node.getChildByName('nodeCombo'); nodeCombo.active = false; let nodeSkill = this.node.getChildByName('bottom').getChildByName('skillNode'); let nodeRounding = this.node.getChildByName('bottom').getChildByName('roundingNode'); this.tweenTarget(nodeSkill, true); this.tweenTarget(nodeRounding, false); } private onSkillStart(skillId: number) { if (skillId == UserSkill.BOMB) { let spNode = this.node.getChildByName('userSkillBombSpine'); spNode.active = true; spNode.getComponent(sp.Skeleton).setCompleteListener(() => { spNode.active = false }); spNode.getComponent(sp.Skeleton).setAnimation(0, 'animation', false); } else if (skillId == UserSkill.SHURIKEN) { let spNode = this.node.getChildByName('userSkillShuriken'); spNode.active = true; spNode.getComponent(sp.Skeleton).setCompleteListener(() => { if (spNode.getComponent(sp.Skeleton).animation == 'in') { spNode.getComponent(sp.Skeleton).setAnimation(0, 'att', false); //调整位置 let p = BattleManager.ins.curBattle.mapData.getPixelPosByGridPos(cc.v2(0, UserSkillShurikenData.maxY)); BattleManager.ins.curBattle.mapView.convertToWorldSpaceAR(p, p); this.node.convertToNodeSpaceAR(p, p); spNode.setPosition(0, p.y); } else { spNode.active = false } }); spNode.getComponent(sp.Skeleton).setAnimation(0, 'in', false); } } onBtnPauseClick() { // BattleManager.ins.leaveBattle(); // UIManager.ins.openScene('Main'); // UIManager.ins.openWindow(WindowName.PAUSE); this._pauseNode.active = true; BattleManager.ins.paused = true; this.refreshSoundBtn(); } onBtnCollectClick() { // if (!this._collecting) { // this._collecting = true; BattleManager.ins.curBattle.collectAll(); // } } onBtnPauseCloseClick() { this._pauseNode.active = false; BattleManager.ins.paused = false; } onBtnMusicClick() { SaveDataManager.ins.setMusicMute(!SaveDataManager.ins.runtimeData.musicMute); this.refreshSoundBtn(); if (SaveDataManager.ins.runtimeData.musicMute) { SoundManager.ins.stopMusic(); } else { SoundManager.ins.playMusic(); } } onBtnMuteClick() { SaveDataManager.ins.setSoundEffectMute(!SaveDataManager.ins.runtimeData.SEMute); this.refreshSoundBtn(); } onBtnRestartClick() { UIManager.ins.openWindow(WindowName.COMMON_NOTICE, this._noticeParam); } onGoldenAimClick() { UIManager.ins.openWindow(WindowName.GOLDEN_AIM); } private onNoticeRestartDoneClick() { // this.leave(); BattleManager.ins.leaveBattle(); UIManager.ins.openScene('Battle'); // UIManager.ins.closeWindow(WindowName.PAUSE, () => { // BattleManager.ins.leaveBattle(); // UIManager.ins.openScene('Main'); // }); } private refreshSoundBtn() { cc.find('New Layout/btnMusic/Background/disable', this._pauseNode).active = SaveDataManager.ins.runtimeData.musicMute; cc.find('New Layout/btnVol/Background/disable', this._pauseNode).active = SaveDataManager.ins.runtimeData.SEMute; } onBtnHomeClick() { BattleManager.ins.leaveBattle(); UIManager.ins.openScene('Main'); } //#endregion }