UIVoyageView.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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.UIRuneShopBT)
  110. --Notice_TeQuanKa
  111. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("Notice_TeQuanKa")
  112. -- local buyNum = ManagerContainer.DataMgr.VoyageDataMgr:GetBuyNum()
  113. -- local buyNumLimit = ManagerContainer.DataMgr.VoyageDataMgr:GetBuyNumLimit()
  114. -- if (buyNumLimit - buyNum) <= 0 then
  115. -- LogError("购买次数达到上限")
  116. -- ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("312")
  117. -- return
  118. -- end
  119. -- local buyType = ManagerContainer.DataMgr.VoyageDataMgr:GetBuyType()
  120. -- local cost = ManagerContainer.DataMgr.VoyageDataMgr:GetBuyCost()
  121. -- ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIDojoBuyTimes, {cost, nil, self.SureBuyChallengeNumBtn, self,nil,buyType})
  122. end
  123. function UIVoyageView:SureBuyChallengeNumBtn()
  124. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_CROSS_YUAN_HANG_TRIAL_BUY_CHALLENGE_NUM_REQ , {})
  125. end
  126. function UIVoyageView:OnClickBackBtn()
  127. ManagerContainer.LuaUIMgr:OpenSourceUI(self)
  128. end
  129. function UIVoyageView:OnClickInfoBtn()
  130. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIPlayRule, {'PlayExplainTitle', 'VoyageExplain'})
  131. end
  132. function UIVoyageView:OnClickRankBtn()
  133. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageRank)
  134. end
  135. function UIVoyageView:OnClickShopBtn()
  136. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIShop, 12)
  137. end
  138. function UIVoyageView:OnClickLogBtn()
  139. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageLog)
  140. end
  141. function UIVoyageView:OnClickChatBtn()
  142. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIChat)
  143. end
  144. function UIVoyageView:OnClickVoyageBtn()
  145. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyagePrepare)
  146. end
  147. function UIVoyageView:OnClickRewardBtn()
  148. local airShipData = self.controller:GetSelfAirShipData()
  149. if not airShipData then
  150. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips01')
  151. return
  152. else
  153. if airShipData.awardReceived then
  154. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips04')
  155. return
  156. elseif ManagerContainer.LuaTimerMgr:CurLuaServerTime() < airShipData.endTime then
  157. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips05')
  158. return
  159. end
  160. end
  161. local errorCode = self.controller:SendGetRewardReq()
  162. if errorCode ~= 0 then
  163. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  164. end
  165. end
  166. function UIVoyageView:OnClickAirShipItem(_, params)
  167. local item = params[0]
  168. if not item then return end
  169. local uid = item.uid
  170. if not uid then return end
  171. local selfuid = ManagerContainer.DataMgr.UserData:GetUserId()
  172. if uid == selfuid then
  173. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips02')
  174. return
  175. end
  176. local airShipData = self.controller:GetAirShipDataByUid(uid)
  177. if not airShipData then
  178. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips01')
  179. return
  180. end
  181. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  182. local sec = (airShipData.endTime - curTime) / 1000
  183. sec = type(sec) == "number" and sec or #sec
  184. local time = airShipData.duration or 1
  185. if sec > time then
  186. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageErrorTips03')
  187. return
  188. elseif sec <= 0 then
  189. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('VoyageFinishTips')
  190. return
  191. end
  192. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageRobbery, uid)
  193. end
  194. function UIVoyageView:RefreshRemindNum()
  195. self.remindNumTxt.text.text = tostring(self.controller:GetRemindNum())
  196. end
  197. function UIVoyageView:RefreshBtnState()
  198. local airShipData = self.controller:GetSelfAirShipData()
  199. if not airShipData then
  200. self.voyageBtn.button.interactable = true
  201. self.voyageBtn.text.text.text = string.formatbykey('BtnAirShipPrepare')
  202. self.voyageBtn:SetActive(true)
  203. self.rewardBtn:SetActive(false)
  204. if self.btnTimer then
  205. self.btnTimer:Stop()
  206. self.btnTimer = nil
  207. end
  208. else
  209. if airShipData.awardReceived then
  210. self.voyageBtn.button.interactable = true
  211. self.voyageBtn.text.text.text = string.formatbykey('BtnAirShipPrepare')
  212. self.voyageBtn:SetActive(true)
  213. self.rewardBtn:SetActive(false)
  214. if self.btnTimer then
  215. self.btnTimer:Stop()
  216. self.btnTimer = nil
  217. end
  218. elseif ManagerContainer.LuaTimerMgr:CurLuaServerTime() < airShipData.endTime then
  219. self.voyageBtn.button.interactable = false
  220. self.voyageBtn:SetActive(true)
  221. self.rewardBtn:SetActive(false)
  222. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  223. local remainTime = (airShipData.endTime - curTime) / 1000
  224. remainTime = type(remainTime) == "number" and remainTime or #remainTime
  225. self.voyageBtn.text.text.text = DateTimeUtil.convertSeconds2TimeStr1(remainTime, true, true, false)
  226. if not self.btnTimer then
  227. self.btnTimer = Timer.New(slot(self.RefreshBtnState, self), 1, -1)
  228. end
  229. if not self.btnTimer.running then
  230. self.btnTimer:Start()
  231. end
  232. else
  233. self.voyageBtn.button.interactable = false
  234. self.voyageBtn:SetActive(false)
  235. self.rewardBtn:SetActive(true)
  236. if self.btnTimer then
  237. self.btnTimer:Stop()
  238. self.btnTimer = nil
  239. end
  240. end
  241. end
  242. end
  243. function UIVoyageView:RefreshAirShips()
  244. local oldShowAirShipMap = self.showAirShipMap or {}
  245. local newShowAirShipMap = {}
  246. -- 缓慢显示,一次刷新,不要去实例化太多对象
  247. local createNum = 0
  248. local airShipDatas = self.controller:GetAirShipDatas()
  249. local lastId = nil
  250. local airShipData = nil
  251. local item = nil
  252. local alginItem = nil
  253. local alginUid = self.alginUid
  254. local len = #airShipDatas
  255. for i = len, 1, -1 do
  256. airShipData = airShipDatas[i]
  257. if not airShipData:GetSlotId() then
  258. if lastId then
  259. lastId = lastId + 1
  260. if lastId > 10 then
  261. lastId = 1
  262. end
  263. else
  264. lastId = len % 10 + 1
  265. end
  266. airShipData:SetSlotId(lastId)
  267. else
  268. lastId = airShipData:GetSlotId()
  269. end
  270. item = oldShowAirShipMap[airShipData.uid]
  271. if not item then
  272. createNum = createNum + 1
  273. if createNum <= 30 then
  274. item = self:GetOneAirShip()
  275. item.transform:SetParent(self['airShipRoot' .. tostring(lastId)].transform)
  276. item.transform:SetAsFirstSibling()
  277. item.transform.localRotation = Quaternion.identity
  278. item.transform.localScale = Vector3.one
  279. end
  280. end
  281. if item then
  282. oldShowAirShipMap[airShipData.uid] = nil
  283. newShowAirShipMap[airShipData.uid] = item
  284. local state = self:RefreshAirShipItem(item, airShipData, false)
  285. if state == 5 and airShipData.uid == alginUid then
  286. alginItem = item
  287. end
  288. end
  289. end
  290. airShipData = self.controller:GetSelfAirShipData()
  291. if airShipData then
  292. item = oldShowAirShipMap[airShipData.uid]
  293. if not item then
  294. lastId = airShipData:GetSlotId()
  295. if not lastId then
  296. lastId = Mathf.Random(1, 10)
  297. airShipData:SetSlotId(lastId)
  298. end
  299. item = self:GetOneAirShip()
  300. item.transform:SetParent(self['airShipRoot' .. tostring(lastId)].transform)
  301. item.transform:SetAsFirstSibling()
  302. item.transform.localRotation = Quaternion.identity
  303. item.transform.localScale = Vector3.one
  304. if not self.isAlginPos then
  305. self.isAlginPos = true
  306. alginItem = nil
  307. end
  308. end
  309. if item then
  310. oldShowAirShipMap[airShipData.uid] = nil
  311. newShowAirShipMap[airShipData.uid] = item
  312. local state = self:RefreshAirShipItem(item, airShipData, true)
  313. if state == 5 and not alginItem then
  314. alginItem = item
  315. end
  316. end
  317. end
  318. for _, olditem in pairs(oldShowAirShipMap) do
  319. self:ReleaseAirShip(olditem)
  320. end
  321. self.showAirShipMap = newShowAirShipMap
  322. if self.isAlginPos and not CommonUtil.TableIsEmpty(newShowAirShipMap) then
  323. self.isAlginPos = false
  324. if alginItem then
  325. local scrollRect = self.traceGroup.scrollRect
  326. local viewport = scrollRect.viewport
  327. local content = scrollRect.content
  328. local viewportRect = viewport.rect
  329. local contentRect = content.rect
  330. local viewportW = viewportRect.width
  331. local contentW = contentRect.width
  332. local validW = contentW - viewportW
  333. scrollRect.horizontalNormalizedPosition = Mathf.Clamp01((alginItem.transform.localPosition.x / validW) + 0.5)
  334. end
  335. end
  336. end
  337. function UIVoyageView:RefreshAirShipItem(item, data, isSelf)
  338. local cfgData = ManagerContainer.CfgMgr:GetVoyageAirShipCfgById(data.quality)
  339. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  340. local sec = (data.endTime - curTime) / 1000
  341. sec = type(sec) == "number" and sec or #sec
  342. local time = (data.duration or 1)
  343. if not item.uid or item.uid ~= data.uid then
  344. item.uid = data.uid
  345. item.me:SetActive(isSelf)
  346. local quality = data.quality
  347. if data.beChallengeNum >= cfgData.RobberyTimes then
  348. quality = 0
  349. end
  350. if quality then
  351. item.switchSprite:ChangeSprite(quality)
  352. item.fan:SetActive(quality > 0)
  353. end
  354. if not isSelf then
  355. self.uiBase:AddButtonUniqueEventListener(item.button, self, self.OnClickAirShipItem, item)
  356. else
  357. item.button.onClick:RemoveAllListeners()
  358. end
  359. else
  360. if data:IsChange() then
  361. local quality = nil
  362. if data:IsChangeQuality() then
  363. quality = data.quality
  364. end
  365. if data:IsChangeNum() then
  366. if data.beChallengeNum >= cfgData.RobberyTimes then
  367. quality = 0
  368. else
  369. quality = data.quality
  370. end
  371. end
  372. if quality then
  373. item.switchSprite:ChangeSprite(quality)
  374. item.fan:SetActive(quality > 0)
  375. end
  376. data:ResetChange()
  377. end
  378. end
  379. local state = 0
  380. local race = self['race' .. tostring(data:GetSlotId())]
  381. local pos = race.start.transform.position
  382. if sec < -1 then
  383. pos.x = pos.x + 400
  384. item.animator:Play('AirShipKeep')
  385. state = 1
  386. elseif sec > time + 1 then
  387. pos.x = pos.x - 400
  388. item.animator:Play('AirShipKeep')
  389. state = 2
  390. else
  391. if sec <= 0 then
  392. item.animator:Play('AirShipFinish')
  393. state = 3
  394. elseif sec > time then
  395. item.animator:Play('AirShipStart')
  396. state = 4
  397. else
  398. item.animator:CrossFade('AirShipRun', 0.2)
  399. state = 5
  400. end
  401. local raceLength = Mathf.Abs(pos.x - race.finsh.transform.position.x)
  402. pos.x = pos.x + (1 - (sec / time)) * raceLength
  403. end
  404. item.transform.position = pos
  405. return state
  406. end
  407. function UIVoyageView:GetOneAirShip()
  408. if self.airShipPool then
  409. local len = #self.airShipPool
  410. if len > 0 then
  411. local item = self.airShipPool[len]
  412. self.airShipPool[len] = nil
  413. return item
  414. end
  415. end
  416. local newGo = UnityEngine.GameObject.Instantiate(self.sourceAirShipItem)
  417. newGo:SetActive(true)
  418. local newTrans = newGo.transform
  419. local item = {}
  420. item.gameObject = newGo
  421. item.transform = newTrans
  422. item.animator = newGo:GetComponent(Enum.TypeInfo.Animator)
  423. item.button = newGo:GetComponent(Enum.TypeInfo.Button)
  424. item.me = newTrans:Find("Me").gameObject
  425. item.fan = newTrans:Find("Ship/Fan").gameObject
  426. item.switchSprite = newTrans:Find("Ship/Body"):GetComponent(Enum.TypeInfo.UIImageSwitchSprite)
  427. return item
  428. end
  429. function UIVoyageView:ReleaseAirShip(item)
  430. if not item then return end
  431. item.uid = nil
  432. item.gameObject:SetActive(false)
  433. if not self.airShipPool then
  434. self.airShipPool = { item }
  435. else
  436. self.airShipPool[#self.airShipPool+1] = item
  437. end
  438. end
  439. function UIVoyageView:DisposeAirShips()
  440. if self.airShipPool then
  441. for _, item in pairs(self.airShipPool) do
  442. if item then
  443. local go = item.gameObject
  444. item.gameObject = nil
  445. item.transform = nil
  446. item.animator = nil
  447. item.button = nil
  448. item.me = nil
  449. item.fan = nil
  450. item.switchSprite = nil
  451. CommonUtil.DestroyGO(go)
  452. end
  453. end
  454. self.airShipPool = nil
  455. end
  456. if self.showAirShipMap then
  457. for _, item in pairs(self.showAirShipMap) do
  458. if item then
  459. local go = item.gameObject
  460. item.gameObject = nil
  461. item.transform = nil
  462. item.animator = nil
  463. item.button = nil
  464. item.me = nil
  465. item.fan = nil
  466. item.switchSprite = nil
  467. CommonUtil.DestroyGO(go)
  468. end
  469. end
  470. self.showAirShipMap = nil
  471. end
  472. end
  473. function UIVoyageView:OnBuyNumAck(data)
  474. LogError(" SC_CROSS_YUAN_HANG_TRIAL_BUY_CHALLENGE_NUM_ACK = " .. Inspect(data))
  475. if ManagerContainer.NetManager:IsErrorData(data) then return end
  476. self.controller:RefNumimit()
  477. self:RefreshRemindNum()
  478. end
  479. return UIVoyageView