From c7b9d97c9635c312b8cc26e6a0c9a3af59c87903 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 3 Jul 2021 09:49:11 +0200 Subject: [PATCH] feat: add a stat manager object --- .../game/abstractmobs/character/equip.lua | 4 +-- .../game/abstractmobs/character/levels.lua | 2 +- .../game/abstractmobs/parent.lua | 14 +++++--- .../game/abstractmobs/statmanager.lua | 32 +++++++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 sonic-radiance.love/game/abstractmobs/statmanager.lua diff --git a/sonic-radiance.love/game/abstractmobs/character/equip.lua b/sonic-radiance.love/game/abstractmobs/character/equip.lua index 6443cba..f5eff45 100644 --- a/sonic-radiance.love/game/abstractmobs/character/equip.lua +++ b/sonic-radiance.love/game/abstractmobs/character/equip.lua @@ -22,14 +22,14 @@ function CharacterEquip:setEquip(category, name) end self.equip[category] = name game.loot:removeItem(category, name, 1) - self.stats = self:createStats() + self.stats:setStats(self:createStats()) 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 = self:createStats() + self.stats:setStats(self:createStats()) end end diff --git a/sonic-radiance.love/game/abstractmobs/character/levels.lua b/sonic-radiance.love/game/abstractmobs/character/levels.lua index 954c4c7..1e67f5f 100644 --- a/sonic-radiance.love/game/abstractmobs/character/levels.lua +++ b/sonic-radiance.love/game/abstractmobs/character/levels.lua @@ -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 = self:createStats() + self.stats:setStats(self:createStats()) end function CharacterLevel:levelUp() diff --git a/sonic-radiance.love/game/abstractmobs/parent.lua b/sonic-radiance.love/game/abstractmobs/parent.lua index 6eb6ca2..a21fcb0 100644 --- a/sonic-radiance.love/game/abstractmobs/parent.lua +++ b/sonic-radiance.love/game/abstractmobs/parent.lua @@ -1,9 +1,13 @@ local Serializable = require "birb.classes.serializable" 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.stats = self:createStats() + + self.stats = statManager() + self.stats:setStats(self:createStats()) self.skills = self:createSkills() self.statuts = {} self:initLife() @@ -47,8 +51,8 @@ end -- Handle HP and stuff like that function AbstractMobParent:initLife() - self.hp = self.stats.hpmax - self.pp = self.stats.ppmax + self.hp = self.stats:get(self.stats.HPMAX) + self.pp = self.stats:get(self.stats.PPMAX) self.status = 0 end @@ -118,7 +122,7 @@ end function AbstractMobParent:setBonus(pvFactor, statFactor) 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.power = self.stats.power * statFactor diff --git a/sonic-radiance.love/game/abstractmobs/statmanager.lua b/sonic-radiance.love/game/abstractmobs/statmanager.lua new file mode 100644 index 0000000..105180d --- /dev/null +++ b/sonic-radiance.love/game/abstractmobs/statmanager.lua @@ -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 \ No newline at end of file