KLGuideManager.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. ---@class KLGuideManager
  2. KLGuideManager = {}
  3. local this = KLGuideManager
  4. function this.Init()
  5. this.showPanelList = {}
  6. this.now_guide_id_list = {}
  7. this.now_guide_next_list = {}
  8. this.rear_guide_list = {}
  9. this.first_guide_list = {}
  10. this.now_guide_to_first_guide = {}
  11. this.now_guide_to_pre_guide = {}
  12. this.career_last_guide = {}
  13. this.InitData()
  14. this.RegistEvents()
  15. end
  16. function this.RegistEvents()
  17. SL:RegisterLUAEvent(LUA_EVENT_OPEN_UI,this.LUA_EVENT_OPEN_UI)
  18. SL:RegisterLUAEvent(LUA_EVENT_MAIN_UI_VISIBLE_AFTER,this.LUA_EVENT_MAIN_UI_VISIBLE_AFTER)
  19. SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN,this.LUA_EVENT_CLOSEWIN)
  20. SL:RegisterLUAEvent(LUA_EVENT_TASK_NEW,this.LUA_EVENT_TASK_NEW)
  21. SL:RegisterLUAEvent(LUA_EVENT_TASK_FINISH,this.LUA_EVENT_TASK_FINISH)
  22. SL:RegisterLUAEvent(LUA_EVENT_LEVELCHANGE,this.LUA_EVENT_LEVELCHANGE)
  23. SL:RegisterLUAEvent(LUA_EVENT_ENTER_MAP,this.LUA_EVENT_ENTER_MAP)
  24. --SL:RegisterLUAEvent(LUA_EVENT_CHANGESCENE,this.LUA_EVENT_CHANGESCENE)
  25. SL:RegisterLUAEvent(LUA_EVENT_BAG_CHANGE_AFTER,this.LUA_EVENT_BAG_CHANGE_AFTER)
  26. --SL:RegisterLUAEvent(LUA_EVENT_BAG_CHANGE_AFTER,this.LUA_EVENT_BAG_CHANGE_AFTER)
  27. end
  28. function this:LUA_EVENT_BAG_CHANGE_AFTER()
  29. this.UpdateGuide()
  30. end
  31. function this:LUA_EVENT_CHANGESCENE()
  32. this.UpdateGuide()
  33. end
  34. function this:LUA_EVENT_ENTER_MAP()
  35. this.Reset()
  36. end
  37. function this:LUA_EVENT_TASK_NEW()
  38. this.UpdateGuide()
  39. end
  40. function this:LUA_EVENT_TASK_FINISH()
  41. this.UpdateGuide()
  42. end
  43. function this:LUA_EVENT_LEVELCHANGE()
  44. this.UpdateGuide()
  45. end
  46. function this.InitData()
  47. local rookie_guide_tbl = SL:GetConfigTable('cfg_rookie_guide')
  48. for _, rookieInfo in pairs(rookie_guide_tbl) do
  49. local rear_id = rookieInfo["rearGuide"]
  50. if rear_id then
  51. table.insert(this.rear_guide_list, rear_id)
  52. end
  53. end
  54. for _, rookieInfo in pairs(rookie_guide_tbl) do
  55. local now_id = rookieInfo["id"]
  56. if not table.contains(this.rear_guide_list, now_id) then
  57. table.insert(this.first_guide_list, rookieInfo)
  58. end
  59. end
  60. -- 保存每个引导的起始引导
  61. for _, rookieInfo in pairs(this.first_guide_list) do
  62. local nowId = rookieInfo["id"]
  63. local rearId = rookieInfo["rearGuide"]
  64. this.now_guide_to_first_guide[nowId] = nowId
  65. while rearId ~= 0 do
  66. this.now_guide_to_first_guide[rearId] = nowId
  67. rearId = SL:GetConfig('cfg_rookie_guide', rearId).rearGuide
  68. end
  69. end
  70. -- 保存当前引导的上一个引导
  71. for _, rookieInfo in pairs(rookie_guide_tbl) do
  72. local nowId = rookieInfo["id"]
  73. local rearId = rookieInfo["rearGuide"]
  74. if rearId ~= 0 then
  75. this.now_guide_to_pre_guide[rearId] = nowId
  76. end
  77. end
  78. -- 获取每个职业的最后一个引导
  79. for _, rookieInfo in pairs(rookie_guide_tbl) do
  80. local sequence = rookieInfo["sequence"]
  81. for i, v in pairs(sequence) do
  82. if this.career_last_guide[v[1]] == nil then
  83. this.career_last_guide[v[1]] = {sort = v[2],id = rookieInfo.id}
  84. else
  85. if this.career_last_guide[v[1]].sort < v[2] then
  86. this.career_last_guide[v[1]] = {sort = v[2],id = rookieInfo.id}
  87. elseif this.career_last_guide[v[1]].sort == v[2] and this.career_last_guide[v[1]].id < rookieInfo.id then
  88. this.career_last_guide[v[1]] = {sort = v[2],id = rookieInfo.id}
  89. end
  90. end
  91. end
  92. end
  93. end
  94. function this.getMaxGuideId()
  95. if this.career_last_guide then
  96. local job = SL:GetMetaValue(EMetaVarGetKey.JOB)
  97. if this.career_last_guide[0].sort > this.career_last_guide[job].sort then
  98. return this.career_last_guide[0].id
  99. else
  100. return this.career_last_guide[job].id
  101. end
  102. end
  103. end
  104. function this.addRookieGuide(guideId)
  105. if this.showPanelList[guideId] == nil then
  106. if this.CheckUiIsActive(guideId) then
  107. this.showPanelList[guideId] = {}
  108. ---@type KLRookieGuidePanel
  109. GUI:UIPanel_Open("dev/outui/Guide/Panel/KLRookieGuide/KLRookieGuidePanel",nil, nil, {guideId=guideId}, true,
  110. function(panel)
  111. this.showPanelList[guideId] = panel
  112. end)
  113. if not table.contains(this.now_guide_id_list, guideId) then
  114. table.insert(this.now_guide_id_list, guideId)
  115. end
  116. end
  117. end
  118. end
  119. function this.CheckRookieGuide(guideId)
  120. if this.showPanelList[guideId] == nil then
  121. if this.CheckUiIsActive(guideId) then
  122. this.showPanelList[guideId] = {}
  123. ---@type KLRookieGuidePanel
  124. GUI:UIPanel_Open("dev/outui/Guide/Panel/KLRookieGuide/KLRookieGuidePanel",nil, nil, {guideId=guideId}, true,
  125. function(panel)
  126. this.showPanelList[guideId] = panel
  127. end)
  128. if not table.contains(this.now_guide_id_list, guideId) then
  129. table.insert(this.now_guide_id_list, guideId)
  130. end
  131. end
  132. end
  133. end
  134. function this:LUA_EVENT_MAIN_UI_VISIBLE_AFTER(info)
  135. local vis = info.vis
  136. local name = info.name
  137. if vis then
  138. SL:ScheduleOnce(Time.deltaTime,function()
  139. this.UiShowGuide(name)
  140. end)
  141. --Coroutine.Start(this.UiShowGuide,name)
  142. else
  143. this.UiHide(name)
  144. end
  145. end
  146. ---@param win UIKmlLuaPanelBase
  147. function this:LUA_EVENT_OPEN_UI(win)
  148. if win then
  149. local openPanelName = win.panelName
  150. SL:ScheduleOnce(Time.deltaTime,function()
  151. this.UiShowGuide(openPanelName)
  152. end)
  153. --Coroutine.Start(this.UiShowGuide,openPanelName)
  154. --this.UiShowGuide(openPanelName)
  155. end
  156. end
  157. function this.UiShowGuide(name)
  158. --Coroutine.Wait(0.1)
  159. --Coroutine.WaitForEndOfFrame()
  160. local already_first_guide_list = this.GetFirstGuideIdList()
  161. for _, rookie_info in pairs(this.first_guide_list) do
  162. local nowId = rookie_info["id"]
  163. if not table.contains(already_first_guide_list, nowId) then
  164. local conditions = rookie_info["conditions"]
  165. local panel_info = string.split(rookie_info["uiAddress"], '&')
  166. local panel_id = panel_info[1]
  167. if panel_id == name then
  168. if SL:CheckCondition(conditions) then
  169. this.addRookieGuide(nowId)
  170. end
  171. end
  172. end
  173. end
  174. for _, nowId in pairs(this.now_guide_next_list) do
  175. local rookie_info = SL:GetConfig('cfg_rookie_guide', nowId)
  176. if not table.contains(already_first_guide_list, nowId) then
  177. local conditions = rookie_info["conditions"]
  178. local panel_info = string.split(rookie_info["uiAddress"], '&')
  179. local panel_id = panel_info[1]
  180. if panel_id == name then
  181. if SL:CheckCondition(conditions) then
  182. this.CheckRookieGuide(nowId)
  183. end
  184. end
  185. end
  186. end
  187. end
  188. function this.HideGuide(guideId)
  189. local panel = this.showPanelList[guideId]
  190. if panel then
  191. GUI:UIPanel_Close(nil, panel)
  192. this.showPanelList[guideId] = nil
  193. end
  194. end
  195. function this:LUA_EVENT_CLOSEWIN(panelName)
  196. this.UiHide(panelName)
  197. end
  198. function this.UiHide(panelName)
  199. if not next(this.now_guide_next_list) and not next(this.now_guide_id_list) then
  200. return
  201. end
  202. for index, guideId in pairs(this.now_guide_id_list) do
  203. local rookie_info = SL:GetConfig('cfg_rookie_guide', guideId)
  204. local panel_info = string.split(rookie_info["uiAddress"], '&')
  205. local panel_id = panel_info[1]
  206. if panel_id == panelName then
  207. this.HideGuide(guideId)
  208. this.now_guide_id_list[index] = nil
  209. end
  210. end
  211. for index, guideId in pairs(this.now_guide_next_list) do
  212. local rookie_info = SL:GetConfig('cfg_rookie_guide', guideId)
  213. local panel_info = string.split(rookie_info["uiAddress"], '&')
  214. local panel_id = panel_info[1]
  215. if panel_id == panelName then
  216. this.HideGuide(guideId)
  217. this.now_guide_next_list[index] = nil
  218. end
  219. end
  220. end
  221. -- 获取正在引导的首个组id列表
  222. function this.GetFirstGuideIdList()
  223. local resultList = {}
  224. for _, guideId in pairs(this.now_guide_id_list) do
  225. table.insert(resultList, guideId)
  226. end
  227. for _, guideId in pairs(this.now_guide_next_list) do
  228. local firstId = this.now_guide_to_first_guide[guideId]
  229. if firstId then
  230. table.insert(resultList, firstId)
  231. end
  232. end
  233. return resultList
  234. end
  235. function this.CheckUiIsActive(guideId)
  236. local rookieCfg = SL:GetConfig('cfg_rookie_guide', guideId)
  237. local address = rookieCfg.uiAddress
  238. local panelId
  239. if address then
  240. local contrTransform
  241. -- 弹出的特殊处理
  242. if table.contains(rookieCfg.showType, EGuideShowType.ToProp) then
  243. contrTransform = GUI:GetPanelTransform(address)
  244. panelId = address
  245. else
  246. local addrSplit = string.split(address, '&')
  247. panelId = addrSplit[1]
  248. local contrName = addrSplit[2]
  249. contrTransform = GUI:GetControlTransform(panelId, contrName)
  250. end
  251. if MainUiStateManager.showPanelMap[panelId] ~= nil then
  252. return MainUiStateManager.showPanelMap[panelId]
  253. end
  254. if not contrTransform then
  255. return false
  256. end
  257. return contrTransform.gameObject.activeSelf
  258. end
  259. return false
  260. end
  261. -- 更新引导
  262. function this.UpdateGuide()
  263. for index, guideId in pairs(this.now_guide_id_list) do
  264. local rookieCfg = SL:GetConfig('cfg_rookie_guide', guideId)
  265. local conditions = rookieCfg["conditions"]
  266. if not SL:CheckCondition(conditions) then
  267. this.HideGuide(guideId)
  268. this.now_guide_id_list[index] = nil
  269. end
  270. end
  271. for index, guideId in pairs(this.now_guide_next_list) do
  272. local rookieCfg = SL:GetConfig('cfg_rookie_guide', guideId)
  273. local conditions = rookieCfg["conditions"]
  274. if not SL:CheckCondition(conditions) then
  275. this.HideGuide(guideId)
  276. this.now_guide_next_list[index] = nil
  277. end
  278. end
  279. local already_first_guide_list = this.GetFirstGuideIdList()
  280. for _, guideInfo in pairs(this.first_guide_list) do
  281. local guideId = guideInfo["id"]
  282. if not table.contains(already_first_guide_list, guideId) then
  283. if SL:CheckCondition(guideInfo["conditions"]) then
  284. this.addRookieGuide(guideId)
  285. end
  286. end
  287. end
  288. end
  289. function this.SetNextGuide(rearId)
  290. local preId = this.now_guide_to_pre_guide[rearId]
  291. if preId then
  292. if table.contains(this.now_guide_id_list, preId) then
  293. this.HideGuide(preId)
  294. table.removeByValue(this.now_guide_id_list, preId)
  295. table.insert(this.now_guide_next_list, rearId)
  296. --Coroutine.Start(function()
  297. -- Coroutine.WaitForEndOfFrame()
  298. -- this.CheckRookieGuide(rearId)
  299. --end)
  300. SL:ScheduleOnce(Time.deltaTime,function()
  301. this.CheckRookieGuide(rearId)
  302. end)
  303. elseif table.contains(this.now_guide_next_list, preId) then
  304. this.HideGuide(preId)
  305. table.removeByValue(this.now_guide_next_list, preId)
  306. table.insert(this.now_guide_next_list, rearId)
  307. --Coroutine.Start(function()
  308. -- Coroutine.WaitForEndOfFrame()
  309. -- this.CheckRookieGuide(rearId)
  310. --end)
  311. SL:ScheduleOnce(Time.deltaTime,function()
  312. this.CheckRookieGuide(rearId)
  313. end)
  314. end
  315. end
  316. end
  317. function this.AddPointControlGuide(panelId, contrId, guideId)
  318. local panel = this.showPanelList[guideId]
  319. if panel then
  320. panel:AddPointControlGuide(panelId, contrId, guideId)
  321. end
  322. end
  323. function this.HidePointControlGuide(guideId)
  324. local panel = this.showPanelList[guideId]
  325. if panel then
  326. panel:HidePointControlGuide()
  327. end
  328. end
  329. function this.Reset()
  330. for guideId, showPanel in pairs(this.showPanelList) do
  331. this.HideGuide(guideId)
  332. end
  333. this.showPanelList = {}
  334. this.now_guide_id_list = {}
  335. this.now_guide_next_list = {}
  336. end
  337. return this