65 lines
1.2 KiB
Lua
65 lines
1.2 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
|
|
end
|
|
|
|
function HeroData:clear()
|
|
self.heroes = {}
|
|
end
|
|
|
|
function HeroData:init(data)
|
|
self.heroes = {}
|
|
if data then
|
|
for _, heroInfo in pairs(data.heroes) do
|
|
self:addHero(heroInfo.cfg_id, heroInfo.lv)
|
|
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:setHeroLv(id, lv)
|
|
local entity = self:getHeroById(id)
|
|
if not entity:isActived() then
|
|
self.data.activeCount = self.data.activeCount + 1
|
|
end
|
|
entity:setLv(lv)
|
|
end
|
|
|
|
function HeroData:setDirty()
|
|
self.data.isDirty = not self.data.isDirty
|
|
end
|
|
|
|
return HeroData |