local BattleTeamEntity = require "app/userdata/battle/team/battle_team_entity" local BattleData = class("BattleData", BaseData) local SKILL_HERO_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 function BattleData:init() self:clear() self.battleLv = 1 self.curBattleExp = 0 self.needBattleExp = self:getLvNeedExp() self.addLvCount = 0 self.data.lvDirty = false self.atkTeam = self:initTeam(BattleConst.SIDE_ATK) self.defTeam = self:initTeam(BattleConst.SIDE_DEF) self:initRogueSkills() 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:getActiveSkill() local cfg = SKILL_HERO_CFG[skillId] self.skillMap[cfg.position] = BATTLE_BOARD_SKILL_ENTITY:create(skillId) for _, id in ipairs(heroEntity:getActiveTogueSkills()) do if not skillmap[id] then table.insert(self.skillPool, id) skillmap[id] = true end end end end end end function BattleData:refreshBoard(board) self.elementTypeMap = {} 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], elementType = info[2] } 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 if self.gridEntities[data.posId]:canLink() then self.elementTypeMap[data.elementType] = (self.elementTypeMap[data.elementType] or 0) + 1 end 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.elementTypeMap = {} -- 同元素得格子数量 self.skillMap = {} self.selectSkillMap = {} end function BattleData:getElementTypeMap() return self.elementTypeMap end function BattleData:getGridSequence() return self.gridSequence end function BattleData:alreadyInsertSequence(posId) return self.gridSequenceMap[posId] == true 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() end end return end function BattleData:insertGridSequence(posId, snapshot) if self:alreadyInsertSequence(posId) then return false end self.gridSequenceMap[posId] = true 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: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:clearSkillInfluenceGrids() end function BattleData:getGridEnties() return self.gridEntities 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:setGridInfo(posId, gridInfo) local entity = self.gridEntities[posId] if not entity then return end if entity:canLink() then local elementType = entity:getElementType() self.elementTypeMap[elementType] = (self.elementTypeMap[elementType] or 0) - 1 if self.elementTypeMap[elementType] < 0 then self.elementTypeMap[elementType] = 0 end end if gridInfo.gridType then entity:setGridType(gridInfo.gridType) end if gridInfo.elementType then entity:setElementType(gridInfo.elementType) if entity:canLink() then self.elementTypeMap[gridInfo.elementType] = (self.elementTypeMap[gridInfo.elementType] or 0) + 1 end end entity:setSkilId() -- 清除skillId 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_HERO_CFG[skillId] if not cfg then return end return self.skillMap[cfg.position] end function BattleData:addSkillEnergy(elementMap) if not self.skillMap then return end for elementType, entity in pairs(self.skillMap) do entity:addEnergy(elementMap[elementType] or 0) end end function BattleData:addSkillCount(skillId) if not self.selectSkillMap[skillId] then self.selectSkillMap[skillId] = 0 end self.selectSkillMap[skillId] = self.selectSkillMap[skillId] + 1 end function BattleData:getSkillCount(skillId) return self.selectSkillMap[skillId] or 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:cacheBoardSkill() self.cacheSkillList = {} self.cacheSkillCount = 0 for posId, entity in pairs(self:getGridEnties()) do if entity:getSkillId() then table.insert(self.cacheSkillList, {skillId = entity:getSkillId(), posId = posId}) 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: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 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 activeSkill = heroEntity:getActiveSkill() -- 主动技能 local hp = heroAttr[ATTR_TYPE.hp] // DEFAULT_FACTOR local unitData = { id = heroId, modelId = heroEntity:getModelId(), matchType = matchType, normalSkill = heroEntity:getHurtSkill(), activeSkills = {activeSkill}, 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, } } Logger.printTable(unitData) 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, normalSkill = monsterInfo.hurt_skill, activeSkills = monsterInfo.skill, 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 return BattleData