MainAssistLayout.lua 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. local MainAssistLayout = class("MainAssistLayout", function()
  2. return cc.Node:create()
  3. end)
  4. local utf8 = require("util/utf8")
  5. local proxyUtils = requireProxy("proxyUtils")
  6. function MainAssistLayout:ctor()
  7. self._path = global.MMO.PATH_RES_PRIVATE .. "main/"
  8. -- 提高进出视野效率
  9. self._updateActorAble = false
  10. self._updateHeroAble = false
  11. self._checkHero = false
  12. end
  13. function MainAssistLayout.create()
  14. local layout = MainAssistLayout.new()
  15. if layout:Init() then
  16. return layout
  17. end
  18. return nil
  19. end
  20. function MainAssistLayout:Init()
  21. self._ui = ui_delegate(self)
  22. return true
  23. end
  24. function MainAssistLayout:InitGUI( ... )
  25. SL:RequireFile(SLDefine.LUAFile.LUA_FILE_MAIN_ASSIST)
  26. MainAssist.main()
  27. self:InitAssist()
  28. self:InitEnemy()
  29. self:InitHero()
  30. self:InitEditMode()
  31. return true
  32. end
  33. function MainAssistLayout:InitEditMode()
  34. local items =
  35. {
  36. "Panel_assist",
  37. "Panel_hide",
  38. "Image_24",
  39. "Button_hide",
  40. }
  41. for _, widgetName in ipairs(items) do
  42. if self._ui[widgetName] then
  43. self._ui[widgetName].editMode = 1
  44. end
  45. end
  46. end
  47. function MainAssistLayout:ChangeHideStatus(data)
  48. MainAssist.ChangeHideStatus(data)
  49. end
  50. function MainAssistLayout:ReloadCUISetWidget( ... )
  51. if self._layoutAssist then
  52. MainAssist._assistPos = GUI:getPosition(self._layoutAssist)
  53. end
  54. if self._ui["Panel_hide"] then
  55. MainAssist._hidePos = GUI:getPosition(self._ui["Panel_hide"])
  56. end
  57. end
  58. function MainAssistLayout:InitAssist()
  59. self._assistGroup = 0 -- 活动 2任务组队 1附近列表 3英雄
  60. self._contentIndex = 0 -- 任务组队显示索引 1任务 2组队
  61. self._enemyIndex = 0 -- 人物怪物显示索引 1玩家 2怪物
  62. self._layoutAssist = self._ui.Panel_assist
  63. -- group
  64. self._layoutGroup = self._layoutAssist:getChildByName("Panel_group")
  65. self._ui_group = ui_delegate(self._layoutGroup)
  66. if not self._ui_group then
  67. return
  68. end
  69. self._ui_group.Button_change:addClickEventListener(function()
  70. local maxGroup = 2
  71. local group = self._assistGroup == 1 and maxGroup or self._assistGroup - 1
  72. if tonumber(SL:GetMetaValue("GAME_DATA","syshero") or 0) == 1 then
  73. maxGroup = 3
  74. if self._assistGroup == 1 then
  75. group = maxGroup
  76. elseif self._assistGroup == 2 then
  77. group = maxGroup - self._assistGroup
  78. elseif self._assistGroup == 3 then
  79. group = 2
  80. end
  81. end
  82. self:ChangeAssistGroup(group)
  83. self:CalcAssistShow()
  84. global.Facade:sendNotification(global.NoticeTable.GuideEventEnded, {name = "GUIDE_ASSIST_MISSION_END"})
  85. end)
  86. -- 任务/组队
  87. self._ui_group.Button_mission:addClickEventListener(function()
  88. self:ChangeContentIndex(1)
  89. self:CalcAssistShow()
  90. end)
  91. self._ui_group.Button_team:addClickEventListener(function()
  92. self:ChangeContentIndex(2)
  93. self:CalcAssistShow()
  94. end)
  95. -- 人物/怪物
  96. self._ui_group.Button_player:addClickEventListener(function()
  97. self:ChangeEnemyIndex(1)
  98. self:CalcAssistShow()
  99. end)
  100. self._ui_group.Button_monster:addClickEventListener(function()
  101. self:ChangeEnemyIndex(2)
  102. self:CalcAssistShow()
  103. end)
  104. -- content
  105. self._layoutContent = self._layoutAssist:getChildByName("Panel_content")
  106. self._ui_content = ui_delegate(self._layoutContent)
  107. self._listviewCells = self._ui_content.ListView_mission
  108. -- enemy
  109. self._layoutEnemy = self._layoutAssist:getChildByName("Panel_enemy")
  110. self._ui_enemy = ui_delegate(self._layoutEnemy)
  111. -- hero
  112. self._layoutHero = self._layoutAssist:getChildByName("Panel_hero")
  113. self._ui_hero = ui_delegate(self._layoutHero)
  114. self:ChangeAssistGroup(2)
  115. self:ChangeContentIndex(1)
  116. self:ChangeEnemyIndex(1)
  117. self:CalcAssistShow()
  118. end
  119. function MainAssistLayout:OnSwitchBackToMission()
  120. self:ChangeAssistGroup(2)
  121. self:ChangeContentIndex(1)
  122. self:ChangeEnemyIndex(1)
  123. self:CalcAssistShow()
  124. end
  125. function MainAssistLayout:ChangeAssistGroup(g)
  126. self._assistGroup = g
  127. self._ui_group.BtnG_content:setVisible(self._assistGroup == 2)
  128. self._ui_group.BtnG_enemy:setVisible(self._assistGroup == 1)
  129. self._ui_group.BtnG_hero:setVisible(self._assistGroup == 3)
  130. end
  131. function MainAssistLayout:ChangeContentIndex(index)
  132. self._contentIndex = index
  133. self._ui_group.Button_mission:setTouchEnabled(self._contentIndex == 2)
  134. self._ui_group.Button_mission:setBright(self._contentIndex == 1)
  135. self._ui_group.Button_team:setTouchEnabled(self._contentIndex == 1)
  136. self._ui_group.Button_team:setBright(self._contentIndex == 2)
  137. if not (self._contentIndex == 1) then
  138. global.Facade:sendNotification(global.NoticeTable.GuideEventEnded, {name = "GUIDE_ASSIST_MISSION_END"})
  139. end
  140. end
  141. function MainAssistLayout:ChangeEnemyIndex(index)
  142. self._enemyIndex = index
  143. self._ui_group.Button_player:setTouchEnabled(self._enemyIndex == 2)
  144. self._ui_group.Button_player:setBright(self._enemyIndex == 1)
  145. self._ui_group.Button_monster:setTouchEnabled(self._enemyIndex == 1)
  146. self._ui_group.Button_monster:setBright(self._enemyIndex == 2)
  147. self:UpdateAllEnemy()
  148. end
  149. function MainAssistLayout:CalcAssistShow()
  150. self._ui_content.nativeUI:setVisible(self._assistGroup == 2)
  151. self._ui_enemy.nativeUI:setVisible(self._assistGroup == 1)
  152. self._ui_hero.nativeUI:setVisible(self._assistGroup == 3)
  153. -- 任务组队
  154. self._ui_content.Panel_mission:setVisible(self._contentIndex == 1)
  155. self._ui_content.Panel_team:setVisible(self._contentIndex == 2)
  156. -- 附近列表
  157. self._ui_enemy.Panel_player:setVisible(self._enemyIndex == 1)
  158. self._ui_enemy.Panel_monster:setVisible(self._enemyIndex == 2)
  159. end
  160. ---------------------------------Enemy begin------------------------------------
  161. function MainAssistLayout:InitEnemy()
  162. self._enemyCells = {}
  163. self._playerCells = {}
  164. self._monsterCells = {}
  165. self._ui_enemy.ListView_player:removeAllItems()
  166. self._ui_enemy.ListView_monster:removeAllItems()
  167. -- 1s检测一次
  168. schedule(self._layoutEnemy, function()
  169. self:CheckAllEnemy()
  170. end, 1)
  171. end
  172. function MainAssistLayout:CheckAllEnemy()
  173. if not self._updateActorAble then
  174. return nil
  175. end
  176. self._updateActorAble = false
  177. if self._enemyIndex == 1 then
  178. self:AutoAddPlayer()
  179. elseif self._enemyIndex == 2 then
  180. self:AutoAddMonster()
  181. end
  182. end
  183. function MainAssistLayout:CreateEnemyCell(data)
  184. local root = cc.Node:create()
  185. local tData = {
  186. actorID = data.actorID
  187. }
  188. if MainAssist and MainAssist.CreateEnemyCell then
  189. return MainAssist.CreateEnemyCell(root, tData)
  190. end
  191. end
  192. function MainAssistLayout:UpdateAllEnemy()
  193. self._enemyCells = {}
  194. self._ui_enemy.ListView_player:removeAllItems()
  195. self._ui_enemy.ListView_monster:removeAllItems()
  196. if self._enemyIndex == 1 then
  197. self:AutoAddPlayer()
  198. elseif self._enemyIndex == 2 then
  199. self:AutoAddMonster()
  200. end
  201. end
  202. function MainAssistLayout:OnMainNearRefresh(data)
  203. local actor = data.actor
  204. local actorID = data.actorID
  205. if actorID == global.playerManager:GetMainPlayerID() then
  206. return
  207. end
  208. if actor:IsPlayer() then
  209. if actor:IsHero() then
  210. self:RmvHero(data)
  211. else
  212. self:RmvPlayer(data)
  213. end
  214. if self._enemyIndex == 1 then
  215. self:AutoAddPlayer()
  216. end
  217. elseif actor:IsMonster() then
  218. self:RmvMonster(data)
  219. if self._enemyIndex == 2 then
  220. self:AutoAddMonster()
  221. end
  222. end
  223. end
  224. function MainAssistLayout:OnActorRevive(data)
  225. local actor = data.actor
  226. local actorID = data.actorID
  227. if actorID == global.playerManager:GetMainPlayerID() then
  228. return
  229. end
  230. self._updateActorAble = true
  231. self._updateHeroAble = true
  232. end
  233. function MainAssistLayout:OnActorInOfView(data)
  234. local actor = data.actor
  235. local actorID = data.actorID
  236. if actorID == global.playerManager:GetMainPlayerID() then
  237. return
  238. end
  239. self._updateActorAble = true
  240. self._updateHeroAble = true
  241. end
  242. function MainAssistLayout:OnActorOutOfView(data)
  243. local actor = data.actor
  244. local actorID = data.actorID
  245. if actorID == global.playerManager:GetMainPlayerID() then
  246. return
  247. end
  248. self._updateActorAble = true
  249. self._updateHeroAble = true
  250. if actor:IsPlayer() then
  251. if actor:IsHero() then
  252. self:RmvHero(data)
  253. elseif actor:IsHumanoid() then
  254. self:RmvMonster(data)
  255. else
  256. self:RmvPlayer(data)
  257. end
  258. elseif actor:IsMonster() then
  259. self:RmvMonster(data)
  260. end
  261. end
  262. function MainAssistLayout:OnActorDie(data)
  263. local actor = data.actor
  264. local actorID = data.actorID
  265. if actorID == global.playerManager:GetMainPlayerID() then
  266. return
  267. end
  268. self._updateActorAble = true
  269. self._updateHeroAble = true
  270. if actor:IsPlayer() then
  271. if actor:IsHero() then
  272. self:RmvHero(data)
  273. elseif actor:IsHumanoid() then
  274. self:RmvMonster(data)
  275. else
  276. self:RmvPlayer(data)
  277. end
  278. elseif actor:IsMonster() then
  279. self:RmvMonster(data)
  280. end
  281. end
  282. function MainAssistLayout:OnRefreshActorHP(data)
  283. self:UpdateEnemy(data)
  284. self:UpdateHero(data)
  285. end
  286. function MainAssistLayout:OnTargetChange(data)
  287. local targetID = data.targetID
  288. for k, v in pairs(self._enemyCells) do
  289. v.imageTarget:setVisible(k == targetID)
  290. end
  291. end
  292. function MainAssistLayout:OnPlayerPKStateChange()
  293. -- 切模式,刷新附近列表
  294. if self._enemyIndex ~= 1 then
  295. return nil
  296. end
  297. self._enemyCells = {}
  298. self._ui_enemy.ListView_player:removeAllItems()
  299. self:AutoAddPlayer()
  300. end
  301. local function checkPeaceModeEnemyTag(actor)
  302. if not actor then
  303. return global.MMO.RS_NO
  304. end
  305. -- 1. oneself
  306. local mainPlayer = global.gamePlayerController:GetMainPlayer()
  307. if not mainPlayer or ( actor:IsPlayer() and actor:IsMainPlayer() ) then
  308. return global.MMO.RS_NO
  309. end
  310. return global.MMO.RS_ENEMY
  311. end
  312. local function checkPeaceLaunchTarget(targetActor)
  313. -- player & monster, only!!!!
  314. if false == targetActor:IsPlayer() and false == targetActor:IsMonster() then
  315. return false
  316. end
  317. local mainPlayer = global.gamePlayerController:GetMainPlayer()
  318. if not mainPlayer then
  319. return false
  320. end
  321. local autoProxy = global.Facade:retrieveProxy(global.ProxyTable.Auto)
  322. if autoProxy:IsAutoFightState() then --所有模式 可以攻击自己的宝宝
  323. ---- hero player / matser is main hero
  324. local HeroPropertyProxy = global.Facade:retrieveProxy( global.ProxyTable.HeroPropertyProxy )
  325. local heroID = HeroPropertyProxy:GetRoleUID()
  326. if heroID and (targetActor:GetID() == heroID or targetActor:GetMasterID() == heroID) then
  327. return false
  328. end
  329. -- main player / matser is main player
  330. if (targetActor:IsPlayer() and targetActor:IsMainPlayer()) or targetActor:GetMasterID() == mainPlayer:GetID() then
  331. return false
  332. end
  333. end
  334. -- dead & born
  335. if targetActor:IsDie() or targetActor:IsDeath() or targetActor:IsBorn() or targetActor:IsCave() then
  336. return false
  337. end
  338. -- hp 0
  339. if targetActor:GetHP() <= 0 then
  340. return false
  341. end
  342. -- humanoid
  343. if targetActor:IsPlayer() and targetActor:IsHumanoid() then
  344. if targetActor:GetMasterID() and checkPeaceModeEnemyTag(targetActor) ~= 1 then --人性怪如果有MasterID, 要判断Master是否是敌友
  345. return false
  346. end
  347. return true
  348. end
  349. -- player, check is enmey
  350. if targetActor:IsPlayer() and checkPeaceModeEnemyTag(targetActor) ~= 1 then
  351. return false
  352. end
  353. if targetActor:IsHero() and checkPeaceModeEnemyTag(targetActor) ~= 1 then
  354. return false
  355. end
  356. -- 采集物
  357. if targetActor:IsCollection() then
  358. return false
  359. end
  360. -- buff的石化怪
  361. if targetActor:IsMonster() and targetActor:GetStoneMode() and not targetActor:IsBorn() then
  362. return false
  363. end
  364. return true
  365. end
  366. function MainAssistLayout:CheckPlayerEnemyTag(actor)
  367. local playerPropertyProxy = global.Facade:retrieveProxy(global.ProxyTable.PlayerProperty)
  368. local pkMode = playerPropertyProxy:GetPKMode()
  369. if pkMode ~= global.MMO.HAM_PEACE then
  370. return proxyUtils.checkEnemyTag(actor)
  371. else
  372. return checkPeaceModeEnemyTag(actor)
  373. end
  374. end
  375. function MainAssistLayout:CheckPlayerLaunchTarget(actor)
  376. local playerPropertyProxy = global.Facade:retrieveProxy(global.ProxyTable.PlayerProperty)
  377. local pkMode = playerPropertyProxy:GetPKMode()
  378. if pkMode ~= global.MMO.HAM_PEACE then
  379. return proxyUtils.checkLaunchTarget(actor)
  380. else
  381. return checkPeaceLaunchTarget(actor)
  382. end
  383. end
  384. function MainAssistLayout:AddPlayer(data)
  385. local actor = data.actor
  386. local actorID = data.actorID
  387. if self._enemyIndex ~= 1 then
  388. return
  389. end
  390. if self._enemyCells[actorID] then
  391. return
  392. end
  393. if false == self:CheckPlayerLaunchTarget(actor) then
  394. -- enemy only
  395. return
  396. end
  397. local listview = self._ui_enemy.ListView_player
  398. local items = listview:getItems()
  399. if #items >= MainAssist.PLAYER_COUNT then
  400. return
  401. end
  402. -- 找到插入位置
  403. local insertIndex = #items
  404. for enemyID, enemyCell in pairs(self._enemyCells) do
  405. local enemy = global.actorManager:GetActor(enemyID)
  406. -- 我日,出错了,啥错啊,难搞
  407. if nil == enemy then
  408. print("ERRORRRRRRRRRRRRRRRRRRR AddPlayer")
  409. return
  410. end
  411. if self:CheckPlayerEnemyTag(enemy) == 1 and self:CheckPlayerEnemyTag(actor) == 1 then
  412. -- 都可以攻击,判断等级
  413. if actor:GetLevel() > enemy:GetLevel() then
  414. insertIndex = math.min(insertIndex, listview:getIndex(enemyCell.layout))
  415. end
  416. elseif self:CheckPlayerEnemyTag(enemy) ~= 1 and self:CheckPlayerEnemyTag(actor) == 1 then
  417. -- 一个可攻击,一个不可以攻击,取可以攻击的
  418. insertIndex = math.min(insertIndex, listview:getIndex(enemyCell.layout))
  419. elseif self:CheckPlayerEnemyTag(enemy) ~= 1 and self:CheckPlayerEnemyTag(actor) ~= 1 then
  420. -- 都不可以攻击,判断等级
  421. if actor:GetLevel() > enemy:GetLevel() then
  422. insertIndex = math.min(insertIndex, listview:getIndex(enemyCell.layout))
  423. end
  424. end
  425. end
  426. local cell = self:CreateEnemyCell(data)
  427. if cell and next(cell) then
  428. listview:insertCustomItem(cell.layout, insertIndex)
  429. self._enemyCells[actorID] = cell
  430. end
  431. end
  432. function MainAssistLayout:RmvPlayer(data)
  433. local actor = data.actor
  434. local actorID = data.actorID
  435. if self._enemyIndex ~= 1 then
  436. return
  437. end
  438. if not self._enemyCells[actorID] then
  439. return
  440. end
  441. local listview = self._ui_enemy.ListView_player
  442. local cell = self._enemyCells[actorID]
  443. listview:removeItem(listview:getIndex(cell.layout))
  444. self._enemyCells[actorID] = nil
  445. end
  446. function MainAssistLayout:AutoAddPlayer()
  447. if self._enemyIndex ~= 1 then
  448. return
  449. end
  450. local listview = self._ui_enemy.ListView_player
  451. local items = listview:getItems()
  452. if #items >= MainAssist.PLAYER_COUNT then
  453. return
  454. end
  455. local actors, nActor = global.playerManager:GetPlayersInCurrViewField()
  456. for i = 1, nActor do
  457. local actor = actors[i]
  458. if actor and not actor:IsMainPlayer() and not actor:IsHero() and not actor:IsHumanoid() and not actor:GetValueByKey(global.MMO.HUD_SNEAK) then
  459. self:AddPlayer({actor = actor, actorID = actor:GetID()})
  460. self:AutoRmvPlayer()
  461. end
  462. end
  463. end
  464. function MainAssistLayout:AutoRmvPlayer()
  465. if self._enemyIndex ~= 1 then
  466. return
  467. end
  468. local listview = self._ui_enemy.ListView_player
  469. local items = listview:getItems()
  470. if #items <= MainAssist.PLAYER_COUNT then
  471. return
  472. end
  473. local rmved = nil
  474. for enemyID, enemyCell in pairs(self._enemyCells) do
  475. local enemy = global.actorManager:GetActor(enemyID)
  476. -- 出错了吧
  477. if nil == enemy then
  478. print("ERRORRRRRRRRRRRRRRRRRRR AutoRmvPlayer")
  479. break
  480. end
  481. if nil == rmved then
  482. rmved = enemy
  483. elseif proxyUtils.checkEnemyTag(enemy) == 1 and proxyUtils.checkEnemyTag(rmved) == 1 then
  484. -- 都可以攻击,判断等级
  485. if rmved:GetLevel() > enemy:GetLevel() then
  486. rmved = enemy
  487. end
  488. elseif proxyUtils.checkEnemyTag(enemy) == 1 and proxyUtils.checkEnemyTag(rmved) ~= 1 then
  489. -- 一个可攻击,一个不可以攻击,取可以攻击的
  490. rmved = enemy
  491. elseif proxyUtils.checkEnemyTag(enemy) ~= 1 and proxyUtils.checkEnemyTag(rmved) ~= 1 then
  492. -- 都不可以攻击,判断等级
  493. if rmved:GetLevel() > enemy:GetLevel() then
  494. rmved = enemy
  495. end
  496. end
  497. end
  498. if rmved then
  499. self:RmvPlayer({actorID = rmved:GetID(), actor = rmved})
  500. end
  501. end
  502. local function getWeight(actor)
  503. local weight = 0
  504. if actor:IsEscort() then
  505. weight = 4
  506. elseif actor:IsBoss() then
  507. weight = 3
  508. elseif actor:IsElite() then
  509. weight = 2
  510. elseif actor:IsMonster() and false == actor:IsHaveMaster() then
  511. weight = 1
  512. end
  513. return weight
  514. end
  515. function MainAssistLayout:AddMonster(data)
  516. local actor = data.actor
  517. local actorID = data.actorID
  518. if self._enemyIndex ~= 2 then
  519. return
  520. end
  521. if self._enemyCells[actorID] then
  522. return
  523. end
  524. if false == proxyUtils.checkLaunchTarget(actor) then
  525. -- enemy only
  526. return
  527. end
  528. local listview = self._ui_enemy.ListView_monster
  529. local items = listview:getItems()
  530. if #items >= MainAssist.MONSTER_COUNT and getWeight(actor) == 0 then
  531. return
  532. end
  533. -- 根据怪物权值找到插入位置
  534. local insertIndex = #items
  535. for enemyID, enemyCell in pairs(self._enemyCells) do
  536. local enemy = global.actorManager:GetActor(enemyID)
  537. if enemy and getWeight(actor) > getWeight(enemy) then
  538. insertIndex = math.min(listview:getIndex(enemyCell.layout), insertIndex)
  539. end
  540. end
  541. local cell = self:CreateEnemyCell(data)
  542. if cell and next(cell) then
  543. listview:insertCustomItem(cell.layout, insertIndex)
  544. self._enemyCells[actorID] = cell
  545. end
  546. end
  547. function MainAssistLayout:RmvMonster(data)
  548. local actor = data.actor
  549. local actorID = data.actorID
  550. if self._enemyIndex ~= 2 then
  551. return
  552. end
  553. if not self._enemyCells[actorID] then
  554. return
  555. end
  556. local listview = self._ui_enemy.ListView_monster
  557. local cell = self._enemyCells[actorID]
  558. listview:removeItem(listview:getIndex(cell.layout))
  559. self._enemyCells[actorID] = nil
  560. end
  561. function MainAssistLayout:AutoAddMonster()
  562. if self._enemyIndex ~= 2 then
  563. return
  564. end
  565. local listview = self._ui_enemy.ListView_monster
  566. local items = listview:getItems()
  567. if #items >= MainAssist.MONSTER_COUNT then
  568. return
  569. end
  570. local actors, nActor = global.monsterManager:FindMonsterInCurrViewField(true)
  571. local playerVec, nPlayer = global.playerManager:FindPlayerInCurrViewField()
  572. for i = 1, nPlayer do
  573. local player = playerVec[i]
  574. if player:IsHumanoid() then
  575. nActor = nActor + 1
  576. actors[nActor] = player
  577. end
  578. end
  579. for i = 1, nActor do
  580. local actor = actors[i]
  581. if actor then
  582. self:AddMonster({actor = actor, actorID = actor:GetID()})
  583. self:AutoRmvMonster()
  584. end
  585. end
  586. end
  587. function MainAssistLayout:AutoRmvMonster()
  588. if self._enemyIndex ~= 2 then
  589. return
  590. end
  591. local listview = self._ui_enemy.ListView_monster
  592. local items = listview:getItems()
  593. if #items <= MainAssist.MONSTER_COUNT then
  594. return
  595. end
  596. local rmved = nil
  597. for enemyID, enemyCell in pairs(self._enemyCells) do
  598. local enemy = global.actorManager:GetActor(enemyID)
  599. -- 出错了吧
  600. if nil == enemy then
  601. print("ERRORRRRRRRRRRRRRRRRRRR AutoRmvMonster")
  602. break
  603. end
  604. -- 根据权重,移除
  605. if getWeight(enemy) == 0 then
  606. rmved = enemy
  607. break
  608. end
  609. if nil == rmved or getWeight(enemy) < getWeight(rmved) then
  610. rmved = enemy
  611. end
  612. end
  613. if rmved then
  614. self:RmvMonster({actorID = rmved:GetID(), actor = rmved})
  615. end
  616. end
  617. function MainAssistLayout:OnActorMonsterBirth(data)
  618. local actor = data.actor
  619. local actorID = data.actorID
  620. self._updateActorAble = true
  621. end
  622. function MainAssistLayout:OnActorMonsterCave(data)
  623. local actor = data.actor
  624. local actorID = data.actorID
  625. self._updateActorAble = true
  626. end
  627. function MainAssistLayout:UpdateEnemy(data)
  628. local actorID = data.actorID
  629. local actor = global.actorManager:GetActor(actorID)
  630. if not actor then
  631. return
  632. end
  633. if not ((self._enemyIndex == 1 and actor:IsPlayer()) or (self._enemyIndex == 2 and actor:IsMonster())) then
  634. return
  635. end
  636. if not self._enemyCells[actorID] then
  637. return
  638. end
  639. local cell = self._enemyCells[actorID]
  640. local curHP = actor:GetHP()
  641. local maxHP = actor:GetMaxHP()
  642. local percent = math.floor((curHP / maxHP * 100))
  643. cell.loadingBarHp:setPercent(percent)
  644. end
  645. ----------------------------------Enemy end-------------------------------------
  646. function MainAssistLayout:InitHero()
  647. self._checkHero = tonumber(SL:GetMetaValue("GAME_DATA","syshero")) == 1
  648. if self._checkHero then
  649. self._heroCells = {}
  650. self._ui_hero.ListView_hero:removeAllItems()
  651. schedule(self._layoutHero, function()
  652. self:CheckAllHero()
  653. end, 1)
  654. end
  655. end
  656. function MainAssistLayout:CheckAllHero( ... )
  657. if not self._updateHeroAble then
  658. return nil
  659. end
  660. self._updateHeroAble = false
  661. if self._assistGroup == 3 and self._checkHero then
  662. local items = self._ui_hero.ListView_hero:getItems()
  663. if #items >= MainAssist.HERO_COUNT then
  664. return
  665. end
  666. local actors, nActor = global.playerManager:FindHeroInCurrViewField()
  667. for i = 1, nActor do
  668. local actor = actors[i]
  669. self:AddHero({actor = actor, actorID = actor:GetID()})
  670. self:AutoRmvHero()
  671. end
  672. end
  673. end
  674. function MainAssistLayout:AddHero(data)
  675. local actor = data.actor
  676. local actorID = data.actorID
  677. if not (self._assistGroup == 3 and self._checkHero) then
  678. return
  679. end
  680. if self._heroCells[actorID] then
  681. return
  682. end
  683. if false == proxyUtils.checkLaunchTarget(actor) then
  684. -- enemy only
  685. return
  686. end
  687. local listview = self._ui_hero.ListView_hero
  688. local items = listview:getItems()
  689. if #items >= MainAssist.HERO_COUNT then
  690. return
  691. end
  692. -- 找到插入位置
  693. local insertIndex = #items
  694. for heroID, heroCell in pairs(self._heroCells) do
  695. local hero = global.actorManager:GetActor(heroID)
  696. if nil == hero then
  697. print("ERRORRRRRRRRRRRRRRRRRRR AddHERO BY ASSIST")
  698. break
  699. end
  700. if proxyUtils.checkEnemyTag(hero) == 1 and proxyUtils.checkEnemyTag(actor) == 1 then
  701. -- 都可以攻击,判断等级
  702. if actor:GetLevel() > hero:GetLevel() then
  703. insertIndex = math.min(insertIndex, listview:getIndex(heroCell.layout))
  704. end
  705. elseif proxyUtils.checkEnemyTag(hero) ~= 1 and proxyUtils.checkEnemyTag(actor) == 1 then
  706. -- 一个可攻击,一个不可以攻击,取可以攻击的
  707. insertIndex = math.min(insertIndex, listview:getIndex(heroCell.layout))
  708. elseif proxyUtils.checkEnemyTag(hero) ~= 1 and proxyUtils.checkEnemyTag(actor) ~= 1 then
  709. -- 都不可以攻击,判断等级
  710. if actor:GetLevel() > hero:GetLevel() then
  711. insertIndex = math.min(insertIndex, listview:getIndex(heroCell.layout))
  712. end
  713. end
  714. end
  715. local cell = self:CreateEnemyCell(data)
  716. if cell and next(cell) then
  717. listview:insertCustomItem(cell.layout, insertIndex)
  718. self._heroCells[actorID] = cell
  719. end
  720. end
  721. function MainAssistLayout:AutoRmvHero()
  722. if not (self._assistGroup == 3 and self._checkHero) then
  723. return
  724. end
  725. local listview = self._ui_hero.ListView_hero
  726. local items = listview:getItems()
  727. if #items <= MainAssist.HERO_COUNT then
  728. return
  729. end
  730. local rmved = nil
  731. for heroID, heroCell in pairs(self._heroCells) do
  732. local hero = global.actorManager:GetActor(heroID)
  733. if nil == hero then
  734. print("ERRORRRRRRRRRRRRRRRRRRR AutoRmvHERO ")
  735. break
  736. end
  737. if nil == rmved then
  738. rmved = hero
  739. elseif proxyUtils.checkEnemyTag(hero) == 1 and proxyUtils.checkEnemyTag(rmved) == 1 then
  740. -- 都可以攻击,判断等级
  741. if rmved:GetLevel() > hero:GetLevel() then
  742. rmved = hero
  743. end
  744. elseif proxyUtils.checkEnemyTag(hero) == 1 and proxyUtils.checkEnemyTag(rmved) ~= 1 then
  745. -- 一个可攻击,一个不可以攻击,取可以攻击的
  746. rmved = hero
  747. elseif proxyUtils.checkEnemyTag(hero) ~= 1 and proxyUtils.checkEnemyTag(rmved) ~= 1 then
  748. -- 都不可以攻击,判断等级
  749. if rmved:GetLevel() > hero:GetLevel() then
  750. rmved = hero
  751. end
  752. end
  753. end
  754. if rmved then
  755. self:RmvHero({actorID = rmved:GetID(), actor = rmved})
  756. end
  757. end
  758. function MainAssistLayout:RmvHero(data)
  759. local actor = data.actor
  760. local actorID = data.actorID
  761. if not (self._assistGroup == 3 and self._checkHero) then
  762. return
  763. end
  764. if not self._heroCells[actorID] then
  765. return
  766. end
  767. local listview = self._ui_hero.ListView_hero
  768. local cell = self._heroCells[actorID]
  769. listview:removeItem(listview:getIndex(cell.layout))
  770. self._heroCells[actorID] = nil
  771. end
  772. function MainAssistLayout:UpdateHero(data)
  773. local actorID = data.actorID
  774. local actor = global.actorManager:GetActor(actorID)
  775. if not actor then
  776. return
  777. end
  778. if not (self._assistGroup == 3 and self._checkHero) then
  779. return
  780. end
  781. if not self._heroCells[actorID] then
  782. return
  783. end
  784. local cell = self._heroCells[actorID]
  785. local curHP = actor:GetHP()
  786. local maxHP = actor:GetMaxHP()
  787. local percent = math.floor((curHP / maxHP * 100))
  788. cell.loadingBarHp:setPercent(percent)
  789. end
  790. return MainAssistLayout