c1_lua/lua/app/userdata/battle/battle_data.lua
xiekaidong c89eb8ef3f 禁锢
2023-05-26 18:08:05 +08:00

678 lines
19 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local BattleTeamEntity = require "app/userdata/battle/team/battle_team_entity"
local BattleSkillEntity = require "app/userdata/battle/skill/battle_skill_entity"
local BattleData = class("BattleData", BaseData)
local SKILL_CFG = ConfigManager:getConfig("skill")
local BattleConst = GConst.BattleConst
local BATTLE_GRID_ENTITY = require "app/userdata/battle/battle_grid_entity"
local BATTLE_BOARD_SKILL_ENTITY = require "app/userdata/battle/skill/battle_board_skill_entity"
local ATTR_TYPE = GConst.ATTR_TYPE
local DEFAULT_FACTOR = BattleConst.DEFAULT_FACTOR
local TIME_SPEED_LEVEL_0 = 0
local TIME_SPEED_LEVEL_1 = 1
local TIME_SPEED_LEVEL_2 = 2
local TIME_SPEED_LEVEL_3 = 3
local TIME_SPEED_LEVEL_SKILL = 100
function BattleData:init()
self:clear()
self.battleLv = 1
self.curBattleExp = 0
self.needBattleExp = self:getLvNeedExp()
self.addLvCount = 0
self.commonSelectSkillCount = 0
self.timeScale = BattleConst.TIME_SCALE.LEVEL_1
self.lockElementMap = {}
self.data.timeSpeed = 1
self.data.lvDirty = false
self.adRefreshSkillCount = 0
self.refreshSkillCount = 0
BattleSkillEntity.sid = 0
self.atkTeam = self:initTeam(BattleConst.SIDE_ATK)
self.defTeam = self:initTeam(BattleConst.SIDE_DEF)
self:initRogueSkills()
end
function BattleData:getTimeScale()
return self.timeScale
end
function BattleData:pauseBattle()
self.cacheSpeed = self.data.timeSpeed
self.cacheTimeScale = self.timeScale
self:setTimeSpeed(TIME_SPEED_LEVEL_0)
end
function BattleData:resumeBattle()
if self.cacheSpeed == nil then
return
end
if self.data.timeSpeed ~= TIME_SPEED_LEVEL_0 then
return
end
self:setTimeSpeed(self.cacheSpeed or TIME_SPEED_LEVEL_1, self.cacheTimeScale)
end
function BattleData:resetTimeSpeed()
if self.cacheSpeed then -- 目前处于暂停状态
self.cacheSpeed = TIME_SPEED_LEVEL_1
return
end
if self.data.timeSpeed <= TIME_SPEED_LEVEL_1 then
return
end
self:setTimeSpeed(TIME_SPEED_LEVEL_1)
end
function BattleData:addTimeSpeed()
if self.data.timeSpeed >= TIME_SPEED_LEVEL_3 then
return
end
if self.cacheSpeed then -- 目前处于暂停状态
if self.cacheSpeed < TIME_SPEED_LEVEL_3 then
self.cacheSpeed = self.cacheSpeed + 1
end
return
end
local timeSpeed = self.data.timeSpeed + 1
self:setTimeSpeed(timeSpeed)
end
function BattleData:setSkillTimeSpeed(timeScale)
self:setTimeSpeed(TIME_SPEED_LEVEL_SKILL, timeScale)
end
function BattleData:setTimeSpeed(timeSpeed, timeScale)
if timeSpeed == self.data.timeSpeed then
return
end
if timeSpeed == TIME_SPEED_LEVEL_0 then
self.timeScale = 0
elseif timeSpeed == TIME_SPEED_LEVEL_1 then
self.timeScale = BattleConst.TIME_SCALE.LEVEL_1
elseif timeSpeed == TIME_SPEED_LEVEL_2 then
self.timeScale = BattleConst.TIME_SCALE.LEVEL_2
elseif timeSpeed == TIME_SPEED_LEVEL_3 then
self.timeScale = BattleConst.TIME_SCALE.LEVEL_3
else
self.timeScale = timeScale or BattleConst.TIME_SCALE.LEVEL_1
end
self.data.timeSpeed = timeSpeed
end
function BattleData:initRogueSkills()
self.skillPool = {}
self.skillMap = {}
local skillmap = {}
local formation = DataManager.FormationData:getStageFormation()
for matchType, heroId in pairs(formation) do
if heroId > 0 then
local heroEntity = DataManager.HeroData:getHeroById(heroId)
if heroEntity then
local skillId = heroEntity:getBaseSkill()
local cfg = SKILL_CFG[skillId]
self.skillMap[cfg.position] = BATTLE_BOARD_SKILL_ENTITY:create(skillId)
self.skillMap[cfg.position]:addUpSkills(heroEntity:getRogueSkillList())
self.skillMap[cfg.position]:setUnlockId(heroEntity:getUnlockRogueId())
for _, id in ipairs(heroEntity:getActiveTogueSkills()) do
if not skillmap[id] then
if not self.skillPool[cfg.position] then
self.skillPool[cfg.position] = {}
end
table.insert(self.skillPool[cfg.position], id)
skillmap[id] = true
end
end
end
end
end
end
function BattleData:refreshBoard(board, blockIcon)
for i, info in ipairs(board) do
local r = 1
local c = 1
local zheng = i // BattleConst.ROW_COUNT
local yu = i % BattleConst.ROW_COUNT
if yu > 0 then
r = zheng + 1
c = yu
else
r = zheng
c = BattleConst.ROW_COUNT
end
local posId = ModuleManager.BattleManager:getPosId(r, c)
local data = {
posId = posId,
gridType = info[1] or BattleConst.GRID_TYPE.EMPTY,
elementType = info[2] or BattleConst.ELEMENT_TYPE.RED
}
if self.gridEntities[data.posId] then
self.gridEntities[data.posId]:clear()
self.gridEntities[data.posId]:setGridType(data.gridType)
self.gridEntities[data.posId]:setElementType(data.elementType)
else
self.gridEntities[data.posId] = BATTLE_GRID_ENTITY:create(data)
end
self.gridEntities[data.posId]:determineIdleStatus()
self.gridEntities[data.posId]:setObstacleIcon(blockIcon)
end
end
function BattleData:getNewGridEntity(posId, gridType, elementType)
local data = {
posId = posId or 0,
gridType = gridType or BattleConst.GRID_TYPE.EMPTY,
elementType = elementType or BattleConst.ELEMENT_TYPE.RED
}
return BATTLE_GRID_ENTITY:create(data)
end
function BattleData:clear()
self:clearGridSequence()
self.gridEntities = {}
self.skillMap = {}
self.selectSkillMap = {}
end
function BattleData:getElementTypeMap()
local elementTypeMap = {}
if self.gridEntities then
for posId, entity in pairs(self.gridEntities) do
if entity:canLink() then
local elementType = entity:getElementType()
elementTypeMap[elementType] = (elementTypeMap[elementType] or 0) + 1
end
end
end
return elementTypeMap
end
function BattleData:getGridSequence()
return self.gridSequence
end
function BattleData:alreadyInsertSequence(posId)
return self.gridSequenceMap[posId] == true
end
function BattleData:getIsVirtual()
return self.isVirtual
end
function BattleData:getSequenceHadSkill()
for _, info in ipairs(self:getGridSequence()) do
local entity = self.gridEntities[info.posId]
if entity and entity:getSkillId() then
return entity:getSkillId(), entity:getPosId()
end
end
return
end
function BattleData:insertGridSequence(posId, snapshot, isVirtual)
if self:alreadyInsertSequence(posId) then
return false
end
self.gridSequenceMap[posId] = true
self.isVirtual = isVirtual
table.insert(self.gridSequence, {posId = posId, snapshot = snapshot})
return true
end
function BattleData:removeGridSequence(posId)
if not self:alreadyInsertSequence(posId) then
return
end
local count = #self.gridSequence
local lastPosId = self.gridSequence[count].posId
local snapshot = self.gridSequence[count].snapshot
if lastPosId ~= posId then
return
end
table.remove(self.gridSequence, count)
self.gridSequenceMap[lastPosId] = false
return snapshot
end
function BattleData:getFirstSequenceSnapshot(posId)
if not self.gridSequence[1] then
return
end
local snapshot = self.gridSequence[1].snapshot
return snapshot
end
function BattleData:cacheSkillInfluenceGrids(grids)
if not self.skillInfluenceGrids then
self.skillInfluenceGrids = {}
end
for posId, info in pairs(grids) do
self.skillInfluenceGrids[posId] = info
end
end
function BattleData:getSkillInfluenceGrids()
return self.skillInfluenceGrids or {}
end
function BattleData:clearSkillInfluenceGrids()
local grids = self:getSkillInfluenceGrids()
self.skillInfluenceGrids = {} -- 技能影响的格子
return grids
end
function BattleData:clearGridSequence()
self.gridSequence = {} -- 格子队列
self.gridSequenceMap = {} -- 格子队列对应的map方面查找
self.isVirtual = nil
self:clearSkillInfluenceGrids()
end
function BattleData:getGridEnties()
return self.gridEntities or {}
end
function BattleData:getGridEntity(posId)
return self.gridEntities[posId]
end
function BattleData:exchangeGridEntities(posId1, posId2)
local e1 = self.gridEntities[posId1]
local e2 = self.gridEntities[posId2]
e1:setPosId(posId2)
e2:setPosId(posId1)
e2:setIsIdle(false)
self.gridEntities[posId1] = e2
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
return
end
if gridInfo.gridType then
entity:setGridType(gridInfo.gridType)
end
if gridInfo.elementType then
entity:setElementType(gridInfo.elementType)
end
entity:setSkilId() -- 清除skillId
entity:determineIdleStatus()
end
function BattleData:setInfoBySnapshop(posId, snapInfo)
local entity = self.gridEntities[posId]
if not entity then
return
end
entity:setInfoBySnapshop(snapInfo)
end
function BattleData:setGridType(posId, gridType)
local entity = self.gridEntities[posId]
if not entity then
return
end
entity:setGridType(gridType)
entity:determineIdleStatus()
end
function BattleData:lockAllSkillGrid(lock)
if not self.gridEntities then
return
end
for posId, entity in pairs(self.gridEntities) do
if entity:getSkillId() then
local gridType = GConst.BattleConst.GRID_TYPE.EMPTY
if lock then
gridType = GConst.BattleConst.GRID_TYPE.LOCK
end
self:setGridType(posId, gridType)
end
end
end
function BattleData:getSkillEntities()
return self.skillMap
end
function BattleData:getSkillEntityByElement(elementType)
if not self.skillMap then
return
end
return self.skillMap[elementType]
end
function BattleData:getSkillEntityBySkillId(skillId)
if not self.skillMap or not skillId then
return
end
local cfg = SKILL_CFG[skillId]
if not cfg then
return
end
return self.skillMap[cfg.position]
end
function BattleData:unlockSkillEntity(elementType)
if self.skillMap[elementType] then
self.skillMap[elementType]:setUnlock()
end
end
function BattleData:isUnlockedSkillElementType(elementType)
if not self.skillMap[elementType] then
return false
end
return self.skillMap[elementType]:getUnlocked()
end
function BattleData:addSkillEnergy(elementMap)
if not self.skillMap then
return
end
for elementType, entity in pairs(self.skillMap) do
if entity:getUnlocked() then
entity:addEnergy(elementMap[elementType] or 0)
end
end
end
function BattleData:addSkillCount(skillId, value)
if not self.selectSkillMap[skillId] then
self.selectSkillMap[skillId] = {
count = 0,
value = 0
}
end
self.selectSkillMap[skillId].count = self.selectSkillMap[skillId].count + 1
if value then
self.selectSkillMap[skillId].value = (self.selectSkillMap[skillId].value) + value
end
end
function BattleData:getSkillCount(skillId)
if self.selectSkillMap[skillId] then
return self.selectSkillMap[skillId].count
end
return 0
end
function BattleData:getSelectSkillMap()
return self.selectSkillMap
end
function BattleData:getSkillPool()
return self.skillPool
end
function BattleData:changeSkillId(elementType, newId)
if not newId then
return
end
local entity = self:getSkillEntityByElement(elementType)
if entity then
entity:refreshSkillId(newId)
end
end
function BattleData:cacheLockElement(elementType, status)
self.lockElementMap[elementType] = status
end
function BattleData:getCacheLockedElement(elementType)
return self.lockElementMap[elementType]
end
function BattleData:cacheBoardSkill()
self.cacheSkillList = {}
self.cacheSkillCount = 0
local list
local count = 0
for posId, entity in pairs(self:getGridEnties()) do
if entity:getSkillId() then
if not list then
list = {}
end
table.insert(list, {skillId = entity:getSkillId(), posId = posId})
count = count + 1
end
end
if list then
if count > BattleConst.MAX_CACHE_SKILL_COUNT then
count = BattleConst.MAX_CACHE_SKILL_COUNT
end
for i = 1, count do
if not list[1] then
break
end
local info = table.remove(list, math.random(1, #list))
table.insert(self.cacheSkillList, info)
self.cacheSkillCount = self.cacheSkillCount + 1
end
end
end
function BattleData:getCacheBoardSkill()
if not self.cacheSkillList then
return self.cacheSkillList, 0
end
return self.cacheSkillList, self.cacheSkillCount
end
function BattleData:clearCacheBoardSkill()
self.cacheSkillList = {}
self.cacheSkillCount = 0
end
function BattleData:getBattleLv()
return self.battleLv
end
function BattleData:getBattleExp()
return self.curBattleExp
end
function BattleData:getBattleNeedExp()
return self.needBattleExp
end
function BattleData:getLvNeedExp(lv)
lv = lv or self.battleLv
local cfg = ConfigManager:getConfig("battle_exp")
if not cfg[lv] then
return cfg[ConfigManager:getConfigNum("battle_exp")].exp
end
return cfg[lv].exp
end
function BattleData:addExp(exp)
self.curBattleExp = self.curBattleExp + exp
while self.curBattleExp >= self.needBattleExp do
self.curBattleExp = self.curBattleExp - self.needBattleExp
self.addLvCount = self.addLvCount + 1
self.battleLv = self.battleLv + 1
self.needBattleExp = self:getLvNeedExp()
end
self.data.lvDirty = not self.data.lvDirty
end
function BattleData:useAddlvCount()
if self.addLvCount <= 0 then
self.addLvCount = 0
return false
end
self.addLvCount = self.addLvCount - 1
return true
end
function BattleData:addCommonSelectSkillCount(count)
self.commonSelectSkillCount = self.commonSelectSkillCount + (count or 1)
end
function BattleData:useCommonSelectSkillCount()
if self.commonSelectSkillCount <= 0 then
self.commonSelectSkillCount = 0
return false
end
self.commonSelectSkillCount = self.commonSelectSkillCount - 1
return true
end
function BattleData:addRefreshSkillCount(isAd)
if isAd then
self.adRefreshSkillCount = self.adRefreshSkillCount + 1
end
self.refreshSkillCount = self.refreshSkillCount + 1
end
function BattleData:getRefreshSkillCount()
return self.refreshSkillCount
end
function BattleData:getADRefreshSkillCount()
return self.adRefreshSkillCount
end
function BattleData:initTeam(side)
local data = nil
if side == BattleConst.SIDE_ATK then
data = self:initHeroData()
end
local team = BattleTeamEntity:create()
team:init(side, data)
return team
end
function BattleData:getAtkTeam()
return self.atkTeam
end
function BattleData:getDefTeam()
return self.defTeam
end
function BattleData:initHeroData()
local units = {}
local skillCfg = ConfigManager:getConfig("skill")
local formation = DataManager.FormationData:getStageFormation()
for matchType, heroId in pairs(formation) do
if heroId > 0 then
local heroEntity = DataManager.HeroData:getHeroById(heroId)
if heroEntity then
local heroAttr = heroEntity:getAllAttr()
local skillId = heroEntity:getBaseSkill()
local hp = heroAttr[ATTR_TYPE.hp] // DEFAULT_FACTOR
local unitData = {
id = heroId,
modelId = heroEntity:getModelId(),
matchType = matchType,
normalSkills = heroEntity:getHurtSkill(),
assistingSkill = heroEntity:getAssistingSkill(),
body = 2, -- 英雄默认是中体型
attr = {
hp = hp,
max_hp = hp,
atk = 0,
atk_red = heroAttr[ATTR_TYPE.atk_red] // DEFAULT_FACTOR,
atk_yellow = heroAttr[ATTR_TYPE.atk_yellow] // DEFAULT_FACTOR,
atk_green = heroAttr[ATTR_TYPE.atk_green] // DEFAULT_FACTOR,
atk_blue = heroAttr[ATTR_TYPE.atk_blue] // DEFAULT_FACTOR,
atk_purple = heroAttr[ATTR_TYPE.atk_purple] // DEFAULT_FACTOR,
}
}
local skillInfo = skillCfg[skillId]
if skillInfo then
if skillInfo.effect_type == 1 then -- 主动
unitData.activeSkills = {skillId}
elseif skillInfo.effect_type == 2 then -- 被动
unitData.passiveSkills = {skillId}
end
end
table.insert(units, unitData)
end
end
end
local data = {
units = units
}
return data
end
function BattleData:addMonster(monsterId, newTeam)
local monsterInfo = ConfigManager:getConfig("monster")[monsterId]
local hp = monsterInfo.hp // DEFAULT_FACTOR
local atk = monsterInfo.atk // DEFAULT_FACTOR
local unitData = {
id = monsterId,
modelId = monsterInfo.model_id,
matchType = 0,
normalSkills = monsterInfo.hurt_skill,
normalSkillCount = monsterInfo.atk_times or 0,
activeSkills = monsterInfo.skill,
passiveSkills = monsterInfo.passive_skill,
assistingSkill = nil,
isBoss = monsterInfo.is_boss,
exp = monsterInfo.monster_exp or 0,
body = monsterInfo.body,
attr = {
hp = hp,
max_hp = hp,
atk = atk,
atk_red = 0,
atk_yellow = 0,
atk_green = 0,
atk_blue = 0,
atk_purple = 0,
}
}
if newTeam then
self.defTeam:init(BattleConst.SIDE_DEF)
end
return self.defTeam:addUnit(unitData)
end
function BattleData:getNormalAttackName(index)
if self.normalAttackName == nil then
self.normalAttackName = {}
end
local name = self.normalAttackName[index]
if name == nil then
name = string.format("attack%02d", index)
self.normalAttackName[index] = name
end
return name
end
return BattleData