c1_lua/lua/app/module/battle/controller/battle_controller.lua
2023-05-11 15:17:42 +08:00

2314 lines
75 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local BattleHelper = require "app/module/battle/helper/battle_helper"
local BattleScheduler = require "app/module/battle/helper/battle_scheduler"
local BattleTeam = require "app/module/battle/team/battle_team"
local BattlePassive = require "app/module/battle/helper/battle_passive"
local BattlePool = require "app/module/battle/helper/battle_pool"
local BattleController = class("BattleController")
local BATTLE_BOARD_SKILL_HANDLE = require "app/module/battle/skill/battle_board_skill_handle"
local BATTLE_ROGUE_SKILL_HANDLE = require "app/module/battle/skill/battle_rogue_skill_handle"
local BattleBuffHandle = require "app/module/battle/helper/battle_buff_handle"
local ELIMINATION_TOUCH_EVENT = GConst.ELIMINATION_TOUCH_EVENT
local BattleConst = GConst.BattleConst
local BUFF_NAME_TO_ATTR = BattleConst.BUFF_NAME_TO_ATTR
-- *************各个子模块的战斗需要重写的方法 START*************
function BattleController:getChapterId()
return 0
end
function BattleController:getInitBoard()
return {}
end
function BattleController:getNotInvolvedSkills()
return {}
end
function BattleController:getSealElementType()
return {}
end
-- 战斗对应的ui
function BattleController:getBattleUIPath()
return "app/ui/battle/battle_ui"
end
-- 战斗结束
function BattleController:controllBattleEnd()
end
-- 不同模块的战斗需要初始化的东西
function BattleController:initOther()
end
-- 需要额外加载的资源
function BattleController:loadOtherRes(callback)
return callback()
end
-- 一共有多少波
function BattleController:getMaxWave()
return 1
end
function BattleController:getMinEliminationCount()
if not self.minEliminationCount then
self.minEliminationCount = GFunc.getConstIntValue("element_combo")
end
return self.minEliminationCount
end
function BattleController:initDefUnits(callback)
callback()
end
function BattleController:findNextDefUnit()
self:enterRoundEnd()
end
function BattleController:onDefDead()
self.defTeam:getMainUnit():playDead()
end
function BattleController:tick(dt)
end
function BattleController:onLinkChange()
self.battleUI:hideAllSfxLine()
local needFalsePosMap = {}
for posId, entity in pairs(self.battleData:getGridEnties()) do
if entity:getCell() then
needFalsePosMap[posId] = entity:getCell()
end
end
local sequence = self.battleData:getGridSequence()
local elementTypeMap = {}
local posIdMap = {}
local mainElementType
local count = 0
for _, info in ipairs(sequence) do
posIdMap[info.posId] = true
local entity = self.battleData:getGridEntity(info.posId)
if not entity:getSkillId() then
local elementType = entity:getElementType()
elementTypeMap[elementType] = (elementTypeMap[elementType] or 0) + 1
mainElementType = elementType
end
count = count + 1
end
for index, info in ipairs(sequence) do
local entity = self.battleData:getGridEntity(info.posId)
if entity:getCell() then
entity:getCell():showHighLight(true, mainElementType, self.battleUI)
end
if needFalsePosMap[info.posId] then
needFalsePosMap[info.posId] = nil
end
if index < count then
local nextPosId = sequence[index + 1].posId
self.battleUI:getSfxLine(index, function(obj)
local curPos = ModuleManager.BattleManager:getPosInfo(info.posId)
local nextPos = ModuleManager.BattleManager:getPosInfo(nextPosId)
local pos, z = ModuleManager.BattleManager:getPosCenterAndDir(curPos, nextPos)
obj:setLocalScale(25, 25, 25)
obj:setLocalPosition(pos.x, pos.y, 0)
obj:setEulerAngles(0, 0, z)
end)
end
if index == count then
if entity:getCell() then
entity:getCell():showAni()
end
end
end
for posId, cell in pairs(needFalsePosMap) do
cell:showHighLight(false)
end
local influenceMap = {}
for posId, info in pairs(self.battleData:getSkillInfluenceGrids()) do
local entity = self.battleData:getGridEntity(posId)
if not influenceMap[posId] and info.direction ~= BattleConst.BOARD_RANGE_TYPE.RANDOM then
posIdMap[posId] = true
influenceMap[posId] = true
if not entity:getSkillId() then
local elementType = entity:getElementType()
elementTypeMap[elementType] = (elementTypeMap[elementType] or 0) + 1
end
if entity:getCell() then
entity:getCell():showCircle(true)
end
end
end
self.battleUI:refreshSkill(elementTypeMap, count > 0)
if mainElementType then
self.atkTeam:changeMainUnit(mainElementType)
end
end
function BattleController:postWaveOver(atkDead, isQuit)
end
function BattleController:postFightStart()
end
function BattleController:onEliminationBegin()
end
---- 障碍格子图片
function BattleController:getBlockIcon()
return "battle_hinder_4"
end
function BattleController:getChessBoardBgName()
return "chessboard_1"
end
-- *************各个子模块的战斗需要重写的方法 END*************
function BattleController:ctor()
self.battleData = DataManager.BattleData
end
function BattleController:init(params)
params = params or {}
self.battleType = params.battleType or GConst.BattleConst.BATTLE_TYPE.STAGE
self.battleStartTime = Time:getServerTime()
self.waveStartTime = Time:getServerTime()
self.eliminateCount = 0
self.eliminateTotalCount = 0
self.realTime = 0
self.chapterId = self:getChapterId()
self.waveIndex = 0
self.maxWaveIndex = self:getMaxWave()
self.victory = false
self.roundStep = BattleConst.BATTLE_ROUND_STEP.WAIT_BEGIN
self.effectTexts = {}
self.instructions = {}
self.delayEffectTextList = {}
self.delayEffectTextCount = 0
self.time = 0
self.battleData:init()
BattleScheduler:init()
BattlePool:init()
BattleHelper:init()
BattlePassive:init()
self:setTimeScale(DataManager.BattleData:getTimeScale())
self:bindData()
self:initBattleTeam()
self:initOther()
self:prepareFight()
end
function BattleController:bindData()
DataManager.BattleData:bind("timeSpeed", ModuleManager.BattleManager, function()
self:setTimeScale(DataManager.BattleData:getTimeScale())
end, false)
end
function BattleController:unBindAll()
DataManager.BattleData:unBind("timeSpeed", ModuleManager.BattleManager)
end
function BattleController:initBattleTeam()
self.atkTeam = BattleTeam:create()
self.atkTeam:init(BattleConst.SIDE_ATK, self)
self.defTeam = BattleTeam:create()
self.defTeam:init(BattleConst.SIDE_DEF, self)
end
function BattleController:setIsPauseHpProgress(value)
self.battleUI:setIsPauseHpProgress(value)
end
function BattleController:moveBattlefield(time)
self.battleUI:moveBattlefield(time)
end
function BattleController:refreshBuff(side, buffList)
self.battleUI:refreshBuff(side, buffList)
end
function BattleController:clearBuff(side)
self.battleUI:clearBuff(side)
end
function BattleController:showBuffTips(side)
if side == BattleConst.SIDE_ATK then
local buffList = self.atkTeam:getBuffList()
if #buffList <= 0 then
return
end
self.battleUI:showLeftBuffTips(buffList)
else
local buffList = self.defTeam:getBuffList()
if #buffList <= 0 then
return
end
self.battleUI:showRightBuffTips(buffList)
end
end
function BattleController:showCombo(count)
self.battleUI:showCombo(count)
end
function BattleController:hideCombo()
self.battleUI:hideCombo()
end
function BattleController:prepareFight()
local count = 0
local totalCount = 3
local function onPreloadFinished()
count = count + 1
if count == totalCount then
self:onLoadComplete()
end
end
UIManager:closeAllUI()
self.battleUI = UIManager:showUI(self:getBattleUIPath())
self.battleUI:setController(self)
self.battleUI:addLoadUICompleteListener(function()
BattleHelper:setEffectTextCache(self.battleUI:getBattleNumberRed(),
self.battleUI:getBattleNumberGreen(),
self.battleUI:getBattleNumberBlue())
self:initAtkUnits(onPreloadFinished)
self:initDefUnits(onPreloadFinished)
self.battleUI:refreshChessBoard(self:getChessBoardBgName())
end)
BattleHelper:setBaseOrder(self.battleUI:getUIOrder())
self:loadOtherRes(onPreloadFinished)
end
function BattleController:initAtkUnits(callback)
local atkTeamEntity = self.battleData:getAtkTeam()
local count = 0
local totalCount = atkTeamEntity:getMembersCount()
local function onloadFinished()
count = count + 1
if count == totalCount then
self.battleUI:refreshAtkHp(atkTeamEntity:getHp(), atkTeamEntity:getHpPercent())
callback()
end
end
local members = atkTeamEntity:getAllMembers()
for k, v in pairs(members) do
local modelId = v:getModelId()
BattleHelper:loadBattleHeroModel(modelId, self.battleUI:getBattleNode(), function(spineObject)
local heroComp = spineObject:addLuaComponent(BattleConst.TYPEOF_LUA_COMP.BATTLE_HERO_COMPONENT)
heroComp:initWithEntity(modelId, v, self)
if v:getIsMainUnit() then
self.atkTeam:addUnit(heroComp, true)
else
self.atkTeam:addUnit(heroComp)
heroComp:hideOutsideScreen()
end
onloadFinished()
end)
end
if totalCount == 0 then
callback()
end
end
function BattleController:refreshHp(side, num, percent)
if side == BattleConst.SIDE_ATK then
self.battleUI:refreshAtkHp(num, percent)
else
self.battleUI:refreshDefHp(num, percent)
end
end
function BattleController:getOtherSideMainUnit(side)
if side == BattleConst.SIDE_ATK then
return self.defTeam:getMainUnit()
else
return self.atkTeam:getMainUnit()
end
end
function BattleController:onLoadComplete()
self:battleStart()
end
function BattleController:battleStart()
self.atkTeam:prepare()
self.defTeam:prepare()
self.isBattleStart = true
self.tickSid = BattleScheduler:scheduleGlobal(function(dt)
self:_tick(dt)
end, 0)
self:enterNextWave()
end
function BattleController:addTimeSpeed()
if self.skillSlowDownDuration then
return
end
DataManager.BattleData:addTimeSpeed()
end
function BattleController:resetTimeSpeed()
DataManager.BattleData:resetTimeSpeed()
if self.skillSlowDownDuration then
self.skillSlowDownDuration = nil
end
end
function BattleController:setSkillSlowDown(timeScale, duration)
self.skillSlowDownDuration = duration
DataManager.BattleData:setSkillTimeSpeed(timeScale)
end
function BattleController:setTimeScale(timeScale)
GFunc.setDOTweenTimeScale(GConst.DOTWEEN_IDS.BATTLE, timeScale)
BattleScheduler:setTimeScale(timeScale)
BattleHelper:setTimeScale(timeScale)
end
function BattleController:_findNextDefUnit()
if self.isBossWave then -- 如果上一波是boss波次则重新生成棋盘
self:putBoardCacheSkill(function()
self.atkTeam:onRoundEnd()
self:findNextDefUnit()
self:generateBoard()
end)
else
self.atkTeam:onRoundEnd()
self:findNextDefUnit()
end
end
---- start 回合步骤
function BattleController:enterNextWave()
local atkTeam = self.battleData:getAtkTeam()
if not atkTeam or atkTeam:getIsDead() then
self.victory = false
self:postWaveOver(true)
self:battleEnd()
return
end
if self.waveIndex >= self.maxWaveIndex then
self.victory = true
self:postWaveOver(false)
self:battleEnd()
return
end
if self.waveIndex ~= 0 then -- 第一波
self:postWaveOver(false)
end
self.waveIndex = self.waveIndex + 1
if self.battleUI then
self.battleUI:refreshWave(self.waveIndex)
end
if self.waveIndex == 1 then -- 第一波
self:generateBoard(true)
else
-- 上报关卡结束
self.defTeam:prepare()
end
self.waveStartTime = Time:getServerTime()
self.eliminateCount = 0
self.isBossWave = self.defTeam:getMainUnit().unitEntity:getIsBoss()
self:postFightStart()
self:enterRoundBegin()
end
function BattleController:enterRoundBegin()
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_BEGIN
self:enterEliminationBegin()
end
function BattleController:enterEliminationBegin()
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_BEGIN
self:onEliminationBegin()
self:enterRefreshBoard(true)
end
function BattleController:enterElimination(needDelay)
if self.showSelectSkillSid then
ModuleManager.BattleManager:unscheduleGlobal(self.showSelectSkillSid)
self.showSelectSkillSid = nil
end
if self.battleData:useAddlvCount() then
local skillList = self:getRandomSkillList()
if needDelay then
self.showSelectSkillSid = ModuleManager.BattleManager:performWithDelayGlobal(function()
self.battleUI:showSelectSkillComp(skillList)
self.showSelectSkillSid = nil
end, 0.3)
else
self.battleUI:showSelectSkillComp(skillList)
end
local totalTime = Time:getServerTime() - self.battleStartTime
BIReport:postShowFightSkillSelect(self.battleType, skillList, self.chapterId, totalTime, self.waveIndex)
else
self.battleUI:hideAllBoardSfxs()
-- 检查棋盘
local find, pathList = self:findAttention()
if not find then -- 如果没找到,就要打乱棋盘
local changeInfo = self:shuffleBoard()
if changeInfo then
self.battleData:setGridEntitiesPosId(changeInfo)
self.battleUI:shuffleBoard(changeInfo, function()
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_ELIMINATION
end)
else
local resetList = {}
for posId, entity in pairs(self.battleData:getGridEnties()) do
if entity:isEmptyIdle() then
table.insert(resetList, posId)
end
end
local resetInfo = self:resetGrids(resetList)
if not resetInfo then
if EDITOR_MODE then
Logger.logHighlight("----- 处于无法消除状态 -----")
end
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_ELIMINATION
else
if self.resetGridSid then
ModuleManager.BattleManager:unscheduleGlobal(self.resetGridSid)
self.resetGridSid = nil
end
self.resetGridSid = ModuleManager.BattleManager:performWithDelayGlobal(function()
local count = 1
for posId, info in pairs(resetInfo) do
self.battleData:setGridInfo(posId, info)
self.battleUI:playChangeElementSfx(posId, count)
count = count + 1
end
self.resetGridSid = nil
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_ELIMINATION
end, 0.5)
end
end
else
self.attentionList = pathList
-- ModuleManager.BattleManager:performWithDelayGlobal(function()
-- for _, posId in ipairs(pathList) do
-- local entity = self.battleData:getGridEntity(posId)
-- if entity and entity:getCell() then
-- entity:getCell():showAni()
-- end
-- end
-- end, 2)
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_ELIMINATION
end
end
end
function BattleController:enterBattleStep()
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_TEAM_ACTION
if not self.battleTeamActionList then
self.battleTeamActionList = {}
else
for i = #self.battleTeamActionList, 1, -1 do
self.battleTeamActionList[i] = nil
end
end
local atkAction = function()
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_ATK_STEP
self:exeInstructions(function()
self:enterNextTeamAction()
end)
end
local defAction = function()
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_DEF_STEP
self.defTeam:mainUnitUseAllSkills(function()
self:enterNextTeamAction()
end)
end
if self.battleData:getAtkTeam():getFirstHand() < self.battleData:getDefTeam():getFirstHand() then
self:addTeamActionList(defAction, 1)
self:addTeamActionList(atkAction, 2)
else
self:addTeamActionList(atkAction, 1)
self:addTeamActionList(defAction, 2)
end
self:enterNextTeamAction()
end
function BattleController:addTeamActionList(func, index)
if not self.battleTeamActionList then
self.battleTeamActionList = {}
end
index = index or 1
table.insert(self.battleTeamActionList, index, func)
end
function BattleController:enterNextTeamAction()
self.atkTeam:onActionOver()
self.defTeam:onActionOver()
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_TEAM_ACTION_OVER
self:hideCombo()
local atkTeam = self.battleData:getAtkTeam()
if not atkTeam or atkTeam:getIsDead() then -- 英雄死了, 直接结算
self:enterNextWave()
return
end
local defTeam = self.battleData:getDefTeam()
if not defTeam or defTeam:getIsDead() then -- 怪物死了, 直接进入刷新逻辑
if self.waveIndex >= self.maxWaveIndex then
self:enterRoundEnd()
else
self.defTeam:removeAllBuff()
self:onDefDead()
self:_findNextDefUnit()
end
return
end
if not self.battleTeamActionList or not self.battleTeamActionList[1] then
self:enterRoundEnd()
return
end
local action = table.remove(self.battleTeamActionList, 1)
action()
end
function BattleController:getIsAtkStep()
return self.roundStep == BattleConst.BATTLE_ROUND_STEP.ON_ATK_STEP
end
function BattleController:enterRefreshBoard(isRoundBeginCheck)
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_REFRESH_BOARD
self:fillBoard(isRoundBeginCheck)
end
function BattleController:enterRoundEnd()
self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_END
local defTeam = self.battleData:getDefTeam()
if not defTeam or defTeam:getIsDead() then -- 怪物死了, 直接进入刷新逻辑
self.atkTeam:onRoundEnd()
self:enterNextWave()
return
end
local atkTeam = self.battleData:getAtkTeam()
if not atkTeam or atkTeam:getIsDead() then -- 英雄死了, 直接结算
self.defTeam:onRoundEnd()
self:enterNextWave()
return
end
self.atkTeam:onRoundEnd()
self.defTeam:onRoundEnd()
self:onRoundEnd()
end
function BattleController:onRoundEnd(toNextWave)
if toNextWave then
self:enterNextWave()
else
self:enterRoundBegin()
end
end
---- end回合步骤
function BattleController:onTouchEvent(eventType, posId, isVirtual)
if self.roundStep ~= BattleConst.BATTLE_ROUND_STEP.ON_ELIMINATION then
return
end
local entity = self.battleData:getGridEntity(posId)
if not entity or not entity:canLink() then
return
end
if eventType == ELIMINATION_TOUCH_EVENT.DOWN then
self:onLinkStart(entity, posId, isVirtual)
elseif eventType == ELIMINATION_TOUCH_EVENT.ENTER then
if self.battleData:getIsVirtual() and not isVirtual then
return
end
self:onLinkEnter(entity, posId, isVirtual)
elseif eventType == ELIMINATION_TOUCH_EVENT.EXIT then
else -- 取消和抬起
self:onLinkCancelOrUp(entity, posId, isVirtual)
end
end
function BattleController:onLinkStart(entity, posId, isVirtual)
if DataManager.TutorialData:getIsInTutorial() then
local elementList = DataManager.TutorialData:getTargetElement()
if elementList then
local find = false
for _, curPosId in ipairs(elementList) do
if curPosId == posId then
find = true
break
end
end
if not find then
return
end
end
end
if not isVirtual then
self.battleUI:showTutorialFinger()
end
if #self.battleData:getGridSequence() > 0 then
self:clearGridSequence()
end
self.battleData:insertGridSequence(posId, self:snapshotBoard(), isVirtual)
local skillEntity = self.battleData:getSkillEntityBySkillId(entity:getSkillId())
local maskElementType = entity:getElementType(skillEntity)
self.battleUI:showBoardMask(maskElementType, skillEntity and entity:getPosId())
self:findSkillInfluenceGrids()
self:onLinkChange()
if not isVirtual then
AudioManager:playEffect(AudioManager.EFFECT_ID.LINK_BO_1)
if skillEntity then
AudioManager:playEffect(AudioManager.EFFECT_ID.LINK_SKILL)
end
end
end
function BattleController:onLinkEnter(entity, posId, isVirtual)
local sequence = self.battleData:getGridSequence()
local sequenceCount = #sequence
local info = sequence[sequenceCount]
local lastPosId = info and info.posId
if not lastPosId then
return
end
local outLineMap = BattleConst.GRID_OUT_LINE_POS_ID[lastPosId]
if not outLineMap or not outLineMap[posId] then
return
end
if self.battleData:alreadyInsertSequence(posId) then
local info = sequence[#sequence - 1]
local beforePosId = info and info.posId
if beforePosId == posId then -- 进入的是倒数第二个,则移除倒数第一个
local snapshot = self.battleData:removeGridSequence(lastPosId)
if snapshot then -- 如果有快照,则恢复一次
for posId, info in pairs(snapshot) do
local entity = self.battleData:getGridEntity(posId)
if entity then
entity:setInfoBySnapshop(info)
end
end
end
local lastEntity = self.battleData:getGridEntity(beforePosId)
local lastSkillEntity = self.battleData:getSkillEntityBySkillId(lastEntity:getSkillId())
local maskElementType = lastEntity:getElementType(lastSkillEntity)
local _, skillPosId = self.battleData:getSequenceHadSkill()
self.battleUI:showBoardMask(maskElementType, skillPosId)
self:findSkillInfluenceGrids()
self:onLinkChange()
end
return
end
local skillId = entity:getSkillId()
local skillEntity = self.battleData:getSkillEntityBySkillId(skillId)
local elementType = entity:getElementType(skillEntity)
local lastEntity = self.battleData:getGridEntity(lastPosId)
local lastSkillId = lastEntity:getSkillId()
local lastSkillEntity = self.battleData:getSkillEntityBySkillId(lastSkillId)
local lastElementType = lastEntity:getElementType(lastSkillEntity)
local hadSkillId, skillPosId = self.battleData:getSequenceHadSkill()
if skillEntity and hadSkillId then
return
end
if not elementType or not lastElementType then
else
if lastElementType ~= elementType then
return
end
end
local maskElementType = elementType or lastElementType
if not skillPosId then
if skillId then
skillPosId = posId
end
end
self.battleUI:showBoardMask(maskElementType, skillPosId)
self.battleData:insertGridSequence(posId, self:snapshotBoard(), isVirtual)
if lastEntity:getNeedChangePos() and not entity:getNeedChangePos() then -- 需要移动到队列末尾
local lastSkillId = lastEntity:getSkillId()
local skillId = entity:getSkillId()
self:setGridSkillId(lastPosId, skillId)
self:setGridSkillId(posId, lastSkillId)
end
local newElementType = elementType or lastElementType
if newElementType then
entity:setElementType(newElementType)
lastEntity:setElementType(newElementType)
end
entity:addLinkSkillCount()
self:findSkillInfluenceGrids()
self:onLinkChange()
if not isVirtual then
if sequenceCount + 1 == self:getMinEliminationCount() then
AudioManager:playEffect(AudioManager.EFFECT_ID.LINK_READY)
end
AudioManager:playEffect(AudioManager:getBoEffectID(sequenceCount + 1))
if skillEntity then
AudioManager:playEffect(AudioManager.EFFECT_ID.LINK_SKILL)
end
end
end
function BattleController:onLinkCancelOrUp(entity, posId, isVirtual)
if isVirtual then
return
end
AudioManager:playEffect(AudioManager.EFFECT_ID.LINK_OVER)
local tutorialElementList
if DataManager.TutorialData:getIsInTutorial() then
tutorialElementList = DataManager.TutorialData:getTargetElement()
end
self.battleUI:showBoardMask(nil)
local sequence = self.battleData:getGridSequence()
local count = #sequence
if count < self:getMinEliminationCount() then
self:clearGridSequence()
if tutorialElementList then
self.battleUI:showTutorialFinger(tutorialElementList)
end
return
end
if tutorialElementList then
local posIdmap = {}
for _, info in ipairs(sequence) do
posIdmap[info.posId] = true
end
EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.ELIMINATION_OVER, posIdmap)
else
self:onLinkOver()
end
end
function BattleController:clearGridSequence()
local sequence = self.battleData:getGridSequence()
local count = #sequence
if count <= 0 then
self.battleData:clearGridSequence()
self:onLinkChange()
return
end
local snapshot = self.battleData:getFirstSequenceSnapshot()
if snapshot then -- 如果有快照,则恢复一次
for posId, info in pairs(snapshot) do
local entity = self.battleData:getGridEntity(posId)
if entity then
entity:setInfoBySnapshop(info)
end
end
end
self.battleData:clearGridSequence()
self:onLinkChange()
end
function BattleController:onLinkOver()
self.battleUI:hideAllSfxLine()
local sequence = self.battleData:getGridSequence()
local count = #sequence
if count < self:getMinEliminationCount() then
return
end
local skillId = self.battleData:getSequenceHadSkill()
local skillEntity
local linkElementType
local influenceElementType
if skillId then
skillEntity = self.battleData:getSkillEntityBySkillId(skillId)
end
local elementTypeMap = {}
local eliminationPosIds = {}
local boomGridIds = {}
local lineCount = 0
local skillPosId
local randomPosList
for _, info in ipairs(sequence) do
if not eliminationPosIds[info.posId] then
local entity = self.battleData:getGridEntity(info.posId)
if not entity:getSkillId() then
local elementType = entity:getElementType()
elementTypeMap[elementType] = (elementTypeMap[elementType] or 0) + 1
linkElementType = elementType
lineCount = lineCount + 1
else
skillPosId = info.posId
end
boomGridIds[info.posId] = BattleConst.AROUND_BOOM_TYPE.LINE
local outline = BattleConst.UP_DOWN_LEFT_RIGHT[info.posId]
for _, aroundPosId in ipairs(outline) do
if not boomGridIds[aroundPosId] then
boomGridIds[aroundPosId] = BattleConst.AROUND_BOOM_TYPE.ACOUND
end
end
local entity = self.battleData:getGridEntity(info.posId)
entity:setIsIdle(true)
eliminationPosIds[info.posId] = true
end
end
for posId, info in pairs(self.battleData:getSkillInfluenceGrids()) do
local entity = self.battleData:getGridEntity(posId)
if not entity:getIsIdle() then
if entity:isEmptyIdle() then
entity:setIsIdle(true)
if not eliminationPosIds[posId] then
eliminationPosIds[posId] = true
table.insert(sequence, {posId = posId})
if not entity:getSkillId() then
local elementType = entity:getElementType()
elementTypeMap[elementType] = (elementTypeMap[elementType] or 0) + 1
end
end
if info.direction == BattleConst.BOARD_RANGE_TYPE.RANDOM then
if not randomPosList then
randomPosList = {}
end
table.insert(randomPosList, posId)
end
if not entity:getSkillId() then
local elementType = entity:getElementType()
if not influenceElementType then
influenceElementType = {}
end
influenceElementType[elementType] = true
end
else
boomGridIds[posId] = BattleConst.AROUND_BOOM_TYPE.SKILL
end
end
end
local gridBreakSfxInfo
for posId, boomType in pairs(boomGridIds) do
local entity = self.battleData:getGridEntity(posId)
if entity then
local gridType = entity:getGridType()
local elementTypeInvalid = entity:isElmentTypeInvalid()
if entity:addAroundEliminationCount(boomType) then
if BattleConst.GRID_TYPE_BREAK_SFX[gridType] then
if not gridBreakSfxInfo then
gridBreakSfxInfo = {}
end
if not gridBreakSfxInfo[gridType] then
gridBreakSfxInfo[gridType] = {}
end
table.insert(gridBreakSfxInfo[gridType], posId)
end
if entity:getIsIdle() then
if not eliminationPosIds[posId] then
eliminationPosIds[posId] = true
table.insert(sequence, {posId = posId, noAni = elementTypeInvalid})
if not entity:getSkillId() and not elementTypeInvalid then
local elementType = entity:getElementType()
elementTypeMap[elementType] = (elementTypeMap[elementType] or 0) + 1
end
end
end
else
if entity:getIsIdle() then
if BattleConst.GRID_TYPE_BREAK_SFX[gridType] then
if not gridBreakSfxInfo then
gridBreakSfxInfo = {}
end
if not gridBreakSfxInfo[gridType] then
gridBreakSfxInfo[gridType] = {}
end
table.insert(gridBreakSfxInfo[gridType], posId)
end
end
end
end
end
---- 播放碎裂特效
if gridBreakSfxInfo then
for gridType, list in pairs(gridBreakSfxInfo) do
for index, posId in ipairs(list) do
self.battleUI:getSfxGridBreak(gridType, index, function(obj)
local pos = ModuleManager.BattleManager:getPosInfo(posId)
obj:setLocalScale(38, 38, 38)
obj:setLocalPosition(pos.x, pos.y, 0)
obj:play()
end)
end
end
end
if skillPosId then
local rangeList = skillEntity:getBoardRange()
self.battleUI:playSkillLineSfx(skillPosId, rangeList, randomPosList)
end
self.battleData:addSkillEnergy(elementTypeMap)
self.battleData:clearGridSequence()
self.battleUI:disableUITouch()
self.battleUI:eliminationAni(sequence, function()
self:generateInstructions(skillEntity, linkElementType, lineCount, influenceElementType, elementTypeMap)
self:enterRefreshBoard()
end)
self.eliminateCount = self.eliminateCount + 1
self.eliminateTotalCount = self.eliminateTotalCount + 1
end
function BattleController:fillBoard(isRoundBeginCheck)
local pathMap = {}
local columnCount = {}
local gridMap = {}
for c = 1, BattleConst.COLUMN_COUNT do
for r = BattleConst.ROW_COUNT, 1, -1 do
local posId = ModuleManager.BattleManager:getPosId(r, c)
local entity = self.battleData:getGridEntity(posId)
if entity:getIsIdle() then
self:fillThisPos(posId, columnCount, gridMap)
end
end
end
while true do
local find = false
for c = 1, BattleConst.COLUMN_COUNT do
local list = gridMap[c]
if list and list[1] then -- 此列有需要填充的元素
local entity = table.remove(list, 1)
if entity then
self.battleData:setGridInfo(entity:getPosId(), self:getRandomGridInfo())
end
find = true
end
end
if not find then
break
end
end
for c = 1, BattleConst.COLUMN_COUNT do
for r = BattleConst.ROW_COUNT, 1, -1 do
local posId = ModuleManager.BattleManager:getPosId(r, c)
local entity = self.battleData:getGridEntity(posId)
if #entity:getPath() > 0 then
pathMap[posId] = entity:getPath()
entity:clearPath()
end
end
end
self.battleUI:fallGrid(pathMap, function()
self:onFillBoardOver(isRoundBeginCheck)
self.battleUI:enableUITouch()
end)
end
function BattleController:onFillBoardOver(isRoundBeginCheck)
if isRoundBeginCheck then
EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.BOARD_FILL_OVER)
self:popBoardCacheSkill(function()
self:enterElimination(true)
end)
else
self:generateSkill(function()
self.battleUI:refreshSkill()
self:enterBattleStep()
end)
end
end
function BattleController:generateInstructions(skillEntity, elementType, lineCount, influenceElementTypeMap, elementTypeMap)
local elementTypeCount = 0
-- local assistingList = nil
---- 援助
for element, count in pairs(elementTypeMap) do
-- if element == elementType then
elementTypeCount = elementTypeCount + count
-- else
-- if assistingList == nil then
-- assistingList = {}
-- end
-- local obj = {
-- count = count,
-- skillMatch = element,
-- }
-- table.insert(assistingList, obj)
-- end
end
---- 技能
if skillEntity then
if elementType == skillEntity:getPosition() then
table.insert(self.instructions, {
name = BattleConst.INSTRUCTION_NAME.PLAY_SKILL,
skillMatch = elementType,
count = elementTypeCount,
})
else
if skillEntity:getEffect() ~= nil then
table.insert(self.instructions, {
name = BattleConst.INSTRUCTION_NAME.PLAY_SKILL,
skillMatch = skillEntity:getPosition(),
count = 0,
})
end
if elementTypeCount > 0 then
table.insert(self.instructions, {
name = BattleConst.INSTRUCTION_NAME.GENERAL_ATTACK,
skillMatch = elementType,
count = elementTypeCount,
})
end
end
else
---- 普攻
if elementTypeCount > 0 then
table.insert(self.instructions, {
name = BattleConst.INSTRUCTION_NAME.GENERAL_ATTACK,
skillMatch = elementType,
count = elementTypeCount,
})
end
end
-- if assistingList then
-- table.insert(self.instructions, {
-- name = BattleConst.INSTRUCTION_NAME.ASSISTING,
-- assistingList = assistingList,
-- })
-- end
---- 加buff
if skillEntity then
local eliminateEffects = skillEntity:getEliminateEffects(self)
if eliminateEffects then
local effectList
for _, buffEntity in ipairs(eliminateEffects) do
if not effectList then
effectList = {}
end
table.insert(effectList, buffEntity)
end
if effectList then
local unit = {
name = BattleConst.INSTRUCTION_NAME.ADD_CUR_ROUND_ATTR,
effectList = effectList
}
table.insert(self.instructions, unit)
end
end
if skillEntity:getLinkEffects() and elementType then
local effectList
for type, buffEntities in pairs(skillEntity:getLinkEffects()) do
local buffEntity = buffEntities[elementType]
if buffEntity then
if not effectList then
effectList = {}
end
table.insert(effectList, buffEntity)
end
end
if effectList then
local unit = {
name = BattleConst.INSTRUCTION_NAME.ADD_CUR_ROUND_ATTR,
effectList = effectList
}
table.insert(self.instructions, unit)
end
end
if skillEntity:getInInfluenceEffects() and influenceElementTypeMap then
for influenceElementType , _ in pairs(influenceElementTypeMap) do
local effectList
for type, buffEntities in pairs(skillEntity:getInInfluenceEffects()) do
local buffEntity = buffEntities[influenceElementType]
if buffEntity then
if not effectList then
effectList = {}
end
table.insert(effectList, buffEntity)
end
end
if effectList then
table.insert(self.instructions, {
name = BattleConst.INSTRUCTION_NAME.ADD_CUR_ROUND_ATTR,
effectList = effectList
})
end
end
end
if skillEntity:getElementCountEffect() and elementType then
local effectList
for type, buffEntities in pairs(skillEntity:getElementCountEffect()) do
local originBuffEntity = buffEntities.origin
local useBuffEntity = buffEntities.use
if not effectList then
effectList = {}
end
local newNum = originBuffEntity:getEffectNum() * lineCount
useBuffEntity:setEffectNum(newNum)
table.insert(effectList, useBuffEntity)
end
if effectList then
table.insert(self.instructions, {
name = BattleConst.INSTRUCTION_NAME.ADD_CUR_ROUND_ATTR,
effectList = effectList
})
end
end
--
if skillEntity:getSkillAttackBeforeEffects() and elementType then
local effectList
for type, buffEntity in pairs(skillEntity:getSkillAttackBeforeEffects()) do
if not effectList then
effectList = {}
end
table.insert(effectList, buffEntity)
end
if effectList then
table.insert(self.instructions, {
name = BattleConst.INSTRUCTION_NAME.ADD_CUR_ROUND_ATTR,
effectList = effectList
})
end
end
end
end
function BattleController:exeInstructions(callback)
if EDITOR_MODE then
Logger.logHighlight("--------exeInstructions----------")
end
if not self.instructions or #self.instructions <= 0 then
callback()
return
end
local instruction = table.remove(self.instructions)
local func = BattleController._doInstruction[instruction.name]
if func then
func(self, instruction, function()
DataManager.BattleData:resetTimeSpeed()
self:exeInstructions(callback)
end)
else
self:exeInstructions(callback)
end
end
function BattleController:generateBoard(isFirst)
local boardList, _ = self:getInitBoard()
if self.curBoardIndex and self.curBoardIndex >= #boardList then
return
end
if not self.battleUI then
return
end
self.curBoardIndex = (self.curBoardIndex or 0) + 1
local board = boardList[self.curBoardIndex]
self.battleUI:switchBoard(function()
self.battleData:refreshBoard(board, self:getBlockIcon())
self.battleUI:initGridCell()
end, function()
-- self:enterRoundBegin()
end, isFirst)
end
function BattleController:putBoardCacheSkill(callback)
if not self.battleUI then
return
end
self.battleData:cacheBoardSkill()
local skillList, skillCount = self.battleData:getCacheBoardSkill()
self.battleUI:cacheSkillAni(skillList, false, callback)
end
function BattleController:popBoardCacheSkill(callback)
local skillList, skillCount = self.battleData:getCacheBoardSkill()
self.battleData:clearCacheBoardSkill()
if self.battleUI and skillCount > 0 then
local posidList = {}
for posId, entity in pairs(self.battleData:getGridEnties()) do
if entity:isEmptyIdle() and not entity:getIsIdle() then
table.insert(posidList, posId)
end
end
posidList = table.shuffle(posidList)
local newSkillId = {}
for index, info in ipairs(skillList) do
if posidList[1] then
newSkillId[index] = info
newSkillId[index].posId = table.remove(posidList)
else
break
end
end
self.battleUI:cacheSkillAni(newSkillId, true, callback)
else
if callback then
callback()
end
end
end
function BattleController:generateSkill(callback)
local map = {}
local excludeMap = {}
for _, skillEntity in pairs(self.battleData:getSkillEntities()) do
if skillEntity:getEnergyEnough() then
local list = self:getSkillElementList(skillEntity:getPosition(), 1, true, excludeMap)
if list[1] then
map[skillEntity:getPosition()] =
{
skillId = skillEntity:getSkillId(),
posId = list[1]
}
excludeMap[list[1]] = true
end
end
end
if not self.battleUI then
if callback then
callback()
end
return
end
self.battleUI:generateSkillAni(map, function()
for elementType, info in pairs(map) do
self:setGridSkillId(info.posId, info.skillId)
end
if callback then
callback()
end
end)
end
function BattleController:setGridSkillId(posId, skillId)
local entity = self.battleData:getGridEntity(posId)
if entity then
entity:setSkilId(skillId)
local skillEntity = self.battleData:getSkillEntityBySkillId(skillId)
if skillEntity and entity:getElementType() ~= skillEntity:getPosition() then
entity:setElementType(skillEntity:getPosition())
end
end
end
function BattleController:generateGridType(skillTypeParameter, monsterPos)
if not skillTypeParameter then
return
end
local list = {}
local count = 0
for posId, entity in pairs(self.battleData:getGridEnties()) do
if entity:isEmptyIdle() then
table.insert(list, entity)
count = count + 1
end
end
list = table.shuffle(list)
if count > 0 then
local map = {}
local minCount = math.min(skillTypeParameter[2], count)
for i = minCount, 1, -1 do
local entity = table.remove(list, math.random(1, i))
map[entity:getPosId()] = {
gridType = skillTypeParameter[1],
elementType = entity:getElementType()
}
end
self.battleUI:showMonsterSkillAni(map, monsterPos, function()
for posId, info in pairs(map) do
self.battleData:setGridInfo(posId, info)
end
end)
end
end
function BattleController:getSkillElementList(elementType, count, useAlternate, excludeMap)
local result = {}
local gridEntities = self.battleData:getGridEnties()
if not gridEntities then
return result
end
local sameElementList = {}
local alternateList = {}
local addCount = 0
for row = 1, BattleConst.ROW_COUNT do
for column = 1, BattleConst.COLUMN_COUNT do
local posId = ModuleManager.BattleManager:getPosId(row, column)
local entity = gridEntities[posId]
if entity and entity:canChangeInfo() and not excludeMap[posId] then
if entity:getElementType() == elementType then
table.insert(sameElementList, posId)
else
table.insert(alternateList, posId)
end
end
end
local remainCount = count - addCount
if remainCount <= 0 then
return result
end
for i = 1, remainCount do
if not sameElementList[i] then
break
end
table.insert(result, table.remove(sameElementList, math.random(1, #sameElementList)))
addCount = addCount + 1
if addCount >= count then
return result
end
end
if addCount < count and useAlternate then
for i = 1, count - addCount do
if not alternateList[1] then
break
end
table.insert(result, table.remove(alternateList, math.random(1, #alternateList)))
addCount = addCount + 1
if addCount >= count then
return result
end
end
end
if addCount >= count then
return result
end
end
return result
end
---- 从一个点直接遍历所有相关的路径
function BattleController:fillThisPos(posId, columnCount, gridMap)
local entity = self.battleData:getGridEntity(posId)
if not entity or not entity:getIsIdle() then
return
end
local list = BattleConst.UP_LINE_FILL_LIST[posId]
if not list[1] then -- 第一排
local rc = ModuleManager.BattleManager:getPosRC(posId)
local c = rc.c
if not columnCount[c] then
columnCount[c] = 0
end
columnCount[c] = columnCount[c] + 1
local fallPosId = posId
local fallEntity = self.battleData:getGridEntity(fallPosId)
if not fallEntity then -- 异常情况,理论上不可能不存在
return
end
local newStartPosId
for i = columnCount[c], 1, -1 do
newStartPosId = ModuleManager.BattleManager:getFirstLineLastRowPosId(i, c)
local curPos = ModuleManager.BattleManager:getPosInfo(newStartPosId)
fallEntity:addPath({x = curPos.x, y = curPos.y})
end
local curPos = ModuleManager.BattleManager:getPosInfo(posId)
fallEntity:addPath({x = curPos.x, y = curPos.y})
self.battleData:exchangeGridEntities(posId, fallPosId)
if not gridMap[c] then
gridMap[c] = {}
end
table.insert(gridMap[c], fallEntity)
else
for index, fallPosId in ipairs(list) do
local fallEntity = self.battleData:getGridEntity(fallPosId)
if not fallEntity then -- 异常情况,理论上不可能不存在
return
end
if not fallEntity:isCantFallType() then
if fallEntity:getIsIdle() then
self:fillThisPos(fallPosId, columnCount, gridMap)
end
fallEntity = self.battleData:getGridEntity(fallPosId)
if not fallEntity:getIsIdle() then
if not fallEntity:getPath()[1] then
local curPos = ModuleManager.BattleManager:getPosInfo(fallPosId)
fallEntity:addPath({x = curPos.x, y = curPos.y})
end
local curPos = ModuleManager.BattleManager:getPosInfo(posId)
fallEntity:addPath({x = curPos.x, y = curPos.y})
self.battleData:exchangeGridEntities(posId, fallPosId)
self:fillThisPos(fallPosId, columnCount, gridMap)
return
end
end
end
end
end
function BattleController:getRandomGridInfo()
local list, fixedRandomGrid = self:getInitBoard()
local fixedRandomList = fixedRandomGrid[self.curBoardIndex]
local gridType = 0
local elementType
if fixedRandomList and fixedRandomList[1] then
elementType = table.remove(fixedRandomList, 1)
else
local map = self.battleData:getElementTypeMap()
local indexs = {}
local typeList = {}
for typeName, typeNum in pairs(BattleConst.ELEMENT_TYPE) do
if not self:getSealElementType()[typeNum] and self.battleData:getSkillEntityByElement(typeNum) then
local weight = ((map[typeNum] or 0) + 1) * BattleConst.ELEMENT_WIGHT
if weight > BattleConst.MAX_ELEMENT_WIGHT then
weight = BattleConst.MAX_ELEMENT_WIGHT
end
table.insert(indexs, weight)
table.insert(typeList, typeNum)
end
end
local index = GFunc.getRandomIndex(indexs)
elementType = typeList[index]
end
return {gridType = gridType, elementType = elementType}
end
function BattleController:findSkillInfluenceGrids()
local girds = self.battleData:clearSkillInfluenceGrids()
for posId, _ in pairs(girds) do
local entity = self.battleData:getGridEntity(posId)
if entity:getCell() then
entity:getCell():showCircle(false)
end
end
local sequence = self.battleData:getGridSequence()
local sequenceEntities = {}
for _, info in ipairs(sequence) do
local entity = self.battleData:getGridEntity(info.posId)
table.insert(sequenceEntities, entity)
end
for _, info in ipairs(sequence) do
local entity = self.battleData:getGridEntity(info.posId)
local skillId = entity:getSkillId()
if skillId then
local skillEntity = self.battleData:getSkillEntityBySkillId(skillId)
if skillEntity then
BATTLE_BOARD_SKILL_HANDLE.activeBoardSkill(info.posId, skillEntity, self.battleData:getGridEnties(), sequenceEntities, self)
end
end
end
end
function BattleController:getRandomSkillList(getCount)
local fixedList = self:getFixedRogueSkill()
if fixedList[1] then
return table.remove(fixedList, 1)
end
getCount = getCount or BattleConst.SKILL_SELECT_COUNT
local result = {}
local cfg = ConfigManager:getConfig("skill_rogue")
local skillPool = self.battleData:getSkillPool()
local map = {}
local count = 0
local newSkillPool = {}
local skillWeight = {}
for elementType, list in pairs(skillPool) do -- 先遍历一下未解锁的技能
if not self.battleData:isUnlockedSkillElementType(elementType) then
local skillEntity = self.battleData:getSkillEntityByElement(elementType)
if skillEntity then
local skillId = skillEntity:getUnlockId()
if skillId and not map[skillId] then
local skillCfg = cfg[skillId]
table.insert(newSkillPool, skillId)
table.insert(skillWeight, skillCfg.weight)
count = count + 1
map[skillId] = true
end
end
end
end
if count >= 3 then -- 如果未解锁的技能大于等于3则直接返回三个解锁技能
for i = 1, 3 do
local index = GFunc.getRandomIndex(skillWeight)
local skillId = table.remove(newSkillPool, index)
table.remove(skillWeight, index)
count = count - 1
table.insert(result, skillId)
if count <= 0 then
break
end
end
return table.shuffle(result)
end
for elementType, list in pairs(skillPool) do
if self.battleData:isUnlockedSkillElementType(elementType) then
for _, skillId in ipairs(list) do
local skillCfg = cfg[skillId]
if skillCfg and (not skillCfg.limit_times or self.battleData:getSkillCount(skillId) < skillCfg.limit_times) then
if not map[skillId] then
table.insert(newSkillPool, skillId)
table.insert(skillWeight, skillCfg.weight)
count = count + 1
map[skillId] = true
end
end
end
end
end
if count > 0 then
local index = GFunc.getRandomIndex(skillWeight)
local skillId = table.remove(newSkillPool, index)
table.remove(skillWeight, index)
count = count - 1
table.insert(result, skillId)
getCount = getCount - 1
end
for skillId, info in pairs(cfg) do
if not self:getNotInvolvedSkills()[skillId] then
if info.universal then
if not info.limit_times or self.battleData:getSkillCount(skillId) < info.limit_times then
if not map[skillId] then
table.insert(newSkillPool, skillId)
table.insert(skillWeight, info.weight)
count = count + 1
end
end
end
end
end
for i = 1, getCount do
local index = GFunc.getRandomIndex(skillWeight)
local skillId = table.remove(newSkillPool, index)
table.remove(skillWeight, index)
count = count - 1
table.insert(result, skillId)
if count <= 0 then
break
end
end
result = table.shuffle(result)
return result
end
function BattleController:onSelectSkill(skillId, value, pos)
self.battleData:addSkillCount(skillId, value)
BATTLE_ROGUE_SKILL_HANDLE.takeEffect(skillId, self.battleData, self, value)
local skillEntities = self.battleData:getSkillEntities()
for _, entity in pairs(skillEntities) do
entity:gotUpSKill(skillId)
end
local totalTime = Time:getServerTime() - self.battleStartTime
BIReport:postFightSkillSelect(self.battleType, {skillId}, self.chapterId, totalTime, self.waveIndex)
local elementType = ModuleManager.HeroManager:getSkillRoguePosition(skillId)
if elementType then
if self.battleUI then
self.battleUI:gotOneSkillAni(skillId, elementType, function()
self:enterElimination()
self.battleUI:refreshBoard()
self.battleUI:refreshSkill()
end, pos)
else
self:enterElimination()
end
else
self:enterElimination()
end
end
function BattleController:changeElementType(count, elementType)
local map = {}
for _, entity in pairs(self.battleData:getGridEnties()) do
if entity:canChangeInfo() and entity:getElementType() ~= elementType then
map[entity] = elementType
count = count - 1
if count <= 0 then
break
end
end
end
self:changeElementTypeByMap(map)
end
function BattleController:changeElementTypeByMap(changeMap)
local index = 1
for entity, elementType in pairs(changeMap) do
self.battleData:setGridInfo(entity:getPosId(), {gridType = entity:getGridType(), elementType = elementType})
self.battleUI:playChangeElementSfx(entity:getPosId(), index)
index = index + 1
end
end
function BattleController:killGrids(posIdList)
if not posIdList[1] then
return
end
for _, posId in ipairs(posIdList) do
local entity = DataManager.BattleData:getGridEntity(posId)
entity:setIsIdle(true)
end
self.battleUI:removeGridOutOfScreen(posIdList)
end
function BattleController:addHeroAttr(attrName, value)
if not self.battleData or not self.battleData.atkTeam then
return
end
local attr = BUFF_NAME_TO_ATTR[attrName]
if attr then
self.battleData.atkTeam:addAttr(attr[1], value, attr[2])
else
local func = BattleBuffHandle.addAttribute[attrName]
if func then
local unitComp
for matchType, comp in pairs(self.atkTeam:getUnitComp()) do
unitComp = comp
break
end
if unitComp then
func(unitComp, value)
end
end
end
end
---- 快照一次棋盘
function BattleController:snapshotBoard()
local snapshot = {}
for _, entity in pairs(self.battleData:getGridEnties()) do
snapshot[entity:getPosId()] = entity:getSnapshoptInfo()
end
return snapshot
end
function BattleController:showAttention()
if not self.attentionList then
return
end
if self.attentionSid then
ModuleManager.BattleManager:unscheduleGlobal(self.attentionSid)
self.attentionSid = nil
end
self.attentionSid = ModuleManager.BattleManager:performWithDelayGlobal(function()
for _, posId in ipairs(self.attentionList) do
local entity = self.battleData:getGridEntity(posId)
if entity and entity:getCell() then
entity:getCell():showAni()
end
end
end, 2)
end
function BattleController:shuffleBoard()
local posList = {}
local gridEntityList = {}
local anySkillList = {}
local posMap = {}
local anySkillCount = 0
local gridEntityCountMap = {}
for posId, entity in pairs(self.battleData:getGridEnties()) do
if not entity:isCantFallType() and not entity:getIsIdle() then
if entity:getSkillId() then
local skillEntity = self.battleData:getSkillEntityBySkillId(entity:getSkillId())
local elementType = skillEntity:getPosition()
if skillEntity:getIgnoreElementType() then
table.insert(anySkillList, entity)
anySkillCount = anySkillCount + 1
else
if not gridEntityList[elementType] then
gridEntityList[elementType] = {}
gridEntityCountMap[elementType] = 0
end
table.insert(gridEntityList[elementType], entity)
gridEntityCountMap[elementType] = (gridEntityCountMap[elementType] or 0) + 1
end
else
local elementType = entity:getElementType()
if not gridEntityList[elementType] then
gridEntityList[elementType] = {}
end
table.insert(gridEntityList[elementType], entity)
gridEntityCountMap[elementType] = (gridEntityCountMap[elementType] or 0) + 1
end
table.insert(posList, posId)
posMap[posId] = true
end
end
for elementType, list in pairs(gridEntityList) do
table.sort(gridEntityList[elementType], function(a, b)
if a:getSkillId() and not b:getSkillId() then
return false
elseif not a:getSkillId() and b:getSkillId() then
return true
else
return a:getPosId() < b:getPosId()
end
end)
end
local needCount = self:getMinEliminationCount()
local tempList
local find = false
for elementType, list in pairs(gridEntityList) do
local count = gridEntityCountMap[elementType] or 0
if count + anySkillCount >= needCount then
local haveSkill = false
local listCount = 0
tempList = {}
find = false
for _, entity in ipairs(list) do
if entity:getSkillId() and haveSkill then
break
end
if entity:getSkillId() then
haveSkill = true
end
table.insert(tempList, entity)
listCount = listCount + 1
if listCount >= needCount then -- 普通的元素+元素技能
find = true
break
end
end
if listCount >= needCount then -- 普通的元素+元素技能
find = true
break
elseif not haveSkill and anySkillCount > 0 then -- 普通元素 + 任意元素技能
table.insert(tempList, anySkillList[1])
listCount = listCount + 1
if listCount >= needCount then
find = true
break
end
end
end
end
if find then
return self:shufflePos(tempList, posMap, needCount, posList)
end
end
function BattleController:shufflePos(tempList, posMap, needCount, posList)
local list = {}
local count = 0
local gotPos = false
for posId, status in pairs(posMap) do
list = {}
count = 0
local findPodId = posId
local temp = {}
while true do
local aounrdList = BattleConst.GRID_OUT_LINE_POS_ID[findPodId]
if not aounrdList then
break
end
local got = false
for id, status in pairs(aounrdList) do
if posMap[id] and not temp[id] then
table.insert(list, id)
count = count + 1
temp[id] = true
findPodId = id
got = true
break
end
end
if not got then
break
end
if count >= needCount then
gotPos = true
break
end
end
if gotPos then
break
end
end
if gotPos then
local changeInfo = {}
local usedPos = {}
local setedPos = {}
for index, entity in ipairs(tempList) do
changeInfo[entity:getPosId()] = list[index]
usedPos[list[index]] = true
setedPos[entity:getPosId()] = true
end
local newList = {}
local newList2 = {}
for index = #posList, 1, -1 do
local posId = posList[index]
if not usedPos[posId] then
table.insert(newList2, posId)
end
if not setedPos[posId] then
table.insert(newList, posId)
end
end
newList2 = table.shuffle(newList2)
for index, posId in ipairs(newList) do
if not newList2[index] then
break
end
changeInfo[posId] = newList2[index]
end
return changeInfo
end
end
function BattleController:findAttention()
local find = false
local posIdMap = {}
local pathList
for r = 1, GConst.BattleConst.ROW_COUNT do
for c = 1, GConst.BattleConst.COLUMN_COUNT do
local posId = ModuleManager.BattleManager:getPosId(r, c)
local gridEntity = self.battleData:getGridEntity(posId)
local mainElementType = gridEntity:getElementType()
if gridEntity:canChangeInfo() then
pathList = {}
posIdMap = {}
self:findLinkLine(posId, posIdMap, false, mainElementType, pathList)
if table.nums(pathList) >= self:getMinEliminationCount() then
find = true
break
end
end
end
if find then
break
end
end
return find, pathList
end
function BattleController:resetGrids(posList)
local needCount = self:getMinEliminationCount()
if not posList[needCount] then
return
end
local posMap = {}
for _, posId in ipairs(posList) do
posMap[posId] = true
end
local useMap = {}
local gotPosList = false
for _, posId in ipairs(posList) do
useMap = {}
local count = 0
local findPodId = posId
gotPosList = false
while true do
local aounrdList = BattleConst.GRID_OUT_LINE_POS_ID[findPodId]
if not aounrdList then
break
end
local got = false
for id, status in pairs(aounrdList) do
if posMap[id] and not useMap[id] then
count = count + 1
useMap[id] = true
findPodId = id
got = true
break
end
end
if not got then
break
end
if count >= needCount then
gotPosList = true
break
end
end
if gotPosList then
break
end
end
if not gotPosList then
return
end
local canRandomElmentList = {}
for typeName, typeNum in pairs(BattleConst.ELEMENT_TYPE) do
if not self:getSealElementType()[typeNum] and self.battleData:getSkillEntityByElement(typeNum) then
table.insert(canRandomElmentList, typeNum)
end
end
if not canRandomElmentList[1] then
return
end
local elementCount = #canRandomElmentList
local mainElement = canRandomElmentList[math.random(1, elementCount)]
local resetPosInfo = {}
for posId, _ in pairs(posMap) do
if useMap[posId] then
resetPosInfo[posId] = {gridType = BattleConst.GRID_TYPE.EMPTY, elementType = mainElement}
else
resetPosInfo[posId] = {gridType = BattleConst.GRID_TYPE.EMPTY, elementType = canRandomElmentList[math.random(1, elementCount)]}
end
end
return resetPosInfo
end
function BattleController:findLinkLine(posId, posIdMap, hadSkill, mainElementType, lineList)
posIdMap[posId] = true
table.insert(lineList, posId)
local list = BattleConst.GRID_OUT_LINE_POS_ID[posId]
if not list then
return hadSkill
end
if #lineList >= self:getMinEliminationCount() then
return hadSkill
end
local lineCount = 0
local maxLineList
local maxPosIdMap
local maxGotSkill
for aroundposId, _ in pairs(list) do
if not posIdMap[aroundposId] then
local gridEntity = self.battleData:getGridEntity(aroundposId)
if gridEntity then
if gridEntity:canLink() and not gridEntity:getSkillId() then
if not mainElementType or gridEntity:getElementType() == mainElementType then
mainElementType = mainElementType or gridEntity:getElementType()
local tempIdMap = GFunc.getTable(posIdMap)
local newList = GFunc.getTable(lineList)
local gotSkill = self:findLinkLine(aroundposId, tempIdMap, hadSkill, mainElementType, newList)
local count = #newList
if count > lineCount then
lineCount = count
maxLineList = newList
maxPosIdMap = tempIdMap
maxGotSkill = gotSkill
end
end
elseif gridEntity:getSkillId() and not hadSkill and mainElementType then
local skillId = gridEntity:getSkillId()
local skillEntity = self.battleData:getSkillEntityBySkillId(skillId)
if skillEntity then
if skillEntity:getIgnoreElementType() or skillEntity:getPosition() == mainElementType then
local tempIdMap = GFunc.getTable(posIdMap)
local newList = GFunc.getTable(lineList)
local gotSkill = self:findLinkLine(aroundposId, tempIdMap, true, mainElementType, newList)
local count = #newList
if count > lineCount then
lineCount = count
maxLineList = newList
maxPosIdMap = tempIdMap
maxGotSkill = gotSkill
end
end
end
end
end
end
end
if lineCount > 0 then
for index, id in ipairs(maxLineList) do
lineList[index] = id
end
for _, id in pairs(maxPosIdMap) do
posIdMap[id] = true
end
hadSkill = maxGotSkill
end
return hadSkill
end
function BattleController:randomDisruptionBoard()
end
function BattleController:addBattleExp(exp)
if not self.battleData or not exp then
return
end
self.battleData:addExp(exp)
end
function BattleController:_tick(dt)
self.realTime = self.realTime + dt
if self.isPause then
return
end
self.time = self.time + dt
self.atkTeam:tick(dt)
self.defTeam:tick(dt)
local count = #self.effectTexts
for i = count, 1, -1 do
self.effectTexts[i]:tick(dt)
if self.effectTexts[i]:getDuration() < 0 then
BattleHelper:recycleEffectText(self.effectTexts[i])
table.remove(self.effectTexts, i)
end
end
self:checkDelayEffectText(dt)
self:checkSkillSlowDown(dt)
self:tick(dt)
end
function BattleController:battleEnd()
if self.battleEndSid then
ModuleManager.BattleManager:unscheduleGlobal(self.battleEndSid)
self.battleEndSid = nil
end
self.battleUI:disableUITouch()
self.battleEndSid = ModuleManager.BattleManager:performWithDelayGlobal(function()
if self.battleUI then
self.battleUI:enableUITouch()
end
self:controllBattleEnd()
end, 1)
end
function BattleController:clear()
if self.alreadyClear then
return
end
self.alreadyClear = true
if self.tickSid then
BattleScheduler:unscheduleGlobal(self.tickSid)
self.tickSid = nil
end
BattleScheduler:clear()
BattleHelper:clear()
BattlePassive:clear()
BattlePool:clear()
self:unBindAll()
DataManager.BattleData:resetTimeSpeed()
ModuleManager.BattleManager:unscheduleAll()
end
function BattleController:endBattleAndExit()
ModuleManager.BattleManager:exitBattle()
end
function BattleController:checkSkillSlowDown(dt)
if self.skillSlowDownDuration == nil then
return
end
self.skillSlowDownDuration = self.skillSlowDownDuration - dt
if self.skillSlowDownDuration >= 0 then
return
end
self.skillSlowDownDuration = nil
self:resetTimeSpeed()
end
function BattleController:checkDelayEffectText(dt)
if self.delayEffectTextCount <= 0 then
return
end
for i = self.delayEffectTextCount, 1, -1 do
local delayEffectText = self.delayEffectTextList[i]
delayEffectText.time = delayEffectText.time - dt
if delayEffectText.time <= 0 then
local effectTextComp = BattleHelper:getEffectText(self.battleUI:getNumberNode(), delayEffectText.colorType)
effectTextComp:showEffectNumber(delayEffectText.effectType, delayEffectText.num, delayEffectText.x, delayEffectText.y)
table.insert(self.effectTexts, effectTextComp)
local tailDelayEffectText = self.delayEffectTextList[self.delayEffectTextCount]
delayEffectText.colorType = tailDelayEffectText.colorType
delayEffectText.effectType = tailDelayEffectText.effectType
delayEffectText.num = tailDelayEffectText.num
delayEffectText.x = tailDelayEffectText.x
delayEffectText.y = tailDelayEffectText.y
delayEffectText.time = tailDelayEffectText.time
self.delayEffectTextCount = self.delayEffectTextCount - 1
end
end
end
function BattleController:showEffectNumber(colorType, effectType, num, x, y, delayTime)
if delayTime > 0 then
self.delayEffectTextCount = self.delayEffectTextCount + 1
local obj = self.delayEffectTextList[self.delayEffectTextCount]
if obj == nil then
obj = {}
self.delayEffectTextList[self.delayEffectTextCount] = obj
end
obj.colorType = colorType
obj.effectType = effectType
obj.num = num
obj.x = x
obj.y = y
obj.time = delayTime
else
local effectTextComp = BattleHelper:getEffectText(self.battleUI:getNumberNode(), colorType)
effectTextComp:showEffectNumber(effectType, num, x, y)
table.insert(self.effectTexts, effectTextComp)
end
end
function BattleController:getFxNode()
return self.battleUI:getFxNode()
end
function BattleController:shakeScreen(shakeType, duration)
self.battleUI:shakeScreen(shakeType, duration)
end
local function _addCurRoundAttr(self, instruction, callback)
if instruction.effectList then
local defComp = self:getOtherSideMainUnit(BattleConst.SIDE_ATK)
local mainComp = self:getOtherSideMainUnit(BattleConst.SIDE_DEF)
for _, effect in ipairs(instruction.effectList) do
local target
if effect:getTartgetSide() == BattleConst.SIDE_DEF then
target = defComp
elseif effect:getTartgetSide() == BattleConst.SIDE_ATK then
target = mainComp
else
local matchType = BattleConst.SIDE_OBJ_TO_MATCH_TYPE[effect:getTartgetSide()]
if matchType then
target = self.atkTeam:getUnitComp()[matchType]
end
end
if target then
mainComp:takeEffect(effect, target)
end
end
end
callback()
end
local function _assisting(self, instruction, callback)
self.atkTeam:useAssistingSkill(instruction.assistingList, #self.instructions == 0, callback)
end
local function _generalAttack(self, instruction, callback)
self.atkTeam:useNormalSkill(instruction.skillMatch, instruction.count, #self.instructions == 0, BattleConst.EFFECT_TYPE.DIRECT, callback)
end
local function _playSkill(self, instruction, callback)
self.atkTeam:useSkill(instruction.skillMatch, instruction.count, #self.instructions == 0, callback)
end
BattleController._doInstruction = {
[BattleConst.INSTRUCTION_NAME.ADD_CUR_ROUND_ATTR] = _addCurRoundAttr,
[BattleConst.INSTRUCTION_NAME.ASSISTING] = _assisting,
[BattleConst.INSTRUCTION_NAME.GENERAL_ATTACK] = _generalAttack,
[BattleConst.INSTRUCTION_NAME.PLAY_SKILL] = _playSkill,
}
return BattleController