135 lines
2.9 KiB
Lua
135 lines
2.9 KiB
Lua
local HeroEntity = require "app/userdata/hero/hero_entity"
|
|
local HeroData = class("HeroData", BaseData)
|
|
|
|
function HeroData:ctor()
|
|
self.heroes = {}
|
|
self.data.activeCount = 0
|
|
self.data.isDirty = false
|
|
self.matchActiveHeroMap = {}
|
|
self.maxHeroLvOnInit = 0
|
|
end
|
|
|
|
function HeroData:clear()
|
|
self.heroes = {}
|
|
end
|
|
|
|
function HeroData:init(data)
|
|
self.heroes = {}
|
|
if data then
|
|
for id, heroInfo in pairs(data) do
|
|
self:addHero(heroInfo.id, heroInfo.level)
|
|
if self.maxHeroLvOnInit < heroInfo.level then
|
|
self.maxHeroLvOnInit = heroInfo.level
|
|
end
|
|
end
|
|
end
|
|
|
|
local heroCfg = ConfigManager:getConfig("hero")
|
|
for heroId, info in pairs(heroCfg) do
|
|
local entity = self:getHeroById(heroId)
|
|
if entity:isActived() then
|
|
local matchType = entity:getMatchType()
|
|
if not self.matchActiveHeroMap[matchType] then
|
|
self.matchActiveHeroMap[matchType] = {}
|
|
end
|
|
self.matchActiveHeroMap[matchType][entity:getCfgId()] = true
|
|
end
|
|
end
|
|
end
|
|
|
|
function HeroData:addHero(cfgId, lv)
|
|
if self.heroes[cfgId] then
|
|
return
|
|
end
|
|
self.heroes[cfgId] = self:getEntity(cfgId, lv)
|
|
self.data.activeCount = self.data.activeCount + 1
|
|
end
|
|
|
|
function HeroData:getEntity(cfgId, lv)
|
|
return HeroEntity:create(cfgId, lv)
|
|
end
|
|
|
|
function HeroData:getHeroById(id)
|
|
if not id then
|
|
return
|
|
end
|
|
if not self.heroes[id] then
|
|
self.heroes[id] = self:getEntity(id, 0)
|
|
end
|
|
return self.heroes[id]
|
|
end
|
|
|
|
function HeroData:getAllHeroes()
|
|
return self.heroes
|
|
end
|
|
|
|
function HeroData:getActiveHeroCount()
|
|
return self.data.activeCount
|
|
end
|
|
|
|
function HeroData:getHeroIsActive(id)
|
|
local entity = self.heroes[id]
|
|
if entity == nil then
|
|
return false
|
|
end
|
|
return entity:isActived()
|
|
end
|
|
|
|
function HeroData:getUnlockHeroCount()
|
|
local count = 0
|
|
for id, entity in pairs(self.heroes) do
|
|
if entity:isUnlock() then
|
|
count = count + 1
|
|
end
|
|
end
|
|
return count
|
|
end
|
|
|
|
function HeroData:setHeroLv(id, lv)
|
|
local entity = self:getHeroById(id)
|
|
if not entity:isActived() then
|
|
self.data.activeCount = self.data.activeCount + 1
|
|
ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_NEW_HERO_GOT)
|
|
end
|
|
entity:setLv(lv)
|
|
if entity:isActived() then
|
|
local matchType = entity:getMatchType()
|
|
if not self.matchActiveHeroMap[matchType] then
|
|
self.matchActiveHeroMap[matchType] = {}
|
|
end
|
|
self.matchActiveHeroMap[matchType][entity:getCfgId()] = true
|
|
end
|
|
end
|
|
|
|
function HeroData:getMatchActiveHeroMap()
|
|
return self.matchActiveHeroMap
|
|
end
|
|
|
|
function HeroData:setDirty()
|
|
self.data.isDirty = not self.data.isDirty
|
|
end
|
|
|
|
function HeroData:getRp()
|
|
local formationMap = DataManager.FormationData:getStageFormation()
|
|
if not formationMap then
|
|
return false
|
|
end
|
|
local heroMap = {}
|
|
for _, heroId in pairs(formationMap) do
|
|
heroMap[heroId] = true
|
|
end
|
|
|
|
for id, entity in pairs(self.heroes) do
|
|
if entity:canLvUp() and not heroMap[id] then
|
|
return true
|
|
end
|
|
end
|
|
|
|
return false
|
|
end
|
|
|
|
function HeroData:getMaxHeroLvOnInit()
|
|
return self.maxHeroLvOnInit
|
|
end
|
|
|
|
return HeroData |