feat: add CharacterStatManager

This commit is contained in:
Kazhnuz 2021-07-03 10:34:13 +02:00
parent c0b4fd41f8
commit 99bee81c90
4 changed files with 32 additions and 8 deletions

View file

@ -22,21 +22,19 @@ 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.stats:setStats(self:createStats(self))
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.stats:setStats(self:createStats(self))
end end
end end
function CharacterEquip:predictStat(statName, category, name) function CharacterEquip:predictStat(statName, category, name)
local data = core.datas:get("items", name) return self.stats:predictStat(statName, category, name)
local boost = data.statsBoost[statName] or 0
return self:getStat(statName, category) + boost
end end
function CharacterEquip:getEquipStats(stat, ignore) function CharacterEquip:getEquipStats(stat, ignore)

View file

@ -10,9 +10,11 @@ AbstractCharacter:implement(CharacterLevels)
AbstractCharacter:implement(CharacterDatas) AbstractCharacter:implement(CharacterDatas)
AbstractCharacter:implement(CharacterEquip) AbstractCharacter:implement(CharacterEquip)
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"}) AbstractCharacter.super.new(self, {"simplename", "level", "exp", "exp_next", "hp", "pp", "statuts", "equip"}, nil, CharacterStatManager)
end end
function AbstractCharacter:initBasicElements() function AbstractCharacter:initBasicElements()

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.stats:setStats(self:createStats(self))
end end
function CharacterLevel:levelUp() function CharacterLevel:levelUp()
@ -29,7 +29,7 @@ function CharacterLevel:levelUp()
self:setLevel(self.level + 1) self:setLevel(self.level + 1)
end end
function CharacterLevel:getComputedStat(statname) function CharacterLevel:getLevelStat(statname)
local baseStat = self.data.stats[statname] local baseStat = self.data.stats[statname]
if (baseStat == nil) then if (baseStat == nil) then
error("Stat " .. statname .. " doesn't exist") error("Stat " .. statname .. " doesn't exist")
@ -48,6 +48,11 @@ function CharacterLevel:getComputedStat(statname)
end end
end end
function CharacterLevel:getComputedStat(statname)
core.debug:warning("CharacterLevel", "Function getComputedStat is deprecated")
return self:getLevelStat(statname)
end
function CharacterLevel:getStatList() function CharacterLevel:getStatList()
return STATS.LIST return STATS.LIST
end end

View file

@ -0,0 +1,19 @@
local StatManager = require "game.abstractmobs.statmanager"
local CharacterStatManager = StatManager:extend()
function CharacterStatManager:new(owner)
CharacterStatManager.super.new(self, owner)
end
function CharacterStatManager:computeStat(statname, ignoreEquip)
local stat = self.owner:getLevelStat(statname) + self.owner:getEquipStats(statname, ignoreEquip)
return stat
end
function CharacterStatManager:predictStat(statname, category, name)
local data = core.datas:get("items", name)
local boost = data.statsBoost[statname] or 0
return self:computeStat(statname, category) + boost
end
return CharacterStatManager