RoleManager.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. RoleManager = {}
  2. local this = RoleManager
  3. -- 将死之人
  4. local _DeadRoleList = {}
  5. -- 重生之人
  6. local _ReliveRoleList = {}
  7. -- 所有角色
  8. local PosList = {}
  9. -- 删除节点
  10. local removeObjList = BattleList.New()
  11. -- 角色对象池
  12. local rolePool = BattleObjectPool.New(function ()
  13. return RoleLogic.New()
  14. end)
  15. local bMyAllDead
  16. local bEnemyAllDead
  17. -- 初始化
  18. function this.Init()
  19. bMyAllDead = false
  20. bEnemyAllDead = false
  21. this.Clear()
  22. end
  23. -- 角色数据添加
  24. local curUid
  25. function this.AddRole(roleData, position)
  26. -- WYLog("RoleManager.AddRole")
  27. -- WYLog(position)
  28. LogGreen("Rolemanager AddRole11")
  29. if not curUid then
  30. curUid = 0
  31. end
  32. curUid = curUid + 1
  33. local role = rolePool:Get()
  34. role:Init(curUid, roleData, position)
  35. -- objList:Add(curUid, role)
  36. if roleData.camp == 0 then
  37. PosList[position] = role -- 1-9 我方英雄
  38. else
  39. PosList[position + 9] = role-- 10-18 敌方英雄
  40. end
  41. -- WYLog("RoleManager.AddRole end")
  42. if not role:IsRealDead() then
  43. -- WYLog("end1")
  44. LogGreen("Rolemanager AddRole22")
  45. BattleLogic.Event:DispatchEvent(BattleEventName.AddRole, role)
  46. end
  47. end
  48. --
  49. function this.Update()
  50. bMyAllDead = true
  51. bEnemyAllDead = true
  52. for _, v in pairs(PosList) do
  53. if not v:IsRealDead() then
  54. v:Update()
  55. if v.camp == 0 then
  56. bMyAllDead = false
  57. else
  58. bEnemyAllDead = false
  59. end
  60. end
  61. end
  62. if bEnemyAllDead then
  63. BattleLogic.Event:DispatchEvent(BattleEventName.BattleOrderEnd, BattleLogic.CurOrder)
  64. end
  65. end
  66. -- 获取结果
  67. function this.GetResult()
  68. if bMyAllDead then
  69. return 0
  70. end
  71. if bEnemyAllDead then
  72. return 1
  73. end
  74. end
  75. -- 加入将死的人
  76. function this.AddDeadRole(role)
  77. _DeadRoleList[role.position + role.camp * 9] = role
  78. end
  79. -- 检测是有人将死
  80. function this.CheckDead()
  81. local isDeadFrame = false
  82. local removePos = {}
  83. for pos, role in pairs(_DeadRoleList) do
  84. if role:GoDead() then
  85. isDeadFrame = true
  86. table.insert(removePos, pos)
  87. end
  88. end
  89. for _, pos in ipairs(removePos) do
  90. _DeadRoleList[pos] = nil
  91. end
  92. return isDeadFrame
  93. end
  94. -- 加入复活之人
  95. function this.AddReliveRole(role)
  96. _ReliveRoleList[role.position + role.camp * 9] = role
  97. end
  98. -- 检测是否有人复活
  99. function this.CheckRelive()
  100. local isReliveFrame = false
  101. local removePos = {}
  102. for pos, role in pairs(_ReliveRoleList) do
  103. if role:Relive() then
  104. isReliveFrame = true
  105. table.insert(removePos, pos)
  106. end
  107. end
  108. for _, pos in ipairs(removePos) do
  109. _ReliveRoleList[pos] = nil
  110. end
  111. return isReliveFrame
  112. end
  113. -- 获取角色数据
  114. function this.GetRole(camp, pos)
  115. return PosList[pos + camp*9]
  116. end
  117. function this.GetRoleByPos(pos)
  118. return PosList[pos]
  119. end
  120. -- 获取某阵营所有角色
  121. -- function this.GetRoleByCamp(camp)
  122. -- local list = {}
  123. -- for i = 1, 6 do
  124. -- list[i] = PosList[i + camp * 6]
  125. -- end
  126. -- return list
  127. -- end
  128. -- 查找角色
  129. function RoleManager.Query(func, inCludeDeadRole)
  130. local list = {}
  131. local index = 1
  132. if func then
  133. for pos, v in pairs(PosList) do
  134. if func(v) and (inCludeDeadRole or not v:IsRealDead()) then
  135. list[index] = v
  136. index = index + 1
  137. end
  138. end
  139. end
  140. table.sort(list, function(a, b)
  141. return a.position < b.position
  142. end)
  143. return list
  144. end
  145. --> 查找死亡角色
  146. function RoleManager.QueryDead(func)
  147. local list = {}
  148. local index = 1
  149. if func then
  150. for pos, v in pairs(PosList) do
  151. if func(v) and v:IsRealDead() then
  152. list[index] = v
  153. index = index + 1
  154. end
  155. end
  156. end
  157. table.sort(list, function(a, b)
  158. return a.position < b.position
  159. end)
  160. return list
  161. end
  162. --对位规则: 1敌方相同对位 2若死亡或不存在,选取相邻阵位最近且阵位索引最小的
  163. function this.GetAggro(role)
  164. -- -- 计算开始位置
  165. -- local startPos = role.position
  166. -- startPos = startPos > 3 and startPos - 3 or startPos
  167. -- local target
  168. -- local enemyCamp = role.camp == 0 and 1 or 0
  169. -- -- c=0 前排 c=1 后排
  170. -- for c = 0, 1 do
  171. -- startPos = startPos + c*3
  172. -- -- 向左
  173. -- for i = startPos, c*3+1, -1 do
  174. -- local pos = i + enemyCamp * 6
  175. -- if PosList[pos] and not PosList[pos]:IsRealDead() then
  176. -- target = PosList[pos]
  177. -- break
  178. -- end
  179. -- end
  180. -- if target then return target end
  181. -- -- 向右
  182. -- for i = startPos + 1, c*3+3 do
  183. -- local pos = i + enemyCamp * 6
  184. -- if PosList[pos] and not PosList[pos]:IsRealDead() then
  185. -- target = PosList[pos]
  186. -- break
  187. -- end
  188. -- end
  189. -- if target then return target end
  190. -- end
  191. -->
  192. -- 计算开始位置
  193. local startPos = role.position
  194. startPos = startPos > 6 and startPos - 6 or (startPos > 3 and startPos - 3 or startPos)
  195. local target
  196. local enemyCamp = role.camp == 0 and 1 or 0
  197. -- c=0 前排 c=1 中排
  198. for c = 0, 2 do
  199. startPos = startPos + c*3
  200. -- 向左
  201. for i = startPos, c*3+1, -1 do
  202. local pos = i + enemyCamp * 9
  203. if PosList[pos] and not PosList[pos]:IsRealDead() then
  204. target = PosList[pos]
  205. break
  206. end
  207. end
  208. if target then return target end
  209. -- 向右
  210. for i = startPos + 1, c*3+3 do
  211. local pos = i + enemyCamp * 9
  212. if PosList[pos] and not PosList[pos]:IsRealDead() then
  213. target = PosList[pos]
  214. break
  215. end
  216. end
  217. if target then return target end
  218. end
  219. end
  220. -- 获取没有进入死亡状态的仇恨目标
  221. function this.GetAliveAggro(role)
  222. -- 计算开始位置
  223. local startPos = role.position
  224. startPos = startPos > 3 and startPos - 3 or startPos
  225. local target
  226. local enemyCamp = role.camp == 0 and 1 or 0
  227. -- c=0 前排 c=1 后排
  228. for c = 0, 1 do
  229. startPos = startPos + c*3
  230. -- 向左
  231. for i = startPos, c*3+1, -1 do
  232. local pos = i + enemyCamp * 6
  233. if PosList[pos] and not PosList[pos]:IsDead() then
  234. target = PosList[pos]
  235. break
  236. end
  237. end
  238. if target then return target end
  239. -- 向右
  240. for i = startPos + 1, c*3+3 do
  241. local pos = i + enemyCamp * 6
  242. if PosList[pos] and not PosList[pos]:IsDead() then
  243. target = PosList[pos]
  244. break
  245. end
  246. end
  247. if target then return target end
  248. end
  249. --> 只有被动用 暂不动
  250. end
  251. --对位规则: 1敌方相同对位 2若死亡或不存在,选取相邻阵位最近且阵位索引最小的
  252. function this.GetArrAggroList(role, arr)
  253. -- -- 重构数据
  254. -- local plist = {}
  255. -- for _, role in ipairs(arr) do
  256. -- plist[role.position] = role
  257. -- end
  258. -- -- 计算开始位置
  259. -- local startPos = role.position
  260. -- startPos = startPos > 3 and startPos - 3 or startPos
  261. -- local targetList = {}
  262. -- -- c=0 前排 c=1 后排
  263. -- for c = 0, 1 do
  264. -- startPos = startPos + c*3
  265. -- -- 向左
  266. -- for i = startPos, c*3+1, -1 do
  267. -- local pos = i
  268. -- if plist[pos] and not plist[pos]:IsRealDead() then
  269. -- table.insert(targetList, plist[pos])
  270. -- end
  271. -- end
  272. -- -- 向右
  273. -- for i = startPos + 1, c*3+3 do
  274. -- local pos = i
  275. -- if plist[pos] and not plist[pos]:IsRealDead() then
  276. -- table.insert(targetList, plist[pos])
  277. -- end
  278. -- end
  279. -- end
  280. -- table.sort(targetList, function(a, b)
  281. -- return a.position < b.position
  282. -- end)
  283. -- return targetList
  284. -->
  285. -- 重构数据
  286. local plist = {}
  287. for _, role in ipairs(arr) do
  288. plist[role.position] = role
  289. end
  290. -- 计算开始位置
  291. local startPos = role.position
  292. startPos = startPos > 6 and startPos - 6 or (startPos > 3 and startPos - 3 or startPos)
  293. local targetList = {}
  294. -- c=0 前排 c=1 中排
  295. for c = 0, 2 do
  296. startPos = startPos + c*3
  297. -- 向左
  298. for i = startPos, c*3+1, -1 do
  299. local pos = i
  300. if plist[pos] and not plist[pos]:IsRealDead() then
  301. table.insert(targetList, plist[pos])
  302. end
  303. end
  304. -- 向右
  305. for i = startPos + 1, c*3+3 do
  306. local pos = i
  307. if plist[pos] and not plist[pos]:IsRealDead() then
  308. table.insert(targetList, plist[pos])
  309. end
  310. end
  311. end
  312. table.sort(targetList, function(a, b)
  313. return a.position < b.position
  314. end)
  315. return targetList
  316. end
  317. --获取对位相邻站位的人 chooseType 1 我方 2 敌方(对位的敌人受到嘲讽的影响,若对位的敌人死亡,则选取相邻最近的作为目标)
  318. function this.GetNeighbor(role, chooseType)
  319. local posList = {}
  320. local target
  321. if chooseType == 1 then
  322. target = role
  323. else
  324. if role.lockTarget and not role.lockTarget:IsRealDead() then
  325. target = role.lockTarget
  326. else
  327. target = this.GetAggro(role)
  328. end
  329. end
  330. if target then
  331. local list = this.Query(function (r) return r.camp == target.camp end)
  332. for i=1, #list do
  333. if not list[i]:IsRealDead() then
  334. if list[i].position == target.position + 3 -- 后排的人
  335. or list[i].position == target.position - 3 -- 前排的人
  336. or (math.abs(target.position - list[i].position) <= 1 and math.floor((target.position-1)/3) == math.floor((list[i].position-1)/3)) then -- 旁边的人和自己
  337. table.insert(posList, list[i])
  338. end
  339. end
  340. end
  341. end
  342. table.sort(posList, function(a, b)
  343. return a.position < b.position
  344. end)
  345. return posList
  346. end
  347. function this.Clear()
  348. for _, obj in pairs(PosList) do
  349. obj:Dispose()
  350. removeObjList:Add(obj)
  351. end
  352. PosList = {}
  353. while removeObjList.size > 0 do
  354. rolePool:Put(removeObjList.buffer[removeObjList.size])
  355. removeObjList:Remove(removeObjList.size)
  356. end
  357. _DeadRoleList = {}
  358. _ReliveRoleList = {}
  359. end
  360. -- 多波
  361. function this.ClearEnemy()
  362. local removePos = {}
  363. for pos, obj in pairs(PosList) do
  364. if obj.camp == 1 then
  365. removePos[pos] = 1
  366. BattleLogic.Event:DispatchEvent(BattleEventName.RemoveRole, obj)
  367. obj:Dispose()
  368. removeObjList:Add(obj)
  369. end
  370. end
  371. for pos, _ in pairs(removePos) do
  372. PosList[pos] = nil
  373. end
  374. end
  375. return this