From 144a42dc9ed0f9bb6ea1085bee9d30df3c119df2 Mon Sep 17 00:00:00 2001 From: Tjakka5 Date: Sun, 22 Dec 2019 23:07:42 +0100 Subject: [PATCH] Automate naming process --- main.lua | 24 ++++++++------- src/assemblage.lua | 13 +------- src/component.lua | 15 ++-------- src/init.lua | 53 +++++++++++++++++++++++++++++++++ src/system.lua | 10 +------ src/world.lua | 10 +------ test/components/test_comp_1.lua | 6 ++++ 7 files changed, 78 insertions(+), 53 deletions(-) create mode 100644 test/components/test_comp_1.lua diff --git a/main.lua b/main.lua index f155a83..1a5ed40 100644 --- a/main.lua +++ b/main.lua @@ -9,8 +9,8 @@ require(file) local Concord = require("src") -local Component = require("src.component") -local Components = require("src.components") +local Component = Concord.component +local Components = Concord.components local System = Concord.system local Systems = Concord.systems @@ -20,20 +20,23 @@ local Entity = Concord.entity local World = Concord.world local Worlds = Concord.worlds -Component("test_comp_1", function(e, x, y) - e.x = x - e.y = y -end) +Concord.loadComponents("test/components") -Component("test_comp_2", function(e, a) + +local test_comp_2 = Component(function(e, a) e.a = a end) +Components.register("test_comp_2", test_comp_2) -Component("test_comp_3", function(e, b) + +local test_comp_3 = Component(function(e, b) e.b = b end) +Components.register("test_comp_3", test_comp_3) -local test_system = System("test_system", {Components.test_comp_1}) + +local test_system = System({Components.test_comp_1}) +Systems.register("test_system", test_system) local function onEntityAdded(pool, e) -- luacheck: ignore print("Added") @@ -61,7 +64,8 @@ function test_system:update2(dt) -- luacheck: ignore end -local world = World("testWorld") +local world = World() +Worlds.register("testWorld", world) local entity = Entity() entity diff --git a/src/assemblage.lua b/src/assemblage.lua index a6befed..79b3cbb 100644 --- a/src/assemblage.lua +++ b/src/assemblage.lua @@ -1,25 +1,14 @@ --- Assemblage -local PATH = (...):gsub('%.[^%.]+$', '') - -local Assemblages = require(PATH..".world") - local Assemblage = {} Assemblage.__index = Assemblage -function Assemblage.new(name, assemble) - if (type(name) ~= "string") then - error("bad argument #1 to 'Assemblage.new' (string expected, got "..type(name)..")", 2) - end - +function Assemblage.new(assemble) local assemblage = setmetatable({ __assemble = assemble, - __name = name, __isAssemblage = true, }, Assemblage) - - Assemblages.register(name, assemblage) return assemblage end diff --git a/src/component.lua b/src/component.lua index fd532ea..90dcdc6 100644 --- a/src/component.lua +++ b/src/component.lua @@ -1,26 +1,17 @@ --- Component -local PATH = (...):gsub('%.[^%.]+$', '') - -local Components = require(PATH..".components") - local Component = {} Component.__index = Component --- Creates a new Component. -- @param populate A function that populates the Bag with values -- @return A Component object -function Component.new(name, populate) - if (type(name) ~= "string") then - error("bad argument #1 to 'Component.new' (string expected, got "..type(name)..")", 2) - end - +function Component.new(populate) if not (type(populate) == "function") then - error("bad argument #2 to 'Component.new' (function expected, got "..type(populate)..")", 2) + error("bad argument #1 to 'Component.new' (function expected, got "..type(populate)..")", 2) end local baseComponent = setmetatable({ - __name = name, __populate = populate, __isBaseComponent = true, @@ -28,8 +19,6 @@ function Component.new(name, populate) baseComponent.__mt = {__index = baseComponent} - Components.register(name, baseComponent) - return baseComponent end diff --git a/src/init.lua b/src/init.lua index 5f32550..66916db 100644 --- a/src/init.lua +++ b/src/init.lua @@ -45,4 +45,57 @@ Concord.worlds = require(PATH..".worlds") Concord.assemblage = require(PATH..".assemblage") Concord.assemblages = require(PATH..".assemblages") +local function load(pathOrFiles, namespace) + if (type(pathOrFiles) ~= "string" and type(pathOrFiles) ~= "table") then + error("bad argument #1 to 'load' (string/table of strings expected, got "..type(pathOrFiles)..")", 3) -- luacheck: ignore + end + + if (type(pathOrFiles) == "string") then + local info = love.filesystem.getInfo(pathOrFiles) -- luacheck: ignore + if (info == nil or info.type ~= "directory") then + error("bad argument #1 to 'load' (path '"..pathOrFiles.."' not found)", 3) -- luacheck: ignore + end + + local files = love.filesystem.getDirectoryItems(pathOrFiles) + + for _, file in ipairs(files) do + local name = file:sub(1, #file - 4) + local path = pathOrFiles.."."..name + + namespace.register(name, require(path)) + end + elseif (type(pathOrFiles == "table")) then + for _, path in ipairs(pathOrFiles) do + if (type(path) ~= "string") then + error("bad argument #2 to 'load' (string/table of strings expected, got table containing "..type(path)..")", 3) -- luacheck: ignore + end + + local name = path + + local dotIndex, slashIndex = path:match("^.*()%."), path:match("^.*()%/") + if (dotIndex or slashIndex) then + name = path:sub((dotIndex or slashIndex) + 1) + end + + namespace.register(name, require(path)) + end + end +end + +function Concord.loadComponents(pathOrFiles) + load(pathOrFiles, Concord.components) +end + +function Concord.loadSystems(pathOrFiles) + load(pathOrFiles, Concord.systems) +end + +function Concord.loadWorlds(pathOrFiles) + load(pathOrFiles, Concord.worlds) +end + +function Concord.loadAssemblages(pathOrFiles) + load(pathOrFiles, Concord.assemblages) +end + return Concord diff --git a/src/system.lua b/src/system.lua index f9b696c..871b9a5 100644 --- a/src/system.lua +++ b/src/system.lua @@ -2,7 +2,6 @@ local PATH = (...):gsub('%.[^%.]+$', '') -local Systems = require(PATH..".systems") local Pool = require(PATH..".pool") local System = {} @@ -36,20 +35,13 @@ System.mt = { --- Creates a new System prototype. -- @param ... Variable amounts of filters -- @return A new System prototype -function System.new(name, ...) - if (type(name) ~= "string") then - error("bad argument #1 to 'System.new' (string expected, got "..type(name)..")", 2) - end - +function System.new(...) local baseSystem = setmetatable({ - __name = name, __isBaseSystem = true, __filter = {...}, }, System.mt) baseSystem.__index = baseSystem - Systems.register(name, baseSystem) - return baseSystem end diff --git a/src/world.lua b/src/world.lua index 63ef1a9..2fe1ca8 100644 --- a/src/world.lua +++ b/src/world.lua @@ -2,7 +2,6 @@ local PATH = (...):gsub('%.[^%.]+$', '') -local Worlds = require(PATH..".worlds") local Type = require(PATH..".type") local List = require(PATH..".list") @@ -11,11 +10,7 @@ World.__index = World --- Creates a new World. -- @return The new World -function World.new(name) - if (type(name) ~= "string") then - error("bad argument #1 to 'World.new' (string expected, got "..type(name)..")", 2) - end - +function World.new() local world = setmetatable({ entities = List(), systems = List(), @@ -27,12 +22,9 @@ function World.new(name) __systemLookup = {}, - __name = name, __isWorld = true, }, World) - Worlds.register(name, world) - return world end diff --git a/test/components/test_comp_1.lua b/test/components/test_comp_1.lua new file mode 100644 index 0000000..ef9b98a --- /dev/null +++ b/test/components/test_comp_1.lua @@ -0,0 +1,6 @@ +local Component = require("src").component + +return Component(function(e, x, y) + e.x = x + e.y = y +end) \ No newline at end of file