local BattleManager = class("BattleManager", BaseModule) local BattleConst = GConst.BattleConst BattleManager.SKILL_HERO_CFG = ConfigManager:getConfig("skill_hero") 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:showSelectSkillUI(skillList) UIManager:showUI("app/ui/battle/battle_skill_select_ui", {skillList = skillList}) end function BattleManager:playBattle(battleType, params, returnFunc) if self.battleController then -- 同一时间只能有一场战斗 self:clear() end self.returnFunc = returnFunc UIManager:stopCurrentBGM() self:_play(battleType, params) end function BattleManager:_play(battleType, params) local controllerPath = BATTLE_CONTROLLER[battleType] or BATTLE_CONTROLLER_BASE self.battleController = require(controllerPath):create() self.battleController:init(params) end function BattleManager:onSelectSkill(skillId) if not self.battleController then return end self.battleController:onSelectSkill(skillId) end function BattleManager:endBattleAndExit() if self.battleController then self.battleController:endBattleAndExit() else self:exitBattle() end end function BattleManager:exitBattle() self:clear() if self.returnFunc then local returnFunc = self.returnFunc self.returnFunc = nil returnFunc() else -- 没有指定返回则直接去主城 UIManager:closeAllUI() ModuleManager.MaincityManager:showMainCityUI() end self:performWithDelayGlobal(function() Game:garbageCollect() end, 0.02) end function BattleManager:clear() if self.battleController == nil then return end self.battleController:clear() self.battleController = nil DataManager.BattleData:clear() end ----------------------- start 一些公共相关的方法 ----------------------------- 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, randomExclusion) local posIdInfos = {} for _, info in ipairs(boardrange) do local list = self:getAroundPosIds(posId, info.type, info.range, cludePosIdsMap, randomExclusion) for _, info in ipairs(list) do table.insert(posIdInfos, info) cludePosIdsMap[info.posId] = nil end end return posIdInfos end function BattleManager:getAroundPosIds(posId, direction, range, cludePosIdsMap, randomExclusion) 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 if not randomExclusion or not randomExclusion[posId] then table.insert(cludePosIds, posId) end end local count = math.min(#cludePosIds, range) for i = 1, count do table.insert(posIdList, {posId = cludePosIds[math.random(1, #cludePosIds)], direction = direction}) 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, {posId = self:getPosId(i, c), direction = direction}) 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, {posId = self:getPosId(i, c), direction = direction}) 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, {posId = self:getPosId(r, i), direction = direction}) 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, {posId = self:getPosId(r, i), direction = direction}) 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, {posId = self:getPosId(j, i), direction = direction}) 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, {posId = self:getPosId(j, i), direction = direction}) 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, {posId = self:getPosId(j, i), direction = direction}) 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, {posId = self:getPosId(j, i), direction = direction}) end end end end end if cludePosIdsMap then local newList = {} for _, info in ipairs(posIdList) do if cludePosIdsMap[info.posId] then table.insert(newList, info) 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 ----------------------- end 一些公共相关的方法 ----------------------------- return BattleManager