feat: add a stat manager object

This commit is contained in:
Kazhnuz 2021-07-03 09:49:11 +02:00
parent b1e1ef614f
commit c7b9d97c96
4 changed files with 44 additions and 8 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 = self:createStats() self.stats:setStats(self:createStats())
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 = self:createStats() self.stats:setStats(self:createStats())
end end
end end

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 = self:createStats() self.stats:setStats(self:createStats())
end end
function CharacterLevel:levelUp() function CharacterLevel:levelUp()

View file

@ -1,9 +1,13 @@
local Serializable = require "birb.classes.serializable" local Serializable = require "birb.classes.serializable"
local AbstractMobParent = Serializable:extend() local AbstractMobParent = Serializable:extend()
local StatManager = require "game.abstractmobs.statmanager"
function AbstractMobParent:new(serializeFields, listSerializable) function AbstractMobParent:new(serializeFields, listSerializable, statManager)
local statManager = statManager or StatManager
self:initBasicElements() self:initBasicElements()
self.stats = self:createStats()
self.stats = statManager()
self.stats:setStats(self:createStats())
self.skills = self:createSkills() self.skills = self:createSkills()
self.statuts = {} self.statuts = {}
self:initLife() self:initLife()
@ -47,8 +51,8 @@ end
-- Handle HP and stuff like that -- Handle HP and stuff like that
function AbstractMobParent:initLife() function AbstractMobParent:initLife()
self.hp = self.stats.hpmax self.hp = self.stats:get(self.stats.HPMAX)
self.pp = self.stats.ppmax self.pp = self.stats:get(self.stats.PPMAX)
self.status = 0 self.status = 0
end end
@ -118,7 +122,7 @@ end
function AbstractMobParent:setBonus(pvFactor, statFactor) function AbstractMobParent:setBonus(pvFactor, statFactor)
self.stats.hpmax = self.stats.hpmax * pvFactor self.stats.hpmax = self.stats.hpmax * pvFactor
self.hp = self.stats.hpmax self.hp = self.stats:get(self.stats.HPMAX)
self.stats.attack = self.stats.attack * statFactor self.stats.attack = self.stats.attack * statFactor
self.stats.power = self.stats.power * statFactor self.stats.power = self.stats.power * statFactor

View file

@ -0,0 +1,32 @@
local StatManager = Object:extend()
local CONST = require "datas.consts.stats"
StatManager.CONST = CONST
StatManager.HPMAX = CONST.HPMAX
StatManager.PPMAX = CONST.PPMAX
StatManager.ATTACK = CONST.ATTACK
StatManager.POWER = CONST.POWER
StatManager.DEFENSE = CONST.DEFENSE
StatManager.MIND = CONST.MIND
StatManager.TECHNIC = CONST.TECHNIC
StatManager.SPEED = CONST.SPEED
function StatManager:new(owner)
self.owner = owner
end
function StatManager:setStats(stats)
for key, value in pairs(stats) do
self[key] = value
end
end
function StatManager:get(statname)
return self:computeStat(statname)
end
function StatManager:computeStat(statname)
return self[statname]
end
return StatManager