2019-02-03 11:04:25 +01:00
|
|
|
-- 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()
|
|
|
|
|
2020-08-04 13:38:45 +02:00
|
|
|
local charutils = require "game.utils.characters"
|
2020-07-19 14:02:22 +02:00
|
|
|
local AbstractCharacter = require "game.abstractmobs.character"
|
2021-04-02 23:26:22 +02:00
|
|
|
local startdata = require "datas.gamedata.startdata"
|
2020-07-19 10:50:43 +02:00
|
|
|
|
2019-02-03 11:04:25 +01:00
|
|
|
function CharacterManager:new(controller)
|
|
|
|
self.controller = controller
|
2019-03-10 13:41:37 +01:00
|
|
|
self.namelist = require "datas.gamedata.characters"
|
2019-02-03 11:04:25 +01:00
|
|
|
self.list = {}
|
2021-04-02 23:26:22 +02:00
|
|
|
self.team = startdata.baseteam
|
2019-02-03 11:04:25 +01:00
|
|
|
self.active = 1
|
|
|
|
self:init()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:init()
|
2020-07-19 13:20:24 +02:00
|
|
|
for k, name in pairs(self.namelist) do
|
2020-07-19 13:41:20 +02:00
|
|
|
if (charutils.charDataExists(name)) then
|
2020-07-19 14:02:22 +02:00
|
|
|
self.list[name] = AbstractCharacter(name)
|
2019-02-03 11:04:25 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-19 14:02:22 +02:00
|
|
|
function CharacterManager:setLevel(name, newlevel)
|
|
|
|
self.list[name]:setLevel(newlevel)
|
2020-07-19 13:20:24 +02:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:02:22 +02:00
|
|
|
function CharacterManager:levelUp(name)
|
|
|
|
self.list[name]:levelUp()
|
2019-02-03 11:04:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:recalculateStats(id)
|
|
|
|
local character = self.list[id]
|
|
|
|
local stats = character.stats
|
|
|
|
local base_stats = character.base_stats
|
|
|
|
|
2020-07-19 13:41:20 +02:00
|
|
|
stats.hpmax = charutils.getHPValue(stats.level, base_stats.hpmax)
|
|
|
|
stats.ppmax = charutils.getPPValue(stats.level, base_stats.ppmax)
|
|
|
|
stats.attack = charutils.getStatValue(stats.level, base_stats.attack)
|
|
|
|
stats.power = charutils.getStatValue(stats.level, base_stats.power)
|
|
|
|
stats.defense = charutils.getStatValue(stats.level, base_stats.defense)
|
|
|
|
stats.mind = charutils.getStatValue(stats.level, base_stats.mind)
|
|
|
|
stats.technic = charutils.getStatValue(stats.level, base_stats.technic)
|
|
|
|
stats.speed = charutils.getStatValue(stats.level, base_stats.speed)
|
2019-02-03 11:04:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:getData()
|
|
|
|
local data = {}
|
|
|
|
data.team = self.team
|
2021-04-04 16:33:48 +02:00
|
|
|
data.list = {}
|
|
|
|
for name, character in pairs(self.list) do
|
|
|
|
data.list[name] = character:getData()
|
|
|
|
end
|
2019-02-03 11:04:25 +01:00
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:setData(data)
|
|
|
|
local data = data
|
|
|
|
self.team = data.team
|
2021-04-04 16:33:48 +02:00
|
|
|
self.list = {}
|
|
|
|
for name, charData in pairs(data.list) do
|
|
|
|
local character = AbstractCharacter(name)
|
|
|
|
character:setData(charData)
|
|
|
|
self.list[name] = character
|
|
|
|
end
|
2019-02-03 11:04:25 +01:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:02:22 +02:00
|
|
|
function CharacterManager:heal(name)
|
|
|
|
self.list[name]:heal()
|
2019-02-03 11:04:25 +01:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:02:22 +02:00
|
|
|
function CharacterManager:addToTeam(name)
|
|
|
|
self:heal(name)
|
2020-08-02 23:47:42 +02:00
|
|
|
if (#self.team < 4) then
|
|
|
|
table.insert(self.team, name)
|
|
|
|
end
|
2019-02-03 11:04:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:removeToTeam(teamid)
|
2020-08-02 23:47:42 +02:00
|
|
|
if (#self.team > 1) then
|
|
|
|
table.remove(self.team, teamid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:getPositionInTeam(charName)
|
|
|
|
local charId = -1
|
|
|
|
for i,name in ipairs(self.team) do
|
|
|
|
if (name == charName) then
|
|
|
|
charId = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return charId
|
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:addOrRemoveToTeam(charName)
|
|
|
|
local charId = self:getPositionInTeam(charName)
|
|
|
|
if (charId == -1) then
|
|
|
|
self:addToTeam(charName)
|
|
|
|
else
|
|
|
|
self:removeToTeam(charId)
|
|
|
|
if (charId < self.active) then
|
|
|
|
self.active = self.active - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self:fixActiveCharacter()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:setActiveCharacter(direction)
|
|
|
|
local direction = direction or 1
|
|
|
|
self.active = self.active + utils.math.sign(direction)
|
|
|
|
if (self.active > #self.team) then
|
|
|
|
self.active = 1
|
|
|
|
end
|
|
|
|
if (self.active < 1) then
|
|
|
|
self.active = #self.team
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:fixActiveCharacter()
|
|
|
|
if (self.active < 1) then
|
|
|
|
self.active = 1
|
|
|
|
elseif (self.active > #self.team) then
|
|
|
|
self.active = #self.team
|
|
|
|
end
|
2019-02-03 11:04:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:getActiveCharacter()
|
|
|
|
return self.team[self.active]
|
|
|
|
end
|
|
|
|
|
2020-08-03 08:59:04 +02:00
|
|
|
function CharacterManager:getActiveCharacterData()
|
|
|
|
return self.list[self.team[self.active]]
|
|
|
|
end
|
|
|
|
|
2020-08-03 10:28:18 +02:00
|
|
|
function CharacterManager:loadSprite(assets, name)
|
|
|
|
assets:addSprite(name, "datas/gamedata/characters/" .. name .. "/sprites")
|
|
|
|
end
|
|
|
|
|
2019-02-03 11:04:25 +01:00
|
|
|
-- DEBUG FUNCTIONS
|
|
|
|
|
|
|
|
function CharacterManager:printCharacter(id)
|
|
|
|
local character = self.list[id]
|
|
|
|
print(id .. ". " .. character.fullname)
|
2020-07-19 14:02:22 +02:00
|
|
|
print("Lvl " .. character.level .. " (" .. character.exp .. "/" .. character.exp_next .. " exp)")
|
2019-02-03 11:04:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function CharacterManager:printTeam()
|
|
|
|
for i,v in ipairs(self.team) do
|
|
|
|
self:printCharacter(v)
|
|
|
|
print("-----")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return CharacterManager
|