From a8ae9ca5ba38b15a84859514542bdcc00a56316d Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 13 Sep 2020 10:03:43 +0200 Subject: [PATCH] chore: refactor a bit items --- .../game/{loot.lua => loot/init.lua} | 48 +------------------ sonic-radiance.love/game/loot/pocket.lua | 45 +++++++++++++++++ .../game/utils/{items.lua => items/init.lua} | 0 3 files changed, 46 insertions(+), 47 deletions(-) rename sonic-radiance.love/game/{loot.lua => loot/init.lua} (59%) create mode 100644 sonic-radiance.love/game/loot/pocket.lua rename sonic-radiance.love/game/utils/{items.lua => items/init.lua} (100%) diff --git a/sonic-radiance.love/game/loot.lua b/sonic-radiance.love/game/loot/init.lua similarity index 59% rename from sonic-radiance.love/game/loot.lua rename to sonic-radiance.love/game/loot/init.lua index 2cafdb7..9f01831 100644 --- a/sonic-radiance.love/game/loot.lua +++ b/sonic-radiance.love/game/loot/init.lua @@ -1,5 +1,5 @@ local LootManager = Object:extend() -local Pocket = Object:extend() +local Pocket = require "game.loot.pocket" local itemutils = require "game.utils.items" @@ -58,50 +58,4 @@ function LootManager:getItemNumber(type, item) return pocket:getItemNumber(item) end --- POCKET OBJECT - -function Pocket:new(pocketdata) - self.name = pocketdata.name - self.fullname = pocketdata.fullname - self.inBattle = pocketdata.inBattle - self.description = pocketdata.description - self.list = {} -end - -function Pocket:addItem(item, number) - local success = false - - for i,itemData in ipairs(self.list) do - if (itemData.name == item) then - itemData.number = itemData.number + number - success = true - end - end - - if (not success) then - itemData = {} - itemData.name = item - itemData.number = number - table.insert(self.list, itemData) - end -end - -function Pocket:getItem(name) - for i, itemData in ipairs(self.list) do - if (itemData.name == name) then - return itemData - end - end - return nil -end - -function Pocket:getItemNumber(name) - local item = self:getItem(name) - if (item == nil) then - return 0 - else - return item.number - end -end - return LootManager diff --git a/sonic-radiance.love/game/loot/pocket.lua b/sonic-radiance.love/game/loot/pocket.lua new file mode 100644 index 0000000..7d24637 --- /dev/null +++ b/sonic-radiance.love/game/loot/pocket.lua @@ -0,0 +1,45 @@ +local Pocket = Object:extend() + +function Pocket:new(pocketdata) + self.name = pocketdata.name + self.fullname = pocketdata.fullname + self.inBattle = pocketdata.inBattle + self.description = pocketdata.description + self.list = {} +end + +function Pocket:addItem(item, number) + local success = false + + for i,itemData in ipairs(self.list) do + if (itemData.name == item) then + itemData.number = itemData.number + number + success = true + end + end + + if (not success) then + itemData = {} + itemData.name = item + itemData.number = number + table.insert(self.list, itemData) + end +end + +function Pocket:getItem(name) + for i, itemData in ipairs(self.list) do + if (itemData.name == name) then + return itemData + end + end + return nil +end + +function Pocket:getItemNumber(name) + local item = self:getItem(name) + if (item == nil) then + return 0 + else + return item.number + end +end diff --git a/sonic-radiance.love/game/utils/items.lua b/sonic-radiance.love/game/utils/items/init.lua similarity index 100% rename from sonic-radiance.love/game/utils/items.lua rename to sonic-radiance.love/game/utils/items/init.lua