UIVoyageView.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. local UIVoyageView = require("UIVoyage/UIVoyageView_Generate")
  2. function UIVoyageView:OnAwake(data)
  3. self.controller = require("UIVoyage/UIVoyageCtr"):new()
  4. self.controller:Init(self)
  5. self.controller:SetData(data)
  6. end
  7. function UIVoyageView:AddEventListener()
  8. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.VOYAGE_NUM_CHANGED, self, self.OnVoyageNumChanged)
  9. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.VOYAGE_SELF_AIRSHIP_DATA_CHANGED, self, self.OnVoyageAirShipDataChanged)
  10. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.VOYAGE_LOCATION_CHANGED, self, self.OnVoyageLocationChanged)
  11. end
  12. function UIVoyageView:FillContent(data, uiBase)
  13. self.uiBase = uiBase
  14. local gameObject = self.uiBase:GetRoot()
  15. if gameObject ~= nil then
  16. self.gameObject = gameObject
  17. self.transform = gameObject.transform
  18. end
  19. self:InitGenerate(self.transform, data)
  20. self:Init()
  21. end
  22. function UIVoyageView:Init()
  23. self.controller:InitData()
  24. self:RefreshRemindNum()
  25. self:RefreshBtnState()
  26. self.isAlginPos = true
  27. self.alginUid = self.controller:GetLastAttackUserId()
  28. self.sourceAirShipItem = self.transform:Find("UIAnimator/Window/AirShipItem").gameObject
  29. if not self.airShipTimer then
  30. self.airShipTimer = Timer.New(slot(self.RefreshAirShips, self), 0.3, -1)
  31. else
  32. self.airShipTimer.time = 0.3
  33. end
  34. if not self.airShipTimer.running then
  35. self.airShipTimer:Start()
  36. end
  37. ManagerContainer.LuaUIMgr:ClosePage(Enum.UIPageName.UIDojoExit)
  38. self.btnLog:SetActive(true)
  39. --self.btnAdd:SetActive(true)
  40. end
  41. function UIVoyageView:RemoveEventListener()
  42. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  43. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_CROSS_YUAN_HANG_TRIAL_BUY_CHALLENGE_NUM_ACK )
  44. end
  45. function UIVoyageView:AddUIEventListener()
  46. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_CROSS_YUAN_HANG_TRIAL_BUY_CHALLENGE_NUM_ACK ,self.OnBuyNumAck,self)
  47. self.uiBase:AddButtonUniqueEventListener(self.backBtn.button, self, self.OnClickBackBtn)
  48. self.uiBase:AddButtonUniqueEventListener(self.btnInfo.button, self, self.OnClickInfoBtn)
  49. self.uiBase:AddButtonUniqueEventListener(self.btnRank.button, self, self.OnClickRankBtn)
  50. self.uiBase:AddButtonUniqueEventListener(self.btnShop.button, self, self.OnClickShopBtn)
  51. self.uiBase:AddButtonUniqueEventListener(self.btnLog.button, self, self.OnClickLogBtn)
  52. self.uiBase:AddButtonUniqueEventListener(self.btnChat.button, self, self.OnClickChatBtn)
  53. self.uiBase:AddButtonUniqueEventListener(self.voyageBtn.button, self, self.OnClickVoyageBtn)
  54. self.uiBase:AddButtonUniqueEventListener(self.rewardBtn.button, self, self.OnClickRewardBtn)
  55. self.uiBase:AddButtonUniqueEventListener(self.btnAdd.button, self, self.OnClickAddBtn)
  56. end
  57. function UIVoyageView:OnHide()
  58. if self.airShipTimer then
  59. self.airShipTimer:Stop()
  60. end
  61. end
  62. function UIVoyageView:OnShow(data)
  63. self.controller:SetData(data)
  64. if not self.airShipTimer then
  65. self.airShipTimer = Timer.New(slot(self.RefreshAirShips, self), 0.3, -1)
  66. else
  67. self.airShipTimer.time = 0.3
  68. end
  69. if not self.airShipTimer.running then
  70. self.airShipTimer:Start()
  71. end
  72. end
  73. function UIVoyageView:OnClose()
  74. end
  75. function UIVoyageView:OnDispose()
  76. self.isAlginPos = nil
  77. self.alginUid = nil
  78. if self.btnTimer then
  79. self.btnTimer:Stop()
  80. self.btnTimer = nil
  81. end
  82. if self.airShipTimer then
  83. self.airShipTimer:Stop()
  84. self.airShipTimer = nil
  85. end
  86. self:DisposeAirShips()
  87. self.controller:OnDispose()
  88. end
  89. function UIVoyageView:OnPageInEnd()
  90. self.super.OnPageInEnd(self)
  91. end
  92. function UIVoyageView:OnVoyageNumChanged()
  93. self:RefreshRemindNum()
  94. end
  95. function UIVoyageView:OnVoyageAirShipDataChanged()
  96. self:RefreshBtnState()
  97. end
  98. function UIVoyageView:OnVoyageLocationChanged(uid)
  99. local scrollRect = self.traceGroup.scrollRect
  100. if scrollRect.velocity ~= Vector2.zero then return end
  101. if ManagerContainer.LuaUIMgr:HasOpenPage(Enum.UIPageName.UIVoyageRobbery) then
  102. return
  103. end
  104. self.isAlginPos = true
  105. self.alginUid = uid
  106. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageRobbery, uid)
  107. end
  108. function UIVoyageView:OnClickAddBtn()
  109. --ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIDojoBuyTimes, {costNum, nil, self.SureBuyChallengeNumBtn, self})
  110. local buyNum = ManagerContainer.DataMgr.VoyageDataMgr:GetBuyNum()
  111. local buyNumLimit = ManagerContainer.DataMgr.VoyageDataMgr:GetBuyNumLimit()
  112. if (buyNumLimit - buyNum) <= 0 then
  113. LogError("购买次数达到上限")
  114. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("312")
  115. return
  116. end
  117. local buyType = ManagerContainer.DataMgr.VoyageDataMgr:GetBuyType()
  118. local cost = ManagerContainer.DataMgr.VoyageDataMgr:GetBuyCost()
  119. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIDojoBuyTimes, {cost, nil, self.SureBuyChallengeNumBtn, self,nil,buyType})
  120. --ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_CROSS_YUAN_HANG_TRIAL_BUY_CHALLENGE_NUM_REQ , {})
  121. end
  122. function UIVoyageView:SureBuyChallengeNumBtn()
  123. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_CROSS_YUAN_HANG_TRIAL_BUY_CHALLENGE_NUM_REQ , {})
  124. end
  125. function UIVoyageView:OnClickBackBtn()
  126. ManagerContainer.LuaUIMgr:OpenSourceUI(self)
  127. end
  128. function UIVoyageView:OnClickInfoBtn()
  129. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIPlayRule, {'PlayExplainTitle', 'VoyageExplain'})
  130. end
  131. function UIVoyageView:OnClickRankBtn()
  132. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageRank)
  133. end
  134. function UIVoyageView:OnClickShopBtn()
  135. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIShop, 12)
  136. end
  137. function UIVoyageView:OnClickLogBtn()
  138. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageLog)
  139. end
  140. function UIVoyageView:OnClickChatBtn()
  141. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIChat)
  142. end
  143. function UIVoyageView:OnClickVoyageBtn()
  144. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyagePrepare)
  145. end
  146. function UIVoyageView:OnClickRewardBtn()
  147. local airShipData = self.controller:GetSelfAirShipData()
  148. if not airShipData then
  149. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips01')
  150. return
  151. else
  152. if airShipData.awardReceived then
  153. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips04')
  154. return
  155. elseif ManagerContainer.LuaTimerMgr:CurLuaServerTime() < airShipData.endTime then
  156. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips05')
  157. return
  158. end
  159. end
  160. local errorCode = self.controller:SendGetRewardReq()
  161. if errorCode ~= 0 then
  162. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  163. end
  164. end
  165. function UIVoyageView:OnClickAirShipItem(_, params)
  166. local item = params[0]
  167. if not item then return end
  168. local uid = item.uid
  169. if not uid then return end
  170. local selfuid = ManagerContainer.DataMgr.UserData:GetUserId()
  171. if uid == selfuid then
  172. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips02')
  173. return
  174. end
  175. local airShipData = self.controller:GetAirShipDataByUid(uid)
  176. if not airShipData then
  177. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips01')
  178. return
  179. end
  180. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  181. local sec = (airShipData.endTime - curTime) / 1000
  182. sec = type(sec) == "number" and sec or #sec
  183. local time = airShipData.duration or 1
  184. if sec > time then
  185. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips03')
  186. return
  187. elseif sec <= 0 then
  188. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageFinishTips')
  189. return
  190. end
  191. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageRobbery, uid)
  192. end
  193. function UIVoyageView:RefreshRemindNum()
  194. self.remindNumTxt.text.text = tostring(self.controller:GetRemindNum())
  195. end
  196. function UIVoyageView:RefreshBtnState()
  197. local airShipData = self.controller:GetSelfAirShipData()
  198. if not airShipData then
  199. self.voyageBtn.button.interactable = true
  200. self.voyageBtn.text.text.text = string.formatbykey('BtnAirShipPrepare')
  201. self.voyageBtn:SetActive(true)
  202. self.rewardBtn:SetActive(false)
  203. if self.btnTimer then
  204. self.btnTimer:Stop()
  205. self.btnTimer = nil
  206. end
  207. else
  208. if airShipData.awardReceived then
  209. self.voyageBtn.button.interactable = true
  210. self.voyageBtn.text.text.text = string.formatbykey('BtnAirShipPrepare')
  211. self.voyageBtn:SetActive(true)
  212. self.rewardBtn:SetActive(false)
  213. if self.btnTimer then
  214. self.btnTimer:Stop()
  215. self.btnTimer = nil
  216. end
  217. elseif ManagerContainer.LuaTimerMgr:CurLuaServerTime() < airShipData.endTime then
  218. self.voyageBtn.button.interactable = false
  219. self.voyageBtn:SetActive(true)
  220. self.rewardBtn:SetActive(false)
  221. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  222. local remainTime = (airShipData.endTime - curTime) / 1000
  223. remainTime = type(remainTime) == "number" and remainTime or #remainTime
  224. self.voyageBtn.text.text.text = DateTimeUtil.convertSeconds2TimeStr1(remainTime, true, true, false)
  225. if not self.btnTimer then
  226. self.btnTimer = Timer.New(slot(self.RefreshBtnState, self), 1, -1)
  227. end
  228. if not self.btnTimer.running then
  229. self.btnTimer:Start()
  230. end
  231. else
  232. self.voyageBtn.button.interactable = false
  233. self.voyageBtn:SetActive(false)
  234. self.rewardBtn:SetActive(true)
  235. if self.btnTimer then
  236. self.btnTimer:Stop()
  237. self.btnTimer = nil
  238. end
  239. end
  240. end
  241. end
  242. function UIVoyageView:RefreshAirShips()
  243. local oldShowAirShipMap = self.showAirShipMap or {}
  244. local newShowAirShipMap = {}
  245. -- 缓慢显示,一次刷新,不要去实例化太多对象
  246. local createNum = 0
  247. local airShipDatas = self.controller:GetAirShipDatas()
  248. local lastId = nil
  249. local airShipData = nil
  250. local item = nil
  251. local alginItem = nil
  252. local alginUid = self.alginUid
  253. local len = #airShipDatas
  254. for i = len, 1, -1 do
  255. airShipData = airShipDatas[i]
  256. if not airShipData:GetSlotId() then
  257. if lastId then
  258. lastId = lastId + 1
  259. if lastId > 10 then
  260. lastId = 1
  261. end
  262. else
  263. lastId = len % 10 + 1
  264. end
  265. airShipData:SetSlotId(lastId)
  266. else
  267. lastId = airShipData:GetSlotId()
  268. end
  269. item = oldShowAirShipMap[airShipData.uid]
  270. if not item then
  271. createNum = createNum + 1
  272. if createNum <= 30 then
  273. item = self:GetOneAirShip()
  274. item.transform:SetParent(self['airShipRoot' .. tostring(lastId)].transform)
  275. item.transform:SetAsFirstSibling()
  276. item.transform.localRotation = Quaternion.identity
  277. item.transform.localScale = Vector3.one
  278. end
  279. end
  280. if item then
  281. oldShowAirShipMap[airShipData.uid] = nil
  282. newShowAirShipMap[airShipData.uid] = item
  283. local state = self:RefreshAirShipItem(item, airShipData, false)
  284. if state == 5 and airShipData.uid == alginUid then
  285. alginItem = item
  286. end
  287. end
  288. end
  289. airShipData = self.controller:GetSelfAirShipData()
  290. if airShipData then
  291. item = oldShowAirShipMap[airShipData.uid]
  292. if not item then
  293. lastId = airShipData:GetSlotId()
  294. if not lastId then
  295. lastId = Mathf.Random(1, 10)
  296. airShipData:SetSlotId(lastId)
  297. end
  298. item = self:GetOneAirShip()
  299. item.transform:SetParent(self['airShipRoot' .. tostring(lastId)].transform)
  300. item.transform:SetAsFirstSibling()
  301. item.transform.localRotation = Quaternion.identity
  302. item.transform.localScale = Vector3.one
  303. if not self.isAlginPos then
  304. self.isAlginPos = true
  305. alginItem = nil
  306. end
  307. end
  308. if item then
  309. oldShowAirShipMap[airShipData.uid] = nil
  310. newShowAirShipMap[airShipData.uid] = item
  311. local state = self:RefreshAirShipItem(item, airShipData, true)
  312. if state == 5 and not alginItem then
  313. alginItem = item
  314. end
  315. end
  316. end
  317. for _, olditem in pairs(oldShowAirShipMap) do
  318. self:ReleaseAirShip(olditem)
  319. end
  320. self.showAirShipMap = newShowAirShipMap
  321. if self.isAlginPos and not CommonUtil.TableIsEmpty(newShowAirShipMap) then
  322. self.isAlginPos = false
  323. if alginItem then
  324. local scrollRect = self.traceGroup.scrollRect
  325. local viewport = scrollRect.viewport
  326. local content = scrollRect.content
  327. local viewportRect = viewport.rect
  328. local contentRect = content.rect
  329. local viewportW = viewportRect.width
  330. local contentW = contentRect.width
  331. local validW = contentW - viewportW
  332. scrollRect.horizontalNormalizedPosition = Mathf.Clamp01((alginItem.transform.localPosition.x / validW) + 0.5)
  333. end
  334. end
  335. end
  336. function UIVoyageView:RefreshAirShipItem(item, data, isSelf)
  337. local cfgData = ManagerContainer.CfgMgr:GetVoyageAirShipCfgById(data.quality)
  338. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  339. local sec = (data.endTime - curTime) / 1000
  340. sec = type(sec) == "number" and sec or #sec
  341. local time = (data.duration or 1)
  342. if not item.uid or item.uid ~= data.uid then
  343. item.uid = data.uid
  344. item.me:SetActive(isSelf)
  345. local quality = data.quality
  346. if data.beChallengeNum >= cfgData.RobberyTimes then
  347. quality = 0
  348. end
  349. if quality then
  350. item.switchSprite:ChangeSprite(quality)
  351. item.fan:SetActive(quality > 0)
  352. end
  353. if not isSelf then
  354. self.uiBase:AddButtonUniqueEventListener(item.button, self, self.OnClickAirShipItem, item)
  355. else
  356. item.button.onClick:RemoveAllListeners()
  357. end
  358. else
  359. if data:IsChange() then
  360. local quality = nil
  361. if data:IsChangeQuality() then
  362. quality = data.quality
  363. end
  364. if data:IsChangeNum() then
  365. if data.beChallengeNum >= cfgData.RobberyTimes then
  366. quality = 0
  367. else
  368. quality = data.quality
  369. end
  370. end
  371. if quality then
  372. item.switchSprite:ChangeSprite(quality)
  373. item.fan:SetActive(quality > 0)
  374. end
  375. data:ResetChange()
  376. end
  377. end
  378. local state = 0
  379. local race = self['race' .. tostring(data:GetSlotId())]
  380. local pos = race.start.transform.position
  381. if sec < -1 then
  382. pos.x = pos.x + 400
  383. item.animator:Play('AirShipKeep')
  384. state = 1
  385. elseif sec > time + 1 then
  386. pos.x = pos.x - 400
  387. item.animator:Play('AirShipKeep')
  388. state = 2
  389. else
  390. if sec <= 0 then
  391. item.animator:Play('AirShipFinish')
  392. state = 3
  393. elseif sec > time then
  394. item.animator:Play('AirShipStart')
  395. state = 4
  396. else
  397. item.animator:CrossFade('AirShipRun', 0.2)
  398. state = 5
  399. end
  400. local raceLength = Mathf.Abs(pos.x - race.finsh.transform.position.x)
  401. pos.x = pos.x + (1 - (sec / time)) * raceLength
  402. end
  403. item.transform.position = pos
  404. return state
  405. end
  406. function UIVoyageView:GetOneAirShip()
  407. if self.airShipPool then
  408. local len = #self.airShipPool
  409. if len > 0 then
  410. local item = self.airShipPool[len]
  411. self.airShipPool[len] = nil
  412. return item
  413. end
  414. end
  415. local newGo = UnityEngine.GameObject.Instantiate(self.sourceAirShipItem)
  416. newGo:SetActive(true)
  417. local newTrans = newGo.transform
  418. local item = {}
  419. item.gameObject = newGo
  420. item.transform = newTrans
  421. item.animator = newGo:GetComponent(Enum.TypeInfo.Animator)
  422. item.button = newGo:GetComponent(Enum.TypeInfo.Button)
  423. item.me = newTrans:Find("Me").gameObject
  424. item.fan = newTrans:Find("Ship/Fan").gameObject
  425. item.switchSprite = newTrans:Find("Ship/Body"):GetComponent(Enum.TypeInfo.UIImageSwitchSprite)
  426. return item
  427. end
  428. function UIVoyageView:ReleaseAirShip(item)
  429. if not item then return end
  430. item.uid = nil
  431. item.gameObject:SetActive(false)
  432. if not self.airShipPool then
  433. self.airShipPool = { item }
  434. else
  435. self.airShipPool[#self.airShipPool+1] = item
  436. end
  437. end
  438. function UIVoyageView:DisposeAirShips()
  439. if self.airShipPool then
  440. for _, item in pairs(self.airShipPool) do
  441. if item then
  442. local go = item.gameObject
  443. item.gameObject = nil
  444. item.transform = nil
  445. item.animator = nil
  446. item.button = nil
  447. item.me = nil
  448. item.fan = nil
  449. item.switchSprite = nil
  450. CommonUtil.DestroyGO(go)
  451. end
  452. end
  453. self.airShipPool = nil
  454. end
  455. if self.showAirShipMap then
  456. for _, item in pairs(self.showAirShipMap) do
  457. if item then
  458. local go = item.gameObject
  459. item.gameObject = nil
  460. item.transform = nil
  461. item.animator = nil
  462. item.button = nil
  463. item.me = nil
  464. item.fan = nil
  465. item.switchSprite = nil
  466. CommonUtil.DestroyGO(go)
  467. end
  468. end
  469. self.showAirShipMap = nil
  470. end
  471. end
  472. function UIVoyageView:OnBuyNumAck(data)
  473. LogError(" SC_CROSS_YUAN_HANG_TRIAL_BUY_CHALLENGE_NUM_ACK = " .. Inspect(data))
  474. if ManagerContainer.NetManager:IsErrorData(data) then return end
  475. self.controller:RefNumimit()
  476. self:RefreshRemindNum()
  477. end
  478. return UIVoyageView