chore: refactor a bit items
This commit is contained in:
parent
b2c9c1d79a
commit
a8ae9ca5ba
3 changed files with 46 additions and 47 deletions
|
@ -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
|
45
sonic-radiance.love/game/loot/pocket.lua
Normal file
45
sonic-radiance.love/game/loot/pocket.lua
Normal file
|
@ -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
|
Loading…
Reference in a new issue