KLMiniMapPanel.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. ---@class KLMiniMapPanel:UIKmlLuaPanelBase
  2. ---@field view KLMiniMapPanelView
  3. local KLMiniMapPanel = class(UIKmlLuaPanelBase)
  4. local this =KLMiniMapPanel
  5. ---创建时调用一次
  6. function this:Init()
  7. --self.sceneImgWidth = self.view.img_scene:GetWidth()
  8. --self.sceneImgHeight = self.view.img_scene:GetHeight()
  9. self.sceneImgWidth,self.sceneImgHeight = GUI:getSizeDelta(self.view.img_scene)
  10. ---@type table<string,UIKmlLuaPanelBase>
  11. self.iconMap = {}
  12. self.TransPoint = {}
  13. self.IsTrans=false
  14. self.MonsterPointMinPool = TablePool() ---小地图
  15. self.monsterPointMinTempList = {}---小地图temp列表保存
  16. self.mapIconVisibleInfo = {}
  17. local s = SL:GetConfig("cfg_global",280).value
  18. local strs = string.split(s,'#')
  19. self.monsterCheckN =tonumber(strs[1])
  20. self.monsterLimitCount =tonumber(strs[2])
  21. end
  22. function this:Show()
  23. if SL:GetIsWebGL() then
  24. GUI:Text_setTextAlignment(self.view.txt_title,"21")
  25. else
  26. GUI:Text_setTextAlignment(self.view.txt_title,"01")
  27. end
  28. end
  29. ---创建或者刷新界面数据时调用
  30. function this:Refresh()
  31. self:OnPlayerMapPosChange()
  32. self:OnPlayerTurnDir(_,SL:GetMetaValue(EMetaVarGetKey.DIR))
  33. self:SetTransferIcon()
  34. self:SetNpcIcon()
  35. local mapName = SL:GetMetaValue(EMetaVarGetKey.MAP_NAME)
  36. local mapLine = SL:GetMetaValue(EMetaVarGetKey.MAP_LINE)
  37. ---@type cfg_map_info_column
  38. local mapInfo = SL:GetConfig("cfg_map_info", SL:GetMetaValue(EMetaVarGetKey.MAP_ID))
  39. if mapInfo and mapInfo.nopositionmove == 0 then
  40. GUI:Text_setString(self.view.txt_title,string.format("%s<color=#fbd994>[%s线]</color>",mapName,mapLine))
  41. else
  42. GUI:Text_setString(self.view.txt_title,string.format("%s",mapName))
  43. end
  44. self.TransPoint = SL:GetAllTransferPointById()
  45. self:ShowMonsterPointMin()
  46. if mapInfo and mapInfo.nopositionmove == 0 then
  47. GUI:setVisible(self.view.btn_switch_line, true)
  48. else
  49. GUI:setVisible(self.view.btn_switch_line, false)
  50. end
  51. self.timeId = SL:Schedule(self.timeId,0,1,-1,function()
  52. self:ShowMonsterPointMinUpdate()
  53. end)
  54. end
  55. ---注册UI事件和服务器消息
  56. function this:RegistEvents()
  57. SL:RegisterLUAEvent(LUA_EVENT_FIGHTEND,self.LUA_EVENT_FIGHTEND,self)
  58. SL:RegisterLUAEvent(LUA_EVENT_MAPINFOCHANGE,self.LUA_EVENT_NPC_LEAVE_NEAR,self) --切换不同地图
  59. SL:RegisterLUAEvent(LUA_EVENT_PLAYER_TURN_DIR,self.OnPlayerTurnDir,self)
  60. SL:RegisterLUAEvent(LUA_EVENT_MAPINFOCHANGE,self.MapEventChange,self)
  61. SL:RegisterLUAEvent(LUA_EVENT_PLAYER_MAPPOS_CHANGE,self.OnPlayerMapPosChange,self)
  62. GUI:AddOnClickEvent(self.view.btn_switch_line,self,self.BtnSwitchLineOnClick)
  63. GUI:AddOnClickEvent(self.view.btn_bg,self,self.BtnBgOnClick)
  64. SL:RegisterLuaNetMsg(MessageDef.ResUpdateViewMessage, self.ShowMonsterPointMin, self)
  65. SL:RegisterLuaNetMsg(MessageDef.ResMonsterEnterViewMessage, self.ShowMonsterPointMin, self)
  66. SL:RegisterLuaNetMsg(MessageDef.ResWalkMessage, self.ResWalkMessage, self)
  67. SL:RegisterLuaNetMsg(MessageDef.ResFightResultMessage, self.ResFightResultMessage, self)
  68. end
  69. function this:LUA_EVENT_FIGHTEND(id,data)
  70. self.IsTrans = false
  71. end
  72. function this:LUA_EVENT_NPC_LEAVE_NEAR(_,dir)
  73. self.IsTrans = false
  74. self.TransPoint = SL:GetAllTransferPointById()
  75. end
  76. function this:OnPlayerTurnDir(_,dir)
  77. GUI:setRotation(self.view.img_player_icon,(dir+1)*-45)
  78. end
  79. function this:MapEventChange()
  80. for k, _ in pairs(self.iconMap) do
  81. self:RemoveIcon(k)
  82. end
  83. self:Refresh()
  84. end
  85. function this:OnPlayerMapPosChange()
  86. local mapFile = SL:GetMetaValue(EMetaVarGetKey.MINIMAP_FILE)
  87. GUI:Image_loadTexture(self.view.img_scene,string.format("Texture/%s.png",mapFile))
  88. ---@type Dot2
  89. local pos = SL:GetMetaValue(EMetaVarGetKey.MAP_PLAYER_POS)
  90. local x = pos.x/256*self.sceneImgWidth - self.sceneImgWidth/2
  91. local z = pos.z/256*self.sceneImgHeight - self.sceneImgHeight/2
  92. GUI:setPosition(self.view.img_scene,-x,-z)
  93. GUI:Text_setString(self.view.txt_coord,pos.x..","..pos.z)
  94. self:JudgeMapTransfer()
  95. end
  96. function this:JudgeMapTransfer()
  97. if #self.TransPoint == 0 then
  98. self.TransPoint = SL:GetAllTransferPointById()
  99. if #self.TransPoint == 0 then
  100. return
  101. end
  102. end
  103. if self.IsTrans == false then
  104. for i, v in pairs(self.TransPoint) do
  105. for j, k in pairs(EDirectionToDot2) do
  106. local disx = math.abs(v.point.position[1] - SL:MeData_GetCoord().x)
  107. local disz = math.abs(v.point.position[2] - SL:MeData_GetCoord().z)
  108. if math.sqrt(disx * disx + disz * disz) <= v.dis then
  109. if v.point.condition then
  110. local canTransfer = SL:CheckCalculateConditionGroup(v.point.condition)
  111. if not canTransfer then
  112. local condition = SL:Split(v.point.condition, "=")
  113. local str = "等级不满足,"..condition[2].."等级后开启"
  114. SL:TipMessage( str, 1, NoticeType.NoticeMid )
  115. return
  116. end
  117. end
  118. local cfgTable = SL:GetConfigTable('cfg_mapMove')
  119. ---@param e cfg_mapMove_column
  120. for q, e in pairs(cfgTable) do
  121. if tonumber(e.mapID) == v.point.targetId then
  122. self.TransMapId = v.point.id
  123. self.IsTrans = true
  124. self:SetTrans()
  125. return
  126. end
  127. end
  128. end
  129. end
  130. end
  131. end
  132. end
  133. function this:SetTrans()
  134. Coroutine.Start(self.ShowTransAni,self)
  135. end
  136. function this:ShowTransAni()
  137. if not SL:Me_GetSellerIsCanMove() then
  138. return
  139. end
  140. self.timeCount=0
  141. --self:PlayTransAni()
  142. SL:SendTransferPointMessage(self.TransMapId)
  143. self.IsTrans=false
  144. end
  145. function this:SetMapIconVisible(id,visible)
  146. self.mapIconVisibleInfo[id] = visible and "1" or "0"
  147. if self.iconMap[id] then
  148. GUI:setVisible(self.iconMap[id].view.root, visible)
  149. end
  150. end
  151. ---@param id string
  152. ---@param item UIKmlLuaPanelBase
  153. function this:SetMapIcon(id, item, x, y)
  154. if self.iconMap[id] then
  155. GUI:UIPanel_Close(_,self.iconMap[id])
  156. self.iconMap[id] = nil
  157. end
  158. --GUI:SetParent(icon,self.view.scene_group)
  159. --item.kmlControl:SetParent(self.view.img_scene.kmlControl)
  160. local miniMapX=x/256*self.sceneImgWidth-self.sceneImgWidth/2
  161. local miniMapY=y/256*self.sceneImgHeight-self.sceneImgHeight/2
  162. GUI:setPosition(item.view.root,miniMapX,miniMapY)
  163. self.iconMap[id] = item
  164. if self.mapIconVisibleInfo[id] then
  165. GUI:setVisible(item.view.root,self.mapIconVisibleInfo[id] == "1")
  166. end
  167. end
  168. ---@param id string
  169. function this:RemoveIcon(id)
  170. if self.iconMap[id] then
  171. GUI:UIPanel_Close(_,self.iconMap[id])
  172. self.iconMap[id] = nil
  173. end
  174. end
  175. ---@设置传送点图标
  176. function this:SetTransferIcon()
  177. local mapId = SL:GetMetaValue(EMetaVarGetKey.MAP_ID)
  178. ---@type cfg_transfer_point_column[]
  179. local transferPoint = SL:FindConfigs('cfg_transfer_point', 'mapId', mapId)
  180. if transferPoint then
  181. for _,v in pairs(transferPoint) do
  182. local transferIcon = GUI:UIPanel_Open("dev/ui/Map/Item/KLMapTransferIcon/KLMapTransferIconItem",self.view.img_scene,self,v,true)
  183. self:SetMapIcon("MapTransferIcon" .. v.id,transferIcon,v.position[1],v.position[2])
  184. end
  185. end
  186. end
  187. function this:SetNpcIcon()
  188. local mapId = SL:GetMetaValue(EMetaVarGetKey.MAP_ID)
  189. ---@type cfg_npclist_column[]
  190. local npcList = SL:FindConfigs('cfg_npclist', 'mapId', mapId)
  191. if npcList then
  192. for _,npcTbl in pairs(npcList) do
  193. local npcIcon = GUI:UIPanel_Open("dev/ui/Map/Item/KLMapNpcIcon/KLMapNpcIconItem",self.view.img_scene,self,{npcTbl,false},true)
  194. self:SetMapIcon("MapNpcIcon" .. npcTbl.id,npcIcon,npcTbl.x,npcTbl.y)
  195. end
  196. end
  197. end
  198. function this:BtnSwitchLineOnClick()
  199. SL.HideMainPanel()
  200. GUI:UIPanel_Open("dev/ui/Map/Panel/KLUIMapBranch/KLUIMapBranchPanel")
  201. end
  202. function this:BtnBgOnClick()
  203. SL:ShowBigMap()
  204. end
  205. ---设置怪物点---------------------------------------------------------------
  206. function this:ShowMonsterPointMinUpdate()
  207. if not self.showMonsterPointMinUpdateTime or Time.time-self.showMonsterPointMinUpdateTime<1 then
  208. return
  209. end
  210. self.showMonsterPointMinUpdateTime = Time.time
  211. if not self.needRemove then
  212. self.needRemove = {}
  213. end
  214. table.clear(self.needRemove)
  215. if not self.needAdd then
  216. self.needAdd = {}
  217. end
  218. table.clear(self.needAdd)
  219. if not self.tempShowMonster then
  220. self.tempShowMonster = {}
  221. end
  222. table.clear(self.tempShowMonster)
  223. local showMonster = SL:GetMetaValue("GET_MONSTER_DATA_LIST")
  224. local maxDist = 0
  225. local curShowCount = 0
  226. local meX,meZ = SL:MeData_Pos()
  227. for k, v in pairs(showMonster) do
  228. local ax = Mathf.Abs(v.pos.x-meX)
  229. local az = Mathf.Abs(v.pos.z-meZ)
  230. if (ax>0 or az>0) and ax<=self.monsterCheckN and az<=self.monsterCheckN then
  231. if curShowCount<self.monsterLimitCount then
  232. self.tempShowMonster[v.data.id] = v
  233. curShowCount=curShowCount+1
  234. end
  235. end
  236. end
  237. for k, v in pairs(self.tempShowMonster) do
  238. local id = v.data.id
  239. if not self.monsterPointMinTempList[id] then
  240. self.needAdd[id] = v
  241. end
  242. end
  243. for k, v in pairs(self.monsterPointMinTempList) do
  244. local id = k
  245. if not self.tempShowMonster[id] then
  246. self.needRemove[id] = v
  247. end
  248. end
  249. for k, v in pairs(self.needRemove) do
  250. local id = k
  251. self.monsterPointMinTempList[id] = nil
  252. v:SetPanelXY(-3000,-3000)
  253. self.MonsterPointMinPool:Push(v)
  254. end
  255. for k, v in pairs(self.monsterPointMinTempList) do
  256. ---@type KLMapMonsterPointItem
  257. local monsterPointTemp = v
  258. local monster = SL:GetMetaValue("GET_MONSTER_DATA_BY_ID",k)
  259. if monster then
  260. local x = monster.pos.x / 256 * self.sceneImgWidth - self.sceneImgWidth / 2
  261. local y = monster.pos.z / 256 * self.sceneImgHeight - self.sceneImgHeight / 2
  262. if monsterPointTemp.x~=x or monsterPointTemp.y~=y then
  263. monsterPointTemp:SetPanelXY(x, y)
  264. end
  265. end
  266. end
  267. --local limitCount = 10
  268. --local index = 1
  269. for i, monster in pairs(self.needAdd) do
  270. --if index>limitCount then
  271. -- break
  272. --end
  273. --index =index+1
  274. ---@type KLMapMonsterPointItem
  275. local monsterPointTemp = self.MonsterPointMinPool:Pop()
  276. if not next(monsterPointTemp) then
  277. monsterPointTemp = GUI:UIPanel_Open("dev/ui/Map/Item/KLMapMonsterPoint/KLMapMonsterPointItem", self.view.img_scene, self, nil, true)
  278. end
  279. --if not string.contains(monsterPointTemp.view.root.kmlControl.rectTransform.name,monsterPointTemp.panelName) then
  280. -- SL:LogError("dddddddddddddddddddddddd")
  281. --end
  282. self.monsterPointMinTempList[monster.data.id] = monsterPointTemp
  283. -- monsterPointTemp:SetActiveUI(true)
  284. local x = monster.pos.x / 256 * self.sceneImgWidth - self.sceneImgWidth / 2
  285. local y = monster.pos.z / 256 * self.sceneImgHeight - self.sceneImgHeight / 2
  286. monsterPointTemp:SetMonsterData(monster.data.id,x,y)
  287. monsterPointTemp:SetPanelXY(x, y)
  288. monsterPointTemp:SetPanelRotation(0)
  289. monsterPointTemp:setScale(1)
  290. monsterPointTemp:ShowNormalMonsterIcon(monster)
  291. end
  292. end
  293. function this:ShowMonsterPointMin()
  294. if not self.showMonsterPointMinUpdateTime then
  295. self.showMonsterPointMinUpdateTime = Time.time
  296. end
  297. end
  298. ---@param message FightProtos.FightResultRes
  299. function this:ResFightResultMessage(_, message)
  300. if message then
  301. for _,v in pairs(message.target) do
  302. if v.hp <=0 and self.monsterPointMinTempList[v.targetId] then
  303. self.monsterPointMinTempList[v.targetId]:SetNormalDead()
  304. end
  305. end
  306. end
  307. end
  308. ---@param message MapProtos.WalkRes
  309. function this:ResWalkMessage(_, message)
  310. if message then
  311. if self.monsterPointMinTempList[message.id] then
  312. local pos = {}
  313. pos.x = message.x / 256 * self.sceneImgWidth - self.sceneImgWidth / 2
  314. pos.y = message.y / 256 * self.sceneImgHeight - self.sceneImgHeight / 2
  315. self.monsterPointMinTempList[message.id]:SetPanelXY(pos.x, pos.y)
  316. end
  317. end
  318. end
  319. function this:Close()
  320. self.showMonsterPointMinUpdateTime = nil
  321. if self.timeId then
  322. SL:UnSchedule(self.timeId)
  323. self.timeId= nil
  324. end
  325. self.monsterPointMinTempList = {}---temp列表保存
  326. self.MonsterPointMinPool:Clear()
  327. if self.iconMap then
  328. for k, v in pairs(self.iconMap) do
  329. GUI:UIPanel_Close(_,v)
  330. end
  331. table.clear(self.iconMap)
  332. end
  333. end
  334. ------------------怪点end---------------------------------------------------------------------------
  335. return this