feat: basic equipements backend

Fixes #80
This commit is contained in:
Kazhnuz 2021-04-22 20:03:46 +02:00
parent 80748476de
commit 0d4fe92a90
4 changed files with 64 additions and 1 deletions

View file

@ -17,6 +17,7 @@ function CharacterData:getData()
data.hp = self.hp data.hp = self.hp
data.pp = self.pp data.pp = self.pp
data.statuts = self.statuts data.statuts = self.statuts
data.equip = self.equip
return data return data
end end
@ -33,6 +34,8 @@ function CharacterData:setData(data)
self.hp = data.hp self.hp = data.hp
self.pp = data.pp self.pp = data.pp
self.statuts = data.statuts self.statuts = data.statuts
self.equip = data.equip or {}
end end
return CharacterData return CharacterData

View file

@ -0,0 +1,52 @@
local CharacterEquip = Object:extend()
local itemutils = require "game.utils.items"
local categories = {"gloves", "shoes", "accessories"}
function CharacterEquip:initEquip()
self.equip = {}
if self.data.inventory == nil then
core.debug:warning("character/equip", "Initial equip not set for " .. self.simplename)
for _, category in ipairs(categories) do
self.equip[category] = ""
end
else
for _, category in ipairs(categories) do
self.equip[category] = self.data.inventory[category] or ""
end
end
end
function CharacterEquip:setEquip(category, name)
if (not utils.string.isEmpty(self.equip[category])) then
game.loot:addItem(category, self.equip[category])
end
self.equip[category] = name
game.loot:removeItem(category, name)
end
function CharacterEquip:predictStat(category, name)
end
function CharacterEquip:getEquipStats(stat, ignore)
local boost = 0
local ignore = ignore or ""
for _, category in ipairs(categories) do
if (category ~= ignore) then
boost = boost + self:getStatByType(stat, category)
end
end
return boost
end
function CharacterEquip:getStatByType(stat, category)
if (not utils.string.isEmpty(self.equip[category])) then
local data = itemutils.getItemData(category, self.equip[category])
local boost = data.statsBoost[stat] or 0
return boost
end
return 0
end
return CharacterEquip

View file

@ -4,9 +4,11 @@ local AbstractCharacter = AbstractMobParent:extend()
local CharacterHealth = require "game.abstractmobs.character.health" local CharacterHealth = require "game.abstractmobs.character.health"
local CharacterLevels = require "game.abstractmobs.character.levels" local CharacterLevels = require "game.abstractmobs.character.levels"
local CharacterDatas = require "game.abstractmobs.character.datas" local CharacterDatas = require "game.abstractmobs.character.datas"
local CharacterEquip = require "game.abstractmobs.character.equip"
AbstractCharacter:implement(CharacterHealth) AbstractCharacter:implement(CharacterHealth)
AbstractCharacter:implement(CharacterLevels) AbstractCharacter:implement(CharacterLevels)
AbstractCharacter:implement(CharacterDatas) AbstractCharacter:implement(CharacterDatas)
AbstractCharacter:implement(CharacterEquip)
function AbstractCharacter:new(name) function AbstractCharacter:new(name)
self.simplename = name self.simplename = name
@ -15,6 +17,7 @@ end
function AbstractCharacter:initBasicElements() function AbstractCharacter:initBasicElements()
self:getCommonData() self:getCommonData()
self:initEquip()
self:initLevel() self:initLevel()
end end
@ -28,6 +31,11 @@ function AbstractCharacter:createStats()
return stats return stats
end end
function AbstractCharacter:getStat(statName, ignoreEquip)
local stat = self:getComputedStat(statName) + self:getEquipStats(statName, ignoreEquip)
return stat
end
function AbstractCharacter:createSkills() function AbstractCharacter:createSkills()
local learnedlist = {} local learnedlist = {}

View file

@ -29,7 +29,7 @@ function CharacterLevel:levelUp()
self:setLevel(self.level + 1) self:setLevel(self.level + 1)
end end
function CharacterLevel:getStat(statname) function CharacterLevel:getComputedStat(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")