c1_lua/lua/app/userdata/battle/battle_data.lua
2023-04-11 22:12:52 +08:00

252 lines
6.5 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 BattleData = class("BattleData", BaseData)
local SKILL_HERO_CFG = ConfigManager:getConfig("skill_hero")
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_borad_skill_entity"
function BattleData:init(board, skillIds, skillPool)
self:clear()
self.atkTeam = self:initTeam(BattleConst.SIDE_ATK)
self.defTeam = self:initTeam(BattleConst.SIDE_DEF)
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]
}
self.gridEntities[data.posId] = BATTLE_GRID_ENTITY:create(data)
if self.gridEntities[data.posId]:canLink() then
self.elementTypeMap[data.elementType] = (self.elementTypeMap[data.elementType] or 0) + 1
end
end
--- todo
skillIds = {
10, 21, 30, 40, 50
}
for _, skillId in ipairs(skillIds) do
local cfg = SKILL_HERO_CFG[skillId]
self.skillMap[cfg.position] = BATTLE_BOARD_SKILL_ENTITY:create(skillId)
end
---- skillPool
self.skillPool = skillPool or {}
for id, info in pairs(ConfigManager:getConfig("skill_rogue")) do
if not info.universal then
table.insert(self.skillPool, id)
end
end
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, status in pairs(grids) do
self.skillInfluenceGrids[posId] = true
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: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:initTeam(side)
local team = BattleTeamEntity:create()
team:init(side)
return team
end
return BattleData