270 lines
9.8 KiB
Lua
270 lines
9.8 KiB
Lua
local BattleController = class("BattleController")
|
|
|
|
local ELIMINATION_TOUCH_EVENT = GConst.ELIMINATION_TOUCH_EVENT
|
|
local BattleConst = GConst.BattleConst
|
|
|
|
-- 战斗对应的ui
|
|
function BattleController:getBattleUIPath()
|
|
return "app/ui/battle/battle_ui"
|
|
end
|
|
|
|
function BattleController:ctor()
|
|
|
|
end
|
|
|
|
function BattleController:init(params)
|
|
params = params or {}
|
|
self.initGridList = params.initGridList or {}
|
|
self.randomGridList = params.randomGridList or {}
|
|
DataManager.BattleData:init()
|
|
|
|
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.battleUI:showBoardMask(entity:getElementType())
|
|
elseif eventType == ELIMINATION_TOUCH_EVENT.ENTER then
|
|
local sequence = DataManager.BattleData:getGridSequence()
|
|
local lastPosId = sequence[#sequence]
|
|
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 not DataManager.BattleData:insertGridSequence(posId) then
|
|
local beforePosId = sequence[#sequence - 1]
|
|
if not beforePosId then
|
|
return
|
|
end
|
|
if beforePosId == posId then -- 进入的是倒数第二个,则移除倒数第一个
|
|
DataManager.BattleData:removeGridSequence(lastPosId)
|
|
end
|
|
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
|
|
return
|
|
end
|
|
local cellList = {}
|
|
for _, posId in ipairs(sequence) do
|
|
local entity = DataManager.BattleData:getGridEntity(posId)
|
|
table.insert(cellList, entity:getCell())
|
|
end
|
|
self.battleUI:eliminationAni(cellList, function()
|
|
for _, posId in ipairs(sequence) do
|
|
local entity = DataManager.BattleData:getGridEntity(posId)
|
|
entity:setIsIdle(true)
|
|
end
|
|
self:fillBoard()
|
|
DataManager.BattleData:clearGridSequence()
|
|
end)
|
|
end
|
|
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
|
|
local pathList = {}
|
|
local targetPosId = self:verticalFall(posId, pathList, columnCount)
|
|
if targetPosId then
|
|
Logger.logHighlight(targetPosId)
|
|
|
|
local targetEntity = DataManager.BattleData:getGridEntity(targetPosId)
|
|
local pathInfoList = {}
|
|
local count = #pathList
|
|
for i = count, 1, -1 do
|
|
table.insert(pathInfoList, pathList[i])
|
|
end
|
|
pathMap[targetPosId] = {entity = targetEntity, pathInfoList = pathInfoList}
|
|
DataManager.BattleData:exchangeGridEntities(posId, targetPosId)
|
|
end
|
|
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:getIsIdle() then
|
|
|
|
-- end
|
|
-- end
|
|
-- end
|
|
|
|
self.battleUI:fallGrid(pathMap)
|
|
end
|
|
|
|
function BattleController:verticalFall(startPostId, pathList, columnCount)
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(startPostId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
local newStartPosId = ModuleManager.BattleManager:getLastRowPosIdByPosId(startPostId)
|
|
local entity = DataManager.BattleData:getGridEntity(newStartPosId)
|
|
if not entity then
|
|
local rc = ModuleManager.BattleManager:getPosRC(startPostId)
|
|
local r = rc.r
|
|
local c = rc.c
|
|
if not columnCount[c] then
|
|
columnCount[c] = 0
|
|
end
|
|
columnCount[c] = columnCount[c] + 1
|
|
for i = 1, columnCount[c] do
|
|
newStartPosId = ModuleManager.BattleManager:getLastRowPosId(r, c)
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(newStartPosId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
end
|
|
return startPostId
|
|
end
|
|
|
|
if entity:isCantFallType() then
|
|
return
|
|
end
|
|
|
|
if not entity:getIsIdle() then
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(newStartPosId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
return newStartPosId
|
|
else
|
|
return self:verticalFall(newStartPosId, pathList, columnCount)
|
|
end
|
|
end
|
|
|
|
function BattleController:findHorizontalFallPath(startPostId, pathList, columnCount)
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(startPostId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
|
|
local list = BattleConst.UP_LINE_FILL_LIST[startPostId]
|
|
if not list[1] then -- 第一排
|
|
local rc = ModuleManager.BattleManager:getPosRC(startPostId)
|
|
local r = rc.r
|
|
local c = rc.c
|
|
if not columnCount[c] then
|
|
columnCount[c] = 0
|
|
end
|
|
columnCount[c] = columnCount[c] + 1
|
|
local newStartPosId
|
|
for i = 1, columnCount[c] do
|
|
newStartPosId = ModuleManager.BattleManager:getLastRowPosId(r, c)
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(newStartPosId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
end
|
|
return startPostId
|
|
else
|
|
for index, posId in ipairs(list) do
|
|
local entity = DataManager.BattleData:getGridEntity(posId)
|
|
if not entity then -- 异常情况,理论上不可能不存在
|
|
return
|
|
end
|
|
if entity then
|
|
if not entity:isCantFallType() and not entity:isIdle() then
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(posId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
return posId
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function BattleController:findVerticalFallAllPath(startPostId, pathMap, columnCount)
|
|
while startPostId ~= nil do
|
|
startPostId = self:findVerticalFallPath(startPostId, pathMap, columnCount)
|
|
end
|
|
end
|
|
|
|
function BattleController:findVerticalFallPath(startPostId, pathList, columnCount)
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(startPostId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
local newStartPosId = ModuleManager.BattleManager:getLastRowPosIdByPosId(startPostId)
|
|
local entity = DataManager.BattleData:getGridEntity(newStartPosId)
|
|
if not entity then
|
|
local rc = ModuleManager.BattleManager:getPosRC(startPostId)
|
|
local r = rc.r
|
|
local c = rc.c
|
|
if not columnCount[c] then
|
|
columnCount[c] = 0
|
|
end
|
|
columnCount[c] = columnCount[c] + 1
|
|
local newStartPosId
|
|
for i = 1, columnCount[c] do
|
|
newStartPosId = ModuleManager.BattleManager:getLastRowPosId(r, c)
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(newStartPosId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
end
|
|
return startPostId
|
|
else
|
|
if not entity:isCantFallType() and not entity:isIdle() then
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(newStartPosId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
return newStartPosId
|
|
end
|
|
end
|
|
end
|
|
|
|
function BattleController:findVerticalFallPath(startPostId, pathMap, columnCount)
|
|
local newStartPosId = ModuleManager.BattleManager:getLastRowPosIdByPosId(startPostId)
|
|
local entity = DataManager.BattleData:getGridEntity(newStartPosId)
|
|
if not entity then
|
|
local rc = ModuleManager.BattleManager:getPosRC(startPostId)
|
|
local r = rc.r
|
|
local c = rc.c
|
|
if not columnCount[c] then
|
|
columnCount[c] = 0
|
|
end
|
|
columnCount[c] = columnCount[c] + 1
|
|
local newStartPosId
|
|
for i = 1, columnCount[c] do
|
|
newStartPosId = ModuleManager.BattleManager:getLastRowPosId(r, c)
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(newStartPosId)
|
|
if not pathMap[startPostId] then
|
|
pathMap[startPostId] = {}
|
|
end
|
|
table.insert(pathMap[startPostId], {x = curPos.x, y = curPos.y})
|
|
end
|
|
return startPostId
|
|
end
|
|
|
|
if entity:isCantFallType() then
|
|
return
|
|
end
|
|
|
|
if entity:isIdle() then
|
|
-- return self:findVerticalFallPath(startPostId, pathMap, columnCount)
|
|
end
|
|
|
|
local curPos = ModuleManager.BattleManager:getPosInfo(newStartPosId)
|
|
table.insert(pathList, {x = curPos.x, y = curPos.y})
|
|
return newStartPosId
|
|
end
|
|
|
|
return BattleController |