import { Component, EventHandler, find, instantiate, Label, macro, Node, Prefab, Vec3, _decorator } from "cc"; import { Http, HttpMethod } from "../core/net/Http"; import { ResourcesUtils } from "../core/resourceManager/ResourcesUtils"; import { BitmapFont } from "../core/ui/BitmapFont"; import { WindowManager } from "../core/ui/window/WindowManager"; import { WindowOpenMode } from "../core/ui/window/WindowOpenMode"; import { ConfigData } from "../Data/ConfigData"; import { g } from "../Data/g"; import { FsUtils } from "../FsUtils/FsUtils"; import { createType } from "./RoleController"; import { SoundSystem } from "./SoundSystem"; const { ccclass, property } = _decorator; @ccclass('MainUIController') export class MainUIController extends Component { @property({ type: BitmapFont, tooltip: "每日分红金额" }) bonusDayBitmapFont: BitmapFont; @property({ type: Label, tooltip: "金币文本" }) moneyLabel: Label; @property({ type: Label, tooltip: "神将每秒产出文本" }) gelenalOutPutLabel: Label; @property({ type: Label, tooltip: "神石文本" }) diamondLabel: Label; @property({ type: Label, tooltip: "神石兑换金额文本" }) cashLabel: Label; @property({ type: Node, tooltip: "出售按钮" }) sellButton: Node; @property({ type: Node, tooltip: "购买按钮" }) buyButton: Node; @property({ type: Label, tooltip: "购买按钮等级文本" }) buyLvLabel: Label; @property({ type: BitmapFont, tooltip: "购买按钮价格文本" }) buyPriceBitmapFont: BitmapFont; @property({ type: Label, tooltip: "神台开放等级文本" }) shenTaiLvLabel: Label; @property({ type: Label, tooltip: "切磋开放等级文本" }) battleLvLabel: Label; @property({ type: Label, tooltip: "等级红包领取提示说明" }) lvRedPacketMemoLabel: Label; @property({ type: Node, tooltip: "神台红点节点" }) shenTaiRedPointNode: Node; @property({ type: Node, tooltip: "切磋红点节点" }) qieCuoRedPointNode: Node; @property({ type: Node, tooltip: "抢红包红点节点" }) qianHongBaoRedPointNode: Node; @property({ type: EventHandler, tooltip: "购买成功后" }) buyHander: EventHandler; @property({ type: EventHandler, tooltip: "被挑战失败后" }) attackLossHander: EventHandler; @property({ type: Http, tooltip: "Http服务" }) http: Http; @property({ tooltip: "等级红包文字提示", type: Label }) public lvRedRedPacket: Label; @property({ tooltip: "等级红包", type: Node }) public lvRedRedPacketBtn: Node; @property({ tooltip: "互推按钮节点", type: Node }) public eachBtnNode: Node; private lvRedPacket = g.gameData.lvRedPacket;//等级红包当前显示已经领取等级 private money = -1; private diamond = -1; private bonus = -1; private userLv = -1; private addMoney = -1; private buyGeneralID = -1; private buyGeneralPrice = -1; /**金币同眇间隔 */ private updateTime: number = 10000; /**下次金币同步时间戳 */ private nextUpdateMoneyTime: number = 0; /**金币增长时间戳 */ private nextAddMoneyTime: number = 0; private generalConfig: any; private systemConfig: any; start() { this.systemConfig = ConfigData.configMap.get("systemConfig"); this.generalConfig = ConfigData.configMap.get("generalBase"); this.eachBtnNode.active = g.gameData.openRecommend; // macro.ENABLE_MULTI_TOUCH = false; SoundSystem.play(0); if (this.lvRedPacket >= 51) { this.lvRedRedPacketBtn.active = false; } else { this.lvRedRedPacket.string = "达到" + (this.lvRedPacket + 1) + "级可领取"; } if (g.userData.lv < 2) { this.addNovice(); } else { if (g.gameData.differTime) { WindowManager.open("Prefabs/Windows/视频金币界面", WindowOpenMode.CloseAndCover, [1, g.gameData.totalOutput, 3, g.gameData.differTime]); } } this.getGameBonus(); this.setUpdateTime(); this.getRedPackets(); this.getBeAttackDatas(); this.getFission(); this.getEachData(); } private async addNovice() { let prefab = await ResourcesUtils.load("Prefabs/新手引导", Prefab, this.node); let novice = instantiate(prefab); novice.setParent(find("Canvas/UI")); } /**更新下次同步时间 */ private setUpdateTime() { let now = Date.now(); this.nextUpdateMoneyTime = now + this.updateTime; this.nextAddMoneyTime = now + 1000; } async update() { if (this.lvRedPacket != g.gameData.lvRedPacket) { this.lvRedPacket = g.gameData.lvRedPacket; if (this.lvRedPacket >= 51) { this.lvRedRedPacketBtn.active = false; } else { this.lvRedRedPacket.string = "达到" + (this.lvRedPacket + 1) + "级可领取"; } } if (this.diamond != g.userData.diamond) { this.diamond = g.userData.diamond; this.diamondLabel.string = FsUtils.goldFormat(g.userData.diamond) this.cashLabel.string = '约' + FsUtils.toFixed(g.userData.diamond / this.systemConfig.moneyRate, 2) + '元'; } if (this.money != g.userData.money) { this.money = g.userData.money; this.moneyLabel.string = FsUtils.moneyFormat(g.userData.money); } if (this.buyGeneralID != g.gameData.buyLv) { this.buyGeneralID = g.gameData.buyLv; this.buyLvLabel.string = `Lv.${this.generalConfig[this.buyGeneralID].lv} ${this.generalConfig[this.buyGeneralID].name} ` } if (this.buyGeneralPrice != g.gameData.buyPrice) { this.buyGeneralPrice = g.gameData.buyPrice; this.buyPriceBitmapFont.string = FsUtils.moneyFormat(this.buyGeneralPrice); } if (this.userLv != g.userData.lv) { this.userLv = g.userData.lv; if (this.userLv < this.systemConfig.openBattleLv) { this.battleLvLabel.string = this.systemConfig.openBattleLv + '级开放'; } else { this.battleLvLabel.string = ''; } } if (g.gameData.needSyncMoney) { g.gameData.needSyncMoney = false; this.syncMoney(); } let now = Date.now(); if (now >= this.nextUpdateMoneyTime) { this.syncMoney(); } if (now >= this.nextAddMoneyTime) { let addMoneyCount: number = 0; let generals = g.gameData.getMyLocalData(g.userData.id).myLocalGenerals; for (let i = 0; i < generals.length; i++) { addMoneyCount += this.generalConfig[generals[i].generalLv].output; } g.userData.money += addMoneyCount; if (addMoneyCount != this.addMoney) { this.addMoney = addMoneyCount; this.gelenalOutPutLabel.string = FsUtils.moneyFormat(this.addMoney) + '/秒'; } this.nextAddMoneyTime = now + 1000; } //#region 红点更新 //神台红点 let tempBoolen = g.gameData.getAllRevenueAmount(); if (tempBoolen != this.shenTaiRedPointNode.active) { this.shenTaiRedPointNode.active = tempBoolen; } //远征红点 if (g.gameData.hasAvengeListUpdate != this.qieCuoRedPointNode.active) { this.qieCuoRedPointNode.active = g.gameData.hasAvengeListUpdate; } //抢红包红点 if (g.gameData.hasNewRedPacketUpdate) { if (!this.qianHongBaoRedPointNode.active) { this.qianHongBaoRedPointNode.active = true; } } else { this.initRedPacketRedPoint(); } //#endregion } private initRedPacketRedPoint() { let _time = Date.now() / 1000; let _bool = false; for (let i = 0; i < g.gameData.myRedPacketDatas.length; i++) { if (g.gameData.myRedPacketDatas[i].canOpenTime <= _time) { _bool = true; break; } } if (this.qianHongBaoRedPointNode.active != _bool) { this.qianHongBaoRedPointNode.active = _bool; } } /**同步用户金币 */ private async syncMoney() { this.setUpdateTime(); if (g.gameData.isPlayAddMoneyAnim || g.gameData.isPlayAddDiamondAnim) { return; } // let result = await this.http.send("/api/wealth/SyncMoney"); let result = await this.http.send("/api/wealth/SyncMoney", null, HttpMethod.GET, false); if (!result.code && result.data) { g.userData.money = result.data.newMoney; // g.userData.moneyPower10 = result.data.newMoneyPower10; // 红点数据比如:[1,0] (0被攻击,请求28号消息;1红包,请求13号消息;2贡献,请求24号消息) | for (let i = 0; i < result.data.redDotDatas.length; i++) {//数据更新 switch (result.data.redDotDatas[i]) { case 0://被别人攻击了显示感叹号 this.getBeAttackDatas(true); // g.gameData.hasBeAttackUpdate = true; break; case 1://有新红包数据显示感叹号 this.getRedPackets(); break; case 2://有新的贡献产生显示感叹号 g.gameData.updateMyFessiongDatas(result.data.fessionDatas); break; case 3://被好友挑战数据改变 g.battleData.friendBattleUpdate = true; break; } } } else { if (g.CodeMsg[result.code]) { WindowManager.showTips(g.CodeMsg[result.code]); } } } /** * @param data 出售角色 */ public async onRoleCanSell(...data) { this.sellButton.active = data.length == 1; this.buyButton.active = data.length == 0; if (data.length && data[0] == 2) { this.sellButton.setScale(new Vec3(1.2, 1.2, 1.2)); } else { this.sellButton.setScale(new Vec3(1, 1, 1)); } } private isBuying = false; /** * 购买角色 */ public async onBuyRole() { if (this.isBuying || g.gameData.isMoveing) { return; } g.gameData.isBuying = true; this.isBuying = true; if (g.gameData.getMyLocalData(g.userData.id).myLocalGenerals.length > 11) { // WindowManager.showTips('位置不足'); this.isBuying = false; g.gameData.isBuying = false; return; } if (g.gameData.buyPrice > g.userData.money) { // WindowManager.showTips("金币不足,看视频获得金币"); let _lv = g.userData.lv > 0 ? g.userData.lv : 1; await WindowManager.open("Prefabs/Windows/视频金币界面", WindowOpenMode.CloseAndCover, [2, ConfigData.configMap.get("flyBox").prizeList[_lv - 1].prize * 5]); this.isBuying = false; g.gameData.isBuying = false; return; } let result = await this.http.send("/api/general/GeneralBuy", { generalLv: this.buyGeneralID }); this.isBuying = false; if (!result.code) { g.gameData.buyLv = result.data.buyLv; g.gameData.buyPrice = result.data.buyPrice; g.userData.money = result.data.newMoney; // g.userData.moneyPower10 = result.data.newMoneyPower10; g.userData.money = g.userData.money < 0 ? 0 : g.userData.money; g.userData.lv = result.data.generalLv > g.userData.lv ? result.data.generalLv : g.userData.lv; this.buyHander.emit([{ lv: result.data.generalLv, index: -1, type: createType.Buy }]); } else { g.gameData.isBuying = false; if (result.code == 104) { this.syncMoney(); } else { WindowManager.showTips(g.CodeMsg[result.code]); } } } /**打开切磋窗口 */ public openBattleUIWindow() { if (g.userData.lv >= this.systemConfig.openBattleLv) { WindowManager.open("Prefabs/Battle/BattleUIWindow", WindowOpenMode.NotCloseAndAdd, { tabIndex: 0 }); } else { WindowManager.showTips(this.systemConfig.openBattleLv + '级开放') } } /**打开神台窗口 */ public openShentaiWindow() { if (g.userData.lv >= this.systemConfig.openShentaiLv) { this.getFission(); WindowManager.open("Prefabs/ShenTai/ShentaiWindow", WindowOpenMode.NotCloseAndAdd); } else { WindowManager.showTips(this.systemConfig.openShentaiLv + '级开放') } } /**打开转盘窗口 */ public openTurntableWindow() { if (g.userData.lv >= this.systemConfig.openTurntableLv) { WindowManager.open("Prefabs/Windows/Turntable", WindowOpenMode.CloseAndAdd); } else { WindowManager.showTips(this.systemConfig.openTurntableLv + '级开放') } } /** * 获取被攻击信息 */ private async getBeAttackDatas(isRedPoint: boolean = false) { let revengeResult = await this.http.send("/api/battle/GetRevenges"); if (!revengeResult.code) { g.gameData.myAvengeDatas = revengeResult.data.revengeDatas; g.gameData.myAvengeDatas.sort((a: any, b: any): number => { return a.createTime > b.createTime ? -1 : 1;//优先最近战斗 }); } else { WindowManager.showTips(g.CodeMsg[revengeResult.code]); } let result = await this.http.send("/api/redDot/GetBeAttackDatas"); if (!result.code) { g.gameData.beAttackDatas = result.data.beAttackDatas; if (isRedPoint) {//更新本地神将数据 let generals = []; for (let i = 0; i < g.gameData.beAttackDatas.length; i++) { if (g.gameData.beAttackDatas[i].beAttackLossType == 3) { generals.push(Number(g.gameData.beAttackDatas[i].beAttackLossNum)); } } if (generals.length > 0) { this.attackLossHander.emit([generals]); } } if (g.gameData.beAttackDatas.length) { WindowManager.open("Prefabs/Battle/BattleInfoWindow", WindowOpenMode.NotCloseAndAdd);//打开切磋消息 } } else { WindowManager.showTips(g.CodeMsg[result.code]); } } /** * 获取红包信息 */ private async getRedPackets() { let getRedPacketsResult = await this.http.send("/api/redPacket/GetRedPackets");//获取红包信息 if (!getRedPacketsResult.code) { g.gameData.myRedPacketDatas = getRedPacketsResult.data.redPacketList; g.gameData.hasNewRedPacketUpdate = true; } else { WindowManager.showTips(g.CodeMsg[getRedPacketsResult.code]); } } /** * 获取裂变信息 */ private async getFission() { let result = await this.http.send("/api/fission/GetFission"); if (!result.code) { g.gameData.myFissionData = result.data.fissionDatas; } else { WindowManager.showTips(g.CodeMsg[result.code]); } } /** * 获取分红信息 */ private async getGameBonus() { let result = await this.http.send("/api/bonus/getGameBonus"); if (!result.code) { g.gameData.gameBonus = result.data; let bonus = Math.floor(g.gameData.gameBonus.bonus / 100) / 100; let bonusStr = ''; if (bonus < 10) { bonusStr = 'ss' + bonus; } else if (bonus < 100) { bonusStr = 's' + bonus; } else { bonusStr = '' + bonus; } this.bonusDayBitmapFont.string = bonusStr; } } private async getEachData() { let result = await this.http.send("/api/recommend/GetRecommendList"); if (result.code == 0) { // platform.umUp("eachPush");//互推埋点 for (let i = 0; i < result.data.plug.length; i++) { let d = result.data.plug[i]; g.downLoadData.recommendData.push({ id: d.placement_id, icon: d.icon, title: d.title, dec: d.introduction, url: d.downloadUrl, nebulaAppId: d.nebulaAppId, packageName: d.packageName }); } } } }