206 lines
6.4 KiB
Lua
206 lines
6.4 KiB
Lua
-- game/characters :: The character handler. This object handle all the character
|
|
-- and is able to get and set datas about them.
|
|
|
|
--[[
|
|
Copyright © 2019 Kazhnuz
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
the Software without restriction, including without limitation the rights to
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
]]
|
|
|
|
local CharacterManager = Object:extend()
|
|
|
|
local statsutils = require "game.abstractmobs.utils"
|
|
|
|
function CharacterManager:new(controller)
|
|
self.controller = controller
|
|
self.namelist = require "datas.gamedata.characters"
|
|
self.list = {}
|
|
self.team = require "datas.gamedata.characters.baseteam"
|
|
self.active = 1
|
|
self:init()
|
|
end
|
|
|
|
function CharacterManager:init()
|
|
for k, name in pairs(self.namelist) do
|
|
if (self:charDataExists(name)) then
|
|
self:initCharacter(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)
|
|
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 = self:getCharacterData(id)
|
|
|
|
stats.level = character.startlevel
|
|
stats.exp = statsutils.getExpValue(stats.level)
|
|
stats.exp_next = statsutils.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 = statsutils.getExpValue(stats.level)
|
|
exp_next = statsutils.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)
|
|
end
|
|
|
|
function CharacterManager:recalculateStats(id)
|
|
local character = self.list[id]
|
|
local stats = character.stats
|
|
local base_stats = character.base_stats
|
|
|
|
stats.hpmax = statsutils.getHPValue(stats.level, base_stats.hpmax)
|
|
stats.ppmax = statsutils.getPPValue(stats.level, base_stats.ppmax)
|
|
stats.attack = statsutils.getStatValue(stats.level, base_stats.attack)
|
|
stats.power = statsutils.getStatValue(stats.level, base_stats.power)
|
|
stats.defense = statsutils.getStatValue(stats.level, base_stats.defense)
|
|
stats.mind = statsutils.getStatValue(stats.level, base_stats.mind)
|
|
stats.technic = statsutils.getStatValue(stats.level, base_stats.technic)
|
|
stats.speed = statsutils.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
|
|
data.team = self.team
|
|
return data
|
|
end
|
|
|
|
function CharacterManager:setData(data)
|
|
local data = data
|
|
self.list = data.list
|
|
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
|
|
end
|
|
|
|
function CharacterManager:addToTeam(id)
|
|
self:heal(id)
|
|
table.insert(self.team, id)
|
|
end
|
|
|
|
function CharacterManager:removeToTeam(teamid)
|
|
self.team[teamid] = ""
|
|
end
|
|
|
|
function CharacterManager:getActiveCharacter()
|
|
return self.team[self.active]
|
|
end
|
|
|
|
-- DEBUG FUNCTIONS
|
|
|
|
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)")
|
|
end
|
|
|
|
function CharacterManager:printTeam()
|
|
for i,v in ipairs(self.team) do
|
|
self:printCharacter(v)
|
|
print("-----")
|
|
end
|
|
end
|
|
|
|
return CharacterManager
|