2020-08-15 22:43:46 +02:00
|
|
|
local ItemUtils = {}
|
|
|
|
|
|
|
|
local DIR = "items"
|
|
|
|
|
|
|
|
function ItemUtils.getBaseDirectory(lua)
|
2021-05-15 15:09:02 +02:00
|
|
|
return utils.datas.concatDataFolder(DIR, lua)
|
2020-08-15 22:43:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ItemUtils.listCategories()
|
|
|
|
return require(ItemUtils.getBaseDirectory(true))
|
|
|
|
end
|
|
|
|
|
|
|
|
function ItemUtils.getCategoryDirectory(directory, lua)
|
2021-05-15 15:09:02 +02:00
|
|
|
return utils.datas.concatFolder(ItemUtils.getBaseDirectory(lua), directory, lua)
|
2020-08-15 22:43:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ItemUtils.getItemsFromCategory(directory)
|
|
|
|
local folder = ItemUtils.getCategoryDirectory(directory, false)
|
|
|
|
local baseTable = love.filesystem.getDirectoryItems(folder)
|
2021-05-15 15:09:02 +02:00
|
|
|
return utils.datas.luaFileListToModuleList(baseTable)
|
2020-08-15 22:43:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function ItemUtils.getItemFile(category, item, lua)
|
|
|
|
local dir = ItemUtils.getCategoryDirectory(category, lua)
|
2021-05-15 15:09:02 +02:00
|
|
|
local path = utils.datas.concatFolder(dir, item, lua)
|
2020-08-15 22:43:46 +02:00
|
|
|
if (not lua) then
|
|
|
|
path = path .. ".lua"
|
|
|
|
end
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
|
|
|
|
function ItemUtils.itemExists(category, item)
|
|
|
|
local path = ItemUtils.getItemFile(category, item, false)
|
|
|
|
local fileinfo = love.filesystem.getInfo(path)
|
|
|
|
return (fileinfo ~= nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ItemUtils.getItemData(category, item)
|
|
|
|
local path = ItemUtils.getItemFile(category, item, true)
|
2021-05-15 15:09:02 +02:00
|
|
|
return utils.datas.copy(path)
|
2020-08-15 22:43:46 +02:00
|
|
|
end
|
|
|
|
|
2020-09-13 13:25:44 +02:00
|
|
|
-- VALIDATION FUNCTIONS
|
|
|
|
|
|
|
|
function ItemUtils.getItemEffectStructure(type)
|
|
|
|
local typeList = require "game.utils.items.arguments"
|
|
|
|
return typeList[type]
|
|
|
|
end
|
|
|
|
|
|
|
|
function ItemUtils.itemEffectExists(type)
|
|
|
|
return (ItemUtils.getItemEffectStructure(type) ~= nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ItemUtils.validateItemEffect(effectBaseData)
|
|
|
|
local structure = ItemUtils.getItemEffectStructure(effectBaseData[1])
|
|
|
|
if (structure == nil) then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return ((#structure + 1) == #effectBaseData)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ItemUtils.getItemEffectData(effectBaseData)
|
|
|
|
local effectData = {}
|
|
|
|
effectData.type = effectBaseData[1]
|
|
|
|
|
|
|
|
if (ItemUtils.validateItemEffect(effectBaseData)) then
|
|
|
|
local structure = ItemUtils.getItemEffectStructure(effectData.type)
|
|
|
|
|
|
|
|
for i, argumentName in ipairs(structure) do
|
|
|
|
local argumentContent = effectBaseData[i + 1]
|
|
|
|
effectData[argumentName] = argumentContent
|
|
|
|
end
|
|
|
|
|
|
|
|
return effectData
|
|
|
|
else
|
|
|
|
error("Le type d'effet d'objet " .. effectData.type .. " à un nbr d'argument incorrect")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-15 22:43:46 +02:00
|
|
|
return ItemUtils
|