From 703d11d8b76a2b90a8c9e63c5891dbc88eb939de Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 4 Apr 2021 16:33:48 +0200 Subject: [PATCH] fix: make sure that character/loot data are saved Fixes #71 --- .../game/abstractmobs/character.lua | 39 +++++++++++++++++-- sonic-radiance.love/game/characters.lua | 12 +++++- sonic-radiance.love/game/init.lua | 2 + sonic-radiance.love/game/loot/init.lua | 9 ++++- sonic-radiance.love/game/loot/pocket.lua | 8 ++++ 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/sonic-radiance.love/game/abstractmobs/character.lua b/sonic-radiance.love/game/abstractmobs/character.lua index 45c2172..3ede7ee 100644 --- a/sonic-radiance.love/game/abstractmobs/character.lua +++ b/sonic-radiance.love/game/abstractmobs/character.lua @@ -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 diff --git a/sonic-radiance.love/game/characters.lua b/sonic-radiance.love/game/characters.lua index e752bce..49d4964 100644 --- a/sonic-radiance.love/game/characters.lua +++ b/sonic-radiance.love/game/characters.lua @@ -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) diff --git a/sonic-radiance.love/game/init.lua b/sonic-radiance.love/game/init.lua index 7a37335..17b84f4 100644 --- a/sonic-radiance.love/game/init.lua +++ b/sonic-radiance.love/game/init.lua @@ -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 diff --git a/sonic-radiance.love/game/loot/init.lua b/sonic-radiance.love/game/loot/init.lua index db2f2f2..52c405f 100644 --- a/sonic-radiance.love/game/loot/init.lua +++ b/sonic-radiance.love/game/loot/init.lua @@ -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() diff --git a/sonic-radiance.love/game/loot/pocket.lua b/sonic-radiance.love/game/loot/pocket.lua index f0616c5..b0be35f 100644 --- a/sonic-radiance.love/game/loot/pocket.lua +++ b/sonic-radiance.love/game/loot/pocket.lua @@ -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