fix: make sure that character/loot data are saved

Fixes #71
This commit is contained in:
Kazhnuz 2021-04-04 16:33:48 +02:00
parent e2df082d92
commit 703d11d8b7
5 changed files with 62 additions and 8 deletions

View file

@ -10,16 +10,20 @@ function AbstractCharacter:new(name)
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: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
@ -89,4 +93,31 @@ 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

@ -70,15 +70,23 @@ end
function CharacterManager:getData()
local data = {}
data.list = self.list
data.team = self.team
data.list = {}
for name, character in pairs(self.list) do
data.list[name] = character:getData()
end
return data
end
function CharacterManager:setData(data)
local data = data
self.list = data.list
self.team = data.team
self.list = {}
for name, charData in pairs(data.list) do
local character = AbstractCharacter(name)
character:setData(charData)
self.list[name] = character
end
end
function CharacterManager:heal(name)

View file

@ -72,12 +72,14 @@ function Game:setData(data)
self.flags = data.flags
self.position = data.position
self.characters:setData(data.characters)
self.loot:setData(data.loot)
end
function Game:getData()
local data = {}
data.gametime = self.gametime
data.characters = self.characters:getData()
data.loot = self.loot:getData()
data.flags = self.flags
data.destroyedGizmo = self.destroyedGizmo
data.variables = self.variables

View file

@ -17,14 +17,19 @@ end
function LootManager:getData()
local data = {}
data.rings = self.rings
data.inventory = self.inventory
data.inventory = {}
for i, pocket in ipairs(self.inventory) do
data.inventory[i] = pocket:getData()
end
return data
end
function LootManager:setData(data)
local data = data
self.rings = data.rings
self.inventory = data.inventory
for i, pocket in ipairs(self.inventory) do
pocket:setData(data.inventory[i])
end
end
function LootManager:generatePockets()

View file

@ -8,6 +8,14 @@ function Pocket:new(pocketdata)
self.list = {}
end
function Pocket:getData()
return self.list
end
function Pocket:setData(data)
self.list = data
end
function Pocket:addItem(item, number)
local success = false