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 end
self.equip[category] = name self.equip[category] = name
game.loot:removeItem(category, name, 1) game.loot:removeItem(category, name, 1)
self.stats:setStats(self:createStats(self)) self:updateHPPP()
end end
function CharacterEquip:removeEquip(category) function CharacterEquip:removeEquip(category)
if (not utils.string.isEmpty(self.equip[category])) then if (not utils.string.isEmpty(self.equip[category])) then
game.loot:addItem(category, self.equip[category], 1) game.loot:addItem(category, self.equip[category], 1)
self.equip[category] = "" self.equip[category] = ""
self.stats:setStats(self:createStats(self)) self:updateHPPP()
end end
end end

View file

@ -15,6 +15,7 @@ local CharacterStatManager = require "game.abstractmobs.character.statmanager"
function AbstractCharacter:new(name) function AbstractCharacter:new(name)
self.simplename = name self.simplename = name
AbstractCharacter.super.new(self, {"simplename", "level", "exp", "exp_next", "hp", "pp", "statuts", "equip"}, nil, CharacterStatManager) AbstractCharacter.super.new(self, {"simplename", "level", "exp", "exp_next", "hp", "pp", "statuts", "equip"}, nil, CharacterStatManager)
self:updateHPPP()
end end
function AbstractCharacter:initBasicElements() function AbstractCharacter:initBasicElements()
@ -26,22 +27,21 @@ end
function AbstractCharacter:createStats() function AbstractCharacter:createStats()
local statNames = self:getStatList() local statNames = self:getStatList()
local stats = {} 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 return stats
end 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) function AbstractCharacter:getStat(statName, ignoreEquip)
local stat = self:getComputedStat(statName) + self:getEquipStats(statName, ignoreEquip) return self.stats:get(statName, ignoreEquip)
return stat
end end
function AbstractCharacter:createSkills() 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 = math.max(math.min(exp, exp_max - 1), exp_min)
self.exp_next = exp_max self.exp_next = exp_max
self.stats:setStats(self:createStats(self)) self:updateHPPP()
end end
function CharacterLevel:levelUp() function CharacterLevel:levelUp()