c1_lua/lua/app/module/battle/controller/battle_controller.lua
2023-04-10 09:58:43 +08:00

368 lines
12 KiB
Lua

local BattleController = class("BattleController")
local ELIMINATION_TOUCH_EVENT = GConst.ELIMINATION_TOUCH_EVENT
local BattleConst = GConst.BattleConst
-- *************各个子模块的战斗需要重写的方法 START*************
function BattleController:getInitBoard()
return {}
end
function BattleController:getFixedRandomGrid()
return {}
end
-- 战斗对应的ui
function BattleController:getBattleUIPath()
return "app/ui/battle/battle_ui"
end
-- 战斗结束
function BattleController:controllBattleEnd()
end
-- 不同模块的战斗需要初始化的东西
function BattleController:initOther()
end
function BattleController:onLinkChange()
local sequence = DataManager.BattleData:getGridSequence()
local elementTypeMap = {}
for _, info in ipairs(sequence) do
local entity = DataManager.BattleData:getGridEntity(info.posId)
if not entity:getSkillId() then
local elementType = entity:getElementType()
elementTypeMap[elementType] = (elementTypeMap[elementType] or 0) + 1
end
end
Logger.logHighlight("---------onLinkChange--------------")
Logger.printTable(elementTypeMap)
end
-- *************各个子模块的战斗需要重写的方法 START*************
function BattleController:ctor()
end
function BattleController:init(params)
params = params or {}
DataManager.BattleData:init(self:getInitBoard())
UIManager:closeAllUI()
self.battleUI = UIManager:showUI(self:getBattleUIPath())
self.battleUI:setController(self)
self.battleUI:addLoadUICompleteListener(function()
end)
end
function BattleController:onTouchEvent(eventType, posId)
local entity = DataManager.BattleData:getGridEntity(posId)
if not entity:canLink() then
return
end
if eventType == ELIMINATION_TOUCH_EVENT.DOWN then
if #DataManager.BattleData:getGridSequence() > 0 then
DataManager.BattleData:clearGridSequence()
end
DataManager.BattleData:insertGridSequence(posId, self:snapshotBoard())
self.battleUI:showBoardMask(entity:getElementType())
self:onLinkChange()
self:findSkillInfluenceGrids()
elseif eventType == ELIMINATION_TOUCH_EVENT.ENTER then
local sequence = DataManager.BattleData:getGridSequence()
local info = sequence[#sequence]
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
local lastEntity = DataManager.BattleData:getGridEntity(lastPosId)
if lastEntity:getElementType() ~= entity:getElementType() then
return
end
if DataManager.BattleData:alreadyInsertSequence(posId) then
local info = sequence[#sequence - 1]
local beforePosId = info and info.posId
if not beforePosId then
return
end
if beforePosId == posId then -- 进入的是倒数第二个,则移除倒数第一个
local snapshot = DataManager.BattleData:removeGridSequence(lastPosId)
if snapshot then -- 如果有快照,则恢复一次
for posId, info in pairs(snapshot) do
local entity = DataManager.BattleData:getGridEntity(posId)
if entity then
entity:setInfoBySnapshop(info)
end
end
end
self:onLinkChange()
end
else
DataManager.BattleData:insertGridSequence(posId, self:snapshotBoard())
self:onLinkChange()
self:findSkillInfluenceGrids()
end
elseif eventType == ELIMINATION_TOUCH_EVENT.EXIT then
else -- 取消和抬起
self.battleUI:showBoardMask(nil)
local sequence = DataManager.BattleData:getGridSequence()
local count = #sequence
if count < BattleConst.ELIMINATION_MIN_COUNT then
local snapshot = DataManager.BattleData:removeGridSequence(sequence[1].posId)
if snapshot then -- 如果有快照,则恢复一次
for posId, info in pairs(snapshot) do
local entity = DataManager.BattleData:getGridEntity(posId)
if entity then
entity:setInfoBySnapshop(info)
end
end
end
DataManager.BattleData:clearGridSequence()
return
end
local cellList = {}
for _, info in ipairs(sequence) do
local entity = DataManager.BattleData:getGridEntity(info.posId)
table.insert(cellList, entity:getCell())
end
self.battleUI:eliminationAni(cellList, function()
self:onEliminationAniOver()
end)
end
end
function BattleController:onEliminationAniOver()
local sequence = DataManager.BattleData:getGridSequence()
local count = #sequence
if count < BattleConst.ELIMINATION_MIN_COUNT then
return
end
local boomGridIds = {}
local eliminationPosIds = {}
for _, info in ipairs(sequence) do
local outline = BattleConst.GRID_OUT_LINE_POS_ID[info.posId]
for aroundPosId, _ in pairs(outline) do
boomGridIds[aroundPosId] = true
end
local entity = DataManager.BattleData:getGridEntity(info.posId)
entity:setIsIdle(true)
eliminationPosIds[info.posId] = true
end
local newIdleList = {}
local cellList = {}
for posId, status in pairs(boomGridIds) do
if not eliminationPosIds[posId] then
local entity = DataManager.BattleData:getGridEntity(posId)
if entity then
entity:addAroundEliminationCount()
if entity:getIsIdle() then
table.insert(newIdleList, entity)
table.insert(cellList, entity:getCell())
end
end
end
end
DataManager.BattleData:clearGridSequence()
if cellList[1] then
self.battleUI:eliminationAni(cellList, function()
self:fillBoard()
end)
return
end
self:fillBoard()
end
function BattleController:fillBoard()
-- todo临时暴力求解
local pathMap = {}
local columnCount = {}
for c = 1, BattleConst.COLUMN_COUNT do
for r = BattleConst.ROW_COUNT, 1, -1 do
local posId = ModuleManager.BattleManager:getPosId(r, c)
local entity = DataManager.BattleData:getGridEntity(posId)
if entity:getIsIdle() then
self:fillThisPos(posId, columnCount)
end
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 = DataManager.BattleData:getGridEntity(posId)
if #entity:getPath() > 0 then
pathMap[posId] = entity:getPath()
entity:clearPath()
end
end
end
self.battleUI:fallGrid(pathMap)
end
---- 从一个点直接遍历所有相关的路径
function BattleController:fillThisPos(posId, columnCount)
local entity = DataManager.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 = DataManager.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})
DataManager.BattleData:exchangeGridEntities(posId, fallPosId)
DataManager.BattleData:setGridInfo(posId, self:getRandomGridInfo())
else
for index, fallPosId in ipairs(list) do
local fallEntity = DataManager.BattleData:getGridEntity(fallPosId)
if not fallEntity then -- 异常情况,理论上不可能不存在
return
end
if fallEntity then
if not fallEntity:isCantFallType() then
if fallEntity:getIsIdle() then
self:fillThisPos(fallPosId, columnCount)
end
fallEntity = DataManager.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})
DataManager.BattleData:exchangeGridEntities(posId, fallPosId)
self:fillThisPos(fallPosId, columnCount)
return
end
end
end
end
end
end
function BattleController:getRandomGridInfo()
if not self.fixedRandomList then
self.fixedRandomList = {}
for _, elementType in ipairs(self:getFixedRandomGrid()) do
table.insert(self.fixedRandomList, elementType)
end
end
local gridType = 0
local elementType
if self.fixedRandomList[1] then
elementType = table.remove(self.fixedRandomList, 1)
else
local map = DataManager.BattleData:getElementTypeMap()
local indexs = {}
local typeList = {}
for typeName, typeNum in pairs(BattleConst.ELEMENT_TYPE) do
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
local index = GFunc.getRandomIndex(indexs)
elementType = typeList[index]
end
return {gridType = gridType, elementType = elementType}
end
function BattleController:findSkillInfluenceGrids(isFinal)
local sequence = DataManager.BattleData:getGridSequence()
for _, info in ipairs(sequence) do
local entity = DataManager.BattleData:getGridEntity(info.posId)
-- local skillId = entity:getSkillId()
local skillId = 71
if skillId then
self:activeBoardSkill(info.posId, skillId, isFinal)
end
end
end
function BattleController:activeBoardSkill(posId, skillId, isFinal)
local skillCard = ConfigManager:getConfig("skill_hero")[skillId]
if not skillCard then
return
end
if isFinal then
if skillCard.method == 2 then
if skillCard.boardrange then
-- body
end
end
else
if skillCard.method == 1 then
if skillCard.boardrange then
local cludePosIdsMap = {}
for posId, entity in pairs(DataManager.BattleData:getGridEnties()) do
if entity:canLink() then
cludePosIdsMap[posId] = true
end
end
local ids = ModuleManager.BattleManager:getAroundPosIdsByList(posId, skillCard.boardrange, cludePosIdsMap)
for _, posId in ipairs(ids) do
local entity = DataManager.BattleData:getGridEntity(posId)
entity:setElementType(skillCard.skill_type_parameter)
end
end
end
end
end
---- 快照一次棋盘
function BattleController:snapshotBoard()
local snapshot = {}
for _, entity in pairs(DataManager.BattleData:getGridEnties()) do
snapshot[entity:getPosId()] = entity:getSnapshoptInfo()
end
return snapshot
end
return BattleController