local BattleManager = class("BattleManager", BaseModule) local BattleConst = GConst.BattleConst local BATTLE_CONTROLLER_BASE = "app/module/battle/controller/battle_controller" local BATTLE_CONTROLLER = { [BattleConst.BATTLE_TYPE.STAGE] = "app/module/battle/controller/battle_controller_stage" } function BattleManager:getPosInfo(posId) local posInfo = GConst.BattleConst.GRID_POS[posId] if not posInfo then local r = self:getPosRC(posId).r local c = self:getPosRC(posId).c local offsetC = c - BattleConst.HALF_COLUMN_COUNT local offsetR = BattleConst.HALF_ROW_COUNT - r local info = {x = offsetC * BattleConst.GRID_STEP_H, y = offsetR * BattleConst.GRID_STEP_H} GConst.BattleConst.GRID_POS[posId] = info posInfo = GConst.BattleConst.GRID_POS[posId] end return posInfo end function BattleManager:getElementIcon(elementType) local icon = GConst.BattleConst.ELEMENT_ICON[elementType] if not icon then return GConst.ATLAS_PATH.COMMON, "common_alpha" end return GConst.ATLAS_PATH.BATTLE, icon end function BattleManager:getGridTypeIcon(gridType) local icon = GConst.BattleConst.GRID_TYPE_ICON[gridType] if not icon then return GConst.ATLAS_PATH.COMMON, "common_alpha" end return GConst.ATLAS_PATH.BATTLE, icon end function BattleManager:getPosId(row, column) if not BattleConst.RC_2_POS_ID[row] then BattleConst.RC_2_POS_ID[row] = {} end if not BattleConst.RC_2_POS_ID[row][column] then local posId local rowValue = row * BattleConst.ROW_STEP if rowValue >= 0 then posId = rowValue + column else rowValue = rowValue + BattleConst.ROW_STEP posId = rowValue - column end BattleConst.RC_2_POS_ID[row][column] = posId end return BattleConst.RC_2_POS_ID[row][column] end function BattleManager:getFirstLineLastRowPosId(row, column) return self:getPosId(1 - row, column) end function BattleManager:getAroundPosIdsByList(posId, boardrange, cludePosIdsMap) local ids = {} for _, info in ipairs(boardrange) do local list = self:getAroundPosIds(posId, info.type, info.range, cludePosIdsMap) for _, posId in ipairs(list) do table.insert(ids, posId) cludePosIdsMap[posId] = nil end end return ids end function BattleManager:getAroundPosIds(posId, direction, range, cludePosIdsMap) local rc = self:getPosRC(posId) local r = rc.r local c = rc.c local posIdList = {} if direction == BattleConst.BOARD_RANGE_TYPE.RANDOM then if cludePosIdsMap then local cludePosIds = {} for posId, _ in pairs(cludePosIdsMap) do table.insert(cludePosIds, posId) end local count = math.min(#cludePosIds, range) for i = 1, count do table.insert(posIdList, cludePosIds[math.random(1, #cludePosIds)]) end end elseif direction == BattleConst.BOARD_RANGE_TYPE.UP then for i = r - 1, r - range, -1 do if i >= 1 then table.insert(posIdList, self:getPosId(i, c)) end end elseif direction == BattleConst.BOARD_RANGE_TYPE.DOWN then for i = r + 1, r + range do if i <= BattleConst.ROW_COUNT then table.insert(posIdList, self:getPosId(i, c)) end end elseif direction == BattleConst.BOARD_RANGE_TYPE.LEFT then for i = c - 1, c - range, -1 do if i >= 1 then table.insert(posIdList, self:getPosId(r, i)) end end elseif direction == BattleConst.BOARD_RANGE_TYPE.RIGHT then for i = c + 1, c + range do if i <= BattleConst.COLUMN_COUNT then table.insert(posIdList, self:getPosId(r, i)) end end elseif direction == BattleConst.BOARD_RANGE_TYPE.LEFT_UP then for i = c - 1, c - range, -1 do if i >= 1 then for j = r - 1, r - range, -1 do if j >= 1 then table.insert(posIdList, self:getPosId(j, i)) end end end end elseif direction == BattleConst.BOARD_RANGE_TYPE.LEFT_DOWN then for i = c - 1, c - range, -1 do if i >= 1 then for j = r + 1, r + range do if j <= BattleConst.ROW_COUNT then table.insert(posIdList, self:getPosId(j, i)) end end end end elseif direction == BattleConst.BOARD_RANGE_TYPE.RIGHT_UP then for i = c + 1, c + range do if i <= BattleConst.COLUMN_COUNT then for j = r - 1, r - range, -1 do if j >= 1 then table.insert(posIdList, self:getPosId(j, i)) end end end end elseif direction == BattleConst.BOARD_RANGE_TYPE.RIGHT_DOWN then for i = c + 1, c + range do if i <= BattleConst.COLUMN_COUNT then for j = r + 1, r + range do if j <= BattleConst.ROW_COUNT then table.insert(posIdList, self:getPosId(j, i)) end end end end end if cludePosIdsMap then local newList = {} for _, posId in ipairs(posIdList) do if cludePosIdsMap[posId] then table.insert(newList, posId) end end return newList else return posIdList end end function BattleManager:getPosRC(posId) if not BattleConst.POS_ID_2_RC[posId] then local r = posId // BattleConst.ROW_STEP local c if r <= 0 then local adsPosId = math.abs(posId) c = adsPosId % BattleConst.ROW_STEP else c = posId % BattleConst.ROW_STEP end BattleConst.POS_ID_2_RC[posId] = {r = r , c = c} end return BattleConst.POS_ID_2_RC[posId] end function BattleManager:playBattle(params, battleType, returnFunc) if self.battleController then -- 同一时间只能有一场战斗 self:clear() end self.returnFunc = returnFunc UIManager:stopCurrentBGM() self:_play(params, battleType) end function BattleManager:_play(params, battleType) local controllerPath = BATTLE_CONTROLLER[battleType] or BATTLE_CONTROLLER_BASE self.battleController = require(controllerPath):create() self.battleController:init(params) end return BattleManager