const SkillApi = require('../net/SkillApi'); const DWTool = require("../utils/DWTool"); const GameModule = require("../utils/GameModule"); const buildingLevel = require('../data/buildingLevel'); const {GameNotificationKey, WechatShareType} = require('../utils/GameEnum'); const WeChat = require('../net/WeChat'); const TapTapTool = require("../utils/TapTapTool"); const AlertManager = require('../utils/AlertManager'); // const ADVideo = require('../utils/ADVideo'); const HomeApi = require('../net/HomeApi'); // const GameRedDot = require('../utils/GameEnum').GameRedDot; var Promise = require('../lib/es6-promise').Promise; // const GameRedDot = require('../utils/GameEnum').GameRedDot; // WeChat cc.Class({ extends: cc.Component, properties: { userSkillButton: { tooltip: '使用技能button', default: null, type: cc.Button }, titleRichText: { tooltip: 'title啦', default: null, type: cc.RichText }, skillIconBg: { tooltip: '技能icon的背景', default: null, type: cc.Sprite }, ///使用技能的按钮背景图片 useSkillButtonBgSprite: cc.Sprite, /// 使用技能的text skillUseTitleRichText: cc.RichText, /// 描述text subTitleRichText: cc.RichText, ///技能icon skillIcon: cc.Sprite, ///总部大楼特有,等级 / 25 fillBuldingNode: cc.Node, fillBuldingSprite: cc.Sprite, lightSpriteFrame: cc.SpriteFrame, graySpriteFrame: cc.SpriteFrame, /// 里程碑奖励显示的button awardButton: cc.Button, /// 升级技能的弹窗 alertNode: cc.Node, ///砖石数 diamondCount: 0, freeSkillNode: cc.Node, freeSkillTitleRichText: cc.RichText, /// 金币数 coinCount: 0, building: 0, _awardCount: 0, _isAlert: false, /// 0说明是用金币 1 说明是用分享 2 看广告 _adState: 0, //// 当前使用的广告状态 // _useAdState: 0, _isInitAd: false }, // onLoad () { // }, start () { this._isAction = false; }, onDestroy() { GameEvent.off("skill3_use_begain", this); GameEvent.off(GameNotificationKey.SkillThreeHasDone, this); GameEvent.off("skill_five_use", this); GameEvent.off(GameNotificationKey.UpBuildingLevel, this); GameEvent.off(GameNotificationKey.ShowShareAction, this); // GameEvent.off(GameNotificationKey.AdBuildingStateUpdate, this); }, update (dt) { /// 如果说是建筑 并且没有处于响应 if (this.fillBuldingNode.active && this._isAction === false) { let isCanUse = this.judgeIsCanUse(); if (CC_WECHATGAME) { let isAd = DWTool.checkIsOldUser() && GameGlobal._upBuildingInfo.maxCount > GameGlobal._upBuildingInfo.count && GameGlobal._upBuildingInfo.cdTime <= 0; /// 如果有广告或者分享 if (!this._isInitAd && isAd) { this._isInitAd = true; this.initBuildingAd(); } if (isAd && !isCanUse) { this.freeSkillNode.active = true; this.userSkillButton.node.active = false; } else { this.freeSkillNode.active = false; this.userSkillButton.node.active = true; } } let inter = this.userSkillButton.interactable; if (isCanUse != inter) { this.setupGrayBg(!inter); } } }, /// 初始化第一个建筑相关的数据 initBuilding(awardCount) { this.initAward(awardCount) this.userSkillButton.interactable = false; this.updateBuildingData(); /// 第三个技能开始使用 GameEvent.on("skill3_use_begain", this, () => { this.updateBuildingData(); }); /// 第三个技能结束 GameEvent.on(GameNotificationKey.SkillThreeHasDone, this, () => { this.updateBuildingData(); }); ////第五个技能开始使用 GameEvent.on("skill_five_use", this, () => { this.updateBuildingData(); }); // /// cd时间到了 // GameEvent.on(GameNotificationKey.AdBuildingStateUpdate, this, () => { // this.initBuildingAd(); // }); GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => { /// 说明是技能的关闭状态 if (adState === 3 && callBack === 'skill') { this.finishAction(); } if (adState === 0 || adState === 1) { this.initBuildingAd(); } }); GameModule.homeGuide.on('Fire_state16', this.awardBtnAction, this); // let buildingLevelInfo = buildingLevel[buildingInfo.buildingLevel - 1]; }, initBuildingAd() { if (!CC_WECHATGAME) { return; } //// 说明有广告 if (GameGlobal._adVideoState == 0) { this._adState = 2; this.freeSkillTitleRichText.string = ' 看视频\n免费升级' + ''; } else if (GameGlobal._adVideoState === 1) { this._adState = 1; this.freeSkillTitleRichText.string = ' 分享\n免费升级' + ''; } }, finishAction() { HomeApi.reportInformation(1, () => { GameGlobal._upBuildingInfo.count += 1; /// 时间重置为5分钟 GameGlobal._upBuildingInfo.cdTime = 5 * 60 * 1000; this.requestUpBuildingLevel(true); }, (code, msg) => { console.log(msg); }) }, //// 初始化技能数据 init(skillData) { this.fillBuldingNode.active = false; this.freeSkillNode.active = false; this.awardButton.node.active = false; this.loadSpriteFrame(skillData); this.skillData = skillData; this.updateData(); this.setupMaxLevel(); /// 更新技能相关 GameEvent.on('skill_update', this, () => { this.updateData(); this.setupMaxLevel(); }); }, /// 初始化领取里程碑奖励数据 initAward(awardCount) { this._awardCount = awardCount; let needAwardCount = parseInt(GameModule.userInfo.buildingLevel / 25); if (needAwardCount > this._awardCount) { this.awardButton.node.active = true; } else { this.awardButton.node.active = false; } }, /// 更新建筑相关数据 updateBuildingData() { let currentLevel = GameModule.userInfo.buildingLevel; let upGet = GameModule.userInfo.perpetualClickMt; this._upGet = upGet; if (currentLevel <= 25) { let buildingObj = buildingLevel.find(n => { return n.level == currentLevel; }); this._upGold = {'n': buildingObj.upGold, 'e': 0}; } else { let mul = TapTapTool.toPower(currentLevel - 26); this._upGold = TapTapTool.multiple({'n': 1056, 'e': 0}, mul); } this.skillUseTitleRichText.string = '+' + TapTapTool.parseToString(this._upGet) + '收益' + `
${TapTapTool.parseToString(this._upGold)}`; // this.skillUseTitleRichText.string = '+' + TapTapTool.parseToString(upGet) + '收益' + `
${TapTapTool.parseToString(this._upGold)}`; this.fillBuldingNode.active = true; this.fillBuldingSprite.fillRange = (currentLevel % 25) / 25; this.diamondCount = 0; this.coinCount = this._upGold; this.building = 0; this.titleRichText.string = '等级' + currentLevel + '' + '' + ' 总部大楼' + ''; this.subTitleRichText.string = ` ${TapTapTool.parseToString(GameModule.userInfo.coinTap)}收益/点击`; }, /// 更新技能相关数据 updateData() { let levelInfo = GameGlobal.BuildingManager.getSkillLevelInfo(this.skillData.skillId, this.skillData.skillLevel); let skillInfo = GameGlobal.BuildingManager.getSkillInfo(this.skillData.skillId); this._skillInfo = skillInfo; //// 如果是没有拥有的 if (this.skillData.isHave == 0) { /// 说明是用其它的来解锁 if (skillInfo.buildingLevel == 0) { /// 用的砖石 技能4是用砖石或者金币来解锁的 this.showBuySkill(); } else { /// 说明总部等级不够,还没有解锁 if (GameModule.userInfo.buildingLevel < skillInfo.buildingLevel) { this.coinCount = {'n': 0, 'e': 0}; this.diamondCount = 0; this.building = skillInfo.buildingLevel; this.skillUseTitleRichText.string = "
总部大楼等级" + skillInfo.buildingLevel + ''; //// 如果是因为建筑锁住的 this._upNoticefacation = false; GameEvent.on(GameNotificationKey.UpBuildingLevel, this, () => { if (!this._upNoticefacation && GameModule.userInfo.buildingLevel >= this.building && this.skillData.isHave == 0) { /// 实时技能直接拥有 this._upNoticefacation = true; if (this.skillData.skillId < 4) { /// 直接解锁它 this.unlockedSkill(); } else { this.showBuySkill(); this.setupGrayBg(this.judgeIsCanUse()); } } }); /// 那就需要去购买了 } else { this.showBuySkill(); } } } else { this.userSkillButton.interactable = true; let diamondCount = levelInfo.bonus[10002]; /// 用的砖石 if (diamondCount != undefined) { this.diamondCount = diamondCount; this.coinCount = {'n': 0, 'e': 0}; this.building = 0; this.skillUseTitleRichText.string = `升级
${diamondCount}钻石`; } } this.setupGrayBg(this.judgeIsCanUse()); if (this.skillData.skillLevel > 0) { this.titleRichText.string = '等级' + + this.skillData.skillLevel + ' ' + '' + skillInfo.name + ''; } else { this.titleRichText.string = '技能' + ' ' + '' + skillInfo.name + ''; } if (levelInfo == undefined) { this.subTitleRichText.string = '' + skillInfo.desc + ''; } else { this.subTitleRichText.string = '' + levelInfo.desc + ''; } }, /// 设置使用技能的背景 setupGrayBg(isActive) { this.userSkillButton.interactable = isActive; this.useSkillButtonBgSprite.spriteFrame = isActive ? this.lightSpriteFrame : this.graySpriteFrame; }, //// 设置技能达到最大值的时候 setupMaxLevel() { if (this.skillData.skillLevel >= this.skillData.maxLevel) { this.userSkillButton.interactable = false; this.skillUseTitleRichText.node.active = false; DWTool.loadResSpriteFrame('./textures/skill/skill_max') .then((spriteFrame) => { this.useSkillButtonBgSprite.spriteFrame = spriteFrame; }); } }, /// 加载技能icon图片 loadSpriteFrame(skillData) { if (skillData.skillId <= 3) { let bgPath = './textures/skill/400' + skillData.skillId + skillData.skillId; DWTool.loadResSpriteFrame(bgPath) .then((spriteFrame) => { this.skillIconBg.spriteFrame = spriteFrame; }); } let iconPath = './textures/skill/4000' + skillData.skillId; DWTool.loadResSpriteFrame(iconPath) .then((spriteFrame) => { this.skillIcon.spriteFrame = spriteFrame; }); }, ///////////// ******* 网络请求 ****** ////// //// 购买技能 buySkill(skillId) { return new Promise((resolve, reject) => { // 获取目标用户的建筑 SkillApi.buySkill(skillId, (respondData) => { resolve(respondData); }, (code, msg) => { reject({code, msg}); }); }); }, /// 升级建筑 upBuildingLevel(upLevel) { return new Promise((resolve, reject) => { // 获取目标用户的建筑 SkillApi.upBuildingLevel(upLevel, (respondData) => { resolve(respondData); }, (code, msg) => { reject({code, msg}); }); }); }, /// 上报里程碑奖励 report() { return new Promise((resolve, reject) => { // 获取目标用户的建筑 SkillApi.report(3, (respondData) => { resolve(respondData); }, (code, msg) => { reject({code, msg}); }); }); }, ///////////******* 技能升级购买相关 *********///////////////////////// //// 购买或者升级技能 useSkillButtonAction() { GameModule.audioMng.playClickButton(); this.userSkillButton.interactable = false; /// 如果是升级建筑 if (this.fillBuldingNode.active) { this.upBuildingLevelAction(); } else { this.skillAction(); } }, /// 免费升级技能的按钮 freeUpSKillAction() { GameModule.audioMng.playClickButton(); if (this._adState == 1) { GameEvent.on(GameNotificationKey.ShowShareAction, this, (type, isOk) => { if (type == WechatShareType.UpBuilding) { if (isOk) { this.finishAction(); } GameEvent.off(GameNotificationKey.ShowShareAction, this); } }); WeChat.shareAction(WechatShareType.UpBuilding, () => { }, () => { console.log('分享失败或取消'); }); /// 如果是看视频 } else { GameGlobal._adVideo.showVideo('skill'); } }, upBuildingLevelAction() { this._isAction = true; this.requestUpBuildingLevel(false); }, requestUpBuildingLevel(isFree) { this.upBuildingLevel(1).then((respondData) => { this.userSkillButton.interactable = true; this._isAction = false; // Global.userData.buildingLevel = respondData.level; GameModule.userInfo.buildingLevel = respondData.level; if (!isFree) { if (GameModule.userInfo.buildingLevel > 5) { this.showSkillBuidingAlert(respondData.gold10, respondData.gold100); } GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this._upGold); } this.initAward(this._awardCount); GameEvent.fire(GameNotificationKey.UpBuildingLevel); GameGlobal._gold10 = respondData.gold10; this.updateBuildingData(); }).catch(({code, msg}) => { GameGlobal.commonAlert.showCommonErrorAlert(msg); console.log(code, msg); this._isAction = false; this.userSkillButton.interactable = true; }); }, ///点击按钮之后 技能相关的响应 skillAction() { /// 那就是解锁啦 let skillId = this.skillData.skillId; /// 说明是购买技能 if (this.skillData.isHave == 0 && skillId > 3) { /// 购买成功 this.buySkill(skillId).then(() => { this.updateGloabData(); this.buyFixSkill(); GameEvent.fire('skill_update'); this.userSkillButton.interactable = true; console.log("技能购买成功"); }).catch(({code, msg}) => { console.log(code, msg); GameGlobal.commonAlert.showCommonErrorAlert(msg); this.userSkillButton.interactable = true; }); /// 升级技能 } else { let levelInfo = GameGlobal.BuildingManager.getSkillLevelInfo(this.skillData.skillId, this.skillData.skillLevel); levelInfo.name = this._skillInfo.name; let nextInfo = GameGlobal.BuildingManager.getSkillLevelInfo(this.skillData.skillId, this.skillData.skillLevel + 1); nextInfo.name = this._skillInfo.name; AlertManager.showSkillBuyAlert(levelInfo, nextInfo, this); this.userSkillButton.interactable = true; } }, //// 购买永久技能 buyFixSkill() { this.skillData.skillLevel = 1; this.skillData.isHave = 1; let skillId = this.skillData.skillId; /// 如果升级永久技能 let skillLevelInfo = GameGlobal.BuildingManager.getSkillLevelInfo(skillId, this.skillData.skillLevel); GameEvent.fire(GameNotificationKey.UpdateFixationSkill, skillLevelInfo); if (skillId == 5) { GameEvent.fire("skill_five_use"); } else if (skillId == 6) { GameGlobal.rcdSkillLevel = 1; GameEvent.fire("skill_six_use"); } let iconPath = './textures/skill/4000' + skillId; AlertManager.showCommonAlert(iconPath, this._skillInfo.desc, this._skillInfo.name); }, /// 显示购买技能 showBuySkill() { // let levelInfo = GameGlobal.BuildingManager.getSkillLevelInfo(this.skillData.skillId, 1); /// 使用金币升级 let diamondCount = this._skillInfo.bonus[10002]; /// 用的砖石 if (diamondCount != undefined) { this.diamondCount = diamondCount; this.coinCount = {'n': 0, 'e': 0}; this.building = 0; this.skillUseTitleRichText.string = `购买
${diamondCount}钻石`; } }, /// 每次升级需要弹窗 showSkillBuidingAlert (gold10, gold100) { this.unschedule(this.hideBuildingAlert, this); this.scheduleOnce(this.hideBuildingAlert, 2); this.alertNode.getComponent('SkillBuildingAlert').updateData(gold10, gold100, this); this.alertNode.active = true; }, hideBuildingAlert () { this.alertNode.active = false; }, hiddenSkillBuidingAlert(gold10, gold100) { this.initAward(this._awardCount); this.updateBuildingData(); this.showSkillBuidingAlert(gold10, gold100); }, /// 解锁某一个技能 unlockedSkill() { let skillId = this.skillData.skillId; this.skillData.isHave = 1; this.skillData.skillLevel = 1; this.updateData() if (skillId <= 3) { GameEvent.fire("TopSkill_unLocked", skillId); } console.log("解锁技能成功"); }, //// 里程碑奖励action awardBtnAction() { this.awardButton.interactable = false; GameModule.audioMng.playClickButton(); this.report().then((respondData) => { this.awardButton.interactable = true; this._awardCount = respondData.awardCount; let needAwardCount = parseInt(GameModule.userInfo.buildingLevel / 25); this.awardButton.node.active = needAwardCount > this._awardCount; GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': 2}); GameEvent.fire(GameNotificationKey.UpBuildingLevel); GameEvent.fire(GameNotificationKey.GameShowAdditionTips,"总部大楼",0); this.updateBuildingData(); let objct = {'cdTime': -6 * 1000, 'infoDesc': '点击提升2倍的金币产出', 'icon': 900001}; GameGlobal._fixInformations.push(objct); GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, false); console.log("领取里程碑奖励成功"); }).catch(({code, msg}) => { console.log(code, msg); this.awardButton.interactable = true; }); }, /// 判断本地是否是解锁状态 judgeIsCanUse() { if (this.coinCount.n > 0 && TapTapTool.compare(GameModule.userInfo.gold, this.coinCount)) { return true; } else if (this.diamondCount > 0 && GameModule.userInfo.diamond >= this.diamondCount) { return true; } else if (this.building > 0 && GameModule.userInfo.buildingLevel >= this.building) { return true; } else { return false; } }, ///更新全局数据 updateGloabData() { if (this.coinCount.n > 0) { GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this.coinCount); } else if (this.diamondCount > 0) { GameModule.userInfo.diamond -= this.diamondCount; } }, });