死局随机棋盘

This commit is contained in:
xiekaidong 2023-04-21 23:16:41 +08:00
parent a6f3a64b07
commit e7c262c34b
3 changed files with 199 additions and 32 deletions

View File

@ -379,8 +379,15 @@ function BattleController:enterEliminationBegin()
-- 检查棋盘
local find, pathList = self:findAttention()
if not find then -- 如果没找到,就要打乱棋盘
-- todo
Logger.logHighlight("----- 处于无法消除状态 -----")
local changeInfo = self:shuffleBoard()
if changeInfo then
self.battleData:setGridEntitiesPosId(changeInfo)
self.battleUI:shuffleBoard(changeInfo, function()
self:enterElimination(true)
end)
else
Logger.logHighlight("----- 处于无法消除状态 -----")
end
else
self.attentionList = pathList
-- ModuleManager.BattleManager:performWithDelayGlobal(function()
@ -1568,37 +1575,161 @@ function BattleController:showAttention()
end
function BattleController:shuffleBoard()
-- local posList = {}
-- local gridEntityList = {}
-- local elementList = {}
-- for posId, entity in pairs(self.battleData:getGridEnties()) do
-- table.insert(posList, posId)
-- table.insert(gridEntityList, entity)
-- if entity:canLink() then
-- table.insert(elementList, entity)
-- end
-- end
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() then
if entity:getSkillId() then
local skillEntity = self.battleData:getSkillEntityBySkillId(entity:getSkillId())
local elementType = skillEntity:getPosition()
if skillEntity:ignoreElementType() 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
-- local needCount = self:getMinEliminationCount()
-- local tempMap = {}
-- while needCount > 0 do
-- local find = false
-- for _, entity1 in ipairs(elementList) do
-- if not tempMap[entity1:getPosId] then
-- tempMap[entity1:getPosId] = true
-- for _, entity2 in ipairs(elementList) do
-- if not tempMap[entity2:getPosId] then
-- if entity2: then
-- -- body
-- end
-- end
-- end
-- end
-- end
-- if not find then
-- break
-- 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 = {}
for index, entity in ipairs(tempList) do
changeInfo[entity:getPosId()] = list[index]
usedPos[list[index]] = true
end
local newList = {}
for index = #posList, 1, -1 do
local posId = posList[index]
if not usedPos[posId] then
table.insert(newList, posId)
else
table.remove(posList, index)
end
end
local newList2 = table.shuffle(newList)
for index, posId in ipairs(newList2) do
changeInfo[posId] = posList[index]
end
return changeInfo
end
end
function BattleController:findAttention()

View File

@ -813,6 +813,29 @@ function BattleUI:hideGenerateSkillGridCells()
end
end
function BattleUI:shuffleBoard(changeInfo, callback)
if self.shuffleBoardSeq then
self.shuffleBoardSeq:Kill()
self.shuffleBoardSeq = nil
end
self.shuffleBoardSeq = self.root:createBindTweenSequence()
for posId, tartgetPos in pairs(changeInfo) do
local entity = DataManager.BattleData:getGridEntity(posId)
local cell = entity:getCell()
local posId = entity:getPosId()
if cell then
local pos = ModuleManager.BattleManager:getPosInfo(posId)
self.shuffleBoardSeq:Insert(0, cell:getBaseObject():getTransform():DOAnchorPos(pos, 1))
end
end
self.shuffleBoardSeq:AppendCallback(function()
if callback then
callback()
end
end)
end
function BattleUI:fallGrid(listInfo, callback)
self:showMask(false)
self.fallAniCount = 0

View File

@ -275,6 +275,19 @@ function BattleData:exchangeGridEntities(posId1, posId2)
self.gridEntities[posId2] = e1
end
function BattleData:setGridEntitiesPosId(changeInfo)
local map = {}
for posId, targetPosId in pairs(changeInfo) do
local entity = self.gridEntities[posId]
entity:setPosId(targetPosId)
map[targetPosId] = entity
end
for posId, entity in pairs(map) do
self.gridEntities[posId] = entity
end
end
function BattleData:setGridInfo(posId, gridInfo)
local entity = self.gridEntities[posId]
if not entity then