208 lines
4.6 KiB
Lua
208 lines
4.6 KiB
Lua
local BattleBuffHandle = require "app/module/battle/helper/battle_buff_handle"
|
|
|
|
local BattleTeam = class("BattleTeam")
|
|
|
|
function BattleTeam:init(side, battleController)
|
|
self.side = side
|
|
self.battleController = battleController
|
|
self.unitList = {}
|
|
self.unitMap = {}
|
|
self.buffList = {}
|
|
self.sameBuffCount = {}
|
|
self.shieldBuffList = {}
|
|
end
|
|
|
|
function BattleTeam:addUnit(unit, isMainUnit)
|
|
unit:setTeam(self)
|
|
self.unitMap[unit:getMatchType()] = unit
|
|
table.insert(self.unitList, unit)
|
|
if isMainUnit then
|
|
self.mainUnit = unit
|
|
end
|
|
end
|
|
|
|
function BattleTeam:prepare()
|
|
for k, v in ipairs(self.unitList) do
|
|
v:prepare()
|
|
end
|
|
end
|
|
|
|
function BattleTeam:getMainUnit()
|
|
return self.mainUnit
|
|
end
|
|
|
|
function BattleTeam:removeAllUnits()
|
|
for k, v in pairs(self.unitMap) do
|
|
self.unitMap[k] = nil
|
|
end
|
|
local count = #self.unitList
|
|
for i = 1, count do
|
|
self.unitList[i]:recycle()
|
|
table.remove(self.unitList)
|
|
end
|
|
self.mainUnit = nil
|
|
end
|
|
|
|
function BattleTeam:useNormalSkill(matchType, count, callback)
|
|
local unit = nil
|
|
if matchType == nil then
|
|
unit = self.unitList[1]
|
|
else
|
|
unit = self.unitMap[matchType]
|
|
end
|
|
if unit == nil then
|
|
return callback()
|
|
end
|
|
self.mainUnit = unit
|
|
self.battleController:setIsPauseHpProgress(true)
|
|
unit:beforeAttack()
|
|
unit:resetBeforeAttack()
|
|
unit:useNormalSkill(count, callback)
|
|
end
|
|
|
|
function BattleTeam:useSkill(matchType, count, callback)
|
|
local unit = nil
|
|
if matchType == nil then
|
|
unit = self.unitList[1]
|
|
else
|
|
unit = self.unitMap[matchType]
|
|
end
|
|
if unit == nil then
|
|
return callback()
|
|
end
|
|
self.mainUnit = unit
|
|
self.battleController:setIsPauseHpProgress(true)
|
|
unit:beforeAttack()
|
|
unit:resetBeforeAttack()
|
|
unit:useSkill(1, count, callback)
|
|
end
|
|
|
|
function BattleTeam:useAssistingSkill(assistingList, callback)
|
|
local count = #assistingList
|
|
if count <= 0 then
|
|
return callback()
|
|
end
|
|
local function finish()
|
|
count = count - 1
|
|
if count == 0 then
|
|
callback()
|
|
end
|
|
end
|
|
for _, v in ipairs(assistingList) do
|
|
local unit = self.unitMap[v.skillMatch]
|
|
if unit then
|
|
unit:resetBeforeAttack()
|
|
unit:useAssistingSkill(v.count, finish)
|
|
else
|
|
finish()
|
|
end
|
|
end
|
|
end
|
|
|
|
function BattleTeam:mainUnitUseAllSkills(callback)
|
|
self.battleController:setIsPauseHpProgress(true)
|
|
self.mainUnit:beforeAttack()
|
|
self.mainUnit:resetBeforeAttack()
|
|
self.mainUnit:useAllSkills(callback)
|
|
end
|
|
|
|
function BattleTeam:changeMainUnit(matchType)
|
|
if self.mainUnit and matchType == self.mainUnit:getMatchType() then
|
|
return
|
|
end
|
|
local unit = self.unitMap[matchType]
|
|
if unit == nil then
|
|
return
|
|
end
|
|
self.mainUnit:playSwitchOut()
|
|
self.mainUnit = unit
|
|
unit:playSwitchIn()
|
|
end
|
|
|
|
-- 回合结束的时候要结算buff和技能
|
|
function BattleTeam:onRoundEnd()
|
|
for k, v in ipairs(self.unitList) do
|
|
v:onRoundEnd()
|
|
end
|
|
self:doBuffWork()
|
|
end
|
|
|
|
function BattleTeam:addShield(buffEffect)
|
|
if buffEffect then
|
|
table.insert(self.shieldBuffList, buffEffect)
|
|
end
|
|
end
|
|
|
|
function BattleTeam:handleShield(reduceShield, unit)
|
|
if #self.shieldBuffList <= 0 then
|
|
return
|
|
end
|
|
|
|
local shieldNum = 0
|
|
local currShieldBuff = self.shieldBuffList[1]
|
|
while currShieldBuff do
|
|
reduceShield = reduceShield + currShieldBuff.result
|
|
if reduceShield > 0 then
|
|
shieldNum = shieldNum + reduceShield - currShieldBuff.result
|
|
currShieldBuff.result = reduceShield
|
|
reduceShield = 0
|
|
break
|
|
else
|
|
shieldNum = shieldNum - currShieldBuff.result
|
|
currShieldBuff.result = 0
|
|
for k, v in ipairs(self.buffList) do
|
|
if v == currShieldBuff then
|
|
self:updateBuffState(currShieldBuff.buff, -1)
|
|
BattleBuffHandle.removeBuff(unit, currShieldBuff)
|
|
currShieldBuff = nil
|
|
break
|
|
end
|
|
end
|
|
if currShieldBuff then
|
|
table.remove(self.shieldBuffList, 1)
|
|
end
|
|
end
|
|
currShieldBuff = self.shieldBuffList[1]
|
|
end
|
|
end
|
|
|
|
function BattleTeam:addBuff(buffEffect)
|
|
table.insert(self.buffList, buffEffect)
|
|
self:updateBuffState(buffEffect.buff, 1)
|
|
end
|
|
|
|
function BattleTeam:doBuffWork()
|
|
local count = #self.buffList
|
|
if count <= 0 then
|
|
return
|
|
end
|
|
local buffEffect = nil
|
|
for i = count, 1, -1 do
|
|
buffEffect = self.buffList[i]
|
|
buffEffect.round = buffEffect.round - 1
|
|
BattleBuffHandle.doBuffWork(self.mainUnit, buffEffect)
|
|
if buffEffect.round <= 0 then
|
|
self:updateBuffState(buffEffect.buff, -1)
|
|
table.remove(self.buffList, i)
|
|
BattleBuffHandle.removeBuff(self.mainUnit, buffEffect)
|
|
end
|
|
end
|
|
end
|
|
|
|
function BattleTeam:updateBuffState(buff, num)
|
|
local buffName = buff:getName()
|
|
local buffNum = (self.sameBuffCount[buffName] or 0) + num
|
|
self.sameBuffCount[buffName] = buffNum
|
|
end
|
|
|
|
function BattleTeam:getUnitComp()
|
|
return self.unitMap
|
|
end
|
|
|
|
function BattleTeam:tick(dt)
|
|
for k, v in ipairs(self.unitList) do
|
|
v:tick(dt)
|
|
end
|
|
end
|
|
|
|
return BattleTeam |