Automate naming process

This commit is contained in:
Tjakka5 2019-12-22 23:07:42 +01:00
parent e0f88025ba
commit 144a42dc9e
7 changed files with 78 additions and 53 deletions

View file

@ -9,8 +9,8 @@ require(file)
local Concord = require("src") local Concord = require("src")
local Component = require("src.component") local Component = Concord.component
local Components = require("src.components") local Components = Concord.components
local System = Concord.system local System = Concord.system
local Systems = Concord.systems local Systems = Concord.systems
@ -20,20 +20,23 @@ local Entity = Concord.entity
local World = Concord.world local World = Concord.world
local Worlds = Concord.worlds local Worlds = Concord.worlds
Component("test_comp_1", function(e, x, y) Concord.loadComponents("test/components")
e.x = x
e.y = y
end)
Component("test_comp_2", function(e, a)
local test_comp_2 = Component(function(e, a)
e.a = a e.a = a
end) 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 e.b = b
end) 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 local function onEntityAdded(pool, e) -- luacheck: ignore
print("Added") print("Added")
@ -61,7 +64,8 @@ function test_system:update2(dt) -- luacheck: ignore
end end
local world = World("testWorld") local world = World()
Worlds.register("testWorld", world)
local entity = Entity() local entity = Entity()
entity entity

View file

@ -1,25 +1,14 @@
--- Assemblage --- Assemblage
local PATH = (...):gsub('%.[^%.]+$', '')
local Assemblages = require(PATH..".world")
local Assemblage = {} local Assemblage = {}
Assemblage.__index = Assemblage Assemblage.__index = Assemblage
function Assemblage.new(name, assemble) function Assemblage.new(assemble)
if (type(name) ~= "string") then
error("bad argument #1 to 'Assemblage.new' (string expected, got "..type(name)..")", 2)
end
local assemblage = setmetatable({ local assemblage = setmetatable({
__assemble = assemble, __assemble = assemble,
__name = name,
__isAssemblage = true, __isAssemblage = true,
}, Assemblage) }, Assemblage)
Assemblages.register(name, assemblage)
return assemblage return assemblage
end end

View file

@ -1,26 +1,17 @@
--- Component --- Component
local PATH = (...):gsub('%.[^%.]+$', '')
local Components = require(PATH..".components")
local Component = {} local Component = {}
Component.__index = Component Component.__index = Component
--- Creates a new Component. --- Creates a new Component.
-- @param populate A function that populates the Bag with values -- @param populate A function that populates the Bag with values
-- @return A Component object -- @return A Component object
function Component.new(name, populate) function Component.new(populate)
if (type(name) ~= "string") then
error("bad argument #1 to 'Component.new' (string expected, got "..type(name)..")", 2)
end
if not (type(populate) == "function") then 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 end
local baseComponent = setmetatable({ local baseComponent = setmetatable({
__name = name,
__populate = populate, __populate = populate,
__isBaseComponent = true, __isBaseComponent = true,
@ -28,8 +19,6 @@ function Component.new(name, populate)
baseComponent.__mt = {__index = baseComponent} baseComponent.__mt = {__index = baseComponent}
Components.register(name, baseComponent)
return baseComponent return baseComponent
end end

View file

@ -45,4 +45,57 @@ Concord.worlds = require(PATH..".worlds")
Concord.assemblage = require(PATH..".assemblage") Concord.assemblage = require(PATH..".assemblage")
Concord.assemblages = require(PATH..".assemblages") 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 return Concord

View file

@ -2,7 +2,6 @@
local PATH = (...):gsub('%.[^%.]+$', '') local PATH = (...):gsub('%.[^%.]+$', '')
local Systems = require(PATH..".systems")
local Pool = require(PATH..".pool") local Pool = require(PATH..".pool")
local System = {} local System = {}
@ -36,20 +35,13 @@ System.mt = {
--- Creates a new System prototype. --- Creates a new System prototype.
-- @param ... Variable amounts of filters -- @param ... Variable amounts of filters
-- @return A new System prototype -- @return A new System prototype
function System.new(name, ...) function System.new(...)
if (type(name) ~= "string") then
error("bad argument #1 to 'System.new' (string expected, got "..type(name)..")", 2)
end
local baseSystem = setmetatable({ local baseSystem = setmetatable({
__name = name,
__isBaseSystem = true, __isBaseSystem = true,
__filter = {...}, __filter = {...},
}, System.mt) }, System.mt)
baseSystem.__index = baseSystem baseSystem.__index = baseSystem
Systems.register(name, baseSystem)
return baseSystem return baseSystem
end end

View file

@ -2,7 +2,6 @@
local PATH = (...):gsub('%.[^%.]+$', '') local PATH = (...):gsub('%.[^%.]+$', '')
local Worlds = require(PATH..".worlds")
local Type = require(PATH..".type") local Type = require(PATH..".type")
local List = require(PATH..".list") local List = require(PATH..".list")
@ -11,11 +10,7 @@ World.__index = World
--- Creates a new World. --- Creates a new World.
-- @return The new World -- @return The new World
function World.new(name) function World.new()
if (type(name) ~= "string") then
error("bad argument #1 to 'World.new' (string expected, got "..type(name)..")", 2)
end
local world = setmetatable({ local world = setmetatable({
entities = List(), entities = List(),
systems = List(), systems = List(),
@ -27,12 +22,9 @@ function World.new(name)
__systemLookup = {}, __systemLookup = {},
__name = name,
__isWorld = true, __isWorld = true,
}, World) }, World)
Worlds.register(name, world)
return world return world
end end

View file

@ -0,0 +1,6 @@
local Component = require("src").component
return Component(function(e, x, y)
e.x = x
e.y = y
end)