UIVoyageView.lua 14 KB

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