MainScene.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /** @format */
  2. import {Data, GameControl, Mgr} from '../GameControl'
  3. import {BaseUI} from './BaseUI'
  4. import {ACTIVITY_TYPE, APP_TYPE, DIS_ID, EVENT, GOODS, GOODS_TYPE, LANGUAGE_TYPE, LOCAL} from '../enums/Enum'
  5. import {Log} from '../utils/LogUtils'
  6. import {ccUtils} from '../utils/ccUtils'
  7. import {UI} from '../enums/UI'
  8. import {msgCmd} from '../proto/msg_cmd'
  9. import {
  10. activityGetConfListRsp,
  11. activityTimeListNty,
  12. adventureDataRsp,
  13. boxGetAwardRsp,
  14. altarBattleRsp,
  15. altarDataRsp,
  16. cityDataSend,
  17. debrisGetDataRsp,
  18. embattleBattleRsp,
  19. embattleGetDataRsp,
  20. equipGetDataRsp,
  21. equipOnekeyUpgradeStarRsp,
  22. equipResetRsp,
  23. equipUpgradeRsp,
  24. equipUpgradeStarRsp,
  25. firstRechargeAwardRsp,
  26. firstRechargeDataRsp,
  27. firstRechargeNty,
  28. heroChangeNty,
  29. heroGetDataRsp,
  30. heroOnekeyUpgradeStarRsp,
  31. heroResetRsp,
  32. heroUpgradeRsp,
  33. heroUpgradeStarRsp,
  34. heroWearEquipRsp,
  35. login,
  36. loginRsp,
  37. manualGetDataRsp,
  38. payCurrency,
  39. payDebrisNty,
  40. rewardInfo,
  41. skillCardGetDataRsp,
  42. skillCardUpgradeRsp,
  43. skillCardUpgradeStarRsp,
  44. talentGetDataRsp,
  45. user,
  46. popGiftNty,
  47. popGiftChangeNty,
  48. popGiftDataRsp,
  49. relicDataNty,
  50. simpleMail,
  51. sendMail,
  52. createOrderNty,
  53. onlineAwardNty,
  54. cityDataRsp,
  55. } from '../proto/game'
  56. import {action} from 'mobx'
  57. import {activityTimeInfo, card, equip, hero, idNum} from '../proto/typedef'
  58. import {TalentConfig} from '../config/TalentConfig'
  59. import {GoodsConfig} from '../config/GoodsConfig'
  60. import {IRewardNty} from '../interface/UIInterface'
  61. import {ICard, IEquip, IRole, ICardDebris, ITimeGift} from '../interface/GlobalInterface'
  62. import {RanksLevelConfig} from '../config/RanksLevelConfig'
  63. import UserGuide from '../userguide/UserGuide'
  64. import {HttpUrl} from '../data/HttpUrl'
  65. /** @format */
  66. const {ccclass, property} = cc._decorator
  67. @ccclass
  68. export default class MainScene extends BaseUI {
  69. async onLoad() {
  70. //屏蔽多点触摸 (屏蔽后setSwallowTouches无法吞噬)
  71. //cc.macro.ENABLE_MULTI_TOUCH = false
  72. //关闭FPS
  73. cc.debug.setDisplayStats(false)
  74. //开启动态合图
  75. cc.macro.CLEANUP_IMAGE_CACHE = false
  76. cc.dynamicAtlasManager.enabled = true
  77. cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE)
  78. await this.startGame()
  79. }
  80. async startGame() {
  81. HttpUrl.init()
  82. let isInit = await GameControl.init()
  83. if (!isInit) {
  84. Log.error('游戏初始化失败')
  85. return
  86. }
  87. Mgr.platform.hideSplash()
  88. let assets: cc.JsonAsset = await Mgr.res.loadSync(cc.resources, 'version', cc.JsonAsset)
  89. let appType = assets?.json.appType
  90. Mgr.event.add(EVENT.webSocketOpen, this, () => {
  91. Data.main.webSocketCon = true
  92. Mgr.ui.hideLoading()
  93. //Mgr.ui.tip(LANGUAGE_TYPE.serverConnect)
  94. //游戏内断线重连
  95. if (Data.user.account && !Mgr.ui.getUI(UI.LoginUI)) {
  96. let loginReq = login.create()
  97. loginReq.sid = 1
  98. loginReq.account = Data.user.account
  99. Mgr.net.send(msgCmd.cmd_login, loginReq)
  100. }
  101. })
  102. Mgr.event.add(EVENT.webSocketClose, this, () => {
  103. Data.main.webSocketCon = false
  104. if (!Mgr.ui.getUI(UI.LoadingUI)) Mgr.ui.showLoading(LANGUAGE_TYPE.connectServer, false, false)
  105. //Mgr.ui.tip(LANGUAGE_TYPE.serverCut)
  106. })
  107. this.registerNetEvent()
  108. Data.main.texBundle = await ccUtils.getBundleAsync('texture')
  109. //加载所有角色spine
  110. Data.main.texBundle.loadDir('Public/role/spine', sp.SkeletonData, (error, assets: sp.SkeletonData[]) => {
  111. if (error) {
  112. Log.error(error)
  113. } else {
  114. let guideSp
  115. for (let i = 0; i < assets.length; i++) {
  116. Data.main.roleSpineMap.set(assets[i].name, assets[i])
  117. if (assets[i].name == 'yuelunche') guideSp = assets[i]
  118. assets[i]['_ref'] = Infinity
  119. }
  120. //初始新手引导
  121. let userGuideNode = cc.find('userguide', this.node)
  122. userGuideNode.zIndex = 9999
  123. Mgr.global.userGuideCom = userGuideNode.getComponent(UserGuide)
  124. Mgr.global.userGuideCom.init(guideSp, Data.main.texBundle)
  125. Mgr.ui.show(UI.LoginUI)
  126. }
  127. })
  128. }
  129. @action
  130. registerNetEvent() {
  131. Mgr.net.addTop(msgCmd.cmd_login_rsp, this, (data: loginRsp) => {
  132. if (Data.main.payData.length > 0) {
  133. for (let payObj of Data.main.payData) {
  134. Mgr.net.send(msgCmd.cmd_recharge_result, payObj)
  135. }
  136. Data.main.payData.length = 0
  137. }
  138. })
  139. Mgr.net.addTop(msgCmd.cmd_user, this, (data: user) => {
  140. Data.user.uid = data.uid
  141. Data.user.nickname = data.nickname
  142. Data.user.level = data.level
  143. Data.user.exp = data.exp
  144. Data.user.avatar = data.avatar
  145. Data.user.renameTime = data.renameTime
  146. Data.user.createTime = data.createtime
  147. for (let idNum of data.currency) {
  148. Mgr.goods.setGoodsNum(idNum.id, idNum.num)
  149. this.addReadyBox(idNum)
  150. }
  151. Data.main.loginTime = data.svrtime
  152. Data.main.serverTime = data.svrtime
  153. //开始全局时间计时器
  154. Data.main.globalTime = 0
  155. Data.main.secondTime = 0
  156. Data.main.minutesTime = 0
  157. Mgr.time.globalTimer()
  158. console.log(Date.Format('当前服务器时间:MM/dd/yyyy hh:mm:ss', data.svrtime))
  159. Mgr.storage.init(data.uid, '1')
  160. Mgr.time.initLoginDate(data.svrtime)
  161. })
  162. Mgr.net.addTop(msgCmd.cmd_pay_currency, this, (data: payCurrency) => {
  163. for (let idNum of data.now) {
  164. Mgr.event.trigger(EVENT.goodsChangeSync, idNum.id, idNum.num - Mgr.goods.getGoodsNum(idNum.id))
  165. Mgr.goods.setGoodsNum(idNum.id, idNum.num)
  166. }
  167. })
  168. Mgr.net.addTop(msgCmd.cmd_reward_info, this, (data: rewardInfo) => {
  169. let changeArr: idNum[] = []
  170. let syncArr: idNum[] = []
  171. let iRoles: IRole[] = []
  172. let iCards: ICard[] = []
  173. let iEquips: IEquip[] = []
  174. let debrisArr: idNum[] = []
  175. for (let idNum of data.added.currency) {
  176. let dur = idNum.num - Mgr.goods.getGoodsNum(idNum.id)
  177. if (dur > 0) {
  178. changeArr.push({id: idNum.id, num: dur})
  179. } else {
  180. syncArr.push({id: idNum.id, num: dur})
  181. }
  182. Mgr.goods.setGoodsNum(idNum.id, idNum.num)
  183. //处理队伍经验
  184. if (idNum.id == GOODS.exp) {
  185. Data.user.exp = idNum.num
  186. let cfg = RanksLevelConfig[Data.user.level]
  187. if (cfg && idNum.num >= cfg.exp) {
  188. Data.user.exp = cfg.exp
  189. }
  190. }
  191. this.addReadyBox(idNum, false)
  192. }
  193. for (let hero of data.added.heroes) {
  194. let role = Mgr.global.buildIRole(hero)
  195. Data.user.roles.push(role)
  196. iRoles.push(role)
  197. }
  198. if (data.added.heroes.length > 0) {
  199. Mgr.goods.sortRole()
  200. Mgr.goods.reactRolesChange()
  201. }
  202. for (let card of data.added.skillCard) {
  203. let iCard = Mgr.global.buildICard(card)
  204. Data.user.cards.push(iCard)
  205. iCards.push(iCard)
  206. }
  207. if (data.added.skillCard.length > 0) {
  208. Mgr.goods.sortCard()
  209. Mgr.goods.reactCardsChange()
  210. }
  211. for (let equip of data.added.equip) {
  212. let iEquip = Mgr.global.buildIEquip(equip)
  213. Data.user.equips.push(iEquip)
  214. iEquips.push(iEquip)
  215. }
  216. if (data.added.equip.length > 0) {
  217. Mgr.goods.sortEquip()
  218. Mgr.goods.reactEquipsChange()
  219. }
  220. for (let debris of data.added.debris) {
  221. let has = Data.user.cardDebris.find(item => item.id == debris.id)
  222. let changeDur = debris.num - (has ? has.num : 0)
  223. let cardDebris = Mgr.global.buildICardDebris(debris, has)
  224. if (!has) Data.user.cardDebris.push(cardDebris)
  225. debrisArr.push({id: debris.id, num: changeDur})
  226. }
  227. let rewardNty: IRewardNty = {
  228. changeArr,
  229. iRoles,
  230. iCards,
  231. iEquips,
  232. debrisArr,
  233. }
  234. syncArr.forEach(v => Mgr.event.trigger(EVENT.goodsChangeSync, v.id, v.num))
  235. //recharge_money 是需要花钱买的 shop_buy是用钻石买的或者是看广告获得的 服务端区分了用法
  236. if (data.flags == 'recharge_money' || data.flags == 'shop_buy') {
  237. Mgr.ui.showReward(rewardNty)
  238. } else {
  239. Data.main.rewardNtyMap.set('cmd_' + data.flags + '_rsp', rewardNty)
  240. }
  241. })
  242. //-------------------------------建筑数据-----------------------------------------
  243. Mgr.net.addTop(msgCmd.cmd_city_data_rsp, this, (data: cityDataRsp) => {
  244. Data.main.buildData = data.list
  245. //城堡技能
  246. Data.user.castleSkills = []
  247. Data.user.useCastleSkillID = data.skillid
  248. for (let i = 0; i < data.skillList.length; i++) {
  249. let iCastleSkill = Mgr.global.buildICastleSkill(data.skillList[i])
  250. Data.user.castleSkills.push(iCastleSkill)
  251. }
  252. })
  253. //-------------------------------装备数据-----------------------------------------
  254. //先获取装备背包,再获取英雄背包
  255. Mgr.net.addTop(msgCmd.cmd_equip_get_data_rsp, this, (data: equipGetDataRsp) => {
  256. Data.user.equips.length = 0
  257. data.list.forEach(equip => {
  258. Data.user.equips.push(Mgr.global.buildIEquip(equip))
  259. })
  260. Mgr.goods.sortEquip()
  261. Mgr.goods.reactEquipsChange()
  262. })
  263. let updateEquip = (equip: equip) => {
  264. Data.user.equips.forEach(v => {
  265. if (equip.sid == v.equip.sid) {
  266. Mgr.global.buildIEquip(equip, v)
  267. }
  268. })
  269. Mgr.goods.reactEquipsChange()
  270. }
  271. Mgr.net.addTop(msgCmd.cmd_equip_upgrade_rsp, this, (data: equipUpgradeRsp) => {
  272. updateEquip(data.data)
  273. })
  274. Mgr.net.addTop(msgCmd.cmd_equip_upgrade_star_rsp, this, (data: equipUpgradeStarRsp) => {
  275. for (let i = Data.user.equips.length - 1; i >= 0; i--) {
  276. let sid = Data.user.equips[i].equip.sid
  277. if (data.same.includes(sid) || data.other.includes(sid)) {
  278. Data.user.equips.splice(i, 1)
  279. }
  280. if (sid == data.data.sid) {
  281. Mgr.global.buildIEquip(data.data, Data.user.equips[i])
  282. }
  283. }
  284. Mgr.goods.reactEquipsChange()
  285. })
  286. Mgr.net.addTop(msgCmd.cmd_equip_onekey_upgrade_star_rsp, this, (data: equipOnekeyUpgradeStarRsp) => {
  287. for (let i = Data.user.equips.length - 1; i >= 0; i--) {
  288. if (data.delList.includes(Data.user.equips[i].equip.sid)) {
  289. Data.user.equips.splice(i, 1)
  290. }
  291. }
  292. for (let i = Data.user.equips.length - 1; i >= 0; i--) {
  293. let find = data.changeList.find(value => value.sid == Data.user.equips[i].equip.sid)
  294. if (find) {
  295. Mgr.global.buildIEquip(find, Data.user.equips[i])
  296. }
  297. }
  298. Mgr.goods.reactEquipsChange()
  299. })
  300. Mgr.net.addTop(msgCmd.cmd_equip_reset_rsp, this, (data: equipResetRsp) => {
  301. updateEquip(data.data)
  302. })
  303. Mgr.net.addTop(msgCmd.cmd_hero_wear_equip_rsp, this, (data: heroWearEquipRsp) => {
  304. let equips = new Array(Data.main.equipMaxNum).fill(null)
  305. Data.user.equips.find(value => {
  306. if (data.list.includes(value.equip.sid)) {
  307. value.equip.hero = data.data.sid
  308. equips[value.cfg.type - 1] = value
  309. } else if (value.equip.hero == data.data.sid) {
  310. value.equip.hero = ''
  311. }
  312. })
  313. Data.user.roles.find(value => {
  314. if (value.hero.sid == data.data.sid) {
  315. value.equips.length = 0
  316. value.equips.push(...equips)
  317. }
  318. })
  319. Mgr.goods.reactEquipsChange()
  320. })
  321. //-------------------------------英雄数据-----------------------------------------
  322. Mgr.net.addTop(msgCmd.cmd_hero_get_data_rsp, this, (data: heroGetDataRsp) => {
  323. Data.user.roles.length = 0
  324. data.list.forEach(hero => {
  325. Data.user.roles.push(Mgr.global.buildIRole(hero))
  326. })
  327. Mgr.goods.sortRole()
  328. Mgr.goods.reactRolesChange()
  329. })
  330. let updateRole = (data: hero) => {
  331. Data.user.roles.forEach(v => {
  332. if (data.sid == v.hero.sid) {
  333. Mgr.global.buildIRole(data, v)
  334. }
  335. })
  336. Mgr.goods.reactRolesChange()
  337. }
  338. Mgr.net.addTop(msgCmd.cmd_hero_upgrade_rsp, this, (data: heroUpgradeRsp) => {
  339. updateRole(data.data)
  340. })
  341. Mgr.net.addTop(msgCmd.cmd_hero_upgrade_star_rsp, this, (data: heroUpgradeStarRsp) => {
  342. for (let i = Data.user.roles.length - 1; i >= 0; i--) {
  343. let sid = Data.user.roles[i].hero.sid
  344. if (data.same.includes(sid) || data.other.includes(sid)) {
  345. Data.user.roles.splice(i, 1)
  346. }
  347. if (sid == data.data.sid) {
  348. Mgr.global.buildIRole(data.data, Data.user.roles[i])
  349. }
  350. }
  351. Mgr.goods.reactRolesChange()
  352. })
  353. Mgr.net.addTop(msgCmd.cmd_hero_onekey_upgrade_star_rsp, this, (data: heroOnekeyUpgradeStarRsp) => {
  354. for (let i = Data.user.roles.length - 1; i >= 0; i--) {
  355. if (data.delList.includes(Data.user.roles[i].hero.sid)) {
  356. Data.user.roles.splice(i, 1)
  357. }
  358. }
  359. for (let i = Data.user.roles.length - 1; i >= 0; i--) {
  360. let findHero = data.changeList.find(value => value.sid == Data.user.roles[i].hero.sid)
  361. if (findHero) {
  362. Mgr.global.buildIRole(findHero, Data.user.roles[i])
  363. }
  364. }
  365. Mgr.goods.reactRolesChange()
  366. })
  367. Mgr.net.addTop(msgCmd.cmd_hero_reset_rsp, this, (data: heroResetRsp) => {
  368. updateRole(data.data)
  369. })
  370. Mgr.net.addTop(msgCmd.cmd_hero_change_nty, this, (data: heroChangeNty) => {
  371. for (let i = 0; i < data.list.length; i++) {
  372. updateRole(data.list[i])
  373. }
  374. })
  375. //-------------------------------卡牌数据-----------------------------------------
  376. Mgr.net.addTop(msgCmd.cmd_skill_card_get_data_rsp, this, (data: skillCardGetDataRsp) => {
  377. Data.user.cards.length = 0
  378. data.list.forEach(card => {
  379. Data.user.cards.push(Mgr.global.buildICard(card))
  380. })
  381. Mgr.goods.sortCard()
  382. Mgr.goods.reactCardsChange()
  383. })
  384. let updateCard = (card: card) => {
  385. Data.user.cards.find(value => {
  386. if (value.card.sid == card.sid) {
  387. Mgr.global.buildICard(card, value)
  388. }
  389. })
  390. Mgr.goods.reactCardsChange()
  391. }
  392. Mgr.net.addTop(msgCmd.cmd_skill_card_upgrade_rsp, this, (data: skillCardUpgradeRsp) => {
  393. updateCard(data.data)
  394. })
  395. Mgr.net.addTop(msgCmd.cmd_skill_card_upgrade_star_rsp, this, (data: skillCardUpgradeStarRsp) => {
  396. updateCard(data.data)
  397. })
  398. //-------------------------------上阵数据-----------------------------------------
  399. let updateTeam = (data: embattleBattleRsp | embattleGetDataRsp) => {
  400. Data.user.teamRole.length = 0
  401. Data.user.teamCard.length = 0
  402. data.list.forEach(item => {
  403. if (item.sid) {
  404. Data.user.teamRole.push(Data.user.roles.find(hero => hero.hero.sid == item.sid))
  405. } else {
  406. Data.user.teamRole.push(null)
  407. }
  408. })
  409. data.cardList.forEach(item => {
  410. if (item.sid) {
  411. Data.user.teamCard.push(Data.user.cards.find(card => card.card.sid == item.sid))
  412. } else {
  413. Data.user.teamCard.push(null)
  414. }
  415. })
  416. Mgr.goods.reactRolesChange()
  417. Mgr.goods.reactCardsChange()
  418. }
  419. Mgr.net.addTop(msgCmd.cmd_embattle_get_data_rsp, this, (data: embattleBattleRsp) => {
  420. updateTeam(data)
  421. })
  422. Mgr.net.addTop(msgCmd.cmd_embattle_battle_rsp, this, (data: embattleBattleRsp) => {
  423. updateTeam(data)
  424. })
  425. //-------------------------------碎片数据-----------------------------------------
  426. Mgr.net.addTop(msgCmd.cmd_debris_get_data_rsp, this, (data: debrisGetDataRsp) => {
  427. Data.user.cardDebris.length = 0
  428. data.list.forEach(cardDebris => {
  429. Data.user.cardDebris.push(Mgr.global.buildICardDebris(cardDebris))
  430. })
  431. })
  432. Mgr.net.addTop(msgCmd.cmd_pay_debris_nty, this, (data: payDebrisNty) => {
  433. for (let i = Data.user.cardDebris.length - 1; i >= 0; i--) {
  434. let find = data.list.find(idNum => idNum.id == Data.user.cardDebris[i].id)
  435. if (find) {
  436. if (find.num == 0) {
  437. Data.user.cardDebris.splice(i, 1)
  438. } else {
  439. Data.user.cardDebris[i].num = find.num
  440. }
  441. }
  442. }
  443. })
  444. //-------------------------------天赋-----------------------------------------
  445. Mgr.net.addTop(msgCmd.cmd_talent_get_data_rsp, this, (data: talentGetDataRsp) => {
  446. Data.user.talents.length = 0
  447. let keys = Object.keys(TalentConfig)
  448. for (let i = 0; i < keys.length; i++) {
  449. let ID = TalentConfig[keys[i]].ID
  450. let type = TalentConfig[keys[i]].talentType
  451. if (data.id1 > 0 && ID <= data.id1 && type == 1) {
  452. Data.user.talents.push(ID)
  453. }
  454. if (data.id2 > 0 && ID <= data.id2 && type == 2) {
  455. Data.user.talents.push(ID)
  456. }
  457. }
  458. Mgr.global.initTalentAdd()
  459. })
  460. //-------------------------------冒险关卡-----------------------------------------
  461. Mgr.net.addTop(msgCmd.cmd_adventure_data_rsp, this, (data: adventureDataRsp) => {
  462. Data.user.adventureId = data.id1
  463. Data.user.eliteId = data.id2
  464. })
  465. //-------------------------------活动列表-----------------------------------------
  466. Mgr.net.addTop(msgCmd.cmd_activity_time_list_nty, this, (data: activityTimeListNty) => {
  467. data.list.forEach(activity => {
  468. Data.main.activityList.set(activity.type, activity)
  469. })
  470. })
  471. //-------------------------------开宝箱-----------------------------------------
  472. Mgr.net.add(msgCmd.cmd_box_get_award_rsp, this, (data: boxGetAwardRsp, rewardNty: IRewardNty) => {
  473. //判断宝箱类型,防止OpenBoxUI也会执行showReward
  474. if (data.type == GOODS_TYPE.randomBox) Mgr.ui.showReward(rewardNty)
  475. })
  476. //-------------------------------邮箱-----------------------------------------
  477. Mgr.net.addTop(msgCmd.cmd_simple_mail, this, (data: simpleMail) => {
  478. Data.main.emailRed = data.needread + data.noread > 0
  479. })
  480. Mgr.net.addTop(msgCmd.cmd_send_mail, this, (data: sendMail) => {
  481. Data.main.emailRed = true
  482. })
  483. //-------------------------------创建订单-----------------------------------------
  484. Mgr.net.addTop(msgCmd.cmd_create_order_nty, this, (data: createOrderNty) => {
  485. Mgr.ui.showLoading('', false)
  486. Mgr.platform.buyItem(data.cfid.toString(), data.id.toString())
  487. })
  488. }
  489. addReadyBox(idNum: idNum, isOnlySelect: boolean = true) {
  490. let isBox =
  491. GoodsConfig[idNum.id].type == GOODS_TYPE.selectBox || GoodsConfig[idNum.id].type == GOODS_TYPE.randomBox
  492. if (isBox) {
  493. for (let i = 0; i < idNum.num; i++) {
  494. Data.user.readyOpenBox.push({id: idNum.id, num: 1})
  495. }
  496. }
  497. }
  498. }