源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

450 lines
15 KiB

  1. SceneLoadView = SceneLoadView or BaseClass(BaseView)
  2. local SceneLoadView = SceneLoadView
  3. SceneLoadView.LOAD_SCENE_TITLE = {type = 1, weight = 0.4}
  4. SceneLoadView.LOAD_PRELOAD_RES = {type = 2, weight = 0.5}
  5. SceneLoadView.LOAD_SCENE_DATA = {type = 3, weight = 0.1}
  6. function SceneLoadView:__init()
  7. self.base_file = "sceneLoad"
  8. self.layout_file = "SceneLoadView"
  9. self.layer_name = "Top"
  10. self.mask_open_sound = true
  11. self.mask_close_sound = true
  12. self.open_guide_close = nil
  13. -- self.use_local_view = true
  14. self.close_mode = CloseMode.CloseVisible
  15. self.destroy_imm = false
  16. self.use_background = true
  17. self.is_delay_callback = false
  18. self.is_set_zdepth = true
  19. self.hide_maincancas = false
  20. self.ignore_use_anim = true
  21. self.background_alpha = 1
  22. self.auto_update_progress = 0
  23. self.scene_load_progress_finished = false
  24. self.progress_time = 7 --进入游戏加载5秒 切换场景3秒
  25. self.trigger_story = nil
  26. self.cur_value = 0
  27. self.is_first_show = true
  28. -- self.is_first_set_res = true
  29. self.last_progress_value = 0
  30. self.curr_progress_value = 0
  31. self.temp_res_target = {}
  32. self.default_bg_abname = "icon_sceneload_1"
  33. self.default_res_name = "1.jpg"
  34. self:AddPreLoadList("uiComponent_asset", {"ProgressBar2", "progressBg", "progressColor"}, LuaResManager.RES_TYPE.SPRITE)
  35. self.curr_progress_with_weight = {}
  36. self.load_callback = function ()
  37. self:LoadSuccess()
  38. self:InitEvent()
  39. end
  40. self.open_callback = function ()
  41. self:OpenFun()
  42. self:SetPromptTxt()
  43. if self.hide_load_progress then
  44. self.gameObject:SetActive(false)
  45. end
  46. self:CheckResLoadTime()
  47. self:StartSmoothTimer()
  48. if MainCamera and MainCamera.Instance then
  49. MainCamera:getInstance():ResetAutoRotateInfo()
  50. end
  51. end
  52. self.close_callback = function ()
  53. --切换角色之后,不需要预更新,等选角之后重新打开再更新加载图
  54. if self.is_change_role then
  55. self.is_change_role = false
  56. self.need_update_bg_in_open = true
  57. else
  58. self:UpdateLoadingBg() --预更新
  59. end
  60. self:Remove()
  61. self.open_callback_func = nil
  62. self.curr_progress_with_weight = {}
  63. GlobalEventSystem:Fire(EventName.PLAY_MAINUI_MOVIE_CENTER_EVENT)
  64. -- self.is_first_set_res = false
  65. end
  66. self.destroy_callback = function()
  67. self:DestroySuccess()
  68. end
  69. end
  70. function SceneLoadView:DestroySuccess( )
  71. if self.change_account_id then
  72. GlobalEventSystem:UnBind(self.change_account_id)
  73. self.change_account_id = nil
  74. end
  75. if self.change_role_id then
  76. GlobalEventSystem:UnBind(self.change_role_id)
  77. self.change_role_id = nil
  78. end
  79. if self.level_up_id then
  80. RoleManager.Instance.mainRoleInfo:UnBind(self.level_up_id)
  81. self.level_up_id = nil
  82. end
  83. end
  84. function SceneLoadView:Open(hide_load_progress,scene_id)
  85. self.open_guide_close = nil --此界面不销毁,需要每次都重置一下该状态
  86. self.hide_load_progress = hide_load_progress or false
  87. self.scene_id = scene_id or false
  88. self.is_show_effect = false
  89. SceneLoadView.super.Open(self)
  90. if self.need_update_bg_in_open then
  91. self.need_update_bg_in_open = false
  92. self:UpdateLoadingBg()
  93. end
  94. end
  95. function SceneLoadView:CheckResLoadTime()
  96. if self.check_close_event then
  97. GlobalTimerQuest:CancelQuest(self.check_close_event)
  98. self.check_close_event = nil
  99. end
  100. local check_close_func = function()
  101. if not self.scene_load_progress_finished then
  102. self.wait_res_load = true
  103. if not IsNull(self.showText) then
  104. self:SetLoadText(self.cur_value)
  105. end
  106. end
  107. self.check_close_event = nil
  108. end
  109. self.check_close_event = setTimeout(check_close_func,20)
  110. end
  111. function SceneLoadView:Remove()
  112. GlobalEventSystem:Fire(EventName.SCENELOADVIEWE_CLOSE)
  113. if self.anima then
  114. destroy(self.anima)
  115. self.anima = nil
  116. end
  117. if self.recording_bind_id then
  118. GlobalTimerQuest:CancelQuest(self.recording_bind_id)
  119. self.recording_bind_id = nil
  120. end
  121. self.scene_load_progress_finished = false
  122. self.auto_update_progress = 0
  123. self.cur_value = 0
  124. self.wait_res_load = false
  125. if not IsNull(self.showText) then
  126. self.showText.gameObject:SetActive(true)
  127. end
  128. if self.check_close_event then
  129. GlobalTimerQuest:CancelQuest(self.check_close_event)
  130. self.check_close_event = nil
  131. end
  132. self:StopSmoothTimer()
  133. self.last_progress_value = 0
  134. self.curr_progress_value = 0
  135. end
  136. function SceneLoadView:LoadSuccess()
  137. self.bg = self:GetChild("bg"):GetComponent("Image")
  138. -- self.bottom_conta = self:GetChild("bottom_conta")
  139. -- self.bottom_conta.sizeDelta = Vector2(SrcScreenWidth,self.bottom_conta.sizeDelta.y)
  140. -- self.bottom_conta.transform.localPosition = Vector3(-SrcScreenWidth / 2, -ScreenHeight / 2, 0)
  141. self.progressConta,
  142. self.showTextTran,
  143. self.bottom_conta
  144. = GetChildTransforms(self.transform, {
  145. "bg/bottom_conta/progressConta",
  146. "bg/bottom_conta/showText",
  147. "bg/bottom_conta"
  148. })
  149. self.showText = self:GetChild("bg/bottom_conta/showText"):GetComponent("TMP_Text")
  150. self.promptText = self:GetChild("bg/bottom_conta/promptText"):GetComponent("TMP_Text")
  151. self.defaultTitleText,
  152. self.defaultText
  153. = GetChildTexts(self.transform, {
  154. "bg/bottom_conta/defaultTitleText",
  155. "bg/bottom_conta/defaultText",
  156. })
  157. self.progress_bar = ProgressBarComponent.New(self.progressConta, 100, 0, 1150, 14, nil, "progressBg", "progressColor", {x = 0, y = 0}, false, false)
  158. -- if ClientConfig.iphone_x_model then
  159. -- local p = self.showText.transform.localPosition
  160. -- self.showText.transform.localPosition = Vector3(p.x, p.y, p.z)
  161. -- end
  162. if ClientConfig.iphone_x_model and SystemRuntimePlatform.IsIphone() then
  163. self.bottom_conta.transform.anchoredPosition = Vector3(0, 50 + ClientConfig.iphone_x_offset_bottom)
  164. end
  165. self.bg.transform.sizeDelta = Vector2(SrcScreenWidth, ScreenHeight)
  166. self.bg.transform.sizeDelta = Vector2(1560, 720)
  167. local function load_callback()
  168. Scene.Instance:SetFirstLoadBgFinish(true)
  169. local main_role = Scene.Instance.main_role
  170. if main_role then
  171. local pos_x = RoleManager.Instance.mainRoleInfo.pos_x
  172. local pos_y = RoleManager.Instance.mainRoleInfo.pos_y
  173. main_role:SetRealPos(pos_x,pos_y, true)
  174. end
  175. end
  176. self:UpdateLoadingBg(load_callback)
  177. end
  178. function SceneLoadView:InitEvent()
  179. local function change_account()
  180. self.is_change_role = true
  181. self.last_role_level = false
  182. end
  183. self.change_account_id = GlobalEventSystem:Bind(EventName.CHANGE_ACCOUNT, change_account)
  184. self.change_role_id = GlobalEventSystem:Bind(EventName.CHANGE_ROLE, change_account)
  185. self:SetDefaultTxt()
  186. local function on_change_level(lv)
  187. self.last_role_level = self.last_role_level or lv
  188. --限制频率
  189. if self.last_role_level and lv - self.last_role_level > 5 then
  190. --只在隐藏的时候更新
  191. if not IsNull(self.gameObject) and not self.gameObject.activeSelf then
  192. self:UpdateLoadingBg()
  193. self.last_role_level = lv
  194. end
  195. end
  196. end
  197. self.level_up_id = RoleManager.Instance.mainRoleInfo:Bind(EventName.CHANGE_LEVEL, on_change_level)
  198. end
  199. function SceneLoadView:OpenFun()
  200. -- lua_resM:clearReference(self.temp_res_target)
  201. GlobalEventSystem:Fire(SceneEventType.SCENE_LOAD_VIEW_HASOPEN)
  202. self.is_first_show = false
  203. self:Remove()
  204. self:SetProgressValue(0, 100)
  205. if self.open_callback_func then
  206. self.open_callback_func()
  207. self.open_callback_func = nil
  208. end
  209. end
  210. function SceneLoadView:UpdateLoadingBg( call_back )
  211. local index = SceneManager:getInstance():CheckMatchLoadingRes()
  212. local res_name = index .. ".jpg"
  213. local ab_name = "icon_sceneload_" .. index
  214. local function load_finish( )
  215. if call_back then
  216. call_back()
  217. end
  218. end
  219. lua_resM:setImageSprite(self.temp_res_target, self.bg, ab_name, res_name, nil, load_finish, ASSETS_LEVEL.HIGHT)
  220. end
  221. function SceneLoadView:SetOpenCallBackFunc(call_back)
  222. self.open_callback_func = call_back
  223. end
  224. function SceneLoadView:AutoToShow(last_time, need_bind_event)
  225. local has_bind_back = false
  226. local progress_back = false
  227. if need_bind_event then
  228. local bind_id
  229. local on_back = function(...)
  230. has_bind_back = true
  231. if progress_back then
  232. has_bind_back = false
  233. self:Close()
  234. end
  235. GlobalEventSystem:UnBind(bind_id)
  236. end
  237. bind_id = GlobalEventSystem:Bind(need_bind_event, on_back)
  238. else
  239. has_bind_back = true
  240. end
  241. local finish = function()
  242. progress_back = true
  243. if has_bind_back then
  244. progress_back = false
  245. self:Close()
  246. end
  247. end
  248. if not self.auto_show_id then
  249. local inter = 0.05
  250. local per_add = 100 / (last_time / inter)
  251. local auto_show_progress = 0
  252. self:Open()
  253. local onTimer = function()
  254. auto_show_progress = auto_show_progress + per_add
  255. self:SetProgressValue(auto_show_progress, 100)
  256. if auto_show_progress >= 100 then
  257. GlobalTimerQuest:CancelQuest(self.auto_show_id)
  258. self.auto_show_id = nil
  259. finish()
  260. end
  261. end
  262. self.auto_show_id = GlobalTimerQuest:AddPeriodQuest(onTimer, inter, -1)
  263. end
  264. end
  265. function SceneLoadView:AddLoadTimer()
  266. if self.recording_bind_id == nil then
  267. local time = 0
  268. local function onTimer()
  269. time = time + 0.05
  270. if not self.trigger_story and time >= self.progress_time - 0.3 then
  271. self.trigger_story = true
  272. GlobalEventSystem:Fire(EventName.TRIGGER_STORY_START)
  273. end
  274. if time <= self.progress_time then
  275. local add = 5 / self.progress_time
  276. self:AutoUpdateProgress(add)
  277. else
  278. GlobalTimerQuest:CancelQuest(self.recording_bind_id)
  279. self.recording_bind_id = nil
  280. self.trigger_story = nil
  281. self.scene_load_progress_finished = true
  282. self.progress_time = 3
  283. end
  284. end
  285. onTimer()
  286. self.recording_bind_id = GlobalTimerQuest:AddPeriodQuest(onTimer, 0.05, -1)
  287. end
  288. end
  289. --真实的进度条 weight权重
  290. function SceneLoadView:RealUpdateProgress(curr_value, max_value, load_type)
  291. if not self.isPop then return end
  292. if load_type == nil then return end
  293. curr_value = curr_value or 0
  294. max_value = max_value or 100
  295. if max_value == 0 then max_value = 100 end
  296. self.curr_progress_with_weight[load_type.type] = math.floor(curr_value / max_value * 100) * load_type.weight
  297. -- self:AutoUpdateProgress()
  298. end
  299. function SceneLoadView:AutoUpdateProgress()
  300. local value = 0
  301. for t, v in pairs(self.curr_progress_with_weight) do
  302. value = value + v
  303. end
  304. if self.is_loaded then
  305. if value > self.curr_progress_value then
  306. self:SetProgressValue(value, 100)
  307. end
  308. -- self:SetLoadText(value)
  309. end
  310. if value >= 100 then
  311. self.trigger_story = nil
  312. self.scene_load_progress_finished = true
  313. end
  314. end
  315. function SceneLoadView:SetProgressValue(value, max_value)
  316. self.curr_progress_value = value
  317. self.progress_bar:SetValue(value, max_value)
  318. self:SetLoadText(value)
  319. if value >= 0.1 and not self.is_show_effect then
  320. self.is_show_effect = true
  321. if self.effectCon then
  322. self.effectCon.gameObject:SetActive(true)
  323. end
  324. end
  325. end
  326. --设置底部的默认提示信息
  327. function SceneLoadView:SetLoadText(value)
  328. self.cur_value = value
  329. if not self.wait_res_load then
  330. self.showText.text = "加载中 " .. HtmlColorTxt(math.floor(value) .. "%", "#FFFFFF")
  331. else
  332. self.promptText.text = "预先加载场景资源可以让游戏体验更好哦(" .. math.floor(value) .. "%)"
  333. end
  334. end
  335. --设置底部的默认提示信息
  336. function SceneLoadView:SetDefaultTxt()
  337. self.defaultTitleText.text = "健康提示忠告"
  338. self.defaultText.text = "抵制不良游戏 拒绝盗版游戏 注意自我保护 谨防受骗上当 适度游戏益脑 沉迷游戏伤身 合理安排时间 享受健康生活"
  339. end
  340. function SceneLoadView:SetPromptTxt()
  341. if self.wait_res_load then
  342. return
  343. end
  344. --math.randomseed(os.time())
  345. local cfg = Config.ConfigLoadView.LoadViewPromptMsg
  346. local str = ""
  347. local index = math.random(1, #cfg)
  348. if cfg and cfg[index] then
  349. str = cfg[index]
  350. end
  351. self.promptText.text = str
  352. end
  353. function SceneLoadView:StopSmoothTimer( )
  354. if self.timer_id then
  355. GlobalTimerQuest:CancelQuest(self.timer_id)
  356. self.timer_id = nil
  357. end
  358. end
  359. function SceneLoadView:StartSmoothTimer( step_time )
  360. self:StopSmoothTimer()
  361. self.smooth_max_value = 10
  362. local function onTimer()
  363. if self.curr_progress_value >= 100 then
  364. self:StopSmoothTimer()
  365. self.scene_load_progress_finished = true
  366. return
  367. end
  368. if self.curr_progress_value > self.smooth_max_value then
  369. local value = 0
  370. for t, v in pairs(self.curr_progress_with_weight) do
  371. value = value + v
  372. end
  373. --未加载完,停在99
  374. if self.curr_progress_value > 98 and value < 100 then
  375. return
  376. end
  377. --已经全部加载完了,则提高进度条加载速度
  378. if value >= 100 and self.logic_step_time == self.real_step_time then
  379. local faster_step = self.logic_step_time / 5
  380. self:StartSmoothTimer(faster_step)
  381. end
  382. end
  383. if self.last_progress_value == self.curr_progress_value then
  384. self.curr_progress_value = self.curr_progress_value + 1
  385. self:SetProgressValue(self.curr_progress_value,100)
  386. end
  387. self.last_progress_value = self.curr_progress_value
  388. end
  389. self.logic_step_time = 0.08
  390. if SystemMemoryLevel.Cur == SystemMemoryLevel.Hight then
  391. self.logic_step_time = 0.06
  392. elseif SystemMemoryLevel.Cur == SystemMemoryLevel.Middle then
  393. self.logic_step_time = 0.07
  394. end
  395. if SystemRuntimePlatform.IsWindows() then
  396. self.logic_step_time = 0.01
  397. end
  398. self.real_step_time = step_time or self.logic_step_time
  399. self.timer_id = GlobalTimerQuest:AddPeriodQuest(onTimer, self.real_step_time, -1)
  400. end