/** @format */ import {Data, GameControl, Mgr} from '../GameControl' import {BaseUI} from './BaseUI' import {ACTIVITY_TYPE, APP_TYPE, DIS_ID, EVENT, GOODS, GOODS_TYPE, LANGUAGE_TYPE, LOCAL} from '../enums/Enum' import {Log} from '../utils/LogUtils' import {ccUtils} from '../utils/ccUtils' import {UI} from '../enums/UI' import {msgCmd} from '../proto/msg_cmd' import { activityGetConfListRsp, activityTimeListNty, adventureDataRsp, boxGetAwardRsp, altarBattleRsp, altarDataRsp, cityDataSend, debrisGetDataRsp, embattleBattleRsp, embattleGetDataRsp, equipGetDataRsp, equipOnekeyUpgradeStarRsp, equipResetRsp, equipUpgradeRsp, equipUpgradeStarRsp, firstRechargeAwardRsp, firstRechargeDataRsp, firstRechargeNty, heroChangeNty, heroGetDataRsp, heroOnekeyUpgradeStarRsp, heroResetRsp, heroUpgradeRsp, heroUpgradeStarRsp, heroWearEquipRsp, login, loginRsp, manualGetDataRsp, payCurrency, payDebrisNty, rewardInfo, skillCardGetDataRsp, skillCardUpgradeRsp, skillCardUpgradeStarRsp, talentGetDataRsp, user, popGiftNty, popGiftChangeNty, popGiftDataRsp, relicDataNty, simpleMail, sendMail, createOrderNty, onlineAwardNty, cityDataRsp, } from '../proto/game' import {action} from 'mobx' import {activityTimeInfo, card, equip, hero, idNum} from '../proto/typedef' import {TalentConfig} from '../config/TalentConfig' import {GoodsConfig} from '../config/GoodsConfig' import {IRewardNty} from '../interface/UIInterface' import {ICard, IEquip, IRole, ICardDebris, ITimeGift} from '../interface/GlobalInterface' import {RanksLevelConfig} from '../config/RanksLevelConfig' import UserGuide from '../userguide/UserGuide' import {HttpUrl} from '../data/HttpUrl' /** @format */ const {ccclass, property} = cc._decorator @ccclass export default class MainScene extends BaseUI { async onLoad() { //屏蔽多点触摸 (屏蔽后setSwallowTouches无法吞噬) //cc.macro.ENABLE_MULTI_TOUCH = false //关闭FPS cc.debug.setDisplayStats(false) //开启动态合图 cc.macro.CLEANUP_IMAGE_CACHE = false cc.dynamicAtlasManager.enabled = true cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE) await this.startGame() } async startGame() { HttpUrl.init() let isInit = await GameControl.init() if (!isInit) { Log.error('游戏初始化失败') return } Mgr.platform.hideSplash() let assets: cc.JsonAsset = await Mgr.res.loadSync(cc.resources, 'version', cc.JsonAsset) let appType = assets?.json.appType Mgr.event.add(EVENT.webSocketOpen, this, () => { Data.main.webSocketCon = true Mgr.ui.hideLoading() //Mgr.ui.tip(LANGUAGE_TYPE.serverConnect) //游戏内断线重连 if (Data.user.account && !Mgr.ui.getUI(UI.LoginUI)) { let loginReq = login.create() loginReq.sid = 1 loginReq.account = Data.user.account Mgr.net.send(msgCmd.cmd_login, loginReq) } }) Mgr.event.add(EVENT.webSocketClose, this, () => { Data.main.webSocketCon = false if (!Mgr.ui.getUI(UI.LoadingUI)) Mgr.ui.showLoading(LANGUAGE_TYPE.connectServer, false, false) //Mgr.ui.tip(LANGUAGE_TYPE.serverCut) }) this.registerNetEvent() Data.main.texBundle = await ccUtils.getBundleAsync('texture') //加载所有角色spine Data.main.texBundle.loadDir('Public/role/spine', sp.SkeletonData, (error, assets: sp.SkeletonData[]) => { if (error) { Log.error(error) } else { let guideSp for (let i = 0; i < assets.length; i++) { Data.main.roleSpineMap.set(assets[i].name, assets[i]) if (assets[i].name == 'yuelunche') guideSp = assets[i] assets[i]['_ref'] = Infinity } //初始新手引导 let userGuideNode = cc.find('userguide', this.node) userGuideNode.zIndex = 9999 Mgr.global.userGuideCom = userGuideNode.getComponent(UserGuide) Mgr.global.userGuideCom.init(guideSp, Data.main.texBundle) Mgr.ui.show(UI.LoginUI) } }) } @action registerNetEvent() { Mgr.net.addTop(msgCmd.cmd_login_rsp, this, (data: loginRsp) => { if (Data.main.payData.length > 0) { for (let payObj of Data.main.payData) { Mgr.net.send(msgCmd.cmd_recharge_result, payObj) } Data.main.payData.length = 0 } }) Mgr.net.addTop(msgCmd.cmd_user, this, (data: user) => { Data.user.uid = data.uid Data.user.nickname = data.nickname Data.user.level = data.level Data.user.exp = data.exp Data.user.avatar = data.avatar Data.user.renameTime = data.renameTime Data.user.createTime = data.createtime for (let idNum of data.currency) { Mgr.goods.setGoodsNum(idNum.id, idNum.num) this.addReadyBox(idNum) } Data.main.loginTime = data.svrtime Data.main.serverTime = data.svrtime //开始全局时间计时器 Data.main.globalTime = 0 Data.main.secondTime = 0 Data.main.minutesTime = 0 Mgr.time.globalTimer() console.log(Date.Format('当前服务器时间:MM/dd/yyyy hh:mm:ss', data.svrtime)) Mgr.storage.init(data.uid, '1') Mgr.time.initLoginDate(data.svrtime) }) Mgr.net.addTop(msgCmd.cmd_pay_currency, this, (data: payCurrency) => { for (let idNum of data.now) { Mgr.event.trigger(EVENT.goodsChangeSync, idNum.id, idNum.num - Mgr.goods.getGoodsNum(idNum.id)) Mgr.goods.setGoodsNum(idNum.id, idNum.num) } }) Mgr.net.addTop(msgCmd.cmd_reward_info, this, (data: rewardInfo) => { let changeArr: idNum[] = [] let syncArr: idNum[] = [] let iRoles: IRole[] = [] let iCards: ICard[] = [] let iEquips: IEquip[] = [] let debrisArr: idNum[] = [] for (let idNum of data.added.currency) { let dur = idNum.num - Mgr.goods.getGoodsNum(idNum.id) if (dur > 0) { changeArr.push({id: idNum.id, num: dur}) } else { syncArr.push({id: idNum.id, num: dur}) } Mgr.goods.setGoodsNum(idNum.id, idNum.num) //处理队伍经验 if (idNum.id == GOODS.exp) { Data.user.exp = idNum.num let cfg = RanksLevelConfig[Data.user.level] if (cfg && idNum.num >= cfg.exp) { Data.user.exp = cfg.exp } } this.addReadyBox(idNum, false) } for (let hero of data.added.heroes) { let role = Mgr.global.buildIRole(hero) Data.user.roles.push(role) iRoles.push(role) } if (data.added.heroes.length > 0) { Mgr.goods.sortRole() Mgr.goods.reactRolesChange() } for (let card of data.added.skillCard) { let iCard = Mgr.global.buildICard(card) Data.user.cards.push(iCard) iCards.push(iCard) } if (data.added.skillCard.length > 0) { Mgr.goods.sortCard() Mgr.goods.reactCardsChange() } for (let equip of data.added.equip) { let iEquip = Mgr.global.buildIEquip(equip) Data.user.equips.push(iEquip) iEquips.push(iEquip) } if (data.added.equip.length > 0) { Mgr.goods.sortEquip() Mgr.goods.reactEquipsChange() } for (let debris of data.added.debris) { let has = Data.user.cardDebris.find(item => item.id == debris.id) let changeDur = debris.num - (has ? has.num : 0) let cardDebris = Mgr.global.buildICardDebris(debris, has) if (!has) Data.user.cardDebris.push(cardDebris) debrisArr.push({id: debris.id, num: changeDur}) } let rewardNty: IRewardNty = { changeArr, iRoles, iCards, iEquips, debrisArr, } syncArr.forEach(v => Mgr.event.trigger(EVENT.goodsChangeSync, v.id, v.num)) //recharge_money 是需要花钱买的 shop_buy是用钻石买的或者是看广告获得的 服务端区分了用法 if (data.flags == 'recharge_money' || data.flags == 'shop_buy') { Mgr.ui.showReward(rewardNty) } else { Data.main.rewardNtyMap.set('cmd_' + data.flags + '_rsp', rewardNty) } }) //-------------------------------建筑数据----------------------------------------- Mgr.net.addTop(msgCmd.cmd_city_data_rsp, this, (data: cityDataRsp) => { Data.main.buildData = data.list //城堡技能 Data.user.castleSkills = [] Data.user.useCastleSkillID = data.skillid for (let i = 0; i < data.skillList.length; i++) { let iCastleSkill = Mgr.global.buildICastleSkill(data.skillList[i]) Data.user.castleSkills.push(iCastleSkill) } }) //-------------------------------装备数据----------------------------------------- //先获取装备背包,再获取英雄背包 Mgr.net.addTop(msgCmd.cmd_equip_get_data_rsp, this, (data: equipGetDataRsp) => { Data.user.equips.length = 0 data.list.forEach(equip => { Data.user.equips.push(Mgr.global.buildIEquip(equip)) }) Mgr.goods.sortEquip() Mgr.goods.reactEquipsChange() }) let updateEquip = (equip: equip) => { Data.user.equips.forEach(v => { if (equip.sid == v.equip.sid) { Mgr.global.buildIEquip(equip, v) } }) Mgr.goods.reactEquipsChange() } Mgr.net.addTop(msgCmd.cmd_equip_upgrade_rsp, this, (data: equipUpgradeRsp) => { updateEquip(data.data) }) Mgr.net.addTop(msgCmd.cmd_equip_upgrade_star_rsp, this, (data: equipUpgradeStarRsp) => { for (let i = Data.user.equips.length - 1; i >= 0; i--) { let sid = Data.user.equips[i].equip.sid if (data.same.includes(sid) || data.other.includes(sid)) { Data.user.equips.splice(i, 1) } if (sid == data.data.sid) { Mgr.global.buildIEquip(data.data, Data.user.equips[i]) } } Mgr.goods.reactEquipsChange() }) Mgr.net.addTop(msgCmd.cmd_equip_onekey_upgrade_star_rsp, this, (data: equipOnekeyUpgradeStarRsp) => { for (let i = Data.user.equips.length - 1; i >= 0; i--) { if (data.delList.includes(Data.user.equips[i].equip.sid)) { Data.user.equips.splice(i, 1) } } for (let i = Data.user.equips.length - 1; i >= 0; i--) { let find = data.changeList.find(value => value.sid == Data.user.equips[i].equip.sid) if (find) { Mgr.global.buildIEquip(find, Data.user.equips[i]) } } Mgr.goods.reactEquipsChange() }) Mgr.net.addTop(msgCmd.cmd_equip_reset_rsp, this, (data: equipResetRsp) => { updateEquip(data.data) }) Mgr.net.addTop(msgCmd.cmd_hero_wear_equip_rsp, this, (data: heroWearEquipRsp) => { let equips = new Array(Data.main.equipMaxNum).fill(null) Data.user.equips.find(value => { if (data.list.includes(value.equip.sid)) { value.equip.hero = data.data.sid equips[value.cfg.type - 1] = value } else if (value.equip.hero == data.data.sid) { value.equip.hero = '' } }) Data.user.roles.find(value => { if (value.hero.sid == data.data.sid) { value.equips.length = 0 value.equips.push(...equips) } }) Mgr.goods.reactEquipsChange() }) //-------------------------------英雄数据----------------------------------------- Mgr.net.addTop(msgCmd.cmd_hero_get_data_rsp, this, (data: heroGetDataRsp) => { Data.user.roles.length = 0 data.list.forEach(hero => { Data.user.roles.push(Mgr.global.buildIRole(hero)) }) Mgr.goods.sortRole() Mgr.goods.reactRolesChange() }) let updateRole = (data: hero) => { Data.user.roles.forEach(v => { if (data.sid == v.hero.sid) { Mgr.global.buildIRole(data, v) } }) Mgr.goods.reactRolesChange() } Mgr.net.addTop(msgCmd.cmd_hero_upgrade_rsp, this, (data: heroUpgradeRsp) => { updateRole(data.data) }) Mgr.net.addTop(msgCmd.cmd_hero_upgrade_star_rsp, this, (data: heroUpgradeStarRsp) => { for (let i = Data.user.roles.length - 1; i >= 0; i--) { let sid = Data.user.roles[i].hero.sid if (data.same.includes(sid) || data.other.includes(sid)) { Data.user.roles.splice(i, 1) } if (sid == data.data.sid) { Mgr.global.buildIRole(data.data, Data.user.roles[i]) } } Mgr.goods.reactRolesChange() }) Mgr.net.addTop(msgCmd.cmd_hero_onekey_upgrade_star_rsp, this, (data: heroOnekeyUpgradeStarRsp) => { for (let i = Data.user.roles.length - 1; i >= 0; i--) { if (data.delList.includes(Data.user.roles[i].hero.sid)) { Data.user.roles.splice(i, 1) } } for (let i = Data.user.roles.length - 1; i >= 0; i--) { let findHero = data.changeList.find(value => value.sid == Data.user.roles[i].hero.sid) if (findHero) { Mgr.global.buildIRole(findHero, Data.user.roles[i]) } } Mgr.goods.reactRolesChange() }) Mgr.net.addTop(msgCmd.cmd_hero_reset_rsp, this, (data: heroResetRsp) => { updateRole(data.data) }) Mgr.net.addTop(msgCmd.cmd_hero_change_nty, this, (data: heroChangeNty) => { for (let i = 0; i < data.list.length; i++) { updateRole(data.list[i]) } }) //-------------------------------卡牌数据----------------------------------------- Mgr.net.addTop(msgCmd.cmd_skill_card_get_data_rsp, this, (data: skillCardGetDataRsp) => { Data.user.cards.length = 0 data.list.forEach(card => { Data.user.cards.push(Mgr.global.buildICard(card)) }) Mgr.goods.sortCard() Mgr.goods.reactCardsChange() }) let updateCard = (card: card) => { Data.user.cards.find(value => { if (value.card.sid == card.sid) { Mgr.global.buildICard(card, value) } }) Mgr.goods.reactCardsChange() } Mgr.net.addTop(msgCmd.cmd_skill_card_upgrade_rsp, this, (data: skillCardUpgradeRsp) => { updateCard(data.data) }) Mgr.net.addTop(msgCmd.cmd_skill_card_upgrade_star_rsp, this, (data: skillCardUpgradeStarRsp) => { updateCard(data.data) }) //-------------------------------上阵数据----------------------------------------- let updateTeam = (data: embattleBattleRsp | embattleGetDataRsp) => { Data.user.teamRole.length = 0 Data.user.teamCard.length = 0 data.list.forEach(item => { if (item.sid) { Data.user.teamRole.push(Data.user.roles.find(hero => hero.hero.sid == item.sid)) } else { Data.user.teamRole.push(null) } }) data.cardList.forEach(item => { if (item.sid) { Data.user.teamCard.push(Data.user.cards.find(card => card.card.sid == item.sid)) } else { Data.user.teamCard.push(null) } }) Mgr.goods.reactRolesChange() Mgr.goods.reactCardsChange() } Mgr.net.addTop(msgCmd.cmd_embattle_get_data_rsp, this, (data: embattleBattleRsp) => { updateTeam(data) }) Mgr.net.addTop(msgCmd.cmd_embattle_battle_rsp, this, (data: embattleBattleRsp) => { updateTeam(data) }) //-------------------------------碎片数据----------------------------------------- Mgr.net.addTop(msgCmd.cmd_debris_get_data_rsp, this, (data: debrisGetDataRsp) => { Data.user.cardDebris.length = 0 data.list.forEach(cardDebris => { Data.user.cardDebris.push(Mgr.global.buildICardDebris(cardDebris)) }) }) Mgr.net.addTop(msgCmd.cmd_pay_debris_nty, this, (data: payDebrisNty) => { for (let i = Data.user.cardDebris.length - 1; i >= 0; i--) { let find = data.list.find(idNum => idNum.id == Data.user.cardDebris[i].id) if (find) { if (find.num == 0) { Data.user.cardDebris.splice(i, 1) } else { Data.user.cardDebris[i].num = find.num } } } }) //-------------------------------天赋----------------------------------------- Mgr.net.addTop(msgCmd.cmd_talent_get_data_rsp, this, (data: talentGetDataRsp) => { Data.user.talents.length = 0 let keys = Object.keys(TalentConfig) for (let i = 0; i < keys.length; i++) { let ID = TalentConfig[keys[i]].ID let type = TalentConfig[keys[i]].talentType if (data.id1 > 0 && ID <= data.id1 && type == 1) { Data.user.talents.push(ID) } if (data.id2 > 0 && ID <= data.id2 && type == 2) { Data.user.talents.push(ID) } } Mgr.global.initTalentAdd() }) //-------------------------------冒险关卡----------------------------------------- Mgr.net.addTop(msgCmd.cmd_adventure_data_rsp, this, (data: adventureDataRsp) => { Data.user.adventureId = data.id1 Data.user.eliteId = data.id2 }) //-------------------------------活动列表----------------------------------------- Mgr.net.addTop(msgCmd.cmd_activity_time_list_nty, this, (data: activityTimeListNty) => { data.list.forEach(activity => { Data.main.activityList.set(activity.type, activity) }) }) //-------------------------------开宝箱----------------------------------------- Mgr.net.add(msgCmd.cmd_box_get_award_rsp, this, (data: boxGetAwardRsp, rewardNty: IRewardNty) => { //判断宝箱类型,防止OpenBoxUI也会执行showReward if (data.type == GOODS_TYPE.randomBox) Mgr.ui.showReward(rewardNty) }) //-------------------------------邮箱----------------------------------------- Mgr.net.addTop(msgCmd.cmd_simple_mail, this, (data: simpleMail) => { Data.main.emailRed = data.needread + data.noread > 0 }) Mgr.net.addTop(msgCmd.cmd_send_mail, this, (data: sendMail) => { Data.main.emailRed = true }) //-------------------------------创建订单----------------------------------------- Mgr.net.addTop(msgCmd.cmd_create_order_nty, this, (data: createOrderNty) => { Mgr.ui.showLoading('', false) Mgr.platform.buyItem(data.cfid.toString(), data.id.toString()) }) } addReadyBox(idNum: idNum, isOnlySelect: boolean = true) { let isBox = GoodsConfig[idNum.id].type == GOODS_TYPE.selectBox || GoodsConfig[idNum.id].type == GOODS_TYPE.randomBox if (isBox) { for (let i = 0; i < idNum.num; i++) { Data.user.readyOpenBox.push({id: idNum.id, num: 1}) } } } }