c1_lua/lua/app/userdata/hero/hero_entity.lua
2023-04-08 17:52:36 +08:00

95 lines
1.7 KiB
Lua

local HeroEntity = class("HeroEntity", BaseData)
function HeroEntity:ctor(cfgId, config)
self.id = cfgId
self.config = config
self.cfgId = cfgId
self.data.lv = 0
self.attrDirty = false
self.baseAttrOriginal = {}
self.allAttr = {}
self:initAttr()
self:updateAttr()
end
function HeroEntity:initAttr()
self.allAttr[GConst.ATTR_TYPE.hp] = 0
self.allAttr[GConst.ATTR_TYPE.atk] = 0
end
function HeroEntity:setLv(lv)
if not lv then
return
end
self.data.lv = lv
self:setDirty()
end
function HeroEntity:getLv()
return self.data.lv
end
function HeroEntity:setAttrDirty()
self.attrDirty = true
end
function HeroEntity:getAllAttr()
if self.attrDirty == true then
self.attrDirty = false
self:updateAttr()
end
return self.allAttr
end
function HeroEntity:updateAttr()
self:updateBaseAttr()
end
function HeroEntity:updateBaseAttr()
self.baseAttrOriginal[GConst.ATTR_TYPE.hp] = self.config.hp or 0
self.baseAttrOriginal[GConst.ATTR_TYPE.atk] = self.config.atk or 0
end
function HeroEntity:getAtk()
return self.allAttr[GConst.ATTR_TYPE.atk] or 0
end
function HeroEntity:getHp()
return self.allAttr[GConst.ATTR_TYPE.hp] or 0
end
function HeroEntity:setDirty()
self.attrDirty = true
end
function HeroEntity:canLvUp()
if self:isMaxLv() then
return false
end
local cost = self:getLvUpMaterials()
if not cost then
return false
end
for _, reward in ipairs(cost) do
if not GFunc.checkCost(reward.id, reward.num, false) then
return false
end
end
return true
end
function HeroEntity:getLvUpMaterials()
local nextLvInfo = ConfigManager:getConfig("hero_up")[self.data.lv + 1]
if not nextLvInfo then
return
end
return nextLvInfo.exp
end
return HeroEntity