2020-08-02 15:56:36 +02:00
|
|
|
local LootManager = Object:extend()
|
2020-08-15 22:43:46 +02:00
|
|
|
local Pocket = Object:extend()
|
|
|
|
|
|
|
|
local itemutils = require "game.utils.items"
|
2020-08-02 15:56:36 +02:00
|
|
|
|
|
|
|
function LootManager:new(controller)
|
|
|
|
self.controller = controller
|
|
|
|
self.rings = 0
|
|
|
|
self.inventory = {}
|
2020-08-15 22:43:46 +02:00
|
|
|
self.pocketIndex = {}
|
|
|
|
|
|
|
|
self:generatePockets()
|
2020-08-02 15:56:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function LootManager:getData()
|
|
|
|
local data = {}
|
|
|
|
data.rings = self.rings
|
|
|
|
data.inventory = self.inventory
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
function LootManager:setData(data)
|
|
|
|
local data = data
|
|
|
|
self.rings = data.rings
|
|
|
|
self.inventory = data.inventory
|
2020-08-15 22:43:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function LootManager:generatePockets()
|
|
|
|
local structure = require "datas.gamedata.items"
|
|
|
|
for i,pocketdata in ipairs(structure) do
|
|
|
|
local pocket = Pocket(pocketdata)
|
|
|
|
self.pocketIndex[pocketdata.name] = i
|
|
|
|
table.insert(self.inventory, pocket)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function LootManager:getPocketById(id)
|
|
|
|
return self.inventory[id]
|
|
|
|
end
|
|
|
|
|
|
|
|
function LootManager:getPocketByName(name)
|
|
|
|
return self.inventory[self.pocketIndex[name]]
|
|
|
|
end
|
|
|
|
|
|
|
|
function LootManager:addItem(type, item, number)
|
|
|
|
local pocket = self:getPocketByName(type)
|
|
|
|
if (itemutils.itemExists(type, item)) then
|
|
|
|
pocket:addItem(item, number)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function LootManager:getItemData(type, item)
|
|
|
|
return itemutils.getItemData(type, item)
|
|
|
|
end
|
|
|
|
|
|
|
|
function LootManager:getItemNumber(type, item)
|
|
|
|
local pocket = self:getPocketByName(type)
|
|
|
|
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
|
2020-08-02 15:56:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return LootManager
|