improvement: do not recompute all stats

This commit is contained in:
Kazhnuz 2021-07-03 10:41:16 +02:00
parent 99bee81c90
commit 2265d7dac9
3 changed files with 14 additions and 14 deletions

View file

@ -22,14 +22,14 @@ function CharacterEquip:setEquip(category, name)
end
self.equip[category] = name
game.loot:removeItem(category, name, 1)
self.stats:setStats(self:createStats(self))
self:updateHPPP()
end
function CharacterEquip:removeEquip(category)
if (not utils.string.isEmpty(self.equip[category])) then
game.loot:addItem(category, self.equip[category], 1)
self.equip[category] = ""
self.stats:setStats(self:createStats(self))
self:updateHPPP()
end
end

View file

@ -15,6 +15,7 @@ local CharacterStatManager = require "game.abstractmobs.character.statmanager"
function AbstractCharacter:new(name)
self.simplename = name
AbstractCharacter.super.new(self, {"simplename", "level", "exp", "exp_next", "hp", "pp", "statuts", "equip"}, nil, CharacterStatManager)
self:updateHPPP()
end
function AbstractCharacter:initBasicElements()
@ -26,22 +27,21 @@ end
function AbstractCharacter:createStats()
local statNames = self:getStatList()
local stats = {}
for _, name in ipairs(statNames) do
stats[name] = self:getStat(name)
end
if (self.hp ~= nil) then
self.hp = math.min(self.hp, stats.hpmax)
end
if (self.pp ~= nil) then
self.pp = math.min(self.pp, stats.ppmax)
end
return stats
end
function AbstractCharacter:updateHPPP()
if (self.hp ~= nil) then
self.hp = math.min(self.hp, self.stats:get(self.stats.HPMAX))
end
if (self.pp ~= nil) then
self.pp = math.min(self.pp, self.stats:get(self.stats.PPMAX))
end
end
function AbstractCharacter:getStat(statName, ignoreEquip)
local stat = self:getComputedStat(statName) + self:getEquipStats(statName, ignoreEquip)
return stat
return self.stats:get(statName, ignoreEquip)
end
function AbstractCharacter:createSkills()

View file

@ -19,7 +19,7 @@ function CharacterLevel:setLevel(newlevel)
self.exp = math.max(math.min(exp, exp_max - 1), exp_min)
self.exp_next = exp_max
self.stats:setStats(self:createStats(self))
self:updateHPPP()
end
function CharacterLevel:levelUp()