local BattleBuffHandle = require "app/module/battle/helper/battle_buff_handle" local BattleHelper = require "app/module/battle/helper/battle_helper" 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:getBuffList() return self.buffList 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 if unit:getIsLimit() then return callback() end self.mainUnit = unit 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 if unit:getIsLimit() then return callback() end self.mainUnit = unit unit:beforeAttack() unit:resetBeforeAttack() unit:useSkill(1, count, callback) end function BattleTeam:useAssistingSkill(assistingList, callback) if self.mainUnit == nil then return callback() end if self.mainUnit:getIsLimit() then return callback() end local count = #assistingList if count <= 0 then return callback() end self:setCentralizedAttack(true) local function finish() count = count - 1 if count == 0 then self:setCentralizedAttack(false) callback() end end local delay = 0 for _, v in ipairs(assistingList) do local unit = self.unitMap[v.skillMatch] if unit then unit:resetBeforeAttack() unit:useAssistingSkill(v.count, delay, finish) delay = delay + BattleHelper:getSupportInterval() else finish() end end end function BattleTeam:mainUnitUseAllSkills(callback) if self.mainUnit == nil then return callback() end if self.mainUnit:getIsLimit() then return callback() end 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 needReedRefreshBuff = false 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 if not needReedRefreshBuff and currShieldBuff.buff:getIcon() then needReedRefreshBuff = true end 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 if needReedRefreshBuff then self.battleController:refreshBuff(self.side, self.buffList) end end function BattleTeam:addBuff(buffEffect) table.insert(self.buffList, buffEffect) self:updateBuffState(buffEffect.buff, 1) if buffEffect.buff:getIcon() then self.battleController:refreshBuff(self.side, self.buffList) end end function BattleTeam:removeAllBuff() local buffEffect = nil local count = #self.buffList for i = count, 1, -1 do buffEffect = self.buffList[i] if buffEffect then self:updateBuffState(buffEffect.buff, -1) table.remove(self.buffList, i) BattleBuffHandle.removeBuff(self.mainUnit, buffEffect) end end self.battleController:clearBuff(self.side) 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 self.battleController:refreshBuff(self.side, self.buffList) 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:getIsBoss() if self.mainUnit == nil then return false end return self.mainUnit:getIsBoss() end function BattleTeam:playRunAction() if self.mainUnit then self.mainUnit:playRunAction() end end function BattleTeam:stopRunAction() if self.mainUnit then self.mainUnit:stopRunAction() end end function BattleTeam:recoverHpOnWaveOver(callback) if self.mainUnit then self.mainUnit:recoverHpOnWaveOver(callback) else callback() end end function BattleTeam:getCentralizedAttack() return self.centralizedAttack end function BattleTeam:setCentralizedAttack(centralizedAttack) self.centralizedAttack = centralizedAttack end function BattleTeam:tick(dt) for k, v in ipairs(self.unitList) do v:tick(dt) end end return BattleTeam