chore: split AbstractCharacter into smaller mixins

This commit is contained in:
Kazhnuz 2021-04-22 18:11:11 +02:00
parent 855c2a2daf
commit 1fe25f2ff1
5 changed files with 161 additions and 126 deletions

View file

@ -1,126 +0,0 @@
local AbstractMobParent = require "game.abstractmobs.parent"
local AbstractCharacter = AbstractMobParent:extend()
local charutils = require "game.utils.characters"
function AbstractCharacter:new(name)
self.simplename = name
self.super.new(self)
end
function AbstractCharacter:initBasicElements()
self:getCommonData(self.simplename)
self.level = self.data.startlevel
self.exp = charutils.getExpValue(self.level)
self.exp_next = charutils.getExpValue(self.level + 1)
end
function AbstractCharacter:getCommonData(simplename)
self.data = charutils.getCharacterData(self.simplename)
self.name = self.data.name
self.fullname = self.data.fullname
self.turns = self.data.turns
end
function AbstractCharacter:createStats()
local stats = {}
local base_stats = self.data.stats
stats.hpmax = charutils.getHPValue(self.level, base_stats.hpmax)
stats.ppmax = charutils.getPPValue(self.level, base_stats.ppmax)
stats.attack = charutils.getStatValue(self.level, base_stats.attack)
stats.power = charutils.getStatValue(self.level, base_stats.power)
stats.defense = charutils.getStatValue(self.level, base_stats.defense)
stats.mind = charutils.getStatValue(self.level, base_stats.mind)
stats.technic = charutils.getStatValue(self.level, base_stats.technic)
stats.speed = charutils.getStatValue(self.level, base_stats.speed)
return stats
end
function AbstractCharacter:createSkills()
local learnedlist = {}
for i, v in ipairs(self.data.skills) do
local tech_name, tech_level, isLearned = v[1], v[2], false
if tech_level <= self.level then
local canLearn = true
for i, learnedSkill in ipairs(learnedlist) do
-- We check if the skill have already been learned, to level-up it
if learnedSkill.name == tech_name then
canLearn = false
learnedSkill.level = learnedSkill.level + 1
end
end
if (canLearn) then
local skilldata = {}
skilldata.name = tech_name
skilldata.level = 1
skilldata.learnedAt = tech_level
table.insert(learnedlist, skilldata)
end
end
-- On continue ensuite d'itérer dans la liste
end
return learnedlist
end
function AbstractCharacter:setLevel(newlevel)
self.level = newlevel
local exp, exp_next, exp_current
local exp_min = charutils.getExpValue(self.level)
local exp_max = charutils.getExpValue(self.level + 1)
exp = self.exp
self.exp = math.max(math.min(exp, exp_max - 1), exp_min)
self.exp_next = exp_max
self.stats = self:createStats()
end
function AbstractCharacter:levelUp()
if (game.difficulty:get("levelUpHeal")) then
self:heal()
end
self:setLevel(self.level + 1)
end
function AbstractCharacter:heal()
self:initLife()
end
function AbstractCharacter:getData()
local data = {}
data.simplename = self.simplename
data.level = self.level
data.exp = self.exp
data.exp_next = self.exp_next
data.hp = self.hp
data.pp = self.pp
data.statuts = self.statuts
return data
end
function AbstractCharacter:setData(data)
self.simplename = data.simplename
self:getCommonData(self.simplename)
self.level = data.level
self.exp = data.exp
self.exp_next = data.exp_next
self:createStats()
self.hp = data.hp
self.pp = data.pp
self.statuts = data.statuts
end
return AbstractCharacter

View file

@ -0,0 +1,38 @@
local CharacterData = Object:extend()
local charutils = require "game.utils.characters"
function CharacterData:getCommonData()
self.data = charutils.getCharacterData(self.simplename)
self.name = self.data.name
self.fullname = self.data.fullname
self.turns = self.data.turns
end
function CharacterData:getData()
local data = {}
data.simplename = self.simplename
data.level = self.level
data.exp = self.exp
data.exp_next = self.exp_next
data.hp = self.hp
data.pp = self.pp
data.statuts = self.statuts
return data
end
function CharacterData:setData(data)
self.simplename = data.simplename
self:getCommonData()
self.level = data.level
self.exp = data.exp
self.exp_next = data.exp_next
self:createStats()
self.hp = data.hp
self.pp = data.pp
self.statuts = data.statuts
end
return CharacterData

View file

@ -0,0 +1,7 @@
local CharacterHealth = Object:extend()
function CharacterHealth:heal()
self:initLife()
end
return CharacterHealth

View file

@ -0,0 +1,61 @@
local AbstractMobParent = require "game.abstractmobs.parent"
local AbstractCharacter = AbstractMobParent:extend()
local CharacterHealth = require "game.abstractmobs.character.health"
local CharacterLevels = require "game.abstractmobs.character.levels"
local CharacterDatas = require "game.abstractmobs.character.datas"
AbstractCharacter:implement(CharacterHealth)
AbstractCharacter:implement(CharacterLevels)
AbstractCharacter:implement(CharacterDatas)
function AbstractCharacter:new(name)
self.simplename = name
self.super.new(self)
end
function AbstractCharacter:initBasicElements()
self:getCommonData()
self:initLevel()
end
function AbstractCharacter:createStats()
local statNames = self:getStatList()
local stats = {}
for _, name in ipairs(statNames) do
stats[name] = self:getStat(name)
end
return stats
end
function AbstractCharacter:createSkills()
local learnedlist = {}
for i, v in ipairs(self.data.skills) do
local tech_name, tech_level, isLearned = v[1], v[2], false
if tech_level <= self.level then
local canLearn = true
for i, learnedSkill in ipairs(learnedlist) do
-- We check if the skill have already been learned, to level-up it
if learnedSkill.name == tech_name then
canLearn = false
learnedSkill.level = learnedSkill.level + 1
end
end
if (canLearn) then
local skilldata = {}
skilldata.name = tech_name
skilldata.level = 1
skilldata.learnedAt = tech_level
table.insert(learnedlist, skilldata)
end
end
-- On continue ensuite d'itérer dans la liste
end
return learnedlist
end
return AbstractCharacter

View file

@ -0,0 +1,55 @@
local CharacterLevel = Object:extend()
local charutils = require "game.utils.characters"
local statList = {"hpmax", "ppmax", "attack", "power", "defense", "mind", "technic", "speed"}
function CharacterLevel:initLevel()
self.level = self.data.startlevel
self.exp = charutils.getExpValue(self.level)
self.exp_next = charutils.getExpValue(self.level + 1)
end
function CharacterLevel:setLevel(newlevel)
self.level = newlevel
local exp
local exp_min = charutils.getExpValue(self.level)
local exp_max = charutils.getExpValue(self.level + 1)
exp = self.exp
self.exp = math.max(math.min(exp, exp_max - 1), exp_min)
self.exp_next = exp_max
self.stats = self:createStats()
end
function CharacterLevel:levelUp()
if (game.difficulty:get("levelUpHeal")) then
self:heal()
end
self:setLevel(self.level + 1)
end
function CharacterLevel:getStat(statname)
local baseStat = self.data.stats[statname]
if (baseStat == nil) then
error("Stat " .. statname .. " doesn't exist")
else
local stat = 0
if (statname == "hpmax") then
stat = charutils.getHPValue(self.level, baseStat)
elseif (statname == "ppmax") then
stat = charutils.getPPValue(self.level, baseStat)
else
stat = charutils.getStatValue(self.level, baseStat)
end
return stat
end
end
function CharacterLevel:getStatList()
return statList
end
return CharacterLevel