c1_lua/lua/app/module/battle/battle_manager.lua
2023-05-26 15:43:01 +08:00

395 lines
13 KiB
Lua

local BattleManager = class("BattleManager", BaseModule)
local BattleConst = GConst.BattleConst
BattleManager.SKILL_CFG = ConfigManager:getConfig("skill")
local BATTLE_CONTROLLER_BASE = "app/module/battle/controller/battle_controller"
local BATTLE_CONTROLLER = {
[BattleConst.BATTLE_TYPE.STAGE] = "app/module/battle/controller/battle_controller_stage",
[BattleConst.BATTLE_TYPE.DAILY_CHALLENGE] = "app/module/battle/controller/battle_controller_daily_challenge",
}
function BattleManager:showPauseUI(battleType)
UIManager:showUI("app/ui/battle/battle_pause_ui", {battleType = battleType})
end
function BattleManager:showBattleResultUI(rewards, combatReport, mysteryBoxIdx)
UIManager:showUI("app/ui/battle/battle_result_ui", {rewards = rewards, combatReport = combatReport, mysteryBoxIdx = mysteryBoxIdx})
end
function BattleManager:showBoxOpenUI(rewards, callback)
UIManager:showUI("app/ui/battle/battle_box_open_ui", {rewards = rewards, callback = callback})
end
function BattleManager:reqSkillRefresh(isAd)
if not isAd then
local cost = GFunc.getConstReward("refresh_skill_cost")
if not GFunc.checkCost(GFunc.getRewardId(cost), GFunc.getRewardNum(cost), true) then
return
end
end
self:sendMessage(ProtoMsgType.FromMsgEnum.BattleSkillRefreshReq, {ad = isAd}, {}, self.rspSkillRefresh, BIReport.ITEM_GET_TYPE.BATTLE_SKILL_REFRESH, true)
end
function BattleManager:rspSkillRefresh(result)
if result.err_code == GConst.ERROR_STR.SUCCESS then
DataManager.BattleData:addRefreshSkillCount(result.reqData.ad)
EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.SKILL_REFRESH_SUCC)
end
end
function BattleManager:playBattle(battleType, params, returnFunc)
params = params or {}
if self.battleController then -- 同一时间只能有一场战斗
self:clear()
end
self.returnFunc = returnFunc
UIManager:stopCurrentBGM()
self:_play(battleType, params)
end
function BattleManager:_play(battleType, params)
params.battleType = battleType
local controllerPath = BATTLE_CONTROLLER[battleType] or BATTLE_CONTROLLER_BASE
self.battleController = require(controllerPath):create()
self.battleController:init(params)
end
function BattleManager:onSelectSkill(skillId, value, pos)
if not self.battleController then
return
end
self.battleController:onSelectSkill(skillId, value, pos)
end
function BattleManager:endBattleAndExit(proactivelyExit)
if self.battleController then
if proactivelyExit then
self.battleController:postWaveOver(false, true)
end
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
GFunc.killDOTween(GConst.DOTWEEN_IDS.BATTLE)
self.battleController:clear()
self.battleController = nil
DataManager.BattleData:clear()
self.bindUnitAttributeData = nil
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: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 posIdInfos = {}
local randomExclusion = {}
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
randomExclusion[info.posId] = true
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 = table.remove(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
function BattleManager:getBuffElementType(buffName)
local cfg = ConfigManager:getConfigWithOtherKey("buff", "name")[buffName]
return cfg and cfg.position
end
function BattleManager:getPosCenterAndDir(pos1, pos2)
local centerPos = {x = 0, y = 0}
centerPos.x = (pos1.x + pos2.x) / 2
centerPos.y = (pos1.y + pos2.y) / 2
local zEuler = - math.deg(math.atan(pos2.x - pos1.x, pos2.y - pos1.y)) + 90
return centerPos, zEuler
end
function BattleManager:getPosIdsByDirection(posId, direction)
local rc = self:getPosRC(posId)
local list = {}
if direction == BattleConst.BOARD_RANGE_TYPE.UP then
for r = rc.r, 1, -1 do
local id = self:getPosId(r, rc.c)
table.insert(list, id)
end
elseif direction == BattleConst.BOARD_RANGE_TYPE.DOWN then
for r = rc.r, BattleConst.ROW_COUNT do
local id = self:getPosId(r, rc.c)
table.insert(list, id)
end
elseif direction == BattleConst.BOARD_RANGE_TYPE.LEFT then
for c = rc.c, 1, -1 do
local id = self:getPosId(rc.r, c)
table.insert(list, id)
end
elseif direction == BattleConst.BOARD_RANGE_TYPE.RIGHT then
for c = rc.c, BattleConst.COLUMN_COUNT do
local id = self:getPosId(rc.r, c)
table.insert(list, id)
end
end
return list
end
----------------------- end 一些公共相关的方法 -----------------------------
function BattleManager:bindBattleUnitAttribute(hashCode, side)
if self.battleController then
local team = nil
if side == 1 then
team = self.battleController.atkTeam
else
team = self.battleController.defTeam
end
local unitAttrHelper = nil
local teamEntity = nil
for _, unit in ipairs(team.unitList) do
local code = unit.baseObject:getGameObject():GetHashCode()
if code == hashCode then
if side == 1 then
unitAttrHelper = unit.baseObject:getComponent(typeof(CS.BF.BattleUnitAttr))
else
unitAttrHelper = unit.baseObject:getComponent(typeof(CS.BF.BattleUnitAttr))
end
teamEntity = unit.unitEntity.team
break
end
end
if unitAttrHelper and teamEntity then
-- 创建并绑定相关数据
if self.bindUnitAttributeData == nil then
self.bindUnitAttributeData = {}
end
-- 组合必要数据
local unitData = {}
unitData.unitAttrHelper = unitAttrHelper
unitData.teamEntity = teamEntity
self.bindUnitAttributeData[hashCode] = unitData
-- bind方法
unitAttrHelper:BindGetAttributeFunc(function(hashCode)
self:getBattleUnitAttribute(hashCode)
end)
-- 刷新数据
self:getBattleUnitAttribute(hashCode)
end
end
end
-- 将lua端属性传回CS端
function BattleManager:getBattleUnitAttribute(hashCode)
if self.bindUnitAttributeData and self.bindUnitAttributeData[hashCode] then
local data = self.bindUnitAttributeData[hashCode]
local unitAttrHelper = data.unitAttrHelper
local teamEntity = data.teamEntity
if unitAttrHelper and teamEntity then
local attr = {}
for key, value in pairs(teamEntity.attr) do
attr[key] = value
end
attr.sheild_hp = teamEntity.shieldHp
unitAttrHelper:GetAttribute(json.encode(attr))
end
end
end
function BattleManager:clearOnExitScene()
if self.battleController == nil then
return
end
self:clear()
self.returnFunc = nil
end
return BattleManager