miduo_client/Assets/ManagedResources/~Lua/Modules/Battle/Logic/Role/RoleManager.lua

485 lines
13 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

RoleManager = {}
local this = RoleManager
-- 将死之人
local _DeadRoleList = {}
-- 重生之人
local _ReliveRoleList = {}
-- 所有角色
local PosList = {}
local RealDeadList={}
-- 删除节点
local removeObjList = BattleList.New()
-- 角色对象池
local rolePool = BattleObjectPool.New(function ()
return RoleLogic.New()
end)
local bMyAllDead
local bEnemyAllDead
-- 初始化
function this.Init()
bMyAllDead = false
bEnemyAllDead = false
this.Clear()
end
-- 角色数据添加
local curUid
function this.AddRole(roleData, position)
if not curUid then
curUid = 0
end
curUid = curUid + 1
local role = rolePool:Get()
role:Init(curUid, roleData, position)
-- objList:Add(curUid, role)
if roleData.camp == 0 then
PosList[position] = role -- 1-6 我方英雄
else
PosList[position + 6] = role-- 7-12 敌方英雄
end
if not role:IsRealDead() then
BattleLogic.Event:DispatchEvent(BattleEventName.AddRole, role)
end
end
function this.GetRole()
local role = rolePool:Get()
return role
end
--
function this.Update()
bMyAllDead = true
bEnemyAllDead = true
for _, v in pairs(PosList) do
if not v:IsRealDead() then
v:Update()
if v.camp == 0 then
bMyAllDead = false
else
bEnemyAllDead = false
end
end
end
if bEnemyAllDead then
BattleLogic.Event:DispatchEvent(BattleEventName.BattleOrderEnd, BattleLogic.CurOrder)
end
end
-- 获取结果
function this.GetResult()
if bMyAllDead then
return 0
end
if bEnemyAllDead then
return 1
end
end
-- 加入将死的人
function this.AddDeadRole(role)
_DeadRoleList[role.position + role.camp * 6] = role
end
function this.GetDeadRole(type)
if RealDeadList and #RealDeadList>0 then
BattleUtil.SortByPos(RealDeadList,1)
for i = 1, #RealDeadList do
if RealDeadList[i].camp==type then
return RealDeadList[i]
end
end
end
end
function this.RemoveDeadRole(role)
if _DeadRoleList[role.position + role.camp * 6] then
_DeadRoleList[role.position + role.camp * 6]=nil
end
end
-- 检测是有人将死
function this.CheckDead()
local isDeadFrame = false
local removePos = {}
for pos, role in pairs(_DeadRoleList) do
if role:GoDead() then
isDeadFrame = true
table.insert(removePos, pos)
end
end
for _, pos in ipairs(removePos) do
table.insert(RealDeadList,_DeadRoleList[pos])
BattleLogic.WaitForTrigger(1,function()
BattleLogic.Event:DispatchEvent(BattleEventName.RoleIsVanish,RealDeadList,_DeadRoleList[pos])
end)
_DeadRoleList[pos] = nil
end
return isDeadFrame
end
-- 加入复活之人
function this.AddReliveRole(role)
_ReliveRoleList[role.position + role.camp * 6] = role
end
-- 检测是否有人复活
function this.CheckRelive()
local isReliveFrame = false
local removePos = {}
for pos, role in pairs(_ReliveRoleList) do
if role:Relive() then
table_removebyvalue(RealDeadList,role)
isReliveFrame = true
table.insert(removePos, pos)
end
end
for _, pos in ipairs(removePos) do
_ReliveRoleList[pos] = nil
end
return isReliveFrame
end
-- 获取角色数据
function this.GetRole(camp, pos)
return PosList[pos + camp*6]
end
function this.GetRoleByCampAndPos(camp, pos)
return PosList[pos + camp*6]
end
-- 获取某阵营所有角色
function this.GetRoleByCamp(camp)
local list = {}
--放逐的不会被获取到
for i = 1, 6 do
if not list[i].isExile then
list[i] = PosList[i + camp * 6]
end
end
return list
end
-- 查找角色
function RoleManager.Query(func, inCludeDeadRole)
local list = {}
local index = 1
if func then
for pos, v in pairs(PosList) do
if func(v) and (inCludeDeadRole or not v:IsRealDead())and not v.isExile then
list[index] = v
index = index + 1
end
end
end
table.sort(list, function(a, b)
return a.position < b.position
end)
return list
end
-- 查找角包括放逐目标
function RoleManager.QueryIncludeExile(func, inCludeDeadRole)
local list = {}
local index = 1
if func then
for pos, v in pairs(PosList) do
if func(v) and (inCludeDeadRole or not v:IsRealDead()) then
list[index] = v
index = index + 1
end
end
end
table.sort(list, function(a, b)
return a.position < b.position
end)
return list
end
-- 查找角包括放逐目标,不包括触发不灭的
function RoleManager.QueryIncludeExileNoNoDead(func, inCludeDeadRole)
local list = {}
local index = 1
if func then
for pos, v in pairs(PosList) do
if func(v) and (inCludeDeadRole or not v:IsRealDead()) and v.deadFilter then
list[index] = v
index = index + 1
end
end
end
table.sort(list, function(a, b)
return a.position < b.position
end)
return list
end
-- 查找角色(不包含死亡和拥有不灭的) 2020/11/28 王振兴
function RoleManager.QueryNoDead(func)
local list = {}
local index = 1
if func then
for pos, v in pairs(PosList) do
if func(v) and not v:IsRealDead() and v.deadFilter and not v.isExile then
list[index] = v
index = index + 1
end
end
end
table.sort(list, function(a, b)
return a.position < b.position
end)
return list
end
-- 查找角色(无排序,无死亡单位,无不灭) 2020/11/28 王振兴
function RoleManager.NoOrder(func)
local list = {}
local index = 1
if func then
for pos, v in pairs(PosList) do
if func(v) and not v:IsRealDead() and v.deadFilter and not v.isExile then
list[index] = v
index = index + 1
end
end
end
BattleUtil.RandomList(list)
return list
end
--对位规则: 1敌方相同对位 2若死亡或不存在选取相邻阵位最近且阵位索引最小的
function this.GetAggro(role)
-- 计算开始位置
local startPos = role.position
startPos = startPos > 3 and startPos - 3 or startPos
local target
local enemyCamp = role.camp == 0 and 1 or 0
-- c=0 前排 c=1 后排
for c = 0, 1 do
startPos = startPos + c*3
-- 向左
for i = startPos, c*3+1, -1 do
local pos = i + enemyCamp * 6
if PosList[pos] and not PosList[pos]:IsRealDead() and not PosList[pos].isExile then
target = PosList[pos]
break
end
end
if target then return target end
-- 向右
for i = startPos + 1, c*3+3 do
local pos = i + enemyCamp * 6
if PosList[pos] and not PosList[pos]:IsRealDead() and not PosList[pos].isExile then
target = PosList[pos]
break
end
end
if target then return target end
end
end
-- 获取没有进入死亡状态的仇恨目标
function this.GetAliveAggro(role)
-- 计算开始位置
local startPos = role.position
startPos = startPos > 3 and startPos - 3 or startPos
local target
local enemyCamp = role.camp == 0 and 1 or 0
-- c=0 前排 c=1 后排
for c = 0, 1 do
startPos = startPos + c*3
-- 向左
for i = startPos, c*3+1, -1 do
local pos = i + enemyCamp * 6
if PosList[pos] and not PosList[pos]:IsDead() and not PosList[pos].isExile then
target = PosList[pos]
break
end
end
if target then return target end
-- 向右
for i = startPos + 1, c*3+3 do
local pos = i + enemyCamp * 6
if PosList[pos] and not PosList[pos]:IsDead() and not PosList[pos].isExile then
target = PosList[pos]
break
end
end
if target then return target end
end
end
--对位规则: 1敌方相同对位 2若死亡或不存在选取相邻阵位最近且阵位索引最小的
function this.GetArrAggroList(role, arr)
-- 重构数据
local plist = {}
for _, role in ipairs(arr) do
plist[role.position] = role
end
-- 计算开始位置
local startPos = role.position
startPos = startPos > 3 and startPos - 3 or startPos
local targetList = {}
-- c=0 前排 c=1 后排
for c = 0, 1 do
startPos = startPos + c*3
-- 向左
for i = startPos, c*3+1, -1 do
local pos = i
if plist[pos] and not plist[pos]:IsRealDead() and not plist[pos].isExile then
table.insert(targetList, plist[pos])
end
end
-- 向右
for i = startPos + 1, c*3+3 do
local pos = i
if plist[pos] and not plist[pos]:IsRealDead() and not plist[pos].isExile then
table.insert(targetList, plist[pos])
end
end
end
table.sort(targetList, function(a, b)
return a.position < b.position
end)
return targetList
end
--对位规则: 1敌方相同对位 2若死亡或不存在选取相邻阵位最近且阵位索引最小的
function this.GetAggroHero(role,arr)
-- 计算开始位置
if not arr then
return
end
local startPos = role.position
local targetList={}
startPos = startPos > 3 and startPos - 3 or startPos
local smallRole=nil
local smallPos=0
local bigRole=nil
local bigPos=0
for _, v in ipairs(arr) do
local pos= v.position>3 and v.position -3 or v.position
if pos==startPos then
table.insert(targetList,v)
break
elseif pos<startPos then
if smallRole then
if pos>smallPos then
smallRole=v
smallPos=pos
end
else
smallRole=v
smallPos=pos
end
elseif pos>startPos then
if bigRole then
if pos<bigPos then
bigRole=v
bigPos=pos
end
else
bigRole=v
bigPos=pos
end
end
end
if #targetList==0 then
if smallRole then
table.insert(targetList,smallRole)
else
if bigRole then
table.insert(targetList,bigRole)
end
end
end
return targetList
end
--获取对位相邻站位的人 chooseType 1 我方 2 敌方(对位的敌人受到嘲讽的影响,若对位的敌人死亡,则选取相邻最近的作为目标)
function this.GetNeighbor(role, chooseType)
local posList = {}
local target
if chooseType == 1 then
target = role
else
if role.lockTarget and not role.lockTarget:IsRealDead() and not role.isExile then
target = role.lockTarget
else
target = this.GetAggro(role)
end
end
if target then
local list = this.Query(function (r) return r.camp == target.camp end)
for i=1, #list do
if not list[i]:IsRealDead() and not list[i].isExile then
if list[i].position == target.position + 3 -- 后排的人
or list[i].position == target.position - 3 -- 前排的人
or (math.abs(target.position - list[i].position) <= 1 and math.floor((target.position-1)/3) == math.floor((list[i].position-1)/3)) then -- 旁边的人和自己
table.insert(posList, list[i])
end
end
end
end
table.sort(posList, function(a, b)
return a.position < b.position
end)
return posList
end
function this.Clear()
for _, obj in pairs(PosList) do
obj:Dispose()
removeObjList:Add(obj)
end
PosList = {}
while removeObjList.size > 0 do
rolePool:Put(removeObjList.buffer[removeObjList.size])
removeObjList:Remove(removeObjList.size)
end
_DeadRoleList = {}
_ReliveRoleList = {}
RealDeadList = {}
end
-- 多波
function this.ClearEnemy()
local removePos = {}
for pos, obj in pairs(PosList) do
if obj.camp == 1 then
removePos[pos] = 1
BattleLogic.Event:DispatchEvent(BattleEventName.RemoveRole, obj)
obj:Dispose()
removeObjList:Add(obj)
end
end
for pos, _ in pairs(removePos) do
PosList[pos] = nil
end
end
return this