From 1935659378b8fc5d55ba4fe7a1bd0c3e41f35e27 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Tue, 4 Aug 2020 16:55:09 +0200 Subject: [PATCH] feat: add a data utils --- sonic-radiance.love/game/utils/characters.lua | 25 ++++++--- sonic-radiance.love/game/utils/datas.lua | 51 +++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 sonic-radiance.love/game/utils/datas.lua diff --git a/sonic-radiance.love/game/utils/characters.lua b/sonic-radiance.love/game/utils/characters.lua index d04ab82..efac69a 100644 --- a/sonic-radiance.love/game/utils/characters.lua +++ b/sonic-radiance.love/game/utils/characters.lua @@ -1,7 +1,8 @@ local CharUtils = {} -local CONST = {} +local datasutils = require "game.utils.datas" +local CONST = {} CONST.EXP_MULTIPLICATOR = 4 CONST.EXP_RATIO = 5 @@ -14,6 +15,18 @@ CONST.MULT_HP = 7.5 CONST.SALT_HP = 35 CONST.MULT_MP = 1.5 +local DIR = "characters" + +function CharUtils.getBaseDirectory(lua) + return datasutils.concatDataFolder(DIR, lua) +end + +function CharUtils.getCharacterDirectory(name, lua) + local baseDirectory = CharUtils.getBaseDirectory(lua) + return datasutils.concatFolder(baseDirectory, name, lua) +end + + function CharUtils.getExpValue(level) return math.floor( ( CONST.EXP_MULTIPLICATOR * ( level ^ 3 ) ) / CONST.EXP_RATIO ) end @@ -31,18 +44,18 @@ function CharUtils.getPPValue(level, base) end function CharUtils.charDataExists(name) - local dir = "datas/gamedata/characters/" .. name .. "/init.lua" + local dir = CharUtils.getCharacterDirectory(name, false) .. "/init.lua" local fileinfo = love.filesystem.getInfo(dir) return (fileinfo ~= nil) end function CharUtils.getCharacterData(charname) -- va eprmettre de récupérer les données d'un personnage - local charfolder = "datas.gamedata.characters." .. charname + local charfolder = CharUtils.getCharacterDirectory(charname, true) local character = require(charfolder) - character.stats = require(charfolder .. ".stats") - character.inventory = require(charfolder .. ".inventory") - character.skills = require(charfolder .. ".skills") + character.stats = datasutils.require(charfolder, "stats") + character.inventory = datasutils.require(charfolder, "inventory") + character.skills = datasutils.require(charfolder, "skills") return character end diff --git a/sonic-radiance.love/game/utils/datas.lua b/sonic-radiance.love/game/utils/datas.lua new file mode 100644 index 0000000..dca4921 --- /dev/null +++ b/sonic-radiance.love/game/utils/datas.lua @@ -0,0 +1,51 @@ +DataUtils = {} + +local DATADIR = "datas" +local GAMEDATADIR = "gamedata" + +function DataUtils.isLuaFile(name) + local extension = name:sub(#name - 4, #name) + return (extension == ".lua") +end + +function DataUtils.luaFileToModule(luaFile) + if (not DataUtils.isLuaFile(luaFile)) then + return luaFile:sub(1, #luaFile - 4) + else + return luaFile + end +end + +function DataUtils.luaFileListToModuleList(luaFileList) + local moduleList = {} + for i,luaFile in ipairs(luaFileList) do + table.insert(moduleList, DataUtils.luaFileToModule(luaFile)) + end + return moduleList +end + +function DataUtils.require(parent, filename) + return require(DataUtils.concatFolder(parent, filename, true)) +end + +function DataUtils.concatDataFolder(folder, lua) + return DataUtils.concatFolder(DataUtils.getGameDataDir(lua), folder, lua) +end + +function DataUtils.getGameDataDir(lua) + return DataUtils.concatFolder(DATADIR, GAMEDATADIR, lua) +end + +function DataUtils.concatFolder(parent, filename, lua) + return parent .. DataUtils.sep(lua) .. filename +end + +function DataUtils.sep(lua) + if (lua) then + return "." + else + return "/" + end +end + +return DataUtils