From a63b4ddeb3ec3a028e7c890e7cc45e07ee48c092 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 19 Jul 2020 14:02:22 +0200 Subject: [PATCH] improvement(game/character): create an AbstractCharacter object --- .../game/abstractmobs/character.lua | 92 ++++++++++++++ .../game/abstractmobs/utils.lua | 2 +- sonic-radiance.love/game/characters.lua | 115 ++---------------- 3 files changed, 105 insertions(+), 104 deletions(-) create mode 100644 sonic-radiance.love/game/abstractmobs/character.lua diff --git a/sonic-radiance.love/game/abstractmobs/character.lua b/sonic-radiance.love/game/abstractmobs/character.lua new file mode 100644 index 0000000..535072a --- /dev/null +++ b/sonic-radiance.love/game/abstractmobs/character.lua @@ -0,0 +1,92 @@ +AbstractMobParent = require "game.abstractmobs.parent" + +AbstractCharacter = AbstractMobParent:extend() + +local charutils = require "game.abstractmobs.utils" + +function AbstractCharacter:new(name) + self.simplename = name + self.super.new(self) +end + +function AbstractCharacter:initBasicElements() + self.data = charutils.getCharacterData(self.simplename) + self.name = self.data.name + self.fullname = self.data.fullname + self.turns = self.data.turns + + self.level = self.data.startlevel + self.exp = charutils.getExpValue(self.level) + self.exp_next = charutils.getExpValue(self.level + 1) +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 + exp_min = charutils.getExpValue(self.level) + 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() + self:setLevel(self.level + 1) +end + +function AbstractCharacter:heal() + self:initLife() +end + +return AbstractCharacter diff --git a/sonic-radiance.love/game/abstractmobs/utils.lua b/sonic-radiance.love/game/abstractmobs/utils.lua index 14f798e..bc56593 100644 --- a/sonic-radiance.love/game/abstractmobs/utils.lua +++ b/sonic-radiance.love/game/abstractmobs/utils.lua @@ -39,7 +39,7 @@ function CharUtils.getCharacterData(charname) -- va eprmettre de récupérer les données d'un personnage local charfolder = "datas.gamedata.characters." .. charname local character = require(charfolder) - character.base_stats = require(charfolder .. ".stats") + character.stats = require(charfolder .. ".stats") character.inventory = require(charfolder .. ".inventory") character.skills = require(charfolder .. ".skills") diff --git a/sonic-radiance.love/game/characters.lua b/sonic-radiance.love/game/characters.lua index fcb072f..6b1dac7 100644 --- a/sonic-radiance.love/game/characters.lua +++ b/sonic-radiance.love/game/characters.lua @@ -25,6 +25,7 @@ local CharacterManager = Object:extend() local charutils = require "game.abstractmobs.utils" +local AbstractCharacter = require "game.abstractmobs.character" function CharacterManager:new(controller) self.controller = controller @@ -38,73 +39,17 @@ end function CharacterManager:init() for k, name in pairs(self.namelist) do if (charutils.charDataExists(name)) then - self:initCharacter(name) + self.list[name] = AbstractCharacter(name) end end end -function CharacterManager:charDataExists(name) - local dir = "datas/gamedata/characters/" .. name .. "/init.lua" - local fileinfo = love.filesystem.getInfo(dir) - return (fileinfo ~= nil) +function CharacterManager:setLevel(name, newlevel) + self.list[name]:setLevel(newlevel) end -function CharacterManager:getCharacterData(charname) - -- va eprmettre de récupérer les données d'un personnage - local charfolder = "datas.gamedata.characters." .. charname - local character = require(charfolder) - character.base_stats = require(charfolder .. ".stats") - character.inventory = require(charfolder .. ".inventory") - character.skills = require(charfolder .. ".skills") - - return character -end - -function CharacterManager:initCharacter(id) - local stats = {} - local character = charutils.getCharacterData(id) - - stats.level = character.startlevel - stats.exp = charutils.getExpValue(stats.level) - stats.exp_next = charutils.getExpValue(stats.level + 1) - stats.hpmax = character.base_stats.hpmax - stats.ppmax = character.base_stats.ppmax - stats.attack = character.base_stats.attack - stats.power = character.base_stats.power - stats.defense = character.base_stats.defense - stats.technic = character.base_stats.technic - stats.mind = character.base_stats.mind - stats.speed = character.base_stats.speed - - character.stats = stats - self.list[id] = character - - self:recalculateStats(id) - - stats.hp = stats.hpmax - stats.pp = stats.ppmax - stats.status = 0 - - character.stats = stats - self.list[id] = character -end - -function CharacterManager:setLevel(id, newlevel) - self.list[id].stats.level = newlevel - local stats = self.list[id].stats - local exp, exp_next, exp_current - exp = charutils.getExpValue(stats.level) - exp_next = charutils.getExpValue(stats.level + 1) - exp_current = self.list[id].stats.exp - - self.list[id].stats.exp = math.max(math.min(exp_current, exp_next - 1), exp) - self.list[id].stats.exp_next = exp_next - - self:recalculateStats(id) -end - -function CharacterManager:levelUp(id) - self:setLevel(id, self.list[id].stats.level + 1) +function CharacterManager:levelUp(name) + self.list[name]:levelUp() end function CharacterManager:recalculateStats(id) @@ -122,39 +67,6 @@ function CharacterManager:recalculateStats(id) stats.speed = charutils.getStatValue(stats.level, base_stats.speed) end -function CharacterManager:getSkillList(id) - local character = self.list[id] - local learnedlist = {} - - for i, v in ipairs(character.skills) do - local tech_name, tech_level, isLearned = v[1], v[2], false - if tech_level <= character.stats.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 CharacterManager:getData() local data = {} data.list = self.list @@ -168,15 +80,13 @@ function CharacterManager:setData(data) self.team = data.team end -function CharacterManager:heal(id) - self.list[id].stats.hp = self.list[id].stats.hpmax - self.list[id].stats.hp = self.list[id].stats.ppmax - self.list[id].stats.status = 0 +function CharacterManager:heal(name) + self.list[name]:heal() end -function CharacterManager:addToTeam(id) - self:heal(id) - table.insert(self.team, id) +function CharacterManager:addToTeam(name) + self:heal(name) + table.insert(self.team, name) end function CharacterManager:removeToTeam(teamid) @@ -191,9 +101,8 @@ end function CharacterManager:printCharacter(id) local character = self.list[id] - local stats = character.stats print(id .. ". " .. character.fullname) - print("Lvl " .. character.stats.level .. " (" .. stats.exp .. "/" .. stats.exp_next .. " exp)") + print("Lvl " .. character.level .. " (" .. character.exp .. "/" .. character.exp_next .. " exp)") end function CharacterManager:printTeam()